jQuery(document).ready(function() {
	var $ = jQuery.noConflict();

	function myTabs(div){
		//make sure we need to do anything at all
		if ($('.'+div+'_tab').length > 0)
		{
			//check if cookie exists
			var cookie = $.cookie(div + '_tabs');
			
			//confirm desired div exists
			if($('#'+cookie).length > 0)
			{
				//if so hide others and add active class to link
				$('.' + div + '_tab:not(#'+cookie+')').addClass('off-screen');
				$('#'+cookie).removeClass('off-screen');
				$('a[href="#'+cookie+'"]').addClass('tab-current').parent('li').addClass('tab-current');			
			}
			else //if cookie doesnt exist show first tab
			{
				//if this is not the first tab, hide it
				$('.' + div + '_tab:not(:first)').addClass('off-screen');
				//to fix u know who
				$('.' + div + '_tab:first').removeClass('off-screen');
				//ad active class to first link
				$('ul#' + div + '_htabs li:first').find('a').addClass('tab-current');
				$('ul#' + div + '_htabs li:first').addClass('tab-current');
			}
	
		 //when we click one of the tabs
		 $('ul#' + div + '_htabs li a').click(function(){
			
			$('ul#' + div + '_htabs li').removeClass('tab-current');
			$(this).parents('li').addClass('tab-current');
			//add an active class to the links							   
			$(this).parents('ul#' + div + '_htabs').find('li a.tab-current').toggleClass('tab-current');
			$(this).toggleClass('tab-current');
										   
			 //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+')').addClass('off-screen');
			 //fix
			 if ($.browser.msie && $.browser.version.substr(0,3) == "6.0") 
			 {
				$('.' + div + '_tab#' + stringref).removeClass('off-screen');
			 }
			 else
			 {
				 //display our tab fading it in
				 $('.' + div + '_tab#' + stringref).removeClass('off-screen');
			 }

			 //stay with me
			 return false;
		 });
		}
	}
	myTabs('business_gallery');
	myTabs('user_message');
	myTabs('user_information');
	
	
	//now with cookie suppport
	function myTabRedirect(div){
			//check if cookie exists
			var cookie = $.cookie(div + '_tabs');

			//if cookie is not null show the conresponding tab
			if($('#'+cookie).length > 0)
			{
				//if so hide others and add active class to link
				$('.' + div + '_tab:not(#'+cookie+')').remove();
				$('#'+cookie).show();
				$('a[href="#'+cookie+'"]').addClass('tab-current').parent('li').addClass('tab-current');			
			}
			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('tab-current');
				$('ul#' + div + '_htabs li:first').addClass('tab-current');
			}
	
		 //when we click one of the tabs
		 $('ul#' + div + '_htabs li a').click(function(){
			
			$('ul#' + div + '_htabs li').removeClass('tab-current');
			$(this).parents('li').addClass('tab-current');
			//add an active class to the links							   
			$(this).parents('ul#' + div + '_htabs').find('li a.tab-current').toggleClass('tab-current');
			$(this).toggleClass('tab-current');
										   
										   
			 //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');
	myTabRedirect('event_list');
});

