var t; var valid;
$(document).ready(function(){
	if(!document.getElementById) {return false;}
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	if($("body")[0].className == "home") {doSlides(); doVideo();}
	doFooter();
});

function doSlides() {
	$("div.slide").css('display', 'block');
	$("div.slide").fadeOut(1);
	$("#top div").removeClass('visible');
	$("#slides li").removeClass();
	var howMany = $("#slides li").length;
	randomNo = Math.floor(Math.random()*howMany);
	$("#slides li").each(function(i, el){
		$(this).click(function(){
			$(this).siblings(".active").each(function(j,elm){
				$(elm).removeClass("active");
				$("#top div.visible").fadeOut(500);
				$("#top div.visible").removeClass("visible");
			});
			$(this).addClass('active', !$(this).hasClass('active'));
			$("#top div:eq("+i+")").fadeIn(500);
			$("#top div:eq("+i+")").addClass('visible');
		});
	});
	$("#slides a").each(function(i, el){
		$(this).click(function(){
			clearTimeout(t);
			slideIt(i);
		});
	});
	$("#top div:eq("+randomNo+")").fadeIn(500);
	$("#top div:eq("+randomNo+")").addClass('visible');
	$("#slides li:eq("+randomNo+")").addClass('active');
	slideIt(randomNo);
}
function slideIt(no) {
	$("#slides li:eq("+no+")").click();
	++no;
	if(no >= $("#slides li").length) {no = 0;}
	t = setTimeout('slideIt('+no+')', 9000);
}
function doFooter() {
	if(($("header").height() + $("#content").height() + $("footer").height()) < $(window).height()) {$("footer").offset({ top: $(window).height()-170, left: 0 });}
}
function doBookmark() {
	var btitle = 'Chrome River - Customer Secure Login Page';
	var burl = 'https://www.chromeriver.com/login/';
	if (window.sidebar){window.sidebar.addPanel(btitle, burl, '');}
	else {window.external.AddFavorite(burl, btitle);}
}
function validateForm(myId) {
	$("form#"+myId).submit(function(){
		clearErrors(myId);
		$("form#"+myId+" input[required='required'], form#"+myId+" textarea[required='required']").each(function(i,el){
			if($(el).attr('value') == '') {
				$("form#"+myId+" label[for='"+$(el).attr('id')+"']").addClass('error');
				$("form#"+myId).prepend('<span class="error"><strong>'+$("form#"+myId+" label[for='"+$(el).attr('id')+"']").html()+'</strong> is a required field.</span>');
				valid = 0;
			}
		});
		$("form#"+myId+" input[type='email']").each(function(i,el){
			if($(el).attr('required') == 'required') {
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(reg.test($(el).attr('value')) == false) {
					$("form#"+myId).prepend('<span class="error"><strong>'+$("form#"+myId+" label[for='"+$(el).attr('id')+"']").html()+'</strong> is not a valid email address.</span>');
					valid = 0;
				}
			}
		});
		if(valid == 0) {return false;}
		else {return true;}
	});
}
function clearErrors(myId) {
	valid = 1;
	$("form#"+myId+" span.error").remove();
	$("form#"+myId+" label").removeClass('error');
}
function showPostcard(i) {
	$("body").append('<div id="black"></div>');
	$("#black").height($(document).height());
	$("#black").click(function(){hidePostcard();});
	$("body").append('<div id="white"></div>');
	var ajax_load = "<img src='//d3msvfqt040hh.cloudfront.net/ui/media/images/content/loading.gif' alt='loading...' id='loading' />";
	var loadUrl = "/postcards/card/"+i;
	$("#white").html(ajax_load).load(loadUrl, null, function(){$("#lightbox").css('top',$(window).scrollTop()+200);});
}
function hidePostcard() {
	$("#white").remove();
	$("#black").remove();
}
function doVideo() {
	$("#youtubevideo").click(function(){
		playVideo($(this).attr('class'));
		return false;
	});
}
function playVideo(videoId) {
	$("body").append('<div id="black"></div>');
	$("#black").height($(document).height());
	$("#black").click(function(){hidePostcard();});
	$("body").append('<div id="white"><div id="lightbox" class="youtube"><a href="javascript:hidePostcard();" class="close"><i>close window</i></a><iframe width="640" height="360" src="http://www.youtube.com/embed/'+videoId+'?rel=0&modestbranding=0&showinfo=0&theme=light" frameborder="0" allowfullscreen id="youtube"></iframe></div></div>');
	$('html, body').animate({scrollTop: 0+'px'}, 500);
	return false;
}
