function getCookie(name){
	if (document.cookie && document.cookie.indexOf(name) > -1)
    	return true;
  	else
		return false;
}

function getCookieValue(name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
		return "";
}
	
function setCookie(name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
	
// IF COOKIE IS SET
if (getCookie('belco')) {
	
}
// IF COOKIE IS NOT SET
else {	
	if (document.title.substr(0,6) != 'splash')
		window.location='/index.php/splash';
}
	
$(document).ready(function() {
	$('#nw_sidebar').hide();
	$('#us_sidebar').hide();
	$('.nw_footer').hide();
	$('.us_footer').hide();
	$('body').supersleight();
	$("a.pop").fancybox();
	
	// SHOW CORRECT STAMP SIDEBAR BASED ON REGIONAL COOKIE SELECTION
	if (getCookieValue('belco') == 'us'){
		$('#us_sidebar').show();
		$('.us_footer').fadeIn("slow")
	}
	else {
		$('#nw_sidebar').show();
		$('.nw_footer').fadeIn("slow")
	}
	
	// HIDE FEATURE TEXT
	$('#feature_wrapper .feature_text_bg').hide();
	$('#feature_wrapper .feature_text').hide();
	
	// BELCO HOUSE HOOVER
	$('#house').children().each(function(index) {
		var image 	= index + 1;
		var div		= $(this).find('div:first');
		$(this).prepend('<img src="/client/assets/images/feature/numbers/off/' + image + '.png" alt="' + image + '"/>');
  		
		var img 	= $(this).find('img:first');
		var source	= img.attr('src');
		
		$(this).hover(
			function(){
				$('#house_wrapper').css('z-index',1001);
				$(this).css('z-index',1001);
				div.show();
				img.attr('src',source.replace("off","on"));
			},
			function() {
				$('#house_wrapper').css('z-index',0);
				$(this).css('z-index',0);
				div.hide();
				img.attr('src',source.replace("on","off"));
			}
		);
  	});
	
	// RUN SLIDESHOW AFTER PAGE IS FULLY DOWNLOADED
	$(window).load(
		 function() {
		 		
  			$('#feature_slideshow').cycle({
				fx:     'fade',
				timeout: 14000

			});

			$('#feature_wrapper .feature_text_bg').slideDown({duration: 1000, easing: "easeInQuad"});
			$('#feature_wrapper .feature_text').slideDown({duration: 1000, easing: "easeInQuad"});
			
			
		}
	);
	
	
	
	// HANDLE SPLASH PAGE REGION 1 HOVER/CLICK
	$('#region1').hover(
		function(){
			var source = $(this).find('img').attr('src').replace('.png','_ovr.png');
			$(this).find('img').attr('src',source);
		},
		function(){
			var source = $(this).find('img').attr('src').replace('_ovr.png','.png');
			$(this).find('img').attr('src',source);
		}
	);
	
	$('#region1').click(
		function () {
			$(this).html('<img src="/client/assets/images/_template/region1_ovr.png" alt="region1">');
			//$(this).prev().html('<img src="/client/assets/images/_template/region2.png" alt="region2">');
			//$('#regiontitle').html('<h1>You selected the Pacific Northwest region</h1>');
			//$(this).fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
			setCookie('belco','nw','365');
			setTimeout("window.location='/index.php/home'",50);

  		}
	
	);
	
	// HANDLE SPLASH PAGE REGION 2 HOVER/CLICK
	$('#region2').hover(
		function(){
			var source = $(this).find('img').attr('src').replace('.png','_ovr.png');
			$(this).find('img').attr('src',source);
		},
		function(){
			var source = $(this).find('img').attr('src').replace('_ovr.png','.png');
			$(this).find('img').attr('src',source);
		}
	);
	
	$('#region2').click(
		function () {
			$(this).html('<img src="/client/assets/images/_template/region2_ovr.png" alt="region2">');
  			//$(this).next().html('<img src="/client/assets/images/_template/region1.png" alt="region1">');
  			
  			//$(this).fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
			setCookie('belco','us','365');
			setTimeout("window.location='/index.php/home'",50);
  		}
	
	);
	
	// HANDLE SPLASH PAGE REGION 3 HOVER/CLICK
	
	$('#region3').click(
		function () {
  			//$(this).fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
			setCookie('belco','us','365');
			setTimeout("window.location='/index.php/home'",50);
  		}
	
	);
	
	// PRELOAD IMAGES
	jQuery.preLoadImages(
		"/client/assets/images/_template/region1_ovr.png", 
		"/client/assets/images/_template/region2_ovr.png",
		"/client/assets/images/feature/numbers/on/1.png",
		"/client/assets/images/feature/numbers/on/2.png",
		"/client/assets/images/feature/numbers/on/3.png",
		"/client/assets/images/feature/numbers/on/4.png",
		"/client/assets/images/feature/numbers/on/5.png",
		"/client/assets/images/feature/numbers/on/6.png",
		"/client/assets/images/feature/numbers/on/7.png",
		"/client/assets/images/feature/numbers/on/8.png"
	);


});
