$(function(){
	selectSize();
    selectFinish();
	//getCurItem();
	//EnableCart();
		   });
   function setMessage(){
	   var checkedrad = $("input:radio:checked[name='size']");
	   if(checkedrad.length>0){//checked option found
	     var rprice = checkedrad.attr('price');
        $("input[name='lead_time']").val(checkedrad.attr('lead_time')); 
	    $("input[name='category']").val(checkedrad.attr('shipping_category')); 
	    $("input[name='price']").val(getPrice(rprice)); 
	    $("input[name='2:price']").val($("input[name='price']").val());// copy to border corners
		}
   };
   function EnableCart(){
	    $('input:radio[name="size"], select[name="finish"]').removeAttr("disabled");
		EnableButton(true);
   };
   function EnableButton(on_off){
	   btn = $('#addToCart');
	   if(on_off){
		  btn.show();
	   //btn.removeAttr("disabled");
	   }else{
		  btn.hide();
		//btn.attr("disabled", true); 
	   }
   };
   function selectSize(){
	  var radio = $("input:radio[name='size']");
 	 if(radio.length>1){ //more then 1 choice
	 	  radio.click(function(){ 
			//EnableButton(false);				   
			hideErr();
			setMessage(); 
			//EnableButton(true);
		}) 
	 }else if(radio.length==1){
	  radio[0].checked = true;
	  setMessage(); 
	 } else {
		 $('.options').hide();
	 }
   };
   function getPrice(price_in){
	    var retprice = price_in;
		 if($("select[name='finish'] option:selected").text() == "Prefinished" ){
			retprice = retprice*1.1;
		 } 
		 return formatCurrency(retprice);
		// return retprice;
   };
   function selectFinish(){
	  $("select[name='finish']").change(function (){
		// EnableButton(false);										  
	  	 setMessage();  
		 AddFinish();
		// EnableButton(true);
       })
    };
 function AddFinish(){
	 var rprice = 0;
     $("table.tblPricing .price").each(function(){ 
		rprice = $(this).attr("price");
		//alert('TPrice:'+rprice);
		rprice = getPrice(rprice);
	   $(this).text('$'+rprice); 
	});
 };
 function fc_PreProcess() {
  var ret = false;
 		if ($("input:radio:checked[name='size']").length>0) { 
		  ret = true;
		} else { 
			showErr('Please select a size. We want to make sure you get exactly what you want.');
			return false; // We return false, the cart is not displayed, and the product is not added.
		}
/*--check if number --*/
       if(!isNaN($("input[name$='quantity']").val())){
			ret = true;
		} else {  
		  showErr('Please enter a valid number for quantity');
		  return false; 
		}
/*---check for minimum quantity*/
		if(parseInt($("input[name='quantity']").val()) >= parseInt($("input[name='quantity_min']").val())){
			ret = true;
		} else {  
		showErr('Please enter the quantity no less then required minimum: '+$("input[name='quantity_min']").val());
		 return false; 
		}
/*--check for $0 --*/
/*
        if(parseInt($("input[name='price']").val()) != 0){
			ret = true;
		} else {  
		showErr('There was an error adding to the cart. Please Refresh the page (hit F5) and try again.');
		 return false; 
		}
	*/	
		hideErr();
		return ret;
 };
function showErr(msg_in){
	$("#message").html('').html(msg_in).addClass('err').show();
}
function hideErr(){
	$("#message").html('').removeClass('err').hide();	
}
function formatCurrency(num_in) {
var num = "0";
num = num_in;
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + /*'$' + */num + '.' + cents);
}
/*
function getCurItem() {
	capture_item($("input[name='code']").val(), $("input[name='name']").val());
}
*/

	$(window).ready(function(){
		var $doc = $(document);
		//Prev
		$doc.selector = "li.prevLink a";
		$doc.live("click", navigateTo);
		//Next
		$doc.selector = "li.nextLink a";
		$doc.live("click", navigateTo);
		
		//Jump To
		$("#jumpToItem").bind("change", navigateTo);
		//ItemDetail.positionLocation is declared on item_detail.tpl since it's a PHP variable
		function navigateTo() {
			var url = "pos.php?"+ItemDetail.positionLocation+"pos_id=",
				$posId = $(this),
				posId = $posId.attr("rel") || $posId.val();
			if(posId) {
				location.href = url+posId;
			}
		}
		
		var $info = $("#popUpDetails").find(".info");
		// Bind Image Popup
		$(".itemImg a").overlay({
			target: '#popUpDetails',
			onLoad: function() { $info.show().fadeIn("slow"); }
		}).gallery({template:"<strong>${title}</strong>", autohide:false });
		
		$(".itemWoodTypes a").overlay({
			target: '#popUpDetails',
			onLoad: function() { $info.show().fadeIn("slow"); }
		}).gallery({template:"<strong>${title}</strong>", autohide:false });			
	});

