// #@(#)jquery.togglecontent.v1.1.js	1.2 13:24:33,10/08/04 (yy/mm/dd)

$(document).ready(function(){

	/**
	 * @author jp
	 * JS Tabs v1.1
	 * Relevant styles for these can be found in styled-boxes.css
	 * Updates in this version:
	 * - Container names made more semantic
	 * - ".tabContent" references removed meaning tabContent is no longer restricted to divs
	 * 
	 * v1.2
	 * - Cookie override added to support MyJobsite section
	 */
	
	//START COOKIE OVERRIDE
	var strLoc = window.location;
	var regex = /#mj.+/g;
	var arrMatch = regex.exec(strLoc);
	
	// Set cookie to tab defined in url if present
	if (arrMatch != null){
		$.cookie('mjWhichTab', arrMatch);
	};
	//END COOKIE OVERRIDE
	
	// Hide all tabs
	$('.tabsContainer .tabContent').hide();
	
	// Set active default tab
	strCurrentTab = $.cookie('mjWhichTab')
	
	// Check to see if cookie is empty
	if (strCurrentTab != null){
		// Loop through tab hrefs and check for links
		setTabClass(strCurrentTab);
		
		// Show "saved" tab
		$('.tabsContainer '+ strCurrentTab).show();				
	}
	else{
	
		// Highlight default link and show default tab
		$('.tabsContainer .tabs li:first').addClass('active');		
		$('.tabsContainer .tabContent:first').show();
	}
	
	// Class added for formatting items when JS enabled
	$('.tabsContainer').addClass('jsOn');
	
	// Watch for click
	$('a.jsActivateTab, a.jsActivateTabInner').click(function(event){
		
		// Remove active class from all tab links 
		$('.tabs li').removeClass('active');
		
		// Hide all child tabs within that tabs div
		$('.tabsContainer .tabContent').hide();
		
		// Assign selected href tab reference to variable
		var strCurrentTab = $(this).attr('href');
		
		// Add active class to current link
		setTabClass(strCurrentTab);
		
		// Show selected tab
		$(strCurrentTab).show();
				
		// Set selected tab value in session cookie
		$.cookie('mjWhichTab', strCurrentTab);
		
		// If the clicked link is an inner page link scroll to top of page
		if($(this).attr('class').indexOf("jsActivateTabInner") > -1){	
			window.scrollTo(0,0);
		}
			
		event.preventDefault();
	});
	
	
	// Function loops through tab links and then assigns active class to li tag
	function setTabClass(strCurrentTab){
		$('.tabs').find('li > a.jsActivateTab').each(function(){
			var strTabName =  $(this).attr('href');
			if(strTabName === strCurrentTab){
				$(this).parent().addClass('active');
			}
		})
	}
	
	/**
	 * @author jp
	 * Toggle <div>
	 */
	// Watch for click
	$("#advancedSearchToggle").click(function(event) {
		event.preventDefault();
		
		// Find current text of link so we can toggle it...
		var strLinkText = $(this).text().toString()
		var defaultText = "More Search Options"
		var toggledText = "Fewer Search Options"			
		
		// Check text then swap out
		if(strLinkText==toggledText){
			$(this).text(defaultText)
			$(this).removeClass('toggled')
		}
		else{
			$(this).text(toggledText)
			$(this).addClass('toggled')
		}
		
		// Show box
		$("#advancedSearch").slideToggle();
	});		

	/**
	 * @author jp
	 * Fade and hide box
	 */
	$('a.jsHideMsg').click(function(event) {
		$(this).parent('.jsHide').fadeOut(500,function(){  
        	$(this).remove();  
		});  
		return false;
	});		
});

