jQuery(document).ready(function() {
	var $ = jQuery.noConflict();
	// setting the tabs in the sidebar hide and show, setting the current tab
	$('div.tabbed div').hide();
	$('div.user_tabbed div').hide();
	
	$('div.t1, .user_tabbed div.t5').show();

	$('div.tabbed ul.tabs li.t1 a').addClass('tab-current');
	$('.tabbed div.t2, .tabbed div.t3, .tabbed div.t4').addClass('tabbed_hidden');
	
	$('div.user_tabbed ul.tabs li.t5 a').addClass('tab-current');
	$('.user_tabbed div.t6, .user_tabbed div.t7, .user_tabbed div.t8').addClass('user_tabbed_hidden');


	// header TABS
	$('div.tabbed ul li a').click(function(){
		var headerClass = this.className.slice(0,2);
		$('div.tabbed div').hide();

		$('div.' + headerClass).show(); //the method by which the content is shown (to make plain, replace .fadeIn() with .show()

		$('div.tabbed ul.tabs li a').removeClass('tab-current');
		$(this).addClass('tab-current');
		$(this).removeClass('tabbed_hidden');
	});

	// header TABS
	$('div.user_tabbed ul li a').click(function(){
		var headerClass = this.className.slice(0,2);
		$('div.user_tabbed div').hide();

		$('div.' + headerClass).show(); //the method by which the content is shown (to make plain, replace .fadeIn() with .show()

		$('div.user_tabbed ul.tabs li a').removeClass('tab-current');
		$(this).addClass('tab-current');
		$(this).removeClass('tabbed_hidden');
	});

	function myTabs(div) {
		//check if cookie exists
		var cookie = $.cookie(div + '_tabs');
		//if cookie is not null show the conresponding tab
		if($(cookie).length)
		{
			//if so hide others and add active class to link
			$('.' + div + '_tab:not(#'+cookie+')').hide();
			$('#'+cookie).show();
			$('a[href="#'+cookie+'"]').addClass('active').parent('li').addClass('active');			
		}
		else //if cookie doesnt exist show first tab
		{
			//if this is not the first tab, hide it
			$('.' + div + '_tab:not(:first)').hide();
			//to fix u know who
			$('.' + div + '_tab:first').show();
			//ad active class to first link
			$('ul#' + div + '_htabs li:first').find('a').addClass('active');
			$('ul#' + div + '_htabs li:first').addClass('active');
		}
		
		//when we click one of the tabs
		$('ul#' + div + '_htabs li a').click(function(){
		
			$('ul#' + div + '_htabs li').removeClass('active');
			$(this).parents('li').addClass('active');
			//add an active class to the links							   
			$(this).parents('ul#' + div + '_htabs').find('li a.active').toggleClass('active');
			$(this).toggleClass('active');
										   
										   
			 //get the ID of the element we need to show
			 stringref = $(this).attr("href").split('#')[1];
			 //write a cookie with the div id name
			  $.cookie(div + '_tabs', stringref);
			
			 //hide the tabs that doesn't match the ID
			 $('.' + div + '_tab:not(#'+stringref+')').hide();
			 //fix
			 if ($.browser.msie && $.browser.version.substr(0,3) == "6.0") {
				$('.' + div + '_tab#' + stringref).show();
			 }
			 else {
				 //display our tab fading it in
				 $('.' + div + '_tab#' + stringref).show();
			 }

			 //stay with me
			 return false;
		});
	}
	myTabs('sidebar');
	myTabs('fragment');
	myTabs('content');


	function myTabRedirect(div){
		//check if cookie exists
		var cookie = $.cookie(div + '_tabs');

		//if cookie is not null show the conresponding tab
		if(cookie)
		{
			//if so hide others and add active class to link
			$('.' + div + '_tab:not(#'+cookie+')').remove();
			$('#'+cookie).show();
			$('a[href="#'+cookie+'"]').addClass('active').parent('li').addClass('active');			
		}
		else //if cookie doesnt exist show first tab
		{
			//if this is not the first tab, hide it
			//var first = $('.' + div + '_tab:first'); 
			//$('.' + div + '_tab:not(:first)').hide();
			//to fix u know who
			$('.' + div + '_tab:first').show();
			var id = $('.' + div + '_tab:first').attr("id");
			$.cookie(div + '_tabs', id); 
			
			$('.' + div + '_tab:first').addClass('active');
			//ad active class to first link
			$('ul#' + div + '_htabs li:first').find('a').addClass('active');
			$('ul#' + div + '_htabs li:first').addClass('active');
		}

		//when we click one of the tabs
		$('ul#' + div + '_htabs li a').click(function(){
			
			$('ul#' + div + '_htabs li').removeClass('active');
			$(this).parents('li').addClass('active');
			//add an active class to the links							   
			$(this).parents('ul#' + div + '_htabs').find('li a.active').toggleClass('active');
			$(this).toggleClass('active');
									   
									   
			//get the ID of the element we need to show
			stringref = $(this).attr("href").split('#')[1];
			//write a cookie with the div id name
			$.cookie(div + '_tabs', stringref);
			
			window.location.reload(true);
			
			//stay with me
			return false;
		});
	}
	myTabRedirect('search');	
});


