var updatePrice = function(type,amount){
		var getColor = $("form#productDisplay .color .selected").attr("title");
		if(getColor){
			var colorstring = getColor.split("and");
			if(colorstring[1]){
				$("#totalprice").text(15 + Number($("form#productDisplay select option:selected .sizeprice").text()));
			}
			else{
				if($("body.custom").length >0){
					return;
				}
				$("#totalprice").text($("form#productDisplay select option:selected .sizeprice").text());
			}
		}
		else{
			$("#totalprice").text($("form#productDisplay select option:selected .sizeprice").text());
		}
};

$(document).ready(function(){
	//Get the text of the h1 to set it as class for change of header
	function getClass(){
		var text = $("#mid #main .title div h1").text();
		if(text.indexOf("{") >= 0){
			text = "cart";
		}
		return text;
	}
	var text = getClass();
	$("body").addClass(text);

	//add border when clicked on colors
	$("form#productDisplay .color img").click(function(){
		$("form#productDisplay .color img").removeClass("selected");
		$("form#productDisplay .color input").attr("checked","");
		$("form#productDisplay .color img").css("border","1px solid #E6E7E4");
		$(this).css("border","1px solid #000");
		$(this).siblings("input").attr("checked","checked");
		$(this).addClass("selected");
		updatePrice("color",0);
	});	
	
	//spacing for multi-colored choices
	$("form#productDisplay .color img[title*=and]").css("margin-top","10px");
	$("form#productDisplay .color img[title*=and]").click(function(){
		updatePrice("color",15);
	});
	
	if($("body.custom").length>0){
		$(".price").append("<span id='perLetterPrice>" + 2 + "</span>");
	}
	
	//if it is custom set new per letter cost, else update price only
	$("form#productDisplay select").bind("change",function(){
		updatePrice("size",0);
		if($("body.custom").length>0){
			var totalChar = $("input#quantity").attr("value");
			if(totalChar>0){
				$("#totalprice").text(Number($("form#productDisplay select option:selected .sizeprice").text())*Number(totalChar));
			}
			else{
				$("#totalprice").text($("form#productDisplay select option:selected .sizeprice").text());
			}
		}
	});
	
	//hide price
	$("form#productDisplay select option").each(function(){
		var thistext = $(this).text();
		var start = thistext.split("- Add $");
		var beginning = start[0] + " $<span class='sizeprice'>";
		if(jQuery.browser["mozilla"]){
		     beginning = start[0] + " <span class='sizeprice'>";
		}
		if(start[1]){
			var price = Number(start[1]);
			price = Number($("#totalprice").text());
			price = Math.round(price*100)/100;
			price += Number(start[1]);	
			$(this).html(beginning + price + "</span>");			
		}
		else{
			$(this).html(beginning + $("#totalprice").text() + "</span>");
		}
	});
	
	//font choosing
	if($("body.custom").length >0){
		$("form#productDisplay .fonts img").click(function(){
			$("form#productDisplay .fonts img").each(function(){
				$(this).attr("src","/images/fontoptions/" + $(this).attr("title") + "_off.gif");
			});
			
			$(this).attr("src","/images/fontoptions/" + $(this).attr("title") + "_on.gif");
			$(this).siblings("input").attr("checked","checked");
			
		});

		$("body.custom img[alt='seriph']").attr("src","/images/fontoptions/seriph_on.gif");
		$("body.custom input[name='radio_12_fonts']:first").attr("checked","checked");	
	}
	
	$("#sizes ul li a").hover(function(e){
		var panel = $(this).attr("href");
		var top = e.pageY - 135;
		var calcwidth = window.innerWidth;
	 	if(jQuery.browser["msie"]){
			calcwidth = document.body.offsetWidth;
		}
		var left = e.pageX - (( calcwidth - $("#content").outerWidth())/2) - 125;
		$(panel).css("top",top + "px");
		$(panel).css("left", left + "px");
		$(panel).show();
	},
	function(){
		var panel = $(this).attr("href");
		$(panel).hide();
	})
	

	
	//create drop down
	//check to make sure we're not on the cart page
	if(($("#temp").length == 0)&&($("#carttable").length ==0)){

		$("#cart p").after("<div id='temp'></div>");
		$("#temp").load("/eaction/ecom.viewcart/ #carttable", function(){
			
			if($("#carttable")){
				$("#temp td.qty,#temp td.delete,#temp td.price,#temp th").remove();
				$("#temp").html($("#carttable tbody").html());	
				$("#temp td").each(function(){
					$(this).replaceWith("<dd "+ "class='" + $(this).attr("class") + "'>" + $(this).html() + "</dd>");
				});
				$("#temp tr").each(function(){
					$(this).replaceWith("<dl " + "class='" + $(this).attr("class") + "'>"+ $(this).html() + "</dl>");
				});	
				$("#temp").append("<a id=\"btnviewbag\" href=\"/eaction/ecom.viewcart/\">view cart</a>");	
			}
			var totalQty =$("#temp dl").length;
			if(totalQty >0){
				totalQty -=2;
				$("#count").html(totalQty);	
				var curprice = $("#temp dl.subtotal .total .price").text();
				curprice = curprice.slice(1);
				$(".subpage #dropcarttotal").html(curprice);
			}
			//if there's no item in the cart, there's no drop down
			if($("#count").text()!=0){

				$("#cart").hover(function(){
					$("#cart #temp").slideDown();
					},
					function(){
						$("#cart #temp").slideUp();
				});
			}
		}); 
	}
	



	//for custom text only
	if($("div#customtextrender")){
		$("img#previewtext").click(function(){
			
			//find value of color chosen
			//hardcoded
			var classname =	"";
			if($("input[name=radio_12_color][checked]").length>0){
			 	var classname = $("input[name=radio_12_color][checked]").val();
				classname = classname.substring(classname.indexOf("_")+1,classname.length-4);	
			}
			else{
				alert("Please choose a color");
				return;
			}
			var curval = $("input[name=radio_12_fonts][checked]").val();			
			curval = curval.split("_");
			classname = classname + "_" + curval[1];
			
			var text = $("textarea[name=choice_12_4]").attr("value");
			text = encodeURI(text);
			if(jQuery.browser['msie']){
				var href="/dtr2/indexie.php?text="+ text +"&class="+classname;
				window.open(href,"fonts","width=530,height=401,scrollbars=no");				
			}
			else{
				$("#customtextrender").html("<IFRAME scrolling='no' frameborder='0' allowtransparency='true' SRC=\"/dtr2/index.php?text="+ text +"&class="+classname+"&date="+(new Date()).getTime()+"\"></iframe>");
			}

		});	
	}
	
	//count characters in preview
	$(".customtextarea").bind("keyup",function(){
		//remove spaces from total count
		var totalChars = $(this).attr("value");
		var totalSpaces = totalChars.split(' ').length;

		var numChar = totalChars.length - totalSpaces +1;
		
		if(numChar >0){
			$("input#quantity").attr("value",numChar);
			var total = numChar * Number($("form#productDisplay select option:selected .sizeprice").text());
			$("#totalprice").html(total);
		}
		else{
			$("#totalprice").html($("form#productDisplay select option:selected .sizeprice").text());
		}
		
	});
	
	
	//hide qty 
	$("img[alt=Custom Letters]").parents("tr").addClass("customLetterRow");
	
		$("form#productDisplay .formButton input").bind("click",function(){
			if($("form#productDisplay .color .selected").length==0){
				alert("Please choose a color");
				return false;
				
			}
		});
	
	
});


