(function($) {
	$.fn.laa_galerie_list_adm = function(objParams)
	{
		var _idDivDisplay = $(this);
		
		var defaultParams =
		{
			namePdv:'GALERIE-PHOTOS',
			pathDirImg:'http://mon.atelier.apicius.com/maj/philippeetjeanpi/images_resized/',   
			configVignettes:{width:'70' , height:'50' , prop:false},
			configPhotos:{width:'760' , height:'525' , prop:true},
			diaporama:false,
			delayDiapo:2800
		};
		
		var makeUniqueId = $(this).attr('id');
		
		
		var _params = $.extend(defaultParams, objParams);
		var isFirstPhoto = true;
		
		var timer = null;
		var arrayVignetteDiapo = [];
		
		var Galerie_list_adm1 = 
		{
			init:function()
			{
				Galerie_list_adm1.createTemplate();
				MoteurAjax.loadXml('../xml/getTabsGallery.php' , {pdv:_params.namePdv});
			},
			createTemplate:function()
			{
				var template = '';
				template += '<div class="conteneurPhotos" id="idConteneurPhotos'+makeUniqueId+'"> </div>';
				
				template += '<div class="conteneurVignettes" id="idConteneurVignettes'+makeUniqueId+'"> </div>';
				
				template += '<div style="clear:left;"> </div>';
				
				/*template += '<div class="conteneurLegendes" id="idConteneurLegendes'+makeUniqueId+'"> </div>';*/
				
				_idDivDisplay.html(template);
			},
			createVignettesPhotoLegendes:function()
			{
				var listVignettes = '';
				listVignettes += '<ul id="idVignettesUl'+makeUniqueId+'" >';
				for(var i = 0 ; i < XmlParser.arrayGalerie.length ; i++)
				{
					var newTaille = ResizeImg.to({width:parseInt(XmlParser.arrayGalerie[i].width) , height:parseInt(XmlParser.arrayGalerie[i].height) , widthMax:_params.configVignettes.width , heightMax:_params.configVignettes.height , prop:_params.configVignettes.prop});
				
					listVignettes += '<li id="idVignettes'+ makeUniqueId + i +'"><img src="' + _params.pathDirImg + XmlParser.arrayGalerie[i].url  + '" width="' + newTaille.width  + '" height="' + newTaille.height  + '" /></li>';
					//console.log(XmlParser.arrayGalerie);
				}//fin for
				listVignettes += '</ul>';
				
				$j('#idConteneurVignettes'+makeUniqueId).html(listVignettes);
				$j("#idConteneurVignettes"+makeUniqueId).css({opacity:0 , cursor:'pointer'});
				$j("#idConteneurVignettes"+makeUniqueId).animate({opacity:1} , 300 , 'linear');
				OverOutClick.defineOverOutClickVignettes();
				
				var divPhotos = '';
				divPhotos = '<div class="photoGalerie" id="idPhotoGalerie'+makeUniqueId+'">';
				divPhotos += '<div>';
				$j('#idConteneurPhotos'+makeUniqueId+'').html(divPhotos);
				$j("#idConteneurPhotos'+makeUniqueId+'").css({opacity:0 , width:_params.configPhotos.width+'px' , height:_params.configPhotos.height+'px'});
				
				var divLegendes
				divLegendes = '<div class="legendesPhotoGalerie" id="idLegendesGalerie'+makeUniqueId+'"><span id="legendeTxt'+makeUniqueId+'">';
				divLegendes += '</span></div>';
				//$j('#idConteneurLegendes'+makeUniqueId+'').html(divLegendes);
				//$j("#idConteneurLegendes'+makeUniqueId+'").css({opacity:0});
				
				$j("#idVignettes"+makeUniqueId+"0").trigger('click');
				
				if(_params.diaporama)
				{
					Galerie_list_adm1.runDiapo();
				}//fin if
				
			},
			changePhoto:function(index)
			{
				var newTaille = ResizeImg.to({width:parseInt(XmlParser.arrayGalerie[index].width) , height:parseInt(XmlParser.arrayGalerie[index].height) , widthMax:_params.configPhotos.width , heightMax:_params.configPhotos.height , prop:_params.prop});
				
				if(isFirstPhoto)
				{
					var newPhoto = '<img src="' + _params.pathDirImg + XmlParser.arrayGalerie[index].url  + '" width="' + newTaille.width  + '" height="' + newTaille.height  + '" />';
					$j("#idPhotoGalerie"+makeUniqueId).html(newPhoto);
					$j("#idConteneurPhotos"+makeUniqueId).animate({opacity:1} , 500 , 'linear');
					
					if(XmlParser.arrayGalerie[index].legende != '')
					{
						//$j("#idConteneurLegendes"+makeUniqueId).css({width:_params.configPhotos.width - parseInt($j("#idConteneurLegendes"+makeUniqueId).css('margin-left'))/2 + 'px' , height:$j('idLegendesGalerie'+makeUniqueId).height()});
						$j("#idLegendesGalerie"+makeUniqueId).html(XmlParser.arrayGalerie[index].legende);
						//$j("#idConteneurLegendes"+makeUniqueId).animate({opacity:1} , 500 , 'linear');
						
						//var newTopLegende = parseInt($j('#idConteneurPhotos'+makeUniqueId).css('top')) + parseInt($j('#idConteneurPhotos'+makeUniqueId).height()) - parseInt($j('#idConteneurLegendes'+makeUniqueId).height()) - parseInt(2 * $j('#idConteneurLegendes'+makeUniqueId).css('padding')) + 'px';
						//$j('#idConteneurLegendes'+makeUniqueId).css({top:newTopLegende});
						
					}//fin if
					isFirstPhoto = false;
				}//fin if
				else
				{
					$j("#idConteneurPhotos"+makeUniqueId).animate({opacity:0} , 500 , 'linear' , function()
					{
						var newPhoto = '<img src="' + _params.pathDirImg + XmlParser.arrayGalerie[index].url  + '" width="' + newTaille.width  + '" height="' + newTaille.height  + '" />';
						$j("#idPhotoGalerie"+makeUniqueId).html(newPhoto);
						$j("#idConteneurPhotos"+makeUniqueId).animate({opacity:1} , 500 , 'linear');
					});
					
					/*$j("#idConteneurLegendes"+makeUniqueId).animate({opacity:0} , 500 , 'linear' , function()
					{
						$j("#idConteneurLegendes"+makeUniqueId).css({width:_params.configPhotos.width - parseInt($j("#idConteneurLegendes"+makeUniqueId).css('margin-left'))/2 + 'px' , height:$j('idLegendesGalerie'+makeUniqueId).height()});
						$j("#idLegendesGalerie"+makeUniqueId).html(XmlParser.arrayGalerie[index].legende);
						$j("#idConteneurLegendes"+makeUniqueId).animate({opacity:1} , 500 , 'linear');
						
						var newTopLegende = parseInt($j('#idConteneurPhotos'+makeUniqueId).css('top')) + parseInt($j('#idConteneurPhotos'+makeUniqueId).height()) - parseInt($j('#idConteneurLegendes'+makeUniqueId).height()) + 'px';
						$j('#idConteneurLegendes'+makeUniqueId).css({top:newTopLegende});
					});*/
					
				}//fin else
			},
			runDiapo:function()
			{
				var plusplus = 1;
				
				$j('#idConteneurVignettes'+makeUniqueId + ' ul li').each(function()
				{
					arrayVignetteDiapo.push($j(this));
				});
				
				//console.log(arrayVignetteDiapo);
				
				timer = setInterval(function()
				{
					
					if(plusplus < $j('#idConteneurVignettes'+makeUniqueId + ' ul li').length)
					{
						arrayVignetteDiapo[plusplus].trigger('click');
						plusplus ++;
					}//fin if
					else if(plusplus == $j('#idConteneurVignettes'+makeUniqueId + ' ul li').length)
					{
						clearTimeout(timer);
						arrayVignetteDiapo[0].trigger('click');
						return;
					}//in else
					
					
				} , _params.delayDiapo);
			}
		}
		
		var OverOutClick = 
		{
			defineOverOutClickVignettes:function()
			{
				for(var i = 0 ; i < XmlParser.arrayGalerie.length ; i++)
				{
					OverOutClick.clickVignettes('idVignettes'+makeUniqueId , i);
					OverOutClick.overOutVignettes('idVignettes'+makeUniqueId , i);
					$j("#idVignettes"+makeUniqueId + i).animate({opacity:1} , 0 , 'linear');
				}//fin for
			},
			clickVignettes:function(idVignette , index)
			{
				$j("#" + idVignette + index).click(function()
				{
					for(var i = 0 ; i < XmlParser.arrayGalerie.length ; i++)
					{
						$j("#idVignettes"+makeUniqueId + i).removeClass('activeVignettes');
						if(i != index)
						{
							$j("#idVignettes"+makeUniqueId + i).trigger('mouseout');
						}//fin if
					}//fin for
					Galerie_list_adm1.changePhoto(index);
					$j("#" + idVignette+makeUniqueId + index).addClass('activeVignettes');
				});
			},
			overOutVignettes:function(idVignette , index)
			{
				$j("#" + idVignette+makeUniqueId + index).hover(function()
				{
					if(!$j("#" + idVignette+makeUniqueId + index).hasClass('activeVignettes'))
					{
						$j("#" + idVignette+makeUniqueId + index).animate({opacity:1} , 200 , 'linear');
					}//fin if
				} , function()
				{
					if(!$j("#" + idVignette+makeUniqueId + index).hasClass('activeVignettes'))
					{
						$j("#" + idVignette+makeUniqueId + index).animate({opacity:0.6} , 200 , 'linear');
					}//fin if
				});
			}
		}
		
		var MoteurAjax = 
		{
			loadXml:function(urlPhp , objData)
			{
				var dataVar;
				
				if(objData == null)
				{
					dataVar = "action";
				}//fin if
				else
				{
					dataVar = eval(objData);
				}//fin else
				
				$j.ajax({ 
					url:urlPhp,
					cache:false,
					type:"POST" ,	  
					data: dataVar ,
					dataType: "xml",
					success: function(xml)
					{
						XmlParser.read(xml);
					},
					error: function(XmlHttpRequest , textStatus , errorThrown)
					{
						alert(XmlHttpRequest + "---" + textStatus + "---" + errorThrown);
					}
				});
			}
		};
		
		var XmlParser = 
		{
			arrayGalerie:[],
			read:function(xml)
			{
				$j(xml).find('galerie').each(function()
				{
					$j(this).find('pointdevente').each(function()
					{
						console.log();
						if ($j(this).attr('langue') == 'FR') {
							var inc = 0;
							var objTemp = {};
							
							$j(this).find('image').each(function()
							{
								//if(inc % 2 == 0)
								//{
									objTemp.url = $j(this).attr('fichier');
									objTemp.width = $j(this).attr('largeur');
									objTemp.height = $j(this).attr('hauteur');
									if (inc< 3) XmlParser.arrayGalerie.push(objTemp);
									objTemp = {};
								/*}//fin if
								else
								{
									
									objTemp.legende = $j(this).attr('legende');
									XmlParser.arrayGalerie.push(objTemp);
									objTemp = {};
								}//fin else*/
								if (inc< 3) console.log($j(this).attr('fichier'));
								
								inc ++;
								
							});
						}
					});
				});
				
				Galerie_list_adm1.createVignettesPhotoLegendes();
			}
		};
		
		
		var ResizeImg = {
			to: function(objParams)
			{
				//traitement des paramètre
				if (objParams.widthMax) 
					var _widthMax = objParams.widthMax;
				if (objParams.heightMax) 
					var _heightMax = objParams.heightMax;
				
				if (objParams.width) 
					var _width = objParams.width;
				if (objParams.height) 
					var _height = objParams.height;
					
				if(objParams.prop)
					var _prop = objParams.prop;
				
				var rapport = _width / _height;
				
				if (_width > _height) 
				{
					_width = _widthMax;
					_height = _widthMax / rapport;
					
					if(!_prop)
					{
						_height = _heightMax;
					}
					
					if(_height > _heightMax)
					{
						_height = _heightMax;
						_width = _heightMax * rapport;
					}//fin if
					
				}//fin if
				else if (_width < _height) 
				{
					_height = _heightMax;
					_width = _heightMax * rapport;
					
					if(!_prop)
					{
						_width = _widthMax;
					}
					
					if(_width > _widthMax)
					{
						_width = _widthMax;
						_height = _widthMax / rapport;
					}//fin if
				}//fin if
				else if (_width == _height) 
				{
					if(_widthMax >= _heightMax)
					{
						_width = _heightMax ;
						_height = _heightMax;
					}//fin if
					else if(_widthMax <= _heightMax)
					{
						_width = _height = _widthMax;
					}
				}//fin if
				
				var tab = [];
				tab = {width:Math.round(_width) , height:Math.round(_height)};
				return tab;
			}
		}
		
		
		
		$(document).ready(function()
		{
			Galerie_list_adm1.init(_params , _idDivDisplay);
		});
		
		return $(this);
	};
})(jQuery);
