$(document).ready(function() {
	function goToResidential() {
		$('#splashBg').remove();
		$('#wrap').show();
	}

	function goToCommercial() {
		document.location.href = _commercialSiteRoot;
	}
		
	if (_cameFromOutside) {
			if ($.cookie('whichCurrellSite') == 'Residential') {
				$('#splashWipe').remove();
				goToResidential();
			} else if ($.cookie('whichCurrellSite') == 'Commercial') {
				$('#splashWipe').remove();
				goToCommercial();
			} else {
				splash = '<div id="splashBg"><div id="splashWrap"><p id="sWelcome">Welcome to Currell.com,</p><p id="sPlease">please select which site you would like to visit</p><div id="sBanner"><div class="clearfix"><a href="#" id="sRes"><img src="/images/splash/residential.jpg"  border="0" alt="Currel Residential - Residential Property Sales and Lettings"/></a><a href="#" id="sCom"><img src="/images/splash/commercial.jpg"  border="0" alt="Currel Commercial - Commercial Property Sales and Lettings"/></a></div></div> <form><input type="checkbox" name="sRemember" id="sRemember" /><label for="sRemember">Remember my selection</label></form><p><img src="/images/splash/logos.jpg"  border="0" alt="rightmove, propertyfinder, globrix, prime location, email4property, findaproperty"/></p></div></div>';
				$('#splashWipe').remove();
				$('body').prepend(splash);
				$('#wrap').hide();
				pageSize = ___getPageSize();
				$('#splashBg').css({ width: pageSize[0], height: pageSize[1] });
			}
		}
		else {
			$('#splashWipe').remove();
		}

		// Thanks http://leandrovieira.com/
		$(window).resize(function() {
			// Get page sizes
			var arrPageSizes = ___getPageSize();
			// Style overlay and show it
			$('#splashBg').css({
				width: arrPageSizes[0],
				height: arrPageSizes[1]
			});
		});

		function setRememberCookie(whichSite) {
			if ($('#sRemember').is(':checked')) {
				$.cookie('whichCurrellSite', whichSite, { expires: 365 });
			}
		}

		function splashClick() {
			$('#splashWrap a#sRes').click(function() {
				setRememberCookie('Residential');
				goToResidential();
			});

			$('#splashWrap a#sCom').click(function() {
				setRememberCookie('Commercial');
				goToCommercial();
			});
		}

		splashClick();

		/* THIRD FUNCTION
		* getPageSize() by quirksmode.com
		*
		* @return Array Return an array with page width, height and window width, height
		*/
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if (document.documentElement.clientWidth) {
					windowWidth = document.documentElement.clientWidth;
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			// for small pages with total height less then height of the viewport
			if (yScroll < windowHeight) {
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if (xScroll < windowWidth) {
				pageWidth = xScroll;
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
			return arrayPageSize;
		};

		//hovers for buttons

		$('#sRes').hover(function() {
			$(this).children('img').attr("src", "/images/splash/residentialOn.jpg");
		}, function() {
			$(this).children('img').attr("src", "/images/splash/residential.jpg");

		});

		$('#sCom').hover(function() {
			$(this).children('img').attr("src", "/images/splash/commercialOn.jpg");
		}, function() {
			$(this).children('img').attr("src", "/images/splash/commercial.jpg");

		});
		//if the url has newspass parameter in it, skip portal and go straight to residential
		comeFrom = window.location;
		query = /newsPass/;
		if(query.test(comeFrom)) {
		//	alert('woo!');
			$('#splashWipe').remove();
			goToResidential();
		}



});
