function toggleRegion()
{
	$j(document).unbind('keydown');

	if ($j('a').hasClass('languageSel')) $j('div').filter('.regionSelectBox').css('right',($j('.languageSel').width()+10)+'px');
	
	if ($j('div').filter('.regionSelectBox').attr('class').indexOf('show') >= 0)
	{
		$j('div').filter('.regionSelectBox').removeClass('show');
		$j('div').filter('.regionSelectBox').children('a').unbind('keydown');
		removeClassFocus();
		$j('div').filter('.regionSelectBox').children('a').focus();
	}
	else
	{
		$j('div').filter('.regionSelectBox').addClass('show');
		
		$j('div').filter('.regionSelectBox').find('a.active').addClass('focus');
		$j('div').filter('.regionSelectBox').find('a.active').focus();
					
		$j(document).bind('keydown',function(event){
			if(event.keyCode == '9')
			{
				$j('div').filter('.regionSelectBox').removeClass('show');
				removeClassFocus();
				$j('div').filter('.regionSelectBox').children('a').focus();
			}
			
			if(event.keyCode == '27')
			{
				toggleRegion();
				removeClassFocus();
				return false;
			}
			
			if(event.keyCode == '38')
			{
				setRegionSelectorFocus('up');
				return false;
			}
			
			if(event.keyCode == '40')
			{
				setRegionSelectorFocus('down');
				return false;
			}
		});
		
		
	}
}

function setRegionSelectorFocus(state)
{
	$j('ul').filter('.regionsRoot').find('a').each(function(i){
		if ($j(this).attr('class').indexOf('focus') >=0)
		{
			if (state == 'up' && i != 0)
			{
				$j(this).removeClass('focus');
				$j($j('ul').filter('.regionsRoot').find('a')[i-1]).addClass('focus');
				$j($j('ul').filter('.regionsRoot').find('a')[i-1]).focus()
			}
			
			
			if(state == 'down' && ((i+1) != $j('ul').filter('.regionsRoot').find('a').length))
			{
				$j($j('ul').filter('.regionsRoot').find('a')[i+1]).addClass('focus');
				$j($j('ul').filter('.regionsRoot').find('a')[i+1]).focus()
				$j(this).removeClass('focus');
				return false;
			}
		}
	});
}

function removeClassFocus()
{
	$j('ul').filter('.regionsRoot').find('a.focus').removeClass('focus');
}

/* Event Mouse Click Select*/
$j('div').filter('.regionSelect').children('.cta').click(toggleRegion);
	
/* Event Focus Select */
$j('div').filter('.regionSelect').children('.cta').focus(function(){
	$j(document).bind('keydown',function(event){
		if(event.keyCode=='40')
		{
			toggleRegion();
		}
		
		if(event.keyCode=='9')
		{
			$j(document).unbind('keydown');
		}
	});
});

/* Event hover of select dropdown */
$j('div').filter('.regionLanguageSelect').hover(function(){
	$j('div').filter('.regionLanguageSelect').children('a').unbind('keydown');
	$j(document).unbind('click');
},function(){
	$j(document).bind('click',function(){
		$j('div').filter('.regionSelectBox').removeClass('show');
		$j('div').filter('.regionLanguageSelect').children('a').unbind('keydown');
		removeClassFocus();
	});
});

$j.extend(
{
	getCookieParamValue: function(cookieName, strParamName, operator, seperator)
	{
		var strReturn = "";
		var bFound=false;
		var cValue = readCookieValue(cookieName);
		
		var cmpstring = strParamName + operator;
		var cmplen = cmpstring.length;
		
		if ( cValue.indexOf(seperator) > -1 )
		{
			var strQueryString = cValue.substr(cValue.indexOf(seperator)+1);
			var aQueryString = strQueryString.split(seperator);
			
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
			{
				if (aQueryString[iParam].substr(0,cmplen)==cmpstring)
				{
					var aParam = aQueryString[iParam].split(operator);
					strReturn = aParam[1];
					bFound=true;
					break;
				}
			}
		}
		
		if (bFound==false) return null;
		return strReturn;
	}
});

$j('#regionSelectionHomePage').removeClass('hidden');

/* Alert Message */
if($j.getCookieParamValue('usercontext','country','#','|') == 'AU') 
{
	$j.ajax({
		type: "GET",
		url: "/alert.html",
		dataType: "html",
		success: function(msg)
		{
			$j('div.main').prepend('<div class="importantMsg"><div class="importantMsgContent"></div></div>');
			$j('div').filter('.importantMsgContent').html(msg);
			if (msg.length > 5)
			{
				$j('div').filter('.importantMsg').slideDown();
			}
		}
	});
}

initSiteSections();