$(function() {
	
	$('#calc_width,#calc_height,#calc_depth').change(function(){ 
															  
															$('.addtocart').attr('disabled','disabled');  
															  });
	 
	calculate_options();
	
	$('#calc_button').click(function() {
			checkprod();
				var calc_width 	= 	$('#calc_width').attr('value');
				var calc_height = 	$('#calc_height').attr('value');
				var calc_depth 	= 	$('#calc_depth').attr('value');
				var calc_pid 	= 	getprodid();
				
				$.get(getbaseurl()+'ajaxproc/prodcalcprice/'+calc_pid+'/'+calc_width+'/'+calc_height+'/'+calc_depth,null,function(data, statusText) {
						$('#calc_price').attr({'value':data});	
						$('#product_calc_price').html(data).formatCurrency({ symbol : '' });
						
						$('#calc_run').attr('value','1');
						calculate_options();
				});
			
			});
	
	
	// Mail Product To Friend
	$('.ecomm_btn_email').click(function() {
		$.get(getbaseurl()+'ajaxproc/emailtofriend/' + getprodid(),null,function(data, statusText) {
			$.facebox('<div style="margin:10px;">'+data+'</div>');
		});
	});	   
	//END
	
	var $product_price = getprodprice();
      $('#container-1').tabs2();
	  	
		function calculate_options() {
			
			var formvals = $('#product_form').serialize();
			$.post(getbaseurl()+'ajaxproc/calcprodprices',formvals,function(data,statusText) {
			
				
				$(data).find("pricedata").each(function() {
					
					var prodprice 		= $(this).find('prodprice').text();
					var vattotal 		= $(this).find('vattotal').text();
					var calcprice 		= $(this).find('calcprice').text();
					var discountamt 	= $(this).find('discountamt').text();
					var vatamt 			= $(this).find('vatamt').text();
					var priceincvat 	= $(this).find('priceincvat').text();
					var prodtotal 		= $(this).find('prodtotal').text();
					var prodpriceorig 	=	$(this).find('prodprice').text();
					$('.product_price').html(priceincvat);
					$('.product_price_vat').html(prodtotal);
					$('#product_calc_price').html(calcprice); 
					$('.product_price_orig').html(prodpriceorig);
					$('.ecomm_offer_discount_amt').html(discountamt);
				});
				
				if($('#calc_run').attr('value') == '1') {
					$('#addtocart').attr('disabled','');
				}	
			
			});
			
		}
		
		function calculate_options2() {
		var $new_price = $product_price;
		

		
			$('.group_option:selected').each(function() {
			
				$new_price = Number($new_price) + Number($(this).attr('optionvalue'));
				
			
			});
			//calculator price
			//alert('S1'+$new_price);
			var cprice = Number($('#calc_price').attr('value'));
			//alert('C1'+cprice);
			$new_price = Number($new_price) + Number(cprice);
			//alert('S2'+$new_price);
			//calc price end
			if(exclvat()) {
				var vatrate = $('#vat_rate').attr('value');
				var $vat = (($new_price / 100) * vatrate);
				$new_price = Number($new_price) + Number($vat);
		
			}
			
			
			var tmp_new_price = $new_price;
			if($('#specialoffer').attr('value') == '1') {
				var so_type = $('#so_type').attr('value');
				var so_amt = $('#so_amt').attr('value');
			
				
				
				if(so_type == '1') { // AMMOUNT
					$new_price = ($new_price - so_amt);
				} else { // PERCENTAGE
					var tmp_price = (($new_price / 100) * so_amt);
					$new_price = ($new_price-tmp_price);
				}
				
			
			}
			
			if($new_price < '0.01') {
				$new_price = tmp_new_price;
			}
			
			
			$('.product_price').text($new_price);
			$('.product_price').formatCurrency({ symbol : '' });
			if($('#calc_run').attr('value') == '1') {
				$('#addtocart').attr('disabled','');
}
			
		}
		
		calculate_options();
		
		$('.option_group').change(function() {
			
			calculate_options();
			
		});
		
		var cartopts = {
			beforeSubmit: checkprod,
			success: showcartmsg
		};
		
		$('#product_form').ajaxForm(cartopts);
		function showcartmsg() {
			$('#addtocart').val('Add to Cart').attr({'disabled':''});
			
		
			reloadcart();
			var isu = $('#is_unique').val();
			if(isu == '1') {
				$('.addtocart').hide();
				$('#unique_cart_label').show();	
			}
		jAlert('This product has been added to your shopping cart.','Added to Cart!');
		}
		
		
		var jqoptions =
            {
                zoomWidth: 250,
                zoomHeight: 250
            }
			if(hasMainImage()) {
            $(".jqzoom").jqzoom(jqoptions);
			}

		$("a[rel='colorbox']").colorbox({transition:"elastic"});
		
		
		function checkprod() {
calculate_options();


	if($('#calc_enabled').attr('value') == '1') {  //HAS CALCULATOR
	
		var width = $('#calc_width').attr('value');
		var height = $('#calc_height').attr('value');
		var depth = $('#calc_depth').attr('value');
		
		if (width == '' || height == '' || depth == '') {
		
			jAlert('You must fill in the product calculator before adding this item to your cart','Warning!');
			return false;
		
		} else {

			return true;	

		
		}
		
	
	}
	
			

}
		
		$('#addtocart').click(function() {
			//$(this).val('Please Wait..').attr({'disabled':'disabled'});						   
		});
		
		
		
});
var uresponse = '';

function setur(value) {
	uresponse = value;
	alert(uresponse+' '+value);
	return uresponse;
}
function checkunique() {
	
	var pid = getprodid();
	var tmp = '';
	$.get(getbaseurl()+'ajaxproc/checkuniqecart/'+pid,null,function(data) {
		//alert('#'+data);
		setur(data);
		//tmp = data;
		
		//if(data == 'fail') {
			//uresponse = 'fail';
		//	return false;
		//} else {
		//	return true
		//}
		//alert(uresponse);
		
	});
	
	//alert(tmp);
	//if(uresponse == '') { checkunique(); }
	
	
}


function checkme() {
	var res = (checkunique()) ? 'true':'false';
	return res;
}
