$(document).ready(function(){
	$(".perstog").click(function () {
		$("#pers-opts").slideToggle("slow");
		$("#pers-opts select").attr("selectedIndex", 0);
		$("#pers-opts input[type=text]").val("");
		$("#rushno").attr("checked", "checked");
		$("#rush-input").val("");
		$("#confirmpers").attr("checked", "");
	});
	$("#item-wrap").click(function (){
		closeWin();
	});
	$("#rushyes").click(function () {
		$("#rush-input").val(rushpageid);
	});
	$("#rushno").click(function () {
		$("#rush-input").val("");
	});
});

jQuery.fn.center = function () {
    this.css("position","absolute");
	var topp = ( $(window).height() - this.height() ) / 2+$(window).scrollTop();
	if (topp < 20){topp = 20;}
    this.css("top", topp + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	$('#item-wrap').css({
		'height':$(document).height(),
		'width':$(window).width()
	});
    return this;
}

jQuery.fn.image = function(src){
	return this.each(function(){
		$("<img>").appendTo(this).each(function(){
			this.src = src;
		});
	});
}

function closeWin(){
	$("#item-wrap").css("display","none");
	$("#item-colors").css("display","none");
	$("#item-colors-cont").text("");
	$("#p1").css("display","none");
	$("#p1-cont").text("");
	$("#p2").css("display","none");
	$("#p2-cont").text("");
	$("#p3").css("display","none");
	$("#p3-cont").text("");
	$("#p4").css("display","none");
	$("#p4-cont").text("");
	$("#p5").css("display","none");
	$("#p5-cont").text("");
}

function showColors(w,h,img){
	$("#item-colors-cont").image(img);
	$("#item-colors-cont").css('width',w+'px');
	$("#item-colors-cont").css('height',h+'px');
	$(".item-colors-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#item-colors").css("display","block");
	$("#item-colors").center();
}

function showP1(w,h,img){
	$("#p1-cont").image(img);
	$("#p1-cont").css('width',w+'px');
	$("#p1-cont").css('height',h+'px');
	$(".p1-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#p1").css("display","block");

	$("#p1").center();
}

function showP2(w,h,img){
	$("#p2-cont").image(img);
	$("#p2-cont").css('width',w+'px');
	$("#p2-cont").css('height',h+'px');
	$(".p2-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#p2").css("display","block");
	$("#p2").center();
}

function showP3(w,h,img){
	$("#p3-cont").image(img);
	$("#p3-cont").css('width',w+'px');
	$("#p3-cont").css('height',h+'px');
	$(".p3-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#p3").css("display","block");
	$("#p3").center();
}

function showP4(w,h,img){
	$("#p4-cont").image(img);
	$("#p4-cont").css('width',w+'px');
	$("#p4-cont").css('height',h+'px');
	$(".p4-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#p4").css("display","block");
	$("#p4").center();
}

function showP5(w,h,img){
	$("#p5-cont").image(img);
	$("#p5-cont").css('width',w+'px');
	$("#p5-cont").css('height',h+'px');
	$(".p5-top").css('width',w+'px');
	$("#item-wrap").css("display","block");
	$("#p5").css("display","block");
	$("#p5").center();
}

function formCheck(){
	var x = document.getElementById('persyes').checked;
	var y = document.getElementById('confirmpers').checked;
	if(x==true){
		if(y==false){
			alert("Please verify that your information is correct.");
			return false;
		} else if($("#quantity-box").val() < persminq){
			alert("There is a minimum order of " + persminq + " for this item when personalized.");
			return false;
		}
	}
	if(x==false){
		var pwrap = document.getElementById("pers-wrapper");
		var nopt = document.getElementById("persno");

		if(nopt.checked){$('#pers-wrapper').remove();}
	}
}

function formCheck2(){
	var x = document.getElementById('persyes').checked;
	var y = document.getElementById('confirmpers').checked;
	if(x==true){
		if(y==false){
			alert("Please verify that your information is correct.");
			return false;
		} else if($("#quantity-box").val() < persminq){ // Pers. min qty
			alert("There is a minimum order of " + persminq + " for this item when personalized.");
			return false;
		} else {
			$("#minq").val(persminq);
		}
	} else {
		if($("#quantity-box").val() < dminq){
			alert("There is a minimum order of " + dminq + " for this item.");
			return false;
		} else {
			$("#minq").val(dminq);
		}
	}
}

function calcMAtotal(){
	var x = $(".ma-qty-box");
	var y = $(".ma-price");
	var amt; var ftotal;
	var total = 0;

	for(var a=0;a<x.length;a++){
		amt=(+y[a].innerHTML);
		total = total + (x[a].value * amt)
	}

	ftotal = total.toFixed(2);
	$("#ma-tot").text(ftotal);
	$("#ma-tot2").text(ftotal);
}

function checkqty(min, obj){
	var val = obj.value;
	if(val < min){
		alert("The minimum orderable quantity for this item is " + min + ". Please adjust your quantity.");
		obj.value = "0";
		calcMAtotal();
	} else {
		calcMAtotal();
	}
}
