/**
  * giftvoucher.js
  * Gift voucher module
  * @category modules
  *
  * @author dids <prestashop@dids.fr>
  * @copyright dids - des idées et des sites - 2009-2011
  * @version 1.0.11
  *
  */

var giftvoucher = {
	jsonProducts : null,
	cartProduct : null,
	no_gitvoucher_to_customize : true,
	ajax_url : giftvoucher_baseDir + '/modules/giftvoucher/giftvoucher-ajax.php',
	orderLink : false,
	ie_alpha : 'alpha(opacity=70)',

	//add product to cart
	addToCart : function(jsonData) {
		if (giftvoucher_mode == 'CUSTOMER') {
			giftvoucher.customCartActions();
			return;
		}
		
		//if (jsonData.products.length == 0) return;

		giftvoucher.jsonProducts = jsonData.products;
		//giftvoucher.cartProduct = giftvoucher.jsonProducts[jsonData.products.length - 1];
		for (aProduct in jsonData.products) if ((aProduct != 'indexOf') && (aProduct != null)) giftvoucher.cartProduct = jsonData.products[aProduct];	// l'expression ci-dessus ne fonctionnant pas sous IE : correctif...
		var id_product = giftvoucher.cartProduct ? giftvoucher.cartProduct.id : null;
		var is_giftvoucher_product = false;
		var tab_giftvoucher_products = giftvoucher_products.split(new RegExp("[,]+", "g"));
		
		for (var i = 0; i < tab_giftvoucher_products.length; i++) {
			if (tab_giftvoucher_products[i] == id_product) {
				is_giftvoucher_product = true;
				break;
			}
		}
		
		if (!is_giftvoucher_product) {
			giftvoucher.customCartActions();
			return;
		}

		giftvoucher.getHTMLGiftVouchersCustomization(false, function() {
			if (giftvoucher.no_gitvoucher_to_customize) {
				giftvoucher.customCartActions();
				return;
			}
			
			if (giftvoucher_mode == 'CHOICE') {
				giftvoucher.askChoice(true);
			} else {
				giftvoucher.askInfos();
			}
		});
	},
	
	askChoice : function(check) {
		$('#giftvoucher_mask').css({'width':$(window).width(),'height':$(document).height()});
		if ($.browser.msie) $('#giftvoucher_mask').css('filter', giftvoucher.ie_alpha);
		$('#giftvoucher_mask').fadeIn('fast');
		$('#giftvoucher_ask_choice_block').css({'top':$(window).height()/2-$('#giftvoucher_ask_choice_block').height()/2,'left':$(window).width()/2-$('#giftvoucher_ask_choice_block').width()/2});
		$('#giftvoucher_ask_choice_block').fadeIn('fast');
	},
	
	answerChoice : function(choice) {
		$('#giftvoucher_mask').fadeOut('fast');
		$('#giftvoucher_ask_choice_block').fadeOut('fast');
		
		if (choice) {
			giftvoucher.askInfos();
		} else {
			giftvoucher.setNoInfo();
			giftvoucher.customCartActions();
		}
			
		return false;
	},
	
	askInfos : function() {
		$('#giftvoucher_mask').css({'width':$(window).width(),'height':$(document).height()});
		if ($.browser.msie) $('#giftvoucher_mask').css('filter', giftvoucher.ie_alpha);
		$('#giftvoucher_mask').fadeIn('fast');
		$('#giftvoucher_ask_infos_block').css({'top':$(window).height()/2-$('#giftvoucher_ask_infos_block').height()/2,'left':$(window).width()/2-$('#giftvoucher_ask_infos_block').width()/2});
		$('#giftvoucher_ask_infos_block').fadeIn('fast');	
	},
	
	aswerInfos : function() {
		giftvoucher.sendGiftVouchersCustomization(function() {
			$('#giftvoucher_mask').fadeOut('fast');
			$('#giftvoucher_ask_infos_block').fadeOut('fast');
		
			if (giftvoucher.orderLink) {
				giftvoucher.continueOrderProcess();
			} else {
				giftvoucher.customCartActions();
			}
		});
		
		return false;
	},
	
	checkAll : function() {
		giftvoucher.orderLink = true;
		
		if (giftvoucher_mode == 'CUSTOMER') {
			giftvoucher.continueOrderProcess();
			return;
		}
		
		giftvoucher.getHTMLGiftVouchersCustomization(true, function() {
			if (giftvoucher.no_gitvoucher_to_customize) {
				giftvoucher.continueOrderProcess();
				return;
			}
			
			if (giftvoucher_mode == 'CHOICE') {
				giftvoucher.askInfos();
			}
		});
	},
	
	continueOrderProcess : function() {
		// do nothing...
	},
	
	getHTMLGiftVouchersCustomization : function(all, callback) {
		//send the ajax request to the server
		$.ajax({
			type: 'GET',
			url: giftvoucher.ajax_url,
			async: true,
			cache: false,
			dataType : "html",
			data: '&customize=true&all=' + (all ? 'true' : 'false') + '&token=' + static_token,
			success: function(htmlData)
			{
				giftvoucher.no_gitvoucher_to_customize = (htmlData.replace(/^\s+/g,'').replace(/\s+$/g,'') == '0');
				if (!giftvoucher.no_gitvoucher_to_customize)
				      $("#giftvoucher_ask_infos_form").html(htmlData);
				callback.apply();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to get infos about gift vouchers.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
		});
	},
	
	setNoInfo : function() {
		//send the ajax request to the server
		$.ajax({
			type: 'GET',
			url: giftvoucher.ajax_url,
			async: true,
			cache: false,
			dataType : "json",
			data: '&no_info=true&token=' + static_token,
			error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to set infos about gift vouchers.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
		});
	},
	
	sendGiftVouchersCustomization : function(callback) {
		if ($("div.error").is(":visible")) $("div.error").hide(500);
	
		var params = {};		
		$("#giftvoucher_ask_infos_form").find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea").filter(":enabled").each(function() { 
			params[this.name || this.id || this.parentNode.name || this.parentNode.id] = this.value;
		}); 
		
		$('#giftvoucher_mask_all').css({'width':$(window).width(),'height':$(document).height()});
		if ($.browser.msie) $('#giftvoucher_mask_all').css('filter', giftvoucher.ie_alpha);
		$('#giftvoucher_mask_all').fadeIn(0);
				
		//send the ajax request to the server
		$.ajax({
			type: 'POST',
			url: giftvoucher.ajax_url + '?customize=true&token=' + static_token,
			async: true,
			cache: false,
			dataType : "json",
			data: params,
			success: function(jsonData)
			{
				$('#giftvoucher_mask_all').fadeOut('fast');
				
				if (!jsonData.hasError) {
					callback.apply();
				} else {
					var html_error = "<ul>";
					for (var i = 0; i < jsonData.errors.length; i++) {
						html_error += "<li>" + jsonData.errors[i] + "</li>";
					}
					html_error += "</ul>";
					$("#giftvoucher_ask_infos_block div.error").html(html_error).filter(":hidden").fadeIn("normal");
					$("#giftvoucher_ask_infos_block div.error").show(500);
					$("#giftvoucher_ask_infos_block div.block_content").animate({scrollTop: 0}, 500);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("TECHNICAL ERROR: unable to send infos about gift vouchers.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
				$('#giftvoucher_mask_all').fadeOut('fast');
			}
		});
		
		return false;
	},
	
	customCartActions : function() {
		// this method is the last called by giftvoucher
		// put here your own code for cart action

		if (giftvoucher.cartProduct) {
			var domIdProduct = giftvoucher.cartProduct.id + (giftvoucher.cartProduct.idCombination ? '_' + giftvoucher.cartProduct.idCombination : '');
		
			// check if new product was added
			if($('dt#cart_block_product_' + domIdProduct + ' .quantity').text() != giftvoucher.cartProduct.quantity) {
				  // this method is the last called by giftvoucher
				  // put here your own code for cart action
			}
		}
	},
	
	updateDivCustomization : function(id) {
		if ($("#gv_no_customize_" + id).is(":checked")) {
			$("#gv_customization_" + id).hide(500);
		} else {
			$("#gv_customization_" + id).show(500);
		}
		
		return true;
	}
};


$(document).ready(function() {
	if ($.browser.msie)
	      giftvoucher.ie_alpha = $('#giftvoucher_mask').css('filter');
});

