$(function(){
	radio = $("input:radio[name='size']");
	if(radio.length>0){
	 selectSize();
     selectFinish();
	 getCurItem();
	 EnableCart();
	 }
	wishlist = new Wishlist;
	wishlist.init();
	   });
   var radio=null;
   var selectedname = "";
   function setMessage(){//copy selected data to cart form
	  // var checkedrad = $("input:radio[name='size']:checked");
	  var checkedrad = $(radio).filter(':checked');
	   if(checkedrad.length>0){//checked option found
	    radio.closest("tr").css("font-weight","normal");
	    checkedrad.closest("tr").css("font-weight","bold");
	     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
		selectedname = checkedrad.closest("label").text()+": $"+$("input[name='price']").val();
		}
   };
   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(){
	 
	     radio[0].checked = true;//set 1st default selected
	     setMessage(); //populate cart form with values
 	 if(radio.length>1){ //more then 1 choice of sizes as medallion
	 	 // radio.click(function(){ 
		 radio.closest("tr").click(function(){
			 radio[$(this).index()].checked = true;
			//EnableButton(false);				   
			hideErr();
			setMessage(); 
			//EnableButton(true);
		}) 
	 }/*else{
	  radio[0].checked = true;
	  setMessage(); 
	 }*/
   };
   function getPrice(price_in){
	    var retprice = price_in;
		 if($("select[name='finish'] option:selected").text() == "Prefinished" ){
			retprice = retprice*1.1;
		 } 
		 return formatCurrency(retprice);
   };
   function selectFinish(){
	  $("select[name='finish']").change(function (){
		 //EnableButton(false);										  
	  	 setMessage();  
		 AddFinish();
		 //EnableButton(true);
       })
    };
 function AddFinish(){
	 var rprice = 0;
     $("td.colprice").each(function(){ 
		rprice = $(this).attr('price');
		//alert('TPrice:'+rprice);
		rprice = getPrice(rprice);
	   $(this).text('$'+rprice); 
	});
 };
 
 fc_PreProcessMy = function(e,arr) {
  var ret = false;
  //showErr(e.tagName);
     if (arr[ 'cart' ] == "view") {
       return true;
    }
 		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.','err');
			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','err');
		  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(),'err');
		 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.','err');
		 return false; 
		}
		
		hideErr();
		return ret;
 };
fcc.events.cart.preprocess.add(fc_PreProcessMy);

function showErr(msg_in, class_in){
	$("#message").html('').html(msg_in).addClass(class_in).show();
}
function hideErr(){
	$("#message").html('').removeClass().hide();	
}
function formatCurrency(num) {
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());
}
/*
	jQuery Wishlist Plugin
*/
Wishlist = function() {
	// initialize the wishlist JSON
	this.json = {};
	// initialize a cookie to last a year (525600 minutes)
	this.COOKIE_NAME = 'wishlist';
	this.COOKIE_OPTIONS = {
		path: '/',
		expires: 365
	}
	if (jQuery.cookie(this.COOKIE_NAME) != null) {
		// wishlist_json = eval(unescape(jQuery.cookie(this.COOKIE_NAME)));
		this.json = JSON.parse(jQuery.cookie(this.COOKIE_NAME));
		// //console.info(unescape(jQuery.cookie(this.COOKIE_NAME)));
		 //console.info(this.json);
	} else {
		jQuery.cookie(this.COOKIE_NAME, '{}', this.COOKIE_OPTIONS);
		 //console.info('No cookie set so we just set one.');
	}
	
	var me = this;
}
Wishlist.prototype.init = function() {
	var me = this;
	jQuery('a.wishlist').each(function(){
		var id = jQuery(this).attr('id').match(/id_(.+)/);
		id = id[1];
		 //console.info('me.json[id] :::: ' + me.json[id]);
		if (me.json[id] != undefined) {
			 //console.info(jQuery(this).attr('id') + ' :::: id: '+id+', :::: me.json[id]: ' + me.json[id]);
			jQuery(this).addClass('wishlist_remove').filter(':not(.wishlist_x)').html('<span>Remove</span> From Wishlist');
		} else {
			jQuery(this).addClass('wishlist_add');
		}
	});
	jQuery('a.wishlist_add').live('click', function(){
		 //console.info('ADD a.wishlist clicked');
		jQuery(this).removeClass('wishlist_add').addClass('wishlist_remove')
			.filter(':not(.wishlist_x)').html('<span>Remove</span> From Wishlist');
		me.add(this);
	});
	jQuery('a.wishlist_remove').live('click', function(){
		 //console.info('REMOVE a.wishlist clicked');
		jQuery(this).removeClass('wishlist_remove').addClass('wishlist_add')
			.filter(':not(.wishlist_x)').html('<span>Add</span> To Wishlist');
		me.remove(this);
	});
	jQuery('a.wishlist_x').live('click', function(){
		 //console.info('X a.wishlist_x clicked');
		me.remove(jQuery(this));
		jQuery(this).closest('tr').find('td').fadeOut('slow', function(){
			//jQuery(this).slideUp('slow', function(){
				jQuery(this).closest('tr').remove();
			//});
		});
	});
	 //console.info('Me? ' + me.COOKIE_NAME);
}
Wishlist.prototype.add = function(e) {
	// Get the wishlist item's attributes
	var id = jQuery(e).attr('id').match(/id_(.+)/);
	id = id[1];
	var name = jQuery(e).attr('rel').match(/wishlist_name\[(.*?)\]/);
	name = name[1];
	name = name+" "+selectedname;
	var image = jQuery(e).attr('rel').match(/wishlist_img\[(.*?)\]/);
	image = image[1];
	//console.info(id+', name: '+name+', '+image);
	// Add it to the JSON cookie
	this.json[id] = {"name": name, "image": image, "url":window.location.href};
	jQuery.cookie(this.COOKIE_NAME, JSON.stringify(this.json), this.COOKIE_OPTIONS);
}
Wishlist.prototype.remove = function(e) {
	 //console.info(e);
	// Get the wishlist item's attributes
	var id = jQuery(e).attr('id').match(/id_(.+)/);
	 //console.info('Removing ID: ' + id);
	id = id[1];
	 //console.info('Removing ID: ' + id);
	// Remove it from the JSON cookie
	delete this.json[id];
	 //console.info(JSON.stringify(this.json));
	jQuery.cookie(this.COOKIE_NAME, JSON.stringify(this.json), this.COOKIE_OPTIONS);
}
Wishlist.prototype.template = '';


