var insertPanier = Class.create( _AjaxElements , {
	
	initialize:function( $super ){
		$super();
		this.pt = null;
		this.rayon = null;
		this.provenance = null;
		this.time = null;
		this.ouvreLa = null;
		this.clearTimoutEtFerme = null;
		this.popupInfos = new Array();
		if( isSet( $('charity') ) ){
			$('submit_charity').observe( 'click' , this.mettreAuPanier.bind( this , 1 , document.forms['charity'].elements['rayon'].value , 'charity' ) );
		}
	},
	
	mettreAuPanier:function( pt , rayon , from ){
		
		var popupSuccess = document.getElementsByClassName( 'finalList');
		for( var i = 0 ; i < popupSuccess.length ; i++ ){
			popupSuccess[i].hide();
			clearTimeout(this.time);
		}

		this.setPT( pt );
		this.setRayon( rayon );
		this.setProvenance( from );
		this.paramsPanier();
	},
	
	setPT:function( value ){ this.pt = value; },
	setRayon:function( value ){ this.rayon = value; },
	setProvenance:function( value ){ this.provenance = value; },
	
	paramsPanier:function(){
		var Rl = '';
		var q = '';
		var cheque = '';
		var rayon = '&rayon='+this.rayon;
		var skuProduct = '';
		var quantite = null;
		var urlProd = '';
		var price = '';
		var charity = '';
		var custo = '' ;
		var prix = $('myprice');

		if ( this.provenance != null && this.provenance != 1 && this.provenance != 2 && this.provenance.indexOf('custo') >= 0 ) {
			var custo = '' ;
			var property = this.provenance.split ('|');
			var nbproperties = property[1];
			skuProduct = document.forms['addtocartform'].elements['skuproduct'].value;
			quantite = document.forms['addtocartform'].elements['quantite'].value;
			for (i = 1 ; i <= nbproperties ; i++ ) { 			
				var propertytxt = document.forms['addtocartform'].elements['custo'+i].value;
				custo += '&custo'+i+'='+propertytxt;
			}
			custo += q+'&custo=1' ;

		}else if( this.pt == 49 ){
			skuProduct = document.forms['addtocartform'].elements['skuproduct'].value;
			quantite = document.forms['addtocartform'].elements['quantite'].value;
			var emailCC = document.forms['addtocartform'].elements['emailCC'].value;
			var nomCC = document.forms['addtocartform'].elements['nomCC'].value;
			var texteCC = document.forms['addtocartform'].elements['texteCC'].value != '' ? document.forms['addtocartform'].elements['texteCC'].value : 'null';
			var prenomCC = document.forms['addtocartform'].elements['prenomCC'].value;
			var nameYear = document.forms['addtocartform'].elements['nameYear'].value;
			var nameMois = document.forms['addtocartform'].elements['nameMois'].value;
			var nameDay = document.forms['addtocartform'].elements['nameDay'].value;

			cheque = '&emailCC='+emailCC+'&nomCC='+nomCC+'&prenomCC='+prenomCC+'&texteCC='+texteCC+'&nameYear='+nameYear+'&nameMois='+nameMois+'&nameDay='+nameDay;

		}else if( this.pt != null && this.pt != 49 ){
			
			Rl = '&rayon_listing='+this.rayon;
			
			if ( this.provenance == 'charity' ) {
				quantite = document.forms['charity'].elements['quantity'].value;				
				skuProduct = document.forms['charity'].elements['skuproduct'].value;
				charity = '&charity=1';
			}else{
				skuProduct = document.forms['addtocaddieform'+this.rayon].elements['skuproduct'].value;
				urlProd = '&product_url='+changeIt(document.forms['addtocaddieform'+this.rayon].elements['product_url'].value);
			}
		}else{
			skuProduct = document.forms['addtocartform'].elements['skuproduct'].value;
			quantite = document.forms['addtocartform'].elements['quantite'].value;
		}
		
		
		q = isSet( quantite ) ? '&quantite='+quantite : '';

	    price = prix != null ? '&price=' + prix.value : '';
	    
	    var sendJson = 'skuProduct='+skuProduct+Rl+q+cheque+rayon+price+charity+custo+urlProd;

	    var jsonElements = { 
	    						'url':'/Panier=ajouterProduitAjax/',
	    						'myClass':this,
	    						'myMethod':'miseaupanier()',
	    						'parametres' : sendJson
	    					};
	    
	    ajaxReq = new _AjaxElements( jsonElements );
	    ajaxReq.getAjaxInformations();
	    
	},

	miseaupanier:function(){

		var pere = this.json;
		var caddyEnCours = isSet( pere.caddyElements ) ? pere.caddyElements[0] : null;
		
		if( isSet( pere.messageErreur ) ){
			if( this.pt != null ){
				var divClassErreur = document.getElementsByClassName('error');
				for( i = 0 ; i < divClassErreur.length ; i++ ){
					divClassErreur[i].update( '' );
				}

				var divErreur = $('Stocknul'+this.rayon);
				
				if ( this.provenance == 'charity' )
					divErreur = $(this.rayon);

				divErreur.update( pere.translation );

				if( $('iamwaiting'+this.rayon) != null )
					$('iamwaiting'+this.rayon).style.display = 'none';
			}else{
				$("addToCartNote").childNodes[0].update( pere.translation );
				var height = $('buttonEtNote').getHeight();
				height = height/2;
				var height2 = $('addToCartBtn').getHeight();
				height2 = height2/2;
				$("addToCartNote").style.visibility = 'visible';
				$('addToCartBtn').stopObserving( 'mouseout' );
				$("addToCartBtn").style.marginTop = ( height-height2)+'px';
			}

		}else{
			if( isSet( $('iamwaiting'+this.rayon) ) ){
				$('iamwaiting'+this.rayon).style.display = 'none';
			}
			
			if( this.provenance == null || this.provenance == 2 || ( this.provenance != null && this.provenance != 1 && this.provenance != 2 && this.provenance.indexOf('custo') >= 0 ) ) {
				switch_visibility('block', 'opacDiv');
				if( this.popupInfos.length > 0 && !isSet( $('specialCrossSelling') ) ){
					$('imgPath').src = this.popupInfos[2];
					$('btn_continuerAchat').href = this.popupInfos[3];
					$('caddiepopup').style.left = this.popupInfos[0]+'px';
					$('caddiepopup').style.top = this.popupInfos[1]+'px';
				}
				
				if( isSet( $('specialCrossSelling') ) && this.provenance != 2 ){
					var test = $('caddiepopup').clone( true );
					test.id = 'caddiepopup2';
					
					$('cadreCommander').update( test );
					$('popupimg').id = 'popupimg2';
					$('popuplabel').id = 'popuplabel2';
					$('popupTop').id = 'popupTop2'; 
					$('qteproduit').id = 'qteproduit2'; 
					$('imgPath').id = 'imgPath2'; 
					$('btn_continuerAchat').id = 'btn_continuerAchat2';
					
					$('caddiepopup2').writeAttribute( 'style' , 'width:345px;float:left;border:none;' );
					$('popupTop2').writeAttribute( 'style' , 'width:345px;float:left;margin:10px 0 15px 5px;' );
					$('popupimg2').hide();
					$('popuplabel2').hide();
					$('opacDiv').hide();
					$('btn_continuerAchat2').removeAttribute('onclick');
					$('qteproduit2').update( pere.quantite );
				}else{
					$('caddiepopup').style.display = 'block';
					$('qteproduit').update( pere.quantite );
				}
			}else if( this.provenance == 'charity' ){
				document.location = '/Panier';			
			}else{
				$('success'+this.rayon).style.display = 'block';
				$('test'+this.rayon).style.display = 'none';
				var that = this;
				this.time = setTimeout( function(){ that.closeSuccess(); } , '4000' );
			}
		
			if ( isSet( $('listing_produits') ) || isSet( $('product-view') ) && typeof( that ) == 'undefined' ) {
				if ( isSet( $('img'+this.rayon) ) ) {
					if( !isSet( $('listing_produits') ) ){
						this.popupInfos = $('caddiepopup').cumulativeOffset();
						this.popupInfos.push( $('imgPath').src );
						this.popupInfos.push( $('btn_continuerAchat').href );
					}

					var imgP = $('img'+this.rayon).src;
					$('imgPath').src = imgP;

					var idProd = $('img'+this.rayon);

					if( isSet( $('product-view') ) ){
						$('btn_continuerAchat').removeAttribute('href');
					}
					
					var leftPos = idProd.cumulativeOffset()[0];
					var topPos = idProd.cumulativeOffset()[1];

					$('caddiepopup').style.left = leftPos+'px';
					$('caddiepopup').style.top = topPos+'px';

				}
			}
			
			if ( this.shopid != 43 && this.provenance != 'charity' ){
				var ppProducts = new popupProducts( caddyEnCours , this.shopid );
			}else{
				$('item_count').update( '('+pere.caddyElements[0].numitemsincaddie+')' );
			}
		}
	},
	
	closeSuccess:function() {
		$('success'+this.rayon).style.display = 'none';
		$('success'+this.rayon).stopObserving();
		clearTimeout( this.time );
	}	
});