var productWindowSize;
var animationDuration;
var totalProducts;
var productWidth;
var totalProductsWidthMinusWindow;
var sliderHomeId;

function resetTimer() {
	clearInterval(sliderHomeId);
	sliderHomeId = setInterval(top_slider_moveToRight, 5000);
}

function top_slider_moveToLeft() {
	var position = $(".products-wrapper").position();
	if (position.left < 0) {
		$('.products-wrapper').stop().animate({"left":"+="+productWidth+"px"}, animationDuration);
	} else {
		$('.products-wrapper').stop().animate({"left":"-" + (totalProductsWidthMinusWindow) + "px"}, animationDuration);
	}
}
function top_slider_moveToRight() {
	var position = $(".products-wrapper").position();
	
	//if (window.console && console.log) console.log("left: ", position.left, " x: ", -(totalProductsWidthMinusWindow));

	if (position.left > -(totalProductsWidthMinusWindow-1)) { // removed 1 for IE being a pain
		$('.products-wrapper').stop().animate({"left":"-="+productWidth+"px"}, animationDuration);
	} else {
		$('.products-wrapper').stop().animate({"left":"0px"}, animationDuration);
	}
}
function pngFixer(ele) {
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	$(ele).each(function() {
		if (ie55 || ie6) {
			// fix css background pngs
			var bgIMG = jQuery(this).css('background-image');
			if (bgIMG.indexOf(".png") != -1) {
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='crop')";
			}
		}
	});
}

Cufon.replace('.transform');

jQuery(document).ready(function($){
								
	var ie = (navigator.appName == "Microsoft Internet Explorer");

	pngFixer("div, img");
	
	// product slider
	productWindowSize = 1; // number of products visible at one time
	animationDuration = 1000; // ms
	
	totalProducts = $(".featured-product").length;
	productWidth = 610; //$('.featured-product').css("width");
	totalProductsWidthMinusWindow = (totalProducts-productWindowSize)*productWidth;
	// productWidth = productWidth.substring(0, productWidth.lastIndexOf("px"));
	
	//$('.featured-products').css("width",(productWindowSize*productWidth)+"px");
	//$('.products-wrapper').css("width",(totalProducts*productWidth)+"px");

	$('.featured-left-arrow').fadeTo("fast", 0.5);
	$('.featured-right-arrow').fadeTo("fast", 0.5);
	
	$('.featured-left-arrow').hover(function(){
		$(this).fadeTo("fast", 1.0);
	},function(){
		$(this).fadeTo("fast", 0.5);
	}).click(function(){
		top_slider_moveToLeft();
		clearInterval(sliderHomeId);
		sliderHomeId = setInterval(top_slider_moveToRight, 5000);
	});

	$('.featured-right-arrow').hover(function(){
		$(this).fadeTo("fast", 1.0);
	},function(){
		$(this).fadeTo("fast", 0.5);
	}).click(function(){
		clearInterval(sliderHomeId);
		sliderHomeId = setInterval(top_slider_moveToRight, 5000);
		top_slider_moveToRight();
	});

	$('.floor-plans .controls a').click(function(){
		$('.floor-plans .controls a').each(function(i){
			$(this).removeClass("selected");
		});
		$(this).addClass("selected");
		var pos = String($(this).attr("href"));
		pos = Number(pos.substring(pos.indexOf("#plan")+5))-1;
		newPos = pos*125;
		$('.plans-wrapper').stop().animate({"top":"-"+newPos + "px"}, animationDuration);
		return false;
	});
	
	// select upper ground floor plan
	$(".floor-plans a[href='#plan2']").click().addClass("selected");
	
	$(".floor-plans .plan").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	$("#visitorname").blur(function(){
		if ($(this).val() != "") {
			$("#comments").val($(this).val()+" has made an inquiry about the Watermarque building.");
		} else {
			$("#comments").val("");
		}
	});

	var zIndexNumber = 1000;
	$('div, ul, li').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	$('.floor-plans .controls').css('zIndex', 5000);

	if (ie && location.pathname == "/availability/specification/") {
		// fixes anchor problems on ie, form submission doesn't work properly
		//$('.wpcf7-form').attr("action", "#");
	}
	
	$("#turnkey").submit(function() {
		$("#server-message").text("Please wait...").show();

		var dataString = 'send=Submit&name='+ $("#name").val() + '&email=' + $("#email").val() + '&tel=' + $("#tel").val() + '&companyname=' + $("#companyname").val();
		//alert(dataString);
		$.ajax({
			type: "POST",
			url: "/wp-content/themes/watermarque/system/turnkey.php",
			data: dataString,
			success: function() {
				$("#server-message").text("Your message has been sent!")
			}
		});
		return false;
	});
});
function checkEmail() {
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#dlltyk-dlltyk").val();
	return pattern.test(emailVal);
}
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
