// JavaScript Document
function QueryString() {
	this.planCode = '';
	this.audience = '';
}

function parseQueryString(myQueryString) {
	
	var vars = myQueryString.replace("?","").split("&");
	var pairs = Array();
	for (var i=0;i<vars.length;i++) {
		pairs[i] = vars[i].split("=");
	}
	for (i in pairs) {
		if (pairs[i][0] == "a")
			queryString.audience = pairs[i][1]
		else if (pairs[i][0] == "p")
			queryString.planCode = pairs[i][1]
	}
	if (queryString.audience != '') {
		setCookie("Audience", queryString.audience)
	} else if (getCookie("Audience") != null) {
		queryString.audience = getCookie("Audience")
	} 
	
	if (queryString.planCode != '') {
		setCookie("PLANCODE", queryString.planCode, 30)
		//setBranding(queryString.planCode)
	} else if (getCookie("PLANCODE") != null) {
		queryString.planCode = getCookie("PLANCODE");
		//setBranding(queryString.planCode)
	}
}


function setCookie(c_name,value,expiredays,path){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) +
	((path==null) ? ";path=/" : ";path="+path);
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function setFooter(myRegion) {
	var date = new Date();
	var htmlStr = '<p>&copy; '+date.getFullYear()+' ';
	switch (myRegion) {
		
		case "ID" :
			htmlStr += 'Regence BlueShield of Idaho.</p>'
			break;
		case "OR" :
			htmlStr += 'Regence BlueCross BlueShield of Oregon.</p>'
			break;
		case "UT" :
			htmlStr += 'Regence BlueCross BlueShield of Utah.</p>'
			break;
		case "WARBS" :
			htmlStr += 'Regence BlueShield.</p>'
			break;
		default:
			htmlStr += 'The Regence Group.</p>'
			break;
	}
	
	$("#trayLeft").html(htmlStr);
}

function writeSWFObject(planCode,audience) {
	var planXML = '';
	var htmlStr = '';
	var cType = '';
	cType = (calcType == undefined || calcType == '') ? '' : calcType+'-';
	var so = new SWFObject(cType+"calculator/shell.swf", "shell", "750", "500", "8", "#ccc");
	switch (planCode) {
		case "ID" :
			htmlStr = "Idaho"
			planXML = "idaho.xml"
			break;
		case "OR" :
			htmlStr = "Oregon"
			planXML = "oregon.xml"
			break;
		case "UT" :
			htmlStr = "Utah"
			planXML = "utah.xml"
			break;
		case "WARBS" :
			htmlStr = "Washington"
			planXML = "regence.xml"
			break;
		default :
			break;
	}
	so.addVariable("configUrl", "config_"+planXML );
	if (cType == '' && (audience == null || audience == ''))
		audience = "Individual"
	if (audience != null && audience != '')
		so.addVariable("planType", audience.toLowerCase() );
	so.addParam("base", "/hsa/"+cType+"calculator/");
	so.write("flashcontent");
	$('#splash').addClass('hidden');
	$('#flashcontainer').removeClass('hidden');
	$('#trayRight span').html(htmlStr);
	setFooter(planCode);
}

$(document).ready(function(){
	
	queryString = new QueryString;

	// Parse Hash onLoad, after anchor link clicks, and at interval to check for browser button
	currentState='';
	defaultState='landing';
	
	// check for queryString object and cookie respectivley and show flash based on PLANCODE
	if (window.location.search.toString().length > 0) {
		parseQueryString(window.location.search.toString())
		writeSWFObject(queryString.planCode,queryString.audience);
	} else if (getCookie("PLANCODE") != null) {
		writeSWFObject(getCookie("PLANCODE"),getCookie("Audience"))
	} else {
		// show chooser div
		$('#splash').removeClass("hidden")
	};
	
	$("#splash a").click(function(){
		parseQueryString($(this).attr("href"))
		writeSWFObject(queryString.planCode,queryString.audience);
		return false;
	});
	
	$("#trayRight a").click(function(){
		$('#splash').removeClass('hidden');
		$('#flashcontainer').addClass('hidden');
	});
});