var MProduct = Class.create();
MProduct.prototype = {
		
	initialize: function () {
		this.loadWaiting = false;
		
		// enveloppe treatment
		this.onEnveloppeInformationComplete = this.updateEnveloppeInformationArea.bindAsEventListener(this);
		this.onEnveloppeInformationSuccess = this.resetLoadWaiting.bindAsEventListener(this);
	},
	
	loadEnveloppeInformation: function(url) {
        $(this.envInfoArea).setStyle({ background: 'white', width: '640px', height:'300px' });
        $(this.envInfoArea).update('<div id="loading"><a id="loadingLink" href="#"></a><div>');
    
		url = url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true');
		
		var request = new Ajax.Request(url, {
                method: 'post',
                onComplete: this.onEnveloppeInformationComplete,
                onSuccess: this.onEnveloppeInformationSuccess
            }
        );
	},
	
	setEnveloppeInformationArea: function(area)
	{
		this.envInfoArea = area;
	},
	
	updateEnveloppeInformationArea: function(transport){
		        
		if (transport.responseText.isJSON()) {
			var response = transport.responseText.evalJSON()
			if (response.error) {
				alert(response.error.message);
            }
			else
			{
				// get title
				var title = response.update_block.title;
				// TODO Bastien je t'ai prèvu un titre à utiliser
				
				// update area
                $('outerImageContainer').setStyle({ background: 'none' });
				$(this.envInfoArea).update(response.update_block.html);
                $(this.envInfoArea).setStyle({ width: '640px', height:$('enveloppes_content').getDimensions().height+'px' });
                
                // selection enveloppe                
                var optionsRadio = $$('.radio_bt');
                optionsRadio.each(function(eachOptionRadio) {
                    if (eachOptionRadio.checked) {
                        var optionValueTab = eachOptionRadio.id.split('_');
                        optionValue = optionValueTab[1]+'_'+optionValueTab[2];
                        checkOptionEnveloppe(optionValue);
                        checkEnveloppe(optionValue);
                        checkOptionRadio(optionValue);
                    }
                    eachOptionRadio.observe('click', selectOption);
                });
                
                var btEnveloppes = $$('.enveloppes');
                btEnveloppes.each(function(eachEnveloppe) {
                    eachEnveloppe.observe('click', selectOption);
                    eachEnveloppe.observe('mouseover', overEnveloppe);
                    eachEnveloppe.observe('mouseout', outEnveloppe);
                });
			}
		}
	},
	
	resetLoadWaiting: function(transport){
		// TODO implement
    }
}