(function($) {
	$.fn.laa_galerie_list_adm = function(objParams)
	{
		var _idDivDisplay = $(this);
		
		var defaultParams =
		{
			namePdv:'GALERIE-PHOTOS',
			pathDirImg:'http://miseajour.apicius.com/photos/',
			configVignettes:{width:'70' , height:'50' , prop:false},
			configPhotos:{width:'760' , height:'525' , prop:true}
		};
		
		var _params = $.extend(defaultParams, objParams);
		var isFirstPhoto = true;
		
		
		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"> </div>';
				
				template += '<div class="conteneurVignettes" id="idConteneurVignettes"> </div>';
				
				template += '<div style="clear:left;"> </div>';
				
				template += '<div class="conteneurLegendes" id="idConteneurLegendes"> </div>';
				
				_idDivDisplay.html(template);
			},
			createVignettesPhotoLegendes:function()
			{
				var listVignettes = '';
				listVignettes += '<ul id="idVignettesUl" >';
				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'+ i +'"><img src="' + _params.pathDirImg + XmlParser.arrayGalerie[i].url  + '" width="' + newTaille.width  + '" height="' + newTaille.height  + '" /></li>';
				}//fin for
				listVignettes += '</ul>';
				
				$j('#idConteneurVignettes').html(listVignettes);
				$j("#idConteneurVignettes").css({opacity:0 , cursor:'pointer'});
				$j("#idConteneurVignettes").animate({opacity:1} , 300 , 'linear');
				OverOutClick.defineOverOutClickVignettes();
				
				var divPhotos = '';
				divPhotos = '<div class="photoGalerie" id="idPhotoGalerie">';
				divPhotos += '<div>';
				$j('#idConteneurPhotos').html(divPhotos);
				$j("#idConteneurPhotos").css({opacity:0 , width:_params.configPhotos.width+'px' , height:_params.configPhotos.height+'px'});
				
				var divLegendes
				divLegendes = '<div class="legendesPhotoGalerie" id="idLegendesGalerie"><span id="legendeTxt">';
				divLegendes += '</span></div>';
				$j('#idConteneurLegendes').html(divLegendes);
				$j("#idConteneurLegendes").css({opacity:0});
				
				$j("#idVignettes0").trigger('click');
				
			},
			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").html(newPhoto);
					$j("#idConteneurPhotos").animate({opacity:1} , 500 , 'linear');
					
					if(XmlParser.arrayGalerie[index].legende != '')
					{
						//$j("#idConteneurLegendes").css({width:_params.configPhotos.width - parseInt($j("#idConteneurLegendes").css('margin-left'))/2 + 'px' , height:$j('idLegendesGalerie').height()});
						$j("#idLegendesGalerie").html(XmlParser.arrayGalerie[index].legende);
						$j("#idConteneurLegendes").animate({opacity:1} , 500 , 'linear');
						
						var newTopLegende = parseInt($j('#idConteneurPhotos').css('top')) + parseInt($j('#idConteneurPhotos').height()) - parseInt($j('#idConteneurLegendes').height()) - parseInt(2 * $j('#idConteneurLegendes').css('padding')) + 'px';
						//$j('#idConteneurLegendes').css({top:newTopLegende});
						
					}//fin if
					isFirstPhoto = false;
				}//fin if
				else
				{
					$j("#idConteneurPhotos").animate({opacity:0} , 500 , 'linear' , function()
					{
						var newPhoto = '<img src="' + _params.pathDirImg + XmlParser.arrayGalerie[index].url  + '" width="' + newTaille.width  + '" height="' + newTaille.height  + '" />';
						$j("#idPhotoGalerie").html(newPhoto);
						$j("#idConteneurPhotos").animate({opacity:1} , 500 , 'linear');
					});
					
					$j("#idConteneurLegendes").animate({opacity:0} , 500 , 'linear' , function()
					{
						//$j("#idConteneurLegendes").css({width:_params.configPhotos.width - parseInt($j("#idConteneurLegendes").css('margin-left'))/2 + 'px' , height:$j('idLegendesGalerie').height()});
						$j("#idLegendesGalerie").html(XmlParser.arrayGalerie[index].legende);
						$j("#idConteneurLegendes").animate({opacity:1} , 500 , 'linear');
						
						var newTopLegende = parseInt($j('#idConteneurPhotos').css('top')) + parseInt($j('#idConteneurPhotos').height()) - parseInt($j('#idConteneurLegendes').height()) + 'px';
						//$j('#idConteneurLegendes').css({top:newTopLegende});
					});
					
				}//fin else
			}
		}
		
		var OverOutClick = 
		{
			defineOverOutClickVignettes:function()
			{
				for(var i = 0 ; i < XmlParser.arrayGalerie.length ; i++)
				{
					OverOutClick.clickVignettes('idVignettes' , i);
					OverOutClick.overOutVignettes('idVignettes' , i);
					$j("#idVignettes" + i).animate({opacity:0.6} , 0 , 'linear');
				}//fin for
			},
			clickVignettes:function(idVignette , index)
			{
				$j("#" + idVignette + index).click(function()
				{
					for(var i = 0 ; i < XmlParser.arrayGalerie.length ; i++)
					{
						$j("#idVignettes" + i).removeClass('activeVignettes');
						if(i != index)
						{
							$j("#idVignettes" + i).trigger('mouseout');
						}//fin if
					}//fin for
					Galerie_list_adm1.changePhoto(index);
					$j("#" + idVignette + index).addClass('activeVignettes');
				});
			},
			overOutVignettes:function(idVignette , index)
			{
				$j("#" + idVignette + index).hover(function()
				{
					if(!$j("#" + idVignette + index).hasClass('activeVignettes'))
					{
						$j("#" + idVignette + index).animate({opacity:1} , 200 , 'linear');
					}//fin if
				} , function()
				{
					if(!$j("#" + idVignette + index).hasClass('activeVignettes'))
					{
						$j("#" + idVignette + 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()
					{
						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');
							}//fin if
							else
							{
								objTemp.legende = $j(this).attr('legende');
								XmlParser.arrayGalerie.push(objTemp);
								objTemp = {};
							}//fin else
							
							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);
