var minimumQuantity;
var currentQuantity;

$(document).ready(function(){
	
	var productsExcluded = new Array();
	minimumQuantity = parseInt($('#minimum_quantity').val());
	currentQuantity = parseInt(minimumQuantity);
	
	// external links
	$("a[@rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide();
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	
	// add products in Assortment Customization
	$('#preloader').hide();
	$('.add-product-block').hide();
	
	$('#available-products').change(function() {
		var id_product = $(this).attr("value");
		var id_catp = $("#id_catp").val();
		var id_cat = $("#id_cat").val();
		var id_subcat = $("#id_subcat").val();
		var id_product_line = $("#id_product_line").val();
		
		document.getElementById("available-products").selectedIndex = 0;
		
		getProductData(id_product, id_catp, id_cat, id_subcat, id_product_line, "#assortment-customize .add-product-block");
		return false;
	}); //end trigger event
	
	$('#assortment-customize ul li a.delete').click(function() {
		//alert('aa');
		var field = $($($(this).parent().get(0)).find('input').get(0)).attr('id'); // like: "quantity23"
		var quantity = $($($(this).parent().get(0)).find('input').get(0)).val(); // quantity
		var id_product = parseInt(field.substring(8)); // delete the string "quantity"
		var product_name = $($($(this).parent().get(0)).find('h2').get(0)).text();
		
		product_name = product_name.toUpperCase();
		
		
		for (var i=1;i<=quantity;i++) {
			// repeat the substract equal to quantity
			
			substractValue(field, false);
			//alert('aa');
		}
		//alert(products_list_array);
		//alert(id_product);
		
	
		var string_products_list_array=products_list_array.toString(); //!!! needed for IE
		var productIdIndex = string_products_list_array.indexOf(id_product, 0);// indexOf doesn't work in IE, for that it needs the above line
		//alert(id_product);
		//alert(productIdIndex);
		products_list_array.splice(productIdIndex, 1);
		//alert(products_list_array);
		products_excluded_array.push(id_product);
		products_excluded_array_names.push(product_name);
		//alert(products_excluded_array);
		//alert(products_excluded_array_names);
		
		populateExcludedProducts(products_excluded_array, products_excluded_array_names);
		
		$(this).parent().remove();
		$('.add-product-block').show();
		return false;
	}); //end trigger event
	
	$("#add-customized_assortment").click( function(){
		if (currentQuantity<minimumQuantity) {
			
			$("#dialog #current-quantity").text(currentQuantity);
			$('#dialog').dialog('open');
			return false;
		}
	});
	
	$("#dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: 400,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			"Ok": function() {
				$(this).dialog('close');
			}
		}
	});
	$('#index #dialog').dialog('open');
	
	
	// Header error message
	$("#form-search .errorMsg-overlay").hide();
	$("#form-login .errorMsg-overlay").hide();
	$("#form-search .errorMsg-overlay").fadeIn(300, function(){ 
		setTimeout(function(){
			$("#form-search .errorMsg-overlay").fadeOut(300);
		}, 1200);
	});
	$("#form-login .errorMsg-overlay").fadeIn(300, function(){ 
		setTimeout(function(){
			$("#form-login .errorMsg-overlay").fadeOut(300);
		}, 1200);
	});
	
	// Header inputs
	$("#form-search #keywords").focus(function(){ 
		if ($(this).val()=="Keywords") {
			$(this).val("");
		}
	});
	$("#form-login #header-username").focus(function(){ 
		if ($(this).val()=="username") {
			$(this).val("");
		}
	});
	$("#form-login #header-password").focus(function(){ 
		if ($(this).val()=="password") {
			$(this).val("");
		}
	});
	
	
	// Added to cart
	$("#product-added-to-cart").hide();
	$("#product-added-to-cart").slideDown(500);
	$("#product-added-to-cart .but-close").click( function(){ 
		$("#product-added-to-cart").slideUp(500);
		return false;
	});
	
	
	// CHARITY
	/*$("#charity-names li").click(function(){
			var aa= $("input[type=radio]:checked").attr('id'); 
			
			var bb = [];
			var id = 0;	
			var cc;
			
			$("input[type=checkbox]:checked").each(function(){
				bb[id] =  $(this).attr('id'); 
				id++;
			});
			
			if (bb.length <1){
				cc = '';
			}
			else{
				cc = bb.join(';'); 
			}
			//alert(aa);
			//alert(cc);
			//alert(cc);
			$.ajax({
				type: "POST",
				url: "ajax-charity.php",
				data: {
					id1 : aa,
					id2: cc
					},
				success: function(data){
					$("#bla").html(data);
				}
			});			
	});
	
	$("#test .charity").click(function(){
		var aa= $("input[type=radio]:checked").attr('id'); 
		
		var bb = [];
			var id = 0;	
			var cc;
			
			$("input[type=checkbox]:checked").each(function(){
				bb[id] =  $(this).attr('id'); 
				id++;
				//$(this).attr('checked');
			});
			
			if (bb.length <1){
				cc = '';
			}
			else{
				cc = bb.join(';'); 
			}
			//alert(aa);
			//alert(cc);
			//alert(cc);
			$.ajax({
				type: "POST",
				url: "ajax-charity.php",
				data: {
					id1 : aa,
					id2: cc
					},
				success: function(data){
					$("#bla").html(data);
				}
			});			
		//alert(aa);
	});*/
	
	$('.video-link').click(function() { 
		
		var id = $(this).attr("rel");
		
		$.blockUI({
			message: $('#'+id), 
			css: { cursor: 'default', width: '800px', left: '50%', top: '15%', margin: '0 0 0 -400px', /*padding: '1px',*/ background: 'transparent', border: '0' },
			overlayCSS: { cursor: 'default', opacity: 0.8 }
		}); 
		$('.blockOverlay').attr('title','Click to close').click($.unblockUI);
		
		return false;
	});
	$('.but-close').click(function() {
		$.unblockUI();
		return false;
	});
	
});
