var tickerSpeed = 5000;

$(document).ready(function(){
	$("#tickerCounter").val(1)
	// code for hiding/revealing product and brand menus
	$(".productsMenuTitle").click(function () {
		if ($("#productsMenu").css("display") == "none") {
			$("#brandsMenu").hide(100);
			$("#productsMenu").show(100);
			$("#productArrow").attr("src", "/images/topProductOpen.gif");
			$("#brandArrow").attr("src", "/images/topProductClosed.gif");
		} else {
			$("#productsMenu").hide(100);
			$("#productArrow").attr("src", "/images/topProductClosed.gif");
		}
		return false;
	});
	$(".brandsMenuTitle").click(function () {
		if ($("#brandsMenu").css("display") == "none") {
			$("#productsMenu").hide(100);
			$("#brandsMenu").show(100);
			$("#productArrow").attr("src", "/images/topProductClosed.gif");
			$("#brandArrow").attr("src", "/images/topProductOpen.gif");
		} else {
			$("#brandsMenu").hide(100);
			$("#brandArrow").attr("src", "/images/topProductClosed.gif");
		}
		return false;
	});
	// hover products menu
	$("#productsMenu li").not("#productsMenu li ul li").each(function() {
		$(this).hover(function () {
			$("#productsMenu li div").hide();
			$(this).find("div").show().hover(function () { 
				$(this).prev("a").addClass("on");
			}, function () { 
				$(this).hide(); 
				$(this).prev("a").removeClass("on");  
			});
		}, function () {
			$("#productsMenu li div").hide();
		});
	});
	// functions to show reveal input values in sidebar
	$('#sidePanel form :input').each(function () {
		if (($(this).attr("type") != "hidden") && ($(this).attr("type") != "submit")) {
			var val = $(this).val();
			if (val.indexOf("Your") > -1) {
				$(this).focus(function () { 
					if ($(this).val() == val) {
						$(this).val('');
					}						
				});
				$(this).blur(function () { 
					if ($(this).val() == '') {
						$(this).val(val);
					}						
				});
			}
		}
	});
	
	// add some simple validation to side forms
	$("#quickContactForm form").submit(function () { 
		return smallFormValidate("#quickContactForm form");
	});
	$("#productUpdatesForm form").submit(function () { 
		return smallFormValidate("#productUpdatesForm form");
	});
	// start the product ticker
	$("#productInfoWrapper").animate({dummy: false}, tickerSpeed, function () { changeTicker(true, true); });
});

// validation for side forms
function smallFormValidate(validateForm) {
	var banned = new Array('Your Name', 'Your E-mail', 'Your Phone', 'Your Questions'); 
	var pass = true;
	$(validateForm+" :input").each(function () {
		var value = $(this).val();
		if ($.inArray(value, banned) > -1) {
			pass = false;
		}
	});
	if (pass == false) {
		alert("Please fill out all fields");
	}
	return pass;
}

// function to move product ticker
function changeTicker(next, timer) {
	if (timer == undefined) var timer = false;
	if (timer == false) {
		$("#productInfoWrapper").stop().clearQueue();
	}
	var speed = 400;
	var num = $("#tickerHidden img").length;
	var current = $("#tickerCounter").val();
	if (next == true) {
		var nextNum = parseInt(current)+1;
		if (nextNum > num) {
			nextNum = 1;
		}
		var nextSrc = $("#product"+nextNum).attr("src");
		var nextAlt = $("#product"+nextNum).attr("alt");
		var html = $("#info"+nextNum).html();
		$("#productSpace").append("<div class=\"productTickerImage\"><img src=\""+nextSrc+"\" alt=\""+nextAlt+"\" /></div>");
		$("#productInfoWrapper").append("<div class=\"productInfoSpace\">"+html+"</div>");
		var topMargin = $(".productInfoSpace").css("height");
		var leftMargin = $(".productTickerImage").css("width");
		$(".productTickerImage:first").animate({marginLeft: "-"+leftMargin}, speed, function () {
			$(this).remove();
			$("#tickerCounter").val(nextNum);
			$("#productInfoWrapper").animate({dummy: false}, tickerSpeed, function () { changeTicker(true, true); });	
		});
		$(".productInfoSpace:first").animate({marginTop: "-"+topMargin}, speed, function () {
			$(this).remove();
		});
	} else {
		var nextNum = parseInt(current)-1;
		if (nextNum < 1) {
			nextNum = num;
		}
		var nextSrc = $("#product"+nextNum).attr("src");
		var nextAlt = $("#product"+nextNum).attr("alt");
		var html = $("#info"+nextNum).html();
		$("#productInfoWrapper").prepend("<div class=\"productInfoSpace\" style=\"margin-top:-180px\">"+html+"</div>");
		$("#productSpace").prepend("<div class=\"productTickerImage\" style=\"margin-left:-530px\"><img src=\""+nextSrc+"\" alt=\""+nextAlt+"\" /></div>");
		$(".productTickerImage:first").animate({marginLeft: 0}, speed, function () {
			$(".productTickerImage:last").remove();
			$("#tickerCounter").val(nextNum);
			$("#productInfoWrapper").animate({dummy: false}, tickerSpeed, function () { changeTicker(true, true); });	
		});
		$(".productInfoSpace:first").animate({marginTop: 0}, speed, function () {
			$(".productInfoSpace:last").remove();
		});	
	}
}
