/**
 * 	Author - Onur Şimşek

 */
 
// Sepete ürün ekleme
$(document).ready(function(){
 
	if ($('input[name=shoppingCartID]').val()) {
		clearShoppingCartBefore();
		addShoppingCartCheckedProducts();
		updateShoppingCart(); 
	}
	
	function updateShoppingCart(){
		$.ajax({
			type	: 'post',
			async	: false,
			url		: '../ajax/updateShoppingCart.php',
			success	: function(data){
				$('#shoppingCart span').html(data);
			}
		});
	} 
	
	
	
	function addShoppingCartCheckedProducts(){
		var productsArray  = new Array(); 
		$('input[type=checkbox]:checked').each(function() { productsArray.push($(this).val()); });
		
		$.ajax({
			type	: 'post',
			async	: false,
			url		: '../ajax/addProductsArrayShoppingCart.php',
			data	: 'productsArray=' + productsArray,
			success	: function(data){
			}
		});
	}
	
	$('#demoTalepButonu').click(function(){
		$.fancybox($('#demoTalepDiv div').fadeIn(), {'padding':0});
	});
	
	$(".getProductInfo").click(function(){ 
		$.ajax({
			type	: 'post',
			async	: false,
			url		: '../ajax/getProductInfo.php',
			data	: 'productURL=' + $(this).attr('id'),
			success	: function(data){
				$('#productDesc div').empty().html(data);
				$.fancybox($('#productDesc div').fadeIn(), {'padding':0});
			}
		});
	});
	
	function clearShoppingCartBefore() {
		var shoppingCartID	= $('input[name=shoppingCartID]').val();
		$.ajax({
			type	: 'post',
			url		: '../ajax/clearShoppingCart.php',
			async	: false,
			data	: 'shoppingCartID='+ shoppingCartID,
			success	: function(data){
				//alert('temizlendi');				
			}
		});
	}
	
	// Toggle 
	$(".toggle_container").hide(); 
	$("h2.trigger").click(function(){
		$(this).toggleClass("active").next().slideToggle("slow");
		return false;
	});
 
	// Required Kategori Alanı
	var uniqueRequiredSelectCategory = $( 'div.requiredSelectCategory input[type=checkbox]'); 	
	
	uniqueRequiredSelectCategory.change(function() {
		var requiredSelectCategoryArea   = $(this).parents('div.requiredSelectCategory');		
		checkedRequiredCheckboxes = requiredSelectCategoryArea.find('input:checked').length;  

		if ( checkedRequiredCheckboxes == 0 ) {
			alert(requiredSelectCategoryArea.find('h2').text() +' '+ requiredSelectCategoryArea.find('h3').text() + ' kategorisi için en az bir seçim yapmak zorundasınız.');			
			$(this).attr('checked','checked');			
		}
		
		clearShoppingCartBefore();
		addShoppingCartCheckedProducts();
		updateShoppingCart();
	});
	
	// Tek Seçim Alanı
	var $unique = $('div.uniqueSelectCategory input[type=checkbox]'); 
	
	$unique.change(function() {		
		var   uniqueSelectCategoryArea = $(this).parents('div.uniqueSelectCategory');				
		uniqueSelectCategoryArea.find('input[type=checkbox]').not(this).removeAttr('checked');
	
		clearShoppingCartBefore();
		addShoppingCartCheckedProducts();
		updateShoppingCart();
	});

	
	$('input[type=checkbox], label').click(function(){
		$('#shoppingCart span').html('<img src="../images/ajax-loader.gif" alt="Yükleniyor..." />');
		
		clearShoppingCartBefore();
		addShoppingCartCheckedProducts();	
		updateShoppingCart();
	});
	
	$('input[name=shoppingEndButton]').click(function(){  
		window.location.href = '/shoppingEnd';
		return false;
	});
	
	
	$('input[type="text"]').addClass("idleField");
	$('input[type="text"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
    	if (this.value == this.defaultValue){
    		this.value = '';
		}
    	if(this.value != this.defaultValue){
    		this.select();
	    	}
		});
	$('input[type="text"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	   
	});
	
	$('textarea').addClass("idleField_textarea");
	
	$('textarea').focus(function() {
		$(this).removeClass("idleField_textarea").addClass("focusField_textarea");
    	if (this.value == this.defaultValue){
    		this.value = '';
		}
    	if(this.value != this.defaultValue){
    		this.select();
	    	}
		});
	$('textarea').blur(function() {
		$(this).removeClass("focusField_textarea").addClass("idleField_textarea");
	  
	});
});
