var slideIn = 2000; var slideOut = 20000;
var $name; var $email; var $comment; var $demo_fname; var $demo_lname; var $demo_email; var valid;
$(document).ready(function(){
	if(!document.getElementById) {return false;}
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	if($("body")[0].className == "home") {doSlides();}
	doRollovers();
	if($("body:has(#mailinglist_form)")[0]) {doNewsletter();}
	if($("body:has(a.tour)")[0]) {doTour();}
	if($("body:has(a.play)")[0]) {doPlay();}
});
function doRollovers() {
	preloads = new Object();
	$("img").each(function(i, el){
		if($(el).parent().get(0).tagName == "A") {
			var imageOffSrc =  el.src + "";
			if(imageOffSrc.indexOf("_off") != -1) {
				var imageOverSrc = imageOffSrc.replace(/_off/g, "_over");
				preloads['imageOff_' + i] = new Image(); preloads['imageOff_' + i].src = imageOffSrc; preloads['imageOver_' + i] = new Image(); preloads['imageOver_' + i].src = imageOverSrc;
				$(el).parent().mouseover(function(){$("img",this).attr("src", $("img",this).attr("src").replace(/_off/g, "_over"));});
				$(el).parent().mouseout(function(){$("img",this).attr("src", $("img",this).attr("src").replace(/_over/g, "_off"));});
			}
		}
	});
	$("input").each(function(i, el){
		if($(el).attr("src")) {
			var imageOffSrc =  el.src + "";
			if(imageOffSrc.indexOf("_off") != -1) {
				var imageOverSrc = imageOffSrc.replace(/_off/g, "_over");
				preloads['imageOff_' + i] = new Image(); preloads['imageOff_' + i].src = imageOffSrc; preloads['imageOver_' + i] = new Image(); preloads['imageOver_' + i].src = imageOverSrc;
				$(el).mouseover(function(){$(this).attr("src", $(this).attr("src").replace(/_off/g, "_over"));});
				$(el).mouseout(function(){$(this).attr("src", $(this).attr("src").replace(/_over/g, "_off"));});
			}
		}
	});
}
function doNewsletter() {
	$("#emailAddress").blur(function(){
		if($(this).attr("value")) {$(this).addClass('focused', !$(this).hasClass('focused'));}
		else {$(this).removeClass('focused');}
	});
	$("#emailAddress").focus(function(){
		$(this).addClass('focused', !$(this).hasClass('focused'));
	});
}
function doSlides() {
	var howMany = $("#scroller li").length;
	randomNo = Math.floor(Math.random()*howMany);
	$("#scroller li:nth-child("+randomNo+")").addClass('active');
	$("#scroller").children().each(function(i,el){
		$(el).click(function(){
			$(this).addClass('active', !$(this).hasClass('active'));
			$(this).siblings().removeClass('active');
			var mL = -(i*970) + "px";
			$("#promo div.slides").animate({
				marginLeft:mL
			}, slideIn, "easeInOutBack");
		});
	});
	randomNo += 1;
	if(randomNo > howMany) {randomNo == 1;}
	var mL2 = -((randomNo - 1)*970) + "px";
	$("#promo div.slides").css('marginLeft',mL2);
	slideIt(randomNo);
}
function slideIt(i) {
	$("#scroller li:nth-child("+i+")").click();
	++i;
	if(i>4) {i=1;}
	setTimeout('slideIt('+i+')', slideOut);
}
function validateComment() {
	$("#comment_form").submit(function(){
		clearErrors();
		if($name.attr('value') == '') {showError($name, "Please enter your name"); valid = 0;}
		if($email.attr('value') == '') {showError($email, "Please enter your email address"); valid = 0;}
		if($email.attr('value') != '') {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($email.attr('value')) == false) {showError($email, "Please enter a valid email address"); valid = 0;}
		}
		if($comment.attr('value') == '') {showError($comment, "Please enter your comment"); valid = 0;}
		if(valid == 1) {return true;}
		else {return false;}
	});
}
function validateContact() {
	$("#contactForm").submit(function(){
		clearErrors();
		if($demo_fname.attr('value') == '') {showError($demo_fname, "Please enter your first name"); valid = 0;}
		if($demo_lname.attr('value') == '') {showError($demo_lname, "Please enter your last name"); valid = 0;}
		if($demo_email.attr('value') == '') {showError($demo_email, "Please enter your email address"); valid = 0;}
		if($demo_email.attr('value') != '') {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($demo_email.attr('value')) == false) {showError($demo_email, "Please enter a valid email address"); valid = 0;}
		}
		if(valid == 1) {return true;}
		else {return false;}
	});
}
function validateDownload() {
	$("#downloadForm").submit(function(){
		clearErrors();
		if($demo_fname.attr('value') == '') {showError($demo_fname, "Please enter your first name"); valid = 0;}
		if($demo_lname.attr('value') == '') {showError($demo_lname, "Please enter your last name"); valid = 0;}
		if($demo_email.attr('value') == '') {showError($demo_email, "Please enter your email address"); valid = 0;}
		if($demo_email.attr('value') != '') {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($demo_email.attr('value')) == false) {showError($demo_email, "Please enter a valid email address"); valid = 0;}
		}
		if(valid == 1) {/*var cs = $("#case_study").attr("value") + ""; window.location = cs; return false;*/return true;}
		else {return false;}
	});
}
function clearErrors() {
	valid = 1;
	$name = $("#name");
	$email = $("#email");
	$comment = $("#comment");
	$demo_fname = $("#first_name");
	$demo_lname = $("#last_name");
	$demo_email = $("#demo_email");
	$("label").each(function(i, el){$(el).removeClass('error');});
	$("#comment_form small").remove();
	$("#contactForm small").remove();
	$("#downloadForm small").remove();
}
function showError(obj, msg) {
	$("label").each(function(i, el){
		if($(el).attr('for') == $(obj).attr('id')) {
			var sml = document.createElement("small");
			sml.className = "error";
			var txt = document.createTextNode(msg);
			sml.appendChild(txt);
			$(el).removeClass(''); $(el).addClass('error');
			$(obj).after(sml);
		}
	});
}
function validateEmail() {
	$("#mailinglist_form").submit(function(){
		$("#mailinglist_form p.gray small").remove();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if($("#emailAddress").attr('value') == ''){
			$("#mailinglist_form p.gray").append("<small class='error'>Please enter your email address!</small>");
			return false;
		}
		else if(reg.test($("#emailAddress").attr('value')) == false) {
			$("#mailinglist_form p.gray").append("<small class='error'>Please enter a valid email address!</small>");
			return false;
		}
		return true;
	});
}
function validateLead() {
	$("#downloadForm").submit(function(){
		var v = 1;
		$("#downloadForm p.gray small").remove();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if($("#firstName").attr('value') == ''){
			$("#downloadForm p.gray").append("<small class='error'>Please enter your first name!</small>");
			v = 0;
		}
		if($("#lastName").attr('value') == ''){
			$("#downloadForm p.gray").append("<small class='error'>Please enter your last name!</small>");
			v = 0;
		}
		if($("#emailAddress").attr('value') == ''){
			$("#downloadForm p.gray").append("<small class='error'>Please enter your email address!</small>");
			v = 0;
		}
		else if(reg.test($("#emailAddress").attr('value')) == false) {
			$("#downloadForm p.gray").append("<small class='error'>Please enter a valid email address!</small>");
			v = 0;
		}
		if(v == 0) {return false;}
		else {return true;}
	});
}
function doTour() {
	$("a.fancybox").attr('rel', 'tour').fancybox({
		'overlayOpacity' : 0.6,
		'overlayColor' : '#293033',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
	});
	$("a.tour").click(function() {$("a.fancybox:first").trigger('click');});
}
function doPlay() {
	$("a.play").fancybox({
		'overlayOpacity' : 0.6,
		'overlayColor' : '#293033',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'width' : 800,
		'height' : 550,
		'type' : 'swf',
		'autoScale' : false
	});
}
