/**
 *	Top Gear Recruitment
 *
 *	@pages			all
 *	@copyright		2010 Phil Thompson
 *	@author			Phil Thompson
 *	@since			26/04/2010
 *	@lastmodified	15/05/2010
 *
 *	Contents 
 *		showHideBranches
 *		document.ready
 */

/**
 * Popup window
 */
function popWin(url,width,height,title) {
    
    window.open(url, title, 'width=' + width + ',height=' + height + ',scrollbars=Yes');
}


/**
 * Confirm window
 */
function confirmAction(msg) {
    
    if(confirm(msg)) {
                    
        return true;
    }
    else {
                    
        return false;
    }   
}

/**
 *	showHideBranches()
 *	All TopGear branches are shown by default for accessibility. We then hide them
 *	and show only the one the user wants (or the default)
 */
function showHideBranches(CSSclass){
	
	var targetBranch, windowHash, branchClass = '';
	
	windowHash = window.location.hash;
	
	if(CSSclass != undefined){
		branchClass = '[class*="' + CSSclass + '"]';
	}
	
	
	// on load hide all except either the first or a pre-selected (@see windowHash) branch
	if(windowHash.length > 0 && $('#content div.column-three div' + windowHash + branchClass).length > 0){
		$('#content div.branch' + branchClass).hide();
		$('#content div.column-three div' + windowHash + branchClass).show().attr('aria-live', 'polite');
		$('#content div.column-two div' + branchClass + ' ul li a[href="' + windowHash + '"]').parent().addClass('active');
	} else{
		$('#content div.branch:not(:first)' + branchClass).hide().attr('aria-live', 'off');
		$('#content div.column-two div' + branchClass + ' ul li:first').addClass('active');
	}

	
	// show/hide branches when user clicks
	$('#content div.column-two div' + branchClass + ' ul li a').click(function(){
		$('#content div.column-two div' + branchClass + ' ul li').removeClass('active');
		$(this).parent().addClass('active');
		targetBranch = $(this).attr('href');
		$('#content div.branch' + branchClass).hide().attr('aria-live', 'off');
		$(targetBranch).show().attr('aria-live', 'polite');
		window.location.hash = targetBranch;
		return false;
	});
	
}


/**
 *	howCanWeHelpRotate
 *	Set the interval for how often the image should rotate
 */
function howCanWeHelpRotate(){

	var item = $('#how-can-we-help');
	
	if(item.length == 0){
		return false;
	}
	
	setInterval("howCanWeHelpRotateClass()", 5000);

}

/**
 *	howCanWeHelpRotateClass
 *	Add remove the relevant class to make the image rotate
 */
function howCanWeHelpRotateClass(){

	var item = $('#how-can-we-help');

	if(item.hasClass('how-can-we-help-active')){
		item.removeClass('how-can-we-help-active');
	} else{
		item.addClass('how-can-we-help-active');
	}
	
}
 

/**
 *	document.ready == window.onload
 */
$(document).ready(function(){

	$('body').addClass('js');
	
	if($('body.workers-branches').length > 0){
		showHideBranches();
	}
	
	if($('body.employers-branches-managed-services').length > 0){
		showHideBranches('first');
		showHideBranches('second');
	}
	
	howCanWeHelpRotate();
	
	
	
	
});
