if( typeof( _DetailController ) != 'undefined' ){
	var extended = _DetailController;
}else
	var extended = insertPanier;

var gestionTailles = Class.create( extended , {

	initialize:function($super){
		this.temps = null;
		this.position = new Array();
		$super();
	},
	
	sizePopup:function() {
		var select_size_zone;
		var boutonsTaille = document.getElementsByClassName( 'boutonTaille' );
		for( var i = 0 ; i < boutonsTaille.length ; i++ ){
			var c = boutonsTaille[i].className;
			
			boutonsTaille[i].observe( 'mouseover' , this.openPopupTailles.bindAsEventListener( this , boutonsTaille[i] ) );
			boutonsTaille[i].observe( 'mouseout' , this.closeSizePopup.bind( this , boutonsTaille[i] , null ) );
		}
	},
	
	openPopupTailles:function( e , classn  , unique){
		this.position = new Array();
		
		var btn = e.element();
		var position =  btn.cumulativeOffset();
		var classnom = classn.className;
		
		if( isNull( unique ) ){
			var regm = new RegExp( "(test\\d+\)" );
			var classAutre =  regm.exec(classnom)[0];
		}else{
			var classAutre =  classn;
		}
		
		var wDiv = $(classAutre).getWidth();
		var bDiv = btn.getWidth();
		
		this.position.push( position[0]-wDiv+bDiv );
		this.position.push( position[1]+btn.getHeight()+10 );

		$(classAutre).style.left = this.position[0]+'px';
		$(classAutre).style.top = this.position[1]+'px';
		
		if( typeof( srememberid ) != 'undefined' && srememberid != classAutre )
			$(srememberid).style.display = 'none';
		
		$(classAutre).style.display = 'block';
		srememberid = classAutre;
		
		if( isNull( unique ) )
			clearTimeout( this.temps );
	},

	closeSizePopup:function( classn , unique ){
		if( isNull( unique ) ){
			var classnom = classn.className;
			var regm = new RegExp( "(test\\d+\)" );
			var classAutre =  regm.exec(classnom)[0];
			$(classAutre).stopObserving();
			var that = this;
		
			$(classAutre).observe( 'mouseover' , function(){ clearTimeout( that.temps ); } );
			$(classAutre).observe( 'mouseout' , function(){ that.temps = setTimeout( function(){that.trueCloseSizePopup( classAutre ); } , '4000' ); } );
		
			this.temps = setTimeout( function(){ that.trueCloseSizePopup( classAutre ); } , '4000' );
		}else{
			$(classn).hide();
		}
	},

	trueCloseSizePopup:function( classAutre ){
		$(classAutre).style.display = 'none';
		$(classAutre).stopObserving();
		clearTimeout( this.temps );
	},
	
	neededForGetTailles:function( tab , prTy ){

		this.sizePopup();
	    var sendJson = {};
	    sendJson.idProd = new Array( tab );
	    sendJson.prodType = new Array( prTy );
	    //'idProd='+tab+'&prodType='+prTy;
	    
	    if( isSet( this.param.Corner ) ){
	    	sendJson.idCorner=this.param.Corner;
		}
	    
	    
	    var jsonElements = { 
	    						'url':'/Ajax=taillesEtStocks',
	    						'myClass':this,
	    						'myMethod':'getTailles()',
	    						'parametres' : sendJson
	    					};
	    
	    ajaxReq = new _AjaxElements( jsonElements );
	    ajaxReq.getAjaxInformations();

	},

	getTailles:function(){
		this.InitImages();
		var pere = this.json.product;
		var epuise = 0;
		for ( var i = 0 ;  i < pere.length ; i++ ){
			var supportNiveau = null;
			if ( isSet( pere[i].niveau ) ){
				var supportNiveau = 'texte';
			}else if( isSet( pere[i].niveauImg ) ){
				var supportNiveau = 'image';
			}

			var divSku = $(pere[i].unID+'_ancre');
			if( isSet( divSku ) ){
				if( pere[i].discount > 0 && this.shopid == 3 ){
					var elemntsFrame = {
							'htmlelmt' : 'span',
							'txtContent' : '-'+pere[i].discount+'%',
							'attributes' : { 'class' : 'classSpanDiscount' }
						   };
					divSku.createFrame( elemntsFrame );
				}

				if( isSet( pere[i].uneImage ) ){
					var elemntsFrame = {
							'htmlelmt' : 'div',
							'attributes' : { 'class' : 'salesdiv' }
						   };
					var divWelcoming = divSku.createFrame( elemntsFrame );
					var img48 = {
							'htmlelmt' : 'img',
							'attributes' : { 'src' : this.urlimg+pere[i].uneImage[0] }
						   };

					divWelcoming.createFrame( img48 );
					if( this.shopid == 3 && pere[i].discount > 0 ){
						divWelcoming.style.left = '35px';
					}
				}
				if( isSet( pere[i].soldes ) && pere[i].soldes != 'non'){
	            	var elemntsFrame = {
							'htmlelmt' : 'div',	
							'attributes' : { 'class' : 'salesdiv2' }
						   };
	            	var divWelcoming = divSku.createFrame( elemntsFrame );
	            	var imgSoldes = {
							'htmlelmt' : 'img',	
							'attributes' : { 'src' : this.urlimg+pere[i].soldes[0] }
						   };
	
					divWelcoming.createFrame( imgSoldes );
	            }
				if( isSet( pere[i].pwyw ) && pere[i].pwyw != 'non'){
					$('price'+tab[nbr]).style.fontSize = '14px';
	            }
			}

			if( parseFloat( pere[i].unPrix ) > 0 ){
				$('price'+pere[i].unID).update( pere[i].unPrix );
			}
			
			var txtNiveau = '';
			if( isSet( supportNiveau ) ){
				if( supportNiveau == 'texte' ){
					txtNiveau = isSet( pere[i].niveau ) ? pere[i].niveau : '';
				}else{
					var imgNiveau = {
							'htmlelmt' : 'img',	
							'attributes' : { 'class' : 'imgNiv' , 'src' : pere[i].niveauImg[0] }
						   };
					divSku.createFrame( imgNiveau );
				}
			}
			if( isSet( pere[i].unSku) ){
				if( isSet( $('ul'+pere[i].unID) ) ){
					for( var j = 0 ; j < pere[i].unSku.length ; j++ ){
						var t = pere[i].uneTaille[j].toString().split(" - ");
						var regm = new RegExp((/Article+/gi));
						var spanStyle = 'padding:0 5px;border:1px solid #888;';
						var avail = 1;
	
						if ( regm.test( t[1] ) == true ) {
							spanStyle += 'background:#888;color:white;';
							avail = 0;
						}else{
							spanStyle += 'background:#fff;color:black;cursor:pointer;';						
						}
						var elemntsFrame = {
								'htmlelmt' : 'div',
								'txtContent' : t[0],
								'attributes' : { 'class' : 'classSpan' , 'style' : spanStyle }
							   };
						var span = $('ul'+pere[i].unID).createFrame( elemntsFrame );
						
						var txt = '';
						if( isSet( t[1] ) ){
							txt = t[1];
							var stock = {
									'htmlelmt' : 'span',
									'txtContent' : t[1], 
									'attributes' : { 'style' : 'display:none;' }
							};
							span.createFrame( stock );
						}
	
						if ( avail == 1 ) {
							span.observe( 'click' , this.selectValue.bind( this , pere[i] , pere[i].unSku[j] ) );
						}
						span.observe( 'mouseover' , this.makeItBlack.bindAsEventListener( this, pere[i].unID , txt , avail ) );
						span.observe( 'mouseout' , this.makeItWhite.bindAsEventListener( this, pere[i].unID , txtNiveau , avail ) );
					}
					
					var styleStockInfo = txtNiveau == '' ? 'display:none;' : 'display:block;';
					var stockInfo = {
								'htmlelmt' : 'div',
								'txtContent' : txtNiveau,
								'attributes' : { 'id' : 'Stockul'+pere[i].unID , 'class' : 'stock' , 'style' : styleStockInfo }
							};
					$('ul'+pere[i].unID).createFrame( stockInfo );
					
					if( pere[i].aBasLeSelect == 'oui'){
						$('express'+pere[i].unID).remove();
						$('alternative'+pere[i].unID).show();
						if( isSet( $('phrase'+pere[i].unID) ) ){
							$('alternative'+pere[i].unID).observe( 'mouseover' , this.openPopupTailles.bindAsEventListener( this , 'phrase'+pere[i].unID , true ) );
							$('alternative'+pere[i].unID).observe( 'mouseout' , this.closeSizePopup.bind( this , 'phrase'+pere[i].unID , true ) );
						}
					}
				}else{
					var select = $('test'+pere[i].unID);
					if( isSet( select ) ){
						for(var nbrT = 0 ; nbrT < pere[i].unSku.length ; nbrT++){
							
							var elemntsFrame = {
									'htmlelmt' : 'option',
									'txtContent' : pere[i].uneTaille[nbrT],
									'attributes' : { 'value' : pere[i].unSku[nbrT] }
							   	};
							select.createFrame( elemntsFrame );
						}
						
						var elemntsFrame = {
								'htmlelmt' : 'span',
								'txtContent' : txtNiveau,
								'attributes' : { 'class' : 'plusQue' }
						   	};
						
						$('plusQue'+pere[i].unID).createFrame( elemntsFrame );
						
					}
					
					if( isSet( $('phrase'+pere[i].unID) ) ){
						$('express'+pere[i].unID).observe( 'mouseover' , this.openPopupTailles.bindAsEventListener( this , 'phrase'+pere[i].unID , true ) );
						$('express'+pere[i].unID).observe( 'mouseout' , this.closeSizePopup.bind( this , 'phrase'+pere[i].unID , true ) );
					}
					
					$('express'+pere[i].unID).observe( 'click' , this.mettreAuPanier.bind( this , 1 , pere[i].unID , 2 ) );
				}
			}else{
				if( isSet( $('img'+pere[i].unID) ) ){
					$(pere[i].unID+'_ancre').update( $('img'+pere[i].unID) );
				}else
					$(pere[i].unID+'_ancre').update('');
					
			}
			
			if( isSet( pere[i].pwyw ) && pere[i].pwyw != 'non'){
				$('express'+pere[i].unID).style.display = 'none';
				$('alternative'+pere[i].unID).style.display = 'block';
			}
			
			if( isSet( pere[i].epuise ) ) {
				if( typeof( idRayonPourFiche ) != 'undefined' ){
          			var reserve = document.getElementsByClassName('product_overview');
          			if( $( pere[i].unID+'_ancre').parentNode.parentNode.parentNode.id == '' )
          				epuise++;
          			$( pere[i].unID+'_ancre').parentNode.parentNode.parentNode.remove();
          		}else{
          			if ( isSet( $('express'+pere[i].unID ) ) )
          				$('express'+pere[i].unID ).style.visibility = 'hidden';
          			
          			
          			$('img'+pere[i].unID).setOpacity( 0.3 );
      				$('brand'+pere[i].unID).setOpacity( 0.3 );
      				$('label'+pere[i].unID).setOpacity( 0.3 );
          			
          			if ( isSet( $('zoom'+pere[i].unID) ) ) {
          				$('zoom'+pere[i].unID).setOpacity( 0.3 );
          			}

          			if ( isSet( $('regular'+pere[i].unID ) ) ){
          				$('regular'+pere[i].unID).style.color = '#ccc';
          			}
          			
          			if ( isSet( $('price'+pere[i].unID) ) ){
          				$('price'+pere[i].unID ).style.color = '#ccc';
          			}
          			
          			if( isSet( $(pere[i].unID+'_ancre').parentNode.parentNode.getElementsByClassName('classSpanDiscount')[0] ) )
          				$(pere[i].unID+'_ancre').parentNode.parentNode.getElementsByClassName('classSpanDiscount')[0].style.color = '#ccc';

          		}
            }
			
			if( isSet( this.json.startDate ) ){
				if( isSet( $('phraseDureeOffre'+pere[i].unID) ) ){
					$('phraseDureeOffre'+pere[i].unID).update( 'Offre valable du '+this.json.startDate+' au '+this.json.endDate );
				}
				if( isSet( $('phraseDureeOffreUnique'+pere[i].unID) ) ){
					$('phraseDureeOffreUnique'+pere[i].unID).update( 'Offre valable du '+this.json.startDate+' au '+this.json.endDate );
				}
				if( isSet( $('phraseDureeOffre'+pere[i].unID) ) && !isSet( $('phraseDureeOffreUnique'+pere[i].unID) ) ){
					$('test'+pere[i].unID).insert( '<p class="phraseDureeOffre">Offre valable du '+this.json.startDate+' au '+this.json.endDate+'</p>' );
				}
			}
			if( isSet( $('image2'+pere[i].unID) ) ){
				var image2 = $('image2'+pere[i].unID);
				$('img'+pere[i].unID).observe( 'mouseover' , this.changeImage.bindAsEventListener( this , image2 ) );
				$('img'+pere[i].unID).observe( 'mouseout' , this.changeImage.bindAsEventListener( this , image2 ) );
			}
		}
		
		
		if( typeof( idRayonPourFiche ) != 'undefined' ){
  			var reserve = document.getElementsByClassName('product_overview');
  			for( var c = 0 , d = 0 ; c < reserve.length ; c++ ){
				if( reserve[c].id != '' ){
					reserve[c].style.display = 'block';
					d++;
				}
				if( d == epuise )
					break;
			}
		}
	},
	
	changeImage:function( e , img ){
		var image1 = e.element();
		var srcOrigine = image1.src;
		image1.src = img.src;
		img.src = srcOrigine;
	},
	
	selectValue:function( pere , sku ){

		var span = $('text'+pere.unID);
		$('test'+pere.unID).style.display = 'none';
		
		document.forms['addtocaddieform'+pere.unID].elements['skuproduct'].value = sku;

		var from = 1;
		
		this.mettreAuPanier( 1 , pere.unID , from );
	
		$('iamwaiting'+pere.unID).style.display = 'block';
		$('iamwaiting'+pere.unID).style.left = this.position[0]+'px';
		$('iamwaiting'+pere.unID).style.top = this.position[1]+'px';
		$('success'+pere.unID).style.left = this.position[0]+'px';
		$('success'+pere.unID).style.top = this.position[1]+'px';
	},
	
	makeItBlack:function( e , id , txt , avail){
		var zone = e.element();
		if ( avail > 0 ) {
			zone.style.background = 'black';
			zone.style.color = 'white';
		}

		if( txt != '' ){
			$('Stockul'+id).update( txt );
			$('Stockul'+id).style.display = 'block';
		}
	},

	makeItWhite:function( e , id , txtNiveau , avail){
		var zone = e.element();
		if ( avail > 0 ) {
			zone.style.background = 'white';
			zone.style.color = 'black';
		}
		$('Stockul'+id).style.display = 'none';
		if( txtNiveau != '' ){
			$('Stockul'+id).update( txtNiveau );
			$('Stockul'+id).style.display = 'block';
		}
	},
	
	InitImages:function() {
		var divNodes = document.getElementsByClassName('cadre2');
		var imgChild = null;
		var zoomChild = null;
		var brand = null;
		var newsrc = null;
		var spanNodes = null;
		var nodes = $A(divNodes);

		nodes.each( function( node ){
			imgChild = node.getElementsByTagName("img")[0];
			if ( !isNull( imgChild ) ) {
				spanNodes = node.getElementsByTagName("span");
				zoomChild = node.getElementsByTagName("img")[1];
				
	 			var oldsrc = imgChild.src;
				var s125 = oldsrc.indexOf('125x125') != -1;
				var s221 = oldsrc.indexOf('221x221') != -1;

				if( s125 ){
					newsrc = oldsrc.replace(/125x125/,'400x400');
				}else if( s221 ){
					newsrc = oldsrc.replace(/221x221/,'350x350');
				}
				var brand = "";
				if( !isNull( zoomChild ) && spanNodes[0].hasChildNodes() ) {

                    if (spanNodes[0].firstChild) {
                      if (spanNodes[0].firstChild.data != null)
                        brand = spanNodes[0].firstChild.data;
                      if (spanNodes[1].firstChild && spanNodes[1].firstChild.data != null)
                        brand = spanNodes[1].firstChild.data;
                    }
				}
				if(typeof( imageTrail ) != 'undefined' && !isNull( newsrc ) ){
					infos = {
							'offsetfrommouse' : [ 30 , -10 ],
							'currentimageheight' : 400,
							'moveLeft' : 410,
							'docwidth' : isSet( $('listing_produits') ) ? $('listing_produits').cumulativeOffset()[0] : isSet( $('product-view') ) ? $('product-view').cumulativeOffset()[0] : null,
							'nameDiv' : 'trailimagediv',
							'contentDiv' : '<img id="trailImg" src="'+newsrc+'" border="0" class="bigarticleimg"><div class="brand">'+brand+'</div>',
							'lanceur' : zoomChild
							};
					var mouseMsg = new imageTrail( infos );
				}

			}

		});
	}
	
});