 function post_coupon_form(target, confirm){

        var form    = jQuery(target).parents('.discount-coupon-form');

        var action = form.attr('action');
        var data = form.serialize();
        data += '&is_ajax=1';
        
        if(confirm){
            data +=  '&confirmation=1';
        }
        
        jQuery.ajax({
            type: 'POST',
            url: action,
            data: data,
            success: function (data){
                if (data.success) {
                    var location = window.location.toString();
                    if (location.search('checkout/cart') != -1) {//on checkout page
                        window.location = window.location;       //reload

                    } else if (typeof(data.url) == 'undefined') {  //the code has no page associated with
                        window.location = '/';                     //go to home page

                    } else {                                        //the code has a dedicated page
                        window.location = data.url;                 //go to code's page
                    }
                }
                
                jQuery.facebox( data.html );
                
                // When new offer are submitted manage selector container color switch
                jQuery('.offer_switch').bind('click', function() {
                	jQuery('.offer_switch').parents('.option.active').removeClass('active');
                	jQuery('.offer_switch:checked').parents('.option').addClass('active');
                	return true;
                })
            },
            dataType: 'json'
        });
        
        return false;
}

function coupon_form_onclick(target, confirm){
    jQuery.facebox.loading();
    post_coupon_form(target, confirm);
    return false;
}


jQuery('document').ready(function() {
    addCartHeaderCallbacks();
});

function addCartHeaderCallbacks() {
    
    jQuery('.bloc_list_arrow').attr('status','folded').click(function(){
        if(jQuery(this).attr('status') == 'folded') {
            jQuery(this).parents('.bloc_list_dynamic').addClass('active');
            jQuery(this).attr('status','');
        }else{
            jQuery(this).parents('.bloc_list_dynamic').removeClass('active');
            jQuery(this).attr('status','folded');
        }
        return false;
    });

    jQuery('.block_infos a[rel*=tooltip]').each(function(e){
    	if (jQuery(jQuery(this).attr('href')).length > 0) {
	        var content = jQuery(jQuery(this).attr('href'))[0].innerHTML;
	        var myPos;
	        myPos = {
	                at : 'left top',
	                my : 'right	bottom',
	                adjust : {screen : true}
	        }
	
	        jQuery(this).qtip({
	                content: fleuranceRenderQtip(content,myPos),
	                position: myPos
	        });
    	}
    });

    jQuery('a.stock_tooltip').each(function(e){
    	if (jQuery(jQuery(this).attr('href')).length > 0) {
	        var content = jQuery(jQuery(this).attr('href'))[0].innerHTML;
	        var myPos;
	        myPos = {
	                at : 'left top',
	                my : 'right bottom',
	                adjust : {x : 60, y : 10}
	        }
	
	        jQuery(this).qtip({
	                content: fleuranceRenderQtip(content,myPos),
	                position: myPos,
	                style:'addtocart-tooltip'
	            });
		}
    });
    
    jQuery('a.order_tooltip').each(function(e){
    	if (jQuery(jQuery(this).attr('href')).length > 0) {
	        var content = jQuery(jQuery(this).attr('href'))[0].innerHTML;
	        var myPos;
	        myPos = {
	                at : 'left top',
	                my : 'right bottom',
	                adjust : {x: -10, y : 10}
	        }
	
	        jQuery(this).qtip({
	                content: fleuranceRenderQtip(content,myPos),
	                position: myPos,
	                style:'addtocart-tooltip'
	            });
		}
    });
    

    jQuery('.item_panier a.lien_sup').each(function() {
        var url     = new String(this.href);
        var regExp  = new RegExp('/uenc/.*/', "g");
        var referer = new String(encode_base64(window.location.href));
        referer = referer.replace('+/=', '-_,');
        url = url.replace(regExp, '/uenc/' + referer + '/');
        this.href = url;
    });
    
}

jQuery(document).ready(function() {
	var coupon_code_links = 'li.virtual-category-link.flash_sales a';
	
	jQuery(coupon_code_links).click(function() {
		var targetUrl = this.href.toString();
		if (targetUrl.match(/checkout\/cart\/couponPost\//g)) {
			// Coupon URL : ajax submit
			jQuery.ajax({
	            type: 'get',
	            url: targetUrl,
	            success: function (data){
	                if (data.success){
	                    var location = window.location.toString();
	                    if (location.search('checkout/cart') != -1) {
	                        window.location = window.location;
	                    } else {
	                        window.location = data.url;
	                    }
	                }
	                jQuery.facebox( data.html );
	            },
	            dataType: 'json'
	        });
			return false;
		}
		
		// Normal url : normal link behavior
		return false;
	})
});

function openHeaderAdvantageCode() {
	jQuery('.block_list').find('.block_list_dynamic').removeClass('active');
	jQuery('.block_list').find('.bloc_list_arrow').attr('status', 'folded');
	
	jQuery('#header-coupon-code .bloc_list_dynamic').addClass('active');
	jQuery('#header-coupon-code .bloc_list_arrow').attr('status', '');
	
	jQuery('body').animate({'scrollTop': '30px'});
	jQuery('html').animate({'scrollTop': '30px'});
	
	return false;
}

function closeHeaderAdvantageCode() {
	jQuery('.block_list').find('.block_list_dynamic').addClass('active');
	jQuery('.block_list').find('.bloc_list_arrow').attr('status', '');

	jQuery('#header-coupon-code .bloc_list_dynamic').removeClass('active');
	jQuery('#header-coupon-code .bloc_list_arrow').attr('status', 'folded');

	jQuery('body').animate({'scrollTop': '0px'});

	return false;
}


/** Product gift chooser form handle */

function handleGiftChooserChange(inputItem) {

    var form = inputItem.form;
    var data = $(form).serialize(true);
    var action = form.action;

    jQuery.ajax({
        type: 'POST',
        url: action,
        data: data,
        success: function (data){}
    });

    jQuery(form.parentNode.parentNode).find('input[type=radio]').removeAttr('checked');
    jQuery(inputItem).attr('checked', true);

    return true;
}

