if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
  var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
  if (viewportmeta) {
    viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
    document.body.addEventListener('gesturestart', function() {
      viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
    }, false);
  }
}

// -------------------------------------------------------------------
// markItUp!
// -------------------------------------------------------------------
// Copyright (C) 2008 Jay Salvat
// http://markitup.jaysalvat.com/
// -------------------------------------------------------------------
// MarkDown tags example
// http://en.wikipedia.org/wiki/Markdown
// http://daringfireball.net/projects/markdown/
// -------------------------------------------------------------------
// Feel free to add more tags
// -------------------------------------------------------------------
var myMarkdownSettings = {
  previewParserPath: '/preview',
  onShiftEnter: {keepDefault:false, openWith:'\n\n'},
  markupSet: [
    {name:'First Level Heading', key:'1', placeHolder:'Din titel...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '=') } },
    {name:'Second Level Heading', key:'2', placeHolder:'Din titel...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '-') } },
    {name:'Heading 3', key:'3', openWith:'### ', placeHolder:'Din titel...' },
    {name:'Heading 4', key:'4', openWith:'#### ', placeHolder:'Din titel...' },
    {separator:'---------------' },
    {name:'Heading 5', key:'5', openWith:'##### ', placeHolder:'Din titel...' },
    {name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'Din titel...' },
    {separator:'---------------' },
    {name:'Bold', key:'B', openWith:'**', closeWith:'**'},
    {name:'Italic', key:'I', openWith:'_', closeWith:'_'},
    {separator:'---------------' },
    {name:'Bulleted List', openWith:'- ' },
    {name:'Numeric List', openWith:function(markItUp) {
      return markItUp.line+'. ';
    }},
    {separator:'---------------' },
    {name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!])'},
    {name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!])', placeHolder:'Din text som ska länkas...' },
    {separator:'---------------'},
    {name:'Quotes', openWith:'> '},
    {name:'Code Block / Code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)'},
    {separator:'---------------'},
    {name:'Preview', call:'preview', className:"preview"}
  ]
};

// mIu nameSpace to avoid conflict.
var miu = {
  markdownTitle: function(markItUp, char) {
    heading = '';
    n = $.trim(markItUp.selection||markItUp.placeHolder).length;
    for(i = 0; i < n; i++) {
      heading += char;
    }
    return '\n'+heading;
  }
};

// Creating custom :external selector
$.expr[':'].external = function(obj){
  return obj.hostname &&
    obj.hostname !== location.hostname &&
      $(obj).children().size() !== 1;
};

function remove(el) {
  if(confirm('Är du säker på att du vill radera?')) {
    $(el).submit();
  }
  return false;
}

$(document).ready(function() {
  $('textarea').markItUp(myMarkdownSettings);

  if(typeof(hljs) !== 'undefined' && hljs != null) {
    // Highlighting
    hljs.tabReplace = '    ';
    hljs.initHighlightingOnLoad();
  }

  // Tabs
  $(".tab_content").hide();
  $("ul.tabs li:first").addClass("active").show();
  $(".tab_content:first").show();

  $("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active");
    $(this).addClass("active");
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn();
    return false;
  });
});

