var productWindowSize;
var animationDuration;
var totalProducts;
var productWidth;
var totalProductsWidthMinusWindow;
var sliderHomeId;

function resetTimer() {
	clearInterval(sliderHomeId);
	sliderHomeId = setInterval(top_slider_moveToRight, 3000);
}

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')";
			}
		}
	});
}

jQuery(document).ready(function($){
								
	pngFixer("div, img");
	
	/* product slider */
	productWindowSize = 4; // number of products visible at one time
	animationDuration = 300; // ms
	
	totalProducts = $(".featured-product").length;
	productWidth = 210; //$('.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();
	});

	$("#newsletter form :submit").click(function() {	
			
		// First, disable the form from submitting
		$('#newsletter form').submit(function() { return false; });

		// Validate email address with regex
		if (!checkEmail()) {
			alert("Please enter a valid email address");
			return;
		}
			
		// Grab form action
		formAction = $("#newsletter form").attr("action");
			
		// Serialize form values to be submitted with POST
		var str = $("#newsletter form").serialize();
			
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
			
		// Submit the form via ajax
		$.ajax({
			url: "/proxy.php",
			type: "POST",
			data: final,
			success: function(html) {
				// If successfully submitted hides the form
				$("#newsletter").slideUp();
				// Shows "Thanks for subscribing" div
				$("#confirmation").slideDown();
			}
		});
	});

	$("#thumbs a").click(function(){
		$("#photo").attr("src", $(this).attr("href"));
		return false;
	});

	$("#bioCopy, #thumbs, #news").jScrollPane({scrollbarWidth:10, scrollbarMargin:0});
});
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 == '');
} }