/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/

 */
  jQuery.fn.tabs=function(options)
  {jQuery.tabs.initialize(this);}
 jQuery.tabs =
 {initialize: function(element) {
        this.element = jQuery(element);
		
        this.options = jQuery.extend({dependentType:"radio", dependentPanel:"body"}, arguments[1] || {});
        this.menu = jQuery("a",this.element).not(".no-tab");
		
		initialTab=jQuery.tabs.getInitialTab();
		
		this.menu.each(function(index, element){jQuery.tabs.hide(element)});
		jQuery.tabs.show(initialTab);
		this.menu.each(function(index, element){jQuery.tabs.setupTab(element)});
	
    },
    setupTab: function(elm) {
        jQuery(elm).click(function(evt){jQuery.tabs.activate(evt)});
    },
    activate: function(ev) {
        var elm = jQuery(ev.target);
        ev.stopPropagation();
		ev.preventDefault();
        jQuery.tabs.show(elm);
		this.menu.not(elm).each(function(index, element){jQuery.tabs.hide(element)});
		
    },
    hide: function(elm) {
			jQuery(elm).removeClass('active-tab');
			jQuery(elm).parents("li").removeClass('current');
            jQuery(jQuery.tabs.tabID(elm)).removeClass('active-tab-body');
			jQuery(jQuery.tabs.tabID(elm)).hide();
        },
    show: function(elm) {
	
			jQuery(jQuery.tabs.tabID(elm)).show();
            jQuery(elm).addClass('active-tab');
			var index=this.menu.index(jQuery(elm));
			jQuery(this.options.dependentPanel).find(":"+ this.options.dependentType).attr("checked", false); jQuery(this.options.dependentPanel).find(":"+ this.options.dependentType).eq(index).attr("checked", true);
			jQuery(elm).parents("li").eq(0).addClass('current');
            jQuery(jQuery.tabs.tabID(elm)).addClass('active-tab-body');
        

    },
    tabID: function(elm) {
        
                ret = jQuery(elm).attr("rel").match(/(.+)/);
				
                return ret[1];
     

    },
    getInitialTab: function() {
			var favoriteTab=jQuery.cookie("favorite");
			
			favoriteTab= favoriteTab && favoriteTab>=0 && favoriteTab<this.menu.length ?favoriteTab : Math.round(Math.random()*(this.menu.length-1));
			return this.menu.eq(favoriteTab);
        
    }
 }
