function optionPriceHandleBlock(){
    var parent = this;

    /*do not unset other already loaded blocks in the page*/
    if(jQuery(parent).attr('option_price_handled') == 'handled'){
        return;
    }
    jQuery(parent).attr('option_price_handled','handled');
    
    /* once radio can be seleted and highlighted at once*/
    jQuery(parent).find('.option input[type=radio]').click(function(){
            /* input radio */
            var current = this;
            /* parent div of radio */
            var currentOption = jQuery(current).parents('.option')[0];

            /* unckeck radios */
            jQuery(current).parents('.block_panier').find('.option input[type=radio]').each(function (){
                if(this != current)
                    this.checked = false;
            });

            jQuery(current).parents('.block_panier').find('.option').each(function (){
                if(this != currentOption)
                    jQuery(this).removeClass('active');
            });
            jQuery(currentOption).addClass('active');
            jQuery(currentOption).parents('.options-cointainer').qtip('hide');
            
            
            /* will ckeck current radio while bubble event*/
            return true;
        });

    /* select default element */
    jQuery(parent).find('.option input.preselected').click();
}

function optionPriceHandle(parents) {
    jQuery(parents).each(optionPriceHandleBlock);
}


function handleAddToCart(form) {

	var cancelSubmit        = false;
	var optionContainerNode = jQuery(form).find('.options-cointainer').get(0);
	
	if (form.select('input[type=radio]').length > 1) {
		if (form.select('input[type=radio]:checked').length == 0) {
			
			var message = jQuery(form).find('.selector-message').html();
	        
			var tooltipPosition = {
        		at : 'left center',
                my : 'right bottom',
                adjust : { screen : true }
            }

			jQuery(optionContainerNode).qtip({
                content: fleuranceRenderQtip(message, tooltipPosition),
                position: tooltipPosition,
				show: {event: false, ready: true},
           		hide: {event: 'option-checked'}
            });
            
            cancelSubmit = true;
		}
	}
	
	if (cancelSubmit == false) {
		
		jQuery(optionContainerNode).qtip('hide');
	    jQuery.facebox.loading();
	
	    data = form.serialize();
	
	    jQuery.ajax({
	        type: 'POST',
	        url: form.action,
	        data: data,
	        success: function (data){
	            jQuery.facebox(data);
	            previewTooltipInit(jQuery('#facebox').get(0));
	            cartRefresh();
	        }
	    });
	}


    return false;
}


function cartRefresh() {
    jQuery.ajax({
       type: 'GET',
       url: cartRefreshUrl,
       success: function (data) {
           jQuery('#cart-header-content').replaceWith(data);
           addCartHeaderCallbacks();

       }
    });
}


QuickAddForm = Class.create();
QuickAddForm.prototype = {
    
    initialize: function(rootNodeId, skuLabel, qtyLabel, rowSize){
        
        this.rootNode = $(rootNodeId);
        this.rowsNode = this.rootNode.select('.form-rows')[0];
        this.form = this.rootNode.select('form')[0];

        this.skuLabel = skuLabel;
        this.qtyLabel = qtyLabel;

        this.rowSize = rowSize;

        if ( this.rootNode.select('.add-button')[0]) {
            this.rootNode.select('.add-button')[0].observe('click', this.addRow.bind(this));
        }

        this.rootNode.select('form')[0].observe('submit', this.formSubmit.bind(this));

        this.rowCount = 0;
    },

    addButton: function(event) {
        this.addRow();
        event.stop();
        return false;
    },

    addRow: function() {

        var rowTemplate = this.getRowTemplate()

        for(var i=0; i < this.rowSize; i++) {
            
            this.rowCount = this.rowCount + 1;

            var row = {
                sku_input_label: this.skuLabel,
                sku_input_name: 'row[' + this.rowCount + '][item]',
                qty_input_label: this.qtyLabel,
                qty_input_name: 'row[' + this.rowCount + '][qty]'
            }

            var rowHtml = rowTemplate.evaluate(row);

            this.rowsNode.insert(rowHtml);
        }
    },

    getRowTemplate: function() {
        var html =  '\
            <fieldset class="commande"> \
                <label>#{sku_input_label}</label> \
                <input type="text" name="#{sku_input_name}" class="text"> \
                <label>#{qty_input_label}</label> \
                <select name="#{qty_input_name}"> \
                    <option value="1">1</option value="2"><option>2</option><option value="3">3</option>\
                    <option value="4">4</option><option value="5">5</option><option value="6">6</option>\
                    <option value="7">7</option><option value="8">8</option><option>9</option><option>10</option> \
                </select> \
            </fieldset> \
            ';

        return new Template(html);
    },

    serialize: function() {
        var current = 1;
        var result = new Object();
        var rows = this.rowsNode.select('fieldset');

        rows.each( function(currentRow) {
            
            if(typeof(currentRow) != 'undefined') {
                
                var currentRowSku = $(currentRow).select('input[type=text]')[0].value;
                
                if (currentRowSku != '') {
                    
                    var currentRowQty = currentRow.select('select')[0].selectedIndex + 1;
                    var object = {sku: currentRowSku, qty: currentRowQty};
                    result[current] = encode_base64(Object.toQueryString(object));
                    current = current + 1;
                }
            }
        })
        
        return Object.toQueryString(result);
    },

    formSubmit: function(event) {
        jQuery.facebox.loading();

        var data = {items: this.serialize()};
        var url = this.form.action;
        
        var params = {
            method: 'post',
            parameters : data,
            onSuccess: this.formSubmitCallback.bind(this)
        }

        new Ajax.Request(url,params);

        event.stop();
        return false;
    },

    formSubmitCallback: function(transport) {
    	cartRefresh();
        jQuery.facebox(transport.responseText);
    }


}
