$(function() {

	if (!$.cookie('marketArea') && ( $('.flor_content').length > 0 || $('.kyin_content').length > 0 )) {
		// display the market area popup
		showMarketAreaPopup();
	}
	else {
		// display the proper content areas
		displayMarketAreaContent(getMarketArea());
	}

});

function showMarketAreaPopup(callback, callback_params){
	var cb = "";

	if (typeof(callback) != 'undefined' && callback != null && callback != '')
	{
		window.cb_params = ((typeof(callback_params) != 'undefined' && callback_params != null && callback_params != "") ? callback_params : null);
		cb = callback+"("+(window.cb_params ? "window.cb_params" : "")+");";
	}

	message = '<h2 style="text-align:center">Please Choose your Market Area</h2>';
	var content = new Array();
	var index = 0;
	content[index++] = message;
	content[index++] = '<div style="margin-top:1em;text-align:center"><strong><a href="#" onclick="setMarketAreaCookie(\'kyin\'); ' + cb + ' $(\'#ex_dis\').hide();return false;">Kentucky/Indiana/Ohio</a></strong>&#160;&#160;or&#160;&#160;<strong><a href="#" onclick="setMarketAreaCookie(\'fla\'); ' + cb + ' $(\'#ex_dis\').hide();return false;">Florida</a></strong>&#160;&#160;or&#160;&#160;<strong><a href="#" onclick="setMarketAreaCookie(\'other\'); ' + cb + ' $(\'#ex_dis\').hide();return false;">Other</a></strong></div>';
	$("#ex_dis").html(content.join("")).show();
	scrollTo(0,0);
}

function setMarketAreaCookie(area) {
	$.cookie('marketArea', area, {expires: 3650, path: '/', secure: false});
	displayMarketAreaContent(area);
}

function displayMarketAreaContent(area) {
	
	switch (area)
	{
		case 'fla':		$('.flor_content').show();
						$('.kyin_content').hide();
						$('.content_marker').hide();
						$('li.only_fla').show();
						$('li.only_kyin').hide();
						break;
		case 'other':
		case 'kyin':	$('.flor_content').hide();
						$('.kyin_content').show();
						$('.content_marker').hide();
						$('li.only_fla').hide();
						$('li.only_kyin').show();
						break;
		default:		$('.flor_content').show();
						$('.kyin_content').show();
						$('.content_marker').show();
						$('li.only_fla').show();
						$('li.only_kyin').show();
						break;
	}

}

function getMarketArea() {
	return $.cookie('marketArea');
}

//	This function will check the area cookie and follow the appropriate link
//	linksList = { fla: Florida , kyin: Kent/Indi }
function cookieLink(linksList)
{
	if (!$.cookie('marketArea'))
	{
//		alert(""+linksList);
		showMarketAreaPopup('cookieLink', linksList);
	}
	
	var follow_link = "";
	var target = "";

	switch (getMarketArea())
	{
		case "fla":		follow_link = linksList['fla']['link'];
						target = linksList['fla']['target'];
						break;
		case 'other':
		case "kyin":	follow_link = linksList['kyin']['link'];
						target = linksList['kyin']['target'];
						break;
		default:
	}
	
	if (target == '_blank')
		target_blank(follow_link);
	else if (follow_link != '')
		document.location.href = follow_link;
}

//	This function will check the area cookie and load the appropriate app
//	The app iframe must have id="marketAreaAppFrame"
//	linksList = { fla: Florida , kyin: Kent/Indi }
function loadMarketAreaApp(linksList)
{
	if (!$.cookie('marketArea'))
	{
		showMarketAreaPopup('loadMarketAreaApp', linksList);
	}

	switch (getMarketArea())
	{
		case "fla":		document.getElementById('marketAreaAppFrame').src = asciiCharsReplace(linksList['fla']);
						break;
		case 'other':
		case "kyin":	document.getElementById('marketAreaAppFrame').src = asciiCharsReplace(linksList['kyin']);
						break;
		default:
	}
}


