//
// create closure
//
(function (jQuery) {
  //
  // plugin definition
  //
  jQuery.fn.convertToTabs = function (options) {
    // build main options before element iteration
    var opts = jQuery.extend({}, jQuery.fn.convertToTabs.defaults, options);
    var o = jQuery.metadata ? jQuery.extend({}, opts, jQuery(this).metadata()) : opts;




    var tabs = jQuery(this).find("." + o.tabclass);

    // Create tab container object
    var tabsHtml = "<div id=\"co3Tabs\"><ul id=\"" + opts.newtabheaderid + "\"></ul></div>";
    var output = jQuery("<div></div>");
    output.html(tabsHtml);

    // Loop tabs and created markup
    var p = 0;
    tabs.each(function (i) {
      var jThis = jQuery(this);
      // Create tabs buttons	
      output.find("#" + opts.newtabheaderid).append("<li class=\"" + o.newtabheaderclass + " zIndex" + p + "\"><a href=\"#" + i + "\" class=\"" + o.newtabheaderclass + "\">" + jThis.find("." + o.headerclass).html() + "</a></li>");
      jThis.find("." + o.headerclass).hide();
      jThis.find("." + opts.headerclass).removeClass(opts.headerclass);
      jThis.hide();
      p++;
    });




    jQuery(this).prepend(output.html());

    jQuery("#" + opts.newtabheaderid + " li").eq(0).addClass("active");
    jQuery("#" + opts.newtabheaderid + " li a").eq(0).addClass("active");
    tabs.eq(0).show();


    // Live event som fyres af ved klik p? overskriften
    jQuery("." + o.newtabheaderclass).live('click', function () { click(this); return false; });




    function click(e) {
      var tabID = 0;
      if (jQuery(e).get(0).tagName == "LI") {
        tabID = jQuery(e).find("a").attr("href").replace("#", "");
      } else {
        tabID = jQuery(e).attr("href").replace("#", "");
      }
      var tabStrings = tabID.split("/");
      tabID=tabStrings[tabStrings.length-1];


      var tabheaders = jQuery("#" + opts.newtabheaderid + " li." + opts.newtabheaderclass);
      tabheaders.removeClass("active");
      tabheaders.find("a").removeClass("active");




      jQuery(e).addClass("active");
      jQuery(e).parent("li").addClass("active");




      var tabs = jQuery("#" + opts.tabcontainerid + " .tab");
      tabs.css("display","none");
      tabs.eq(tabID * 1).css("display","block");
    }
  };




  //
  // plugin defaults
  //
  jQuery.fn.convertToTabs.defaults = {
    tabcontainerid: 'tabColumn',
    tabclass: 'tab',
    headerclass: 'tabHeader',
    orientation: 'left',
    newtabheaderclass: 'tabHeader',
    newtabheaderid: 'tabHeaders'
  };
  //
  // end of closure
  //
})(jQuery);
















