checkBrowserWidth();

attachEventListener(window, "resize", checkBrowserWidth, false);




function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	setHomePromoWidth(theWidth)
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)kfh_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}


	if (theWidth > 1200)
	{
		setStylesheet("XtraLarge");
		document.cookie = "kfh_layout=" + escape("XtraLarge");
	}
	else if (theWidth > 990)
	{
		setStylesheet("Large");
		document.cookie = "kfh_layout=" + escape("Large");
	}
	else
	{
		setStylesheet("Small");
		document.cookie = "kfh_layout=";
	}

	setFloorPlanWidth(theWidth);

	
	return true;
};




function setHomePromoWidth(page_width)
{
	var flashDimensions = "468x252";
	if(page_width > 1200) {
		flashDimensions = "898x252";
	}
	else if(page_width > 990) {
		flashDimensions = "688x252";
	}
		
	var divId = 'flash-header';
	var imgId = 'flash-header-img';
	var divobj = document.getElementById(divId);
	var imgobj = document.getElementById(imgId);

	var wrapper = document.getElementById("wrapper");
	
	if(wrapper)
	{
		try
		{
			//only change to home promo when on home page
			if(wrapper.className.indexOf("residential-home")>=0)
			{
				if(divobj) {
					var fo = new FlashObject("/images/promos/home_2009/Flash_"+flashDimensions+".swf", "flash", "100%", "252", "8", "#ffffff");
					//var fo = new FlashObject("/images/promos/home_2009/kfh_898x252.swf", "flash", "100%", "252", "8", "#ffffff");
					fo.addVariable("newLink", "/Register.aspx");
					fo.write(divId);
				}

				if(imgobj) {
					imgobj.src = "/images/promos/home_2009/FINALstill_"+flashDimensions+".png";
				}
			}
		}
		catch(err)
		{
		
		}
	}
	
}


function setFloorPlanWidth(page_width)
{
	var img_width = 450;
	if(page_width > 1200)
		img_width = 900;
	else if(page_width > 990)
		img_width = 690;

	
	var img_obj = document.getElementById('property_floorplan_img');
	if(img_obj) 
	{
		var split_arr = img_obj.src.split("&w=");
		var split2_arr = split_arr[1].split("&h=");
	        //alert(img_width + " " + page_width);
		document.getElementById('property_floorplan_img').src = split_arr[0] + "&w=" + img_width + "&h=" + split2_arr[1];
	}
		
};




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};




function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};