var papersUCase = new Array("Ann-Arbor","Bay-City","Detroit","Flint","Grand-Rapids","Jackson","Kalamazoo","Muskegon","Saginaw","Statewide");
var links = new Array("home","news","business","sports","entertainment","living","interact");

var fullurl = window.location.href;
var linkDomain = fullurl.replace(/^(http:\/\/)?(.+?)(\.net|\.com).*$/, "$2$3");
if (linkDomain.match("com")) { linkDomain = linkDomain.replace(/^([^\.]+?)\./, "www."); }
var cookieDomain = linkDomain.replace(/^[^\.]+\.([^\.]+\.com)$/, "$1");
var uri = fullurl.replace(/^.+(\.net|\.com)\/(.*)$/, "$2");
uri = uri.replace(/(index|\?).*$/, "");
uri = uri.replace(/\/$/, "");

//alert("linkDomain is " + linkDomain + " cookieDomain is " + cookieDomain + " uri is " + uri);

var tmp;
//var xRegion;
var flag = 0;

var papersUCaseByKey = new Array();
for (x=0; x<papersUCase.length; x++) { tmp = papersUCase[x].toLowerCase(); papersUCaseByKey[tmp] = papersUCase[x]; }

if ((uri.match("http"))||(uri == "")) {
	var theSection = "home";
	var theRegion = "statewide";
} else if (uri.match("/")) {
	// If uri DOES match one of the items defined in array "papersUCase"
	var uri = uri.split("/");
	var theSection = uri[0];
	var theRegion = uri[1];
} else {
	for (x=0; x<papersUCase.length; x++) { tmp = papersUCase[x].toLowerCase(); if (tmp.match(uri)) { flag += 1;  } else { flag += 0; } }
	// If uri DOES match one of the items defined in array "papersUCase"
	if (flag == 1) {
		var theSection = "home";
		var theRegion = uri;
	} else {
		flag=0; // reset flag
		for (x=0; x<links.length; x++) { tmp = links[x].toLowerCase(); if (tmp.match(uri)) { flag += 1;  } else { flag += 0; } }
		// If uri DOES match one of the items defined in array "links"
		if (flag == 1) {
			var theSection = uri;
			var theRegion = "statewide";
		// If uri doesn't match one of the items defined in array "links"
		} else {
			var theSection = "home";
			var theRegion = "statewide";
		}
	}
}

//alert("theSection is " + theSection + " and theRegion is " + theRegion);

function checkCookie(name, theValue, resp) {
	if (getCookie(name) == "") {
		if (resp=="YES")  setCookie(name, theValue); 
		else if (resp=="NO")  setCookie('region', 'Statewide');
		return true; 	
	}	
	else { 
		return false; 
	}	
}

var ckeValue;

function setCookie(name, theValue) { 
	var today = new Date();
	var theCke = getCookie(name);
	var expiry = new Date(today.getTime() + 5 * 365 * 24 * 60 * 60 * 1000);
	ckeValue = theValue;
	document.cookie=name+"="+escape(ckeValue)+"; expires=" + expiry.toGMTString() + "; domain=" + cookieDomain + "; path=/";
}

function getCookie(name) {
	var CookieString = document.cookie;
	var CookieSet = CookieString.split(';');
	var SetSize = CookieSet.length;
	var CookiePieces = new Array();
	var ReturnValue = "";
	var x = 0;
	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
		CookiePieces = CookieSet[x].split('=');
		if (CookiePieces[0].substring(0, 1) == ' ') {
			CookiePieces[0] = CookiePieces[0].substring(1, CookiePieces[0].length);
		}
		if (CookiePieces[0] == name) {
			ReturnValue = unescape(CookiePieces[1]);
		}
	}
	return ReturnValue + "";
}

function clearCookie(name) {
	document.cookie=name+"="+"; expires=-1" + "; domain=" + cookieDomain + "; path=/";	
	return false;
}

var theCookie = getCookie('region');
if (theCookie == "") { theCookie = "Statewide"; }

function updateLev1Links() {
	//TOPRAIL LEVEL1 LINK REWRITES
	if (theCookie != "Statewide") {
		for (x=0; x<links.length; x++) {
			if (links[x] == "home") { var part0 = ""; } else { var part0 = links[x].toLowerCase() + "/"; }
			document.getElementById(links[x]).href="http://" + linkDomain + "/" + part0 + theCookie.toLowerCase() + "/";
		}
	}
}

function updateRegBarLinks() { 
	//REGION BAR LINK REWRITES
	for (x=0; x<papersUCase.length; x++) {
		if (theSection == "home") { var part1 = ""; } else { var part1 = theSection + "/"; }
		if ((papersUCase[x] == "Statewide")&&(theSection == "home")) { 
			var part2 = "#statewide";
		} else if (papersUCase[x] == "Statewide") {
			var part2 = "";
		} else {
			var part2 = papersUCase[x].toLowerCase() + "/";
		}
		document.getElementById(papersUCase[x]).href="http://" + linkDomain + "/" + part1 + part2;
	}

	//REGION BAR HIDE ITEMS
	if (theRegion.length > 1) { 
		var rm_el_region = document.getElementById(papersUCaseByKey[theRegion]); 
		rm_el_region.parentNode.style.padding = "0";
		rm_el_region.parentNode.removeChild(rm_el_region); 

		var rm_el_p = document.getElementById('Region_Menu_UL');		
		document.getElementById(rm_el_p.getElementsByTagName('a')[0].id).parentNode.style.border = "0";
		document.getElementById(rm_el_p.getElementsByTagName('a')[0].id).parentNode.style.paddingLeft = "0";

		var rgn_img = document.getElementById('Region_Image');
		rgn_img.style.backgroundImage = "url('/08design/images/regions_bar_" + theRegion + ".gif')";
				
		//document.getElementById(papersUCaseByKey[theRegion]).style.display = "none";
	}

}



// START: jQuery 

// jquery + javascript positioning
function positionThis(zoverlay,zlink) {	
	var static_box = jQuery(zlink);
	var moving_box = jQuery(zoverlay);	
	
	//adjust width and height
	if(zlink.search(/regions_bar/i)>0) { 
		var sb_width = (static_box.width()-moving_box.width()) / 2; 
		var mb_height = moving_box.height() - 2;
	}	else {
		var sb_width = static_box.width() + 6; 
		var mb_height = (moving_box.height()/2) - 8;
	}		
	
	//set coordinatees
	var coordinates = static_box.offset();
	var x = coordinates.left + sb_width;
	var y = coordinates.top - mb_height;	
	position(x,y,zoverlay);
}
function position(x,y,overlay) {
	var moving_box = jQuery(overlay);
	moving_box.css("position","absolute");
	moving_box.css("top",y);
	moving_box.css("left",x);
}

	
// jQuery for Roundbox Overlay
jQuery(function ($) {	

	//Start: IF Region Bar exist
	if (document.getElementById('Module-Statewide_Regions_Bar')) {
	
			//check if region cookie exist
			if ( checkCookie('region',$('#Overlay_Region_Name').text()) && getCookie('region') != "Statewide" ) { 		
				//Display Individual Region Overlay
				$('#Overlay_Region_Save').jqm({ overlay: 0.01, trigger: 'a.Overlay_Region_Save', toTop: true })
				$('.region_menu_box ul li a').click(function(){																								 
						positionThis('#'+$(this).attr('class'),'#'+$(this).attr('id')); //position overlay 			
						$('#Overlay_Region_Name').text($(this).text());	//display region name inside the overlay	
						$('#Region_Save_Name').attr({ value: $(this).attr('id') }); //assign region id inside hidden input				}
						return false; 
					});
			} else {		
				//$('.region_menu_box ul li:first').css({ border: '0', paddingLeft: '0'});				
			}
		
			if ( !checkCookie('region',$('#Overlay_Region_Name').text()) == "" && getCookie('region_intro') != "visited" ) {
				//Display Region Bar Description Overlay and hide after 20sec	
				positionThis('#Overlay_Region_Desc','#Module-Statewide_Regions_Bar');
				$('#Overlay_Region_Desc').show();
				//window.HideBoxDelay = setTimeout( function(){	$('#Overlay_Region_Desc').animate({opacity: "hide"}, "fast"); }, 20000);
			}
				
			
			//Yes or No answer inside overlay
			$('div.yes_no a').click(function(){			
					checkCookie('region',$('#Region_Save_Name').attr('value'),$(this).text()); //Set cookie for this region
					$('#Overlay_Region_Save').jqmHide();
					var region_dir = $('#Region_Save_Name').attr('value').toLowerCase()
											if (theSection == "home") { var part1 = ""; } else { var part1 = theSection + "/"; }
					if ((theCookie == "Statewide")||(theCookie == "")) { region_dir = ""; }
					window.location.href = "http://" + linkDomain + "/" + part1 + region_dir;
					return false;
			});
			
			//"X Close" button 
			$('div.x_close a').click(function(){			
					$('#Overlay_Region_Desc').animate({opacity: "hide"});
					if(window.HideBoxDelay) { clearTimeout(window.HideBoxDelay); }		
					setCookie('region_intro', 'visited');
					return false;
			});
	
	}//End: IF Region Bar Exist
	
});	

// END: jQuery

var m_regionsbar = theCookie;

