$(function() {
	var photoListAuto = false;
	var photoListTimer;
	var currentHover = -1;
	$(".navigation > ul > li").hover(function() {
		if(currentHover != $(this).index()) {
			hideSecondaryNavHover();
			currentHover = $(this).index();
			var children = $(this).find(".children li");
			if(children.length > 0 && !$(this).hasClass("current_page_item") && !$(this).hasClass("current_page_parent")) {
				$(this).addClass("hovered");
				$(this).find("ul").stop(true,true).slideDown("fast").addClass("hovered");
				$(".subheader").stop(true, true).animate({"marginTop" : 20}, "fast");
			}
		}
	});
	
	$("[rel*=external]").each(function() {
		$(this).attr("target", "_blank");
	});
	
	$("#s").focus(function() {
		$(this).select();
	}).mouseup(function() {
		return false;
	});
	
	$(".logo, .feature_photo, .subheader a, .search_area, .content").hover(function() {
		hideSecondaryNavHover();
		currentHover = -1;
	});
	
	$(".photo_list > a").click(function() {
		if (!photoListAuto) {
			clearInterval(photoListTimer);
		}
		else {
			photoListAuto = false;
		}
		
		var num = $(this).index();
		$(".photo_list .active").removeClass("active");
		$(this).addClass("active");

		$(".photo_list_descriptions .description").eq(num).addClass("active");
		if($(this).attr("href") != $(".feature_photo img").attr("src")) {
			$(".feature_photo img").fadeOut("fast", function() {
				$(this).remove();
			});
			var imgLink = $(this).attr("href");
			var img = new Image();
			
			$(img).load(function() {
				$(this).hide().appendTo(".feature_photo").fadeIn("fast");
			}).attr("src", imgLink);
		}
		
		return false;
	});
	
	$(".navigation a").each(function() {
		$(this).attr("title", "");
	});
	
	$("#banner_login input").live('focus mouseup', function(e) {
		if($(this).hasClass("password")) {
			var pfield = $('<input type="password" val="" id="Password" name="Password" />');
			$(this).after(pfield);
			$(this).remove();
			pfield.focus();
		}
		$(this).select();
		return false;
	});
	
	function startPhotoList() {
		var curPhoto = $(".photo_list a.active").index();
		var totPhoto = $(".photo_list a");
		if (totPhoto.length > 1) {
			photoListTimer = setInterval(function() {
				curPhoto = (curPhoto + 1) % totPhoto.length;
				photoListAuto = true;
				totPhoto.eq(curPhoto).click();
			}, 7000);
		}
	}
	
	if($(".location_overlay .location").length > 0) addLocationTriggers();
	updateTickers();
	startPhotoList();
	lazyLoadPhotos();
	fixAfterSelectors();
});

function lazyLoadPhotos() {
	var cont = $("<div style='display: none;' id='lazy_loaded_photos' />");
	$(".photo_list > a").each(function() {
		var src = $(this).attr("href");
		cont.append($("<img src='" + src + "' />"));
	});
	$("body").append(cont);
}

function hideSecondaryNavHover() {
	$(".navigation li.hovered").removeClass("hovered");
	$(".navigation .hovered").stop(true,true).slideUp("fast").removeClass("hovered");
	$(".subheader").stop(true, true).animate({"marginTop" : 0}, "fast");
}

function updateTickers() {
	var ct = $("#carpet_ticker_value span");
	var wt = $("#water_ticker_value span");
	
	var ct_arr = getTickerObject("carpet");
	var wt_arr = getTickerObject("water");
	
	for (var i = 0; i < ct_arr.length; i++) {
		ct.eq(i).text(ct_arr[i]);
		wt.eq(i).text(wt_arr[i]);
	}
	
	setInterval(function() {
		updateCarpetTicker();
	}, 1700);
	setInterval(function() {
		updateWaterTicker();
	}, 2300);
}

function getTickerObject(ticker){
	var str = "";
	if (ticker == "carpet") {
		var now = new Date().valueOf() / 1000;
		var jan1 = new Date("Jan 1, 2012 00:00:00").valueOf() / 1000;
		var feet = 520000000 + Math.floor((now - jan1) * (1900000 / 604800));
		
		str = padZero(feet.toString(), 9);
	}
	else if (ticker == "water") {
		var now = new Date().valueOf() / 1000;
		var jan1 = new Date("Jan 1, 2012 00:00:00").valueOf() / 1000;
		var feet = 22369880 + Math.floor((now - jan1) * (81736 / 604800));
		
		str = padZero(feet.toString(), 9);
	}
	return [str.substring(0,3), str.substring(3,6), str.substring(6)];
}

function padZero(str, len) {
	while(str.length < len) str = "0" + str;
	return str;
}

function updateCarpetTicker() {
	var arr = getTickerObject("carpet");
	var ct0 = $("#carpet_ticker_value span").eq(0);
	var ct1 = $("#carpet_ticker_value span").eq(1);
	var ct2 = $("#carpet_ticker_value span").eq(2);
	
	if (ct0.text() != arr[0]) switchTickerFade(ct0, arr[0]);
	if (ct1.text() != arr[1]) switchTickerFade(ct1, arr[1]);
	if (ct2.text() != arr[2]) switchTickerFade(ct2, arr[2]);
	
}
function updateWaterTicker() {
	var arr = getTickerObject("water");
	var wt0 = $("#water_ticker_value span").eq(0);
	var wt1 = $("#water_ticker_value span").eq(1);
	var wt2 = $("#water_ticker_value span").eq(2);
	
	if (wt0.text() != arr[0]) switchTickerFade(wt0, arr[0]);
	if (wt1.text() != arr[1]) switchTickerFade(wt1, arr[1]);
	if (wt2.text() != arr[2]) switchTickerFade(wt2, arr[2]);
}

function switchTickerFade(el, newText) {
	el.fadeOut("fast", function() {
		el.text(newText).fadeIn("fast");
	});
}

function addLocationTriggers() {
	$(".location_overlay .location").each(function() {
		$(this).find(".trigger").hover(function() {
			$(".location_overlay .active").removeClass("active");
			$(this).parents(".location").addClass("active");
		}, function() {
			$(this).parents(".location").removeClass("active");
			$(".location_overlay .location .show").parents(".location").addClass("active");
		});
	});
}

function fixAfterSelectors() {
	if ($.browser.msie && $.browser.version <= 7 ) {
		$(".clearfix").append($('<div style="clear: both; width: 100%; height: 1px;" />'));
	}
}
