$(document).ready(function() {
	var product_price = $("#productPrice").html();
	var productId = $('.pid').attr('id');
	var addToChart = $("#addToCart").html();

	$('#termsandconditions').live('click', function() {
		$('#terms_and_conditions')
		.dialog({
			autoOpen: false,
			resizable: true,
			modal: true,
			title: 'Mesafeli Satış Sözleşmesi',
			width: 600,
			height: 500
		
		});
		//alert($('input[name="custom_gateway"]').attr('title'));
		$('#aliciodeme').html($('input[name="custom_gateway"]').attr('title'));
		$('#faturaalici, #faturaalici2').html($('#wpsc_checkout_form_2').val()+' '+$('#wpsc_checkout_form_3').val());
		$('#faturaadres').html($('#wpsc_checkout_form_4').val());		
		$('#alici').html($('#wpsc_checkout_form_11').val()+' '+$('#wpsc_checkout_form_12').val());
		$('#aliciadres').html($('#wpsc_checkout_form_13').val());
		$('#terms_and_conditions').dialog('open');		
	});	
	
	$.post(targetURL, { 
		gp_get_cart: true},  function(data) {
			fillCart(data);
		}
	);    
	$('#shippingSameBilling').live('click', function() {
		if($('#shippingSameBilling').is(':checked')) {
			$('.table-2 tr:gt(1)').hide();
		} else {
			$('.table-2 tr:gt(1)').show();
		
		}
	});
	$('.clearbasket').live('click', function() {	
		$.post(targetURL, { 
			gp_empty_cart: true
			},  function(data) {
					var obj = fillCart(data);

				}
		);    	
		show_shop_cart();
	});
	$('.product-thumb').live('mouseover', function() {
		$('#big-image-product').attr('src', $(this).attr('rel'));
		$('#big-image-product-href').attr('href', $(this).attr('rel'));
	});			
	


	$('#addToCartTxt').live('click', function() {	
		var tURL = targetURL;
		var variations = []; 
		var controller = true;
		$('.wpsc_variation_forms').find('select').each(function(){
			if($(this).val() == 0) {
				$(this).effect("shake", { times:2 }, 200);
			 	controller = false;
			} else {
				variations[$(this).attr('name')] = $(this).val();
			}
		});
		if (controller) {
			var $resultDialog = $('<div></div>')
			.dialog({
			  autoOpen: false,
			  title: 'Sepete Ekle',
				resizable: false,
				show: 'fade',
				hide: 'fade',
				height:200,
				modal: true,
				open: function(){
					setTimeout(function(){$resultDialog.dialog("close")}, 5000);
				}
			});
			$resultDialog.html('<div style="text-align: center; margin-top: 15px;"><img src="'+baseURL+'/wp-content/themes/ozpack/images/loader.gif" /></div>');
			$resultDialog.dialog('open');		
			$.post(targetURL, { 
				gp_add_to_chart: "add_to_cart", 
				product_id: productId,
				quantity: $('#wpsc_quantity_update_'+productId).val(),
				variation:variations  
				},  function(data) {
						var obj = fillCart(data);
						$resultDialog.height($("#shop_cart_list").height()+25);
						$resultDialog.html('Ürün sepetinize eklendi.<div id="shop_cart_list_dialog">'+$("#shop_cart_list").html()+'</div>');
					}
			);    	
			show_shop_cart();
		}
	});	
	
	$('.wpsc_variation_forms').find('select').each(function(){
		$(this).change(function() {
			$('#price-loader').show();
			$("#productPrice").find('span').html(' ');				
			var variations = [];
			var controller = true;
			$('.wpsc_variation_forms').find('select').each(function(){
				if($(this).val() == 0) {
					 //$(this).effect("shake", { times:2 }, 200);
					 controller = false;
				} else {
					variations[$(this).attr('name')] = $(this).val();
				}
			});
			if (variations.length==0 || !controller) {
				$('#price-loader').hide();
				$("#productPrice").html(product_price);
			} else if(controller) {
				$.post(targetURL, { 
					gp_update_price: true,
					product_id: productId,
					variation:variations  
				},  function(data) {
					var obj = jQuery.parseJSON(data);
					$('#price-loader').hide();
					
					if(!obj.stock || obj.new_price == '0.00') {
						$("#out-of-stock").html(obj.stock_string);
						$("#out-of-stock").html(obj.stock_string);
						$("#addToCart").html('<span>Add To Cart</span>');
					}else {
						if(obj.special_price) {
							$(".oldPrice").html(obj.old_price);
							$(".oldPriceTrl").html(obj.old_currency);
							$(".savePrice").html('You save: '+ obj.save_amount+ '('+obj.save_percentage+'%)');
						}
						$(".newPrice").html(obj.new_price);
						$(".newPriceTrl").html(obj.new_currency);
						$("#addToCart").html(addToChart);	
					}
					//$("#productPrice").html(data);
					/*-if(data.indexOf('stock')!= -1) {
						$("#addToCart").html('<span>Add To Cart</span>');
					}else {
						$("#addToCart").html(addToChart);						
					}*/
				}); 
			}
		});
	});
	/*$('.submit-star').rating({
		callback: function(value, link){
			$.post(targetURL, { 
				gp_update_product_rating: true,
				product_id: productId,
				product_rating:value  
			},  function(data) {
				$("#product_ratings").html(data);
				alert("Vote success");
			});			
		}
	});*/
	$('.productRemoveCart').live('click', function() {
		$.post(targetURL, { gp_remove_chart: "true", key: this.id, 'quantity':0 },  function(data) {
			fillCart(data); 
		});		
	}); 
    var $resultDialog = $('<div></div>')
    .dialog({
      autoOpen: false,
      title: 'Result',
        resizable: true,
        show: 'fade',
        hide: 'explode',
        height:200,
        modal: true,
        open: function(){
            setTimeout(function(){$resultDialog.dialog("close")}, 5000);
        }
    });
	$('.user-order-detail').live('click', function() {	
		//alert(".status_"+this.id);
		$("#status_"+this.id).toggle();
	});


});
function fillCart(data){
	var obj = jQuery.parseJSON(data);
	var cart_content = '';
	if (obj.state) {
		if(obj.empty){
			cart_content += obj.empty_message;
		}else {
			$.each(obj.products, function() {
				cart_content += '<div class="shop_cart_product shop_cart_product-'+this.key+'">'
				
				cart_content += '<span class="productNameCart"><a title="'+this.key+'" target="_blank" href="'+this.url+'">'+this.name+'</a></span>';
				cart_content += '<span class="productPriceCart">'+this.price+'</span>';
				cart_content += '<span class="productRemoveCart" id="'+this.key+'"">&nbsp;</span>';
				cart_content += '<div style="clear:both"></div></div>';
			});
			cart_content += '<span class="productTotalCart">';
			cart_content += '<span class="productTotalNum">Kargo</span><span class="productTotalNum2">'+obj.shipping+'</span></span>';
			cart_content += '<span class="productTotalNum">Toplam</span><span class="productTotalNum2">'+obj.total+'</span></span>';
			cart_content += '<span class="productButtonCart">';
			cart_content += '<span class="cartBt"><a href="'+obj.url+'" class="gocheckout">Satın Al</a></span>';
			cart_content += '<span class="cartBt2"><a class="clearbasket">Sepeti Temizle</a></span>';
			cart_content += '</span></span>';	
		}
		$("#shop_cart_list").html(cart_content);
	//	$("<div>Success</div>").dialog({ title: 'Result' });//alert('eklenemedi');		
	} 
return obj;
	
}/*
function premove(cart_id, surl){
	var tURL = targetURL;
	$.post(targetURL, { gp_remove_chart: "true", key: cart_id, 'quantity':0 },  function(data) {
		$("#shop_cart_list").html(data); 
	});
}*/

