(function($)
{	
	
	var $this = null;
	var params = null;
	var template;
	
	var arrayImg = [];
	var currentImg;
	var newSize = null;
	
	var heightViewport = 0;
	
	var isFullScreen = false;
	
	//methode public
	var PublicMethodes =
	{
		init:function(paramUser)
		{
			//BrowserDetect.fullDetection();
			
			$this = $(this);
			return this.each(function()
			{ 
				var defaultParams = 
				{
					div:'divStockImgGalerie',
					imgWidth:'1150',
					imgHeight:'600'
				}
				params = $.extend(defaultParams , paramUser);
				PrivateMethodes.getImgFromPage(params.div);
				PrivateMethodes.createTemplate();
				
				Positionning.galerie();
				Positionning.arrow();
				
				EventHandler.clickArrows();
				EventHandler.clickBtnFullScreen();
				
				EventHandler.overOutArrow();
				EventHandler.overOutFullScreen();
				
				EventHandler.bindResize();
				
				PatchCss.activate();
			});
		}
	};
	
	var PatchCss = 
	{
		activate:function()
		{
			if(isIe9)
			{
				//$j('#idFullScreen').css({top:'0px'});
			}//fin if
			if(isFirefox)
			{
				
			}//fin if
		}
	}
	
	//methode privée
	var PrivateMethodes = 
	{
		createTemplate : function()
		{
			template = '';
			
			template += '<div class="galerie" id="idGalerie">';
			template += '<div class="conteneurImgGalerie" id="idConteneurImgGalerie"><img src="'+arrayImg[0].url+'" /></div>';
			template += '<div class="btnFullScreen" id="idFullScreen"></div>';
			template += '<div class="arrowLeft arrowClick" id="idArrowLeft"></div>';
			template += '<div class="arrowRight arrowClick" id="idArrowRight"></div>';
			template += '</div>';
			
			$this.html(template);
			currentImg = 0;
			
			if(isIe7)
			{
				$j('#idFullScreen').css({display:'none'});
			}//fin if
		},
		getImgFromPage:function(div)
		{
			arrayImg = [];
			$('.' + div + ' img').each(function()
			{
				arrayImg.push({url:$(this).attr('src')});
			});
		}
	}
	
	var EventHandler = 
	{
		overOutArrow:function()
		{
			$('.arrowClick').hover(function()
			{
				if($(this).attr('id') == 'idArrowLeft')
				{
					$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') -50px -50px no-repeat'});
				}//fin if
				else if($(this).attr('id') == 'idArrowRight')
				{
					$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') -50px -100px no-repeat'});
				}//fin else
				
			} , function()
			{
				if($(this).attr('id') == 'idArrowLeft')
				{
					$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') 0px -50px no-repeat'});
				}//fin if
				else if($(this).attr('id') == 'idArrowRight')
				{
					$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') 0px -100px no-repeat'});
				}//fin else
			});
		},
		clickArrows:function()
		{
			$('.arrowClick').click(function()
			{
				var value = null;
				if($(this).attr('id') == 'idArrowLeft')
				{
					currentImg++;
					if(currentImg == arrayImg.length)
					{
						currentImg = 0;
					}//fin if
					
					value = newSize.width;
				}//fin if
				else if($(this).attr('id') == 'idArrowRight')
				{
					currentImg--;
					if(currentImg == -1)
					{
						currentImg = arrayImg.length-1;
					}//fin if
					
					value = -newSize.width;
				}//fin else
				
				$('#idConteneurImgGalerie').append('<img src="'+arrayImg[currentImg].url+'" width="'+newSize.width+'" height="'+newSize.height+'" />');
				
				$('#idConteneurImgGalerie img:gt(0)').css({marginLeft:value});
				$('#idConteneurImgGalerie img:gt(0)').stop(true , true);
				$('#idConteneurImgGalerie img:first').animate({marginLeft:-value} , 500 , 'linear');
				$('#idConteneurImgGalerie img:gt(0)').animate({marginLeft:0} , 500 , 'linear' , function()
				{
					$('#idConteneurImgGalerie img:first').remove();
				});
			});
		},
		overOutFullScreen:function()
		{
			$('#idFullScreen').hover(function()
			{
				$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') -50px 0px no-repeat'});
			} , function()
			{
				$j(this).css({background:'url(\'../images/galerie/element/spriteGalerie.png\') 0px 0px no-repeat'});
			});
		},
		clickBtnFullScreen:function()
		{
			$('#idFullScreen').click(function()
			{
				Positionning.fullScreen();
			});
		},
		bindResize:function()
		{
			$(window).resize(function()
			{
				if(!isFullScreen)
				{
					Positionning.galerie();
					Positionning.arrow();
				}//fin if
				else
				{
					$j('#idFullScreen').trigger('click');
				}//fin else
			});
		}
	}
	
	var Positionning = 
	{
		galerie:function()
		{
			if(!isFullScreen)
			{
				$(window).load(function()
				{
					$('#idGalerie').css( {width:'100%' , height:$(window).height() - ( $('#idConteneurNav').height() + $('#footer').height() + parseInt($('.tableauContenu').css('padding-top')) + parseInt($('.tableauContenu').css('padding-bottom'))) , paddingTop:'20px'});
					
					
					newSize = ResizeImg.to({imgWidth:params.imgWidth , imgHeight:params.imgHeight , widthMax:$('#idGalerie').width() - 100 , heightMax:$('#idGalerie').height() - 100 , proportional:true });
					$('#idGalerie img:first').attr({width:newSize.width , height:newSize.height});
					$('#idGalerie').css( {width:newSize.width});
					$('#idConteneurImgGalerie').css( {width:newSize.width , height:newSize.height});
					
				});
				
				$('#idGalerie').css( {width:'100%' , height:$(window).height() - ( $('#idConteneurNav').height() + $('#footer').height() + parseInt($('.tableauContenu').css('padding-top')) + parseInt($('.tableauContenu').css('padding-bottom'))) , paddingTop:'20px'});
				
				newSize = ResizeImg.to({imgWidth:params.imgWidth , imgHeight:params.imgHeight , widthMax:$('.tableauContenu').width() , heightMax:$('#idGalerie').height() , proportional:true });
			}//fin if
			else
			{
				$('#idGalerie').css({width:'100%' , height:$(window).height() , top:0});
				newSize = ResizeImg.to({imgWidth:params.imgWidth , imgHeight:params.imgHeight , widthMax:$('#idGalerie').width()- ( parseInt($('.tableauContenu').css('padding-left')) + parseInt($('.tableauContenu').css('padding-right'))) , heightMax:$('#idGalerie').height() - ( parseInt($('.tableauContenu').css('padding-top')) + parseInt($('.tableauContenu').css('padding-bottom'))) , proportional:true });
			}//fin else
			
			$('#idGalerie img:first').attr({width:newSize.width , height:newSize.height});
			$('#idGalerie').css( {width:newSize.width});
			$('#idConteneurImgGalerie').css( {width:newSize.width , height:newSize.height});
		},
		arrow:function()
		{
			$(window).load(function()
			{
				$('#idArrowLeft').css({top:($('#idGalerie img').height() - $('#idArrowLeft').height())/2 , left:0});
				$('#idArrowRight').css({top:($('#idGalerie img').height() - $('#idArrowLeft').height())/2 , right:0});
			});
			$('#idArrowLeft').css({top:($('#idGalerie img').height() - $('#idArrowLeft').height())/2 , left:0});
			$('#idArrowRight').css({top:($('#idGalerie img').height() - $('#idArrowLeft').height())/2 , right:0});
			//$j('#idFullScreen').css({marginTop:'20px'});
		},
		fullScreen:function()
		{
			$('#header').stop(true , true);
			$('#footer').stop(true , true);
			$('#idGalerie').stop(true , true);
			if(!isFullScreen)
			{
				isFullScreen = true;
				$('#header').animate({top:'-='+$('#header').height()} , 500 , 'linear');
				$('#footer').animate({marginBottom:'-='+$('#footer').height()} , 500 , 'linear');
				
				
				
				$('#idGalerie').animate({opacity:0} , 500 , 'linear' , function()
				{
					$('.btnSavoirPlusInLine').css({opacity:0});
					$('#header').css({opacity:0});
					$('#footer').css({opacity:0});
					$j('.tableauContenu').css({marginTop:-$('#header').height() , height:$j(window).height() - (parseInt($j('.tableauContenu').css('padding-top')) + parseInt($j('.tableauContenu').css('padding-bottom'))) });
					
					$('#idGalerie').css({width:($j('.tableauContenu').width() -200 ) , height:$j(window).height() - (parseInt($j('.tableauContenu').css('padding-top')) + parseInt($j('.tableauContenu').css('padding-bottom')))  , top:'-30px'});
					$('.article , .slider , .coda-slider-wrapper , .coda-slider').css({width:($j('.tableauContenu').width() ) , height:$j(window).height() - (parseInt($j('.tableauContenu').css('padding-top')) + parseInt($j('.tableauContenu').css('padding-bottom')))+40  , top:'-30px'});
					
					newSize = ResizeImg.to({imgWidth:params.imgWidth , imgHeight:params.imgHeight , widthMax:$('#idGalerie').width()  , heightMax:$('#idGalerie').height()  , proportional:true });
					$('#idGalerie img:first').attr({width:newSize.width , height:newSize.height});
					
					$('#idGalerie').css( {width:newSize.width});
					$('#idConteneurImgGalerie').css( {width:newSize.width , height:newSize.height});
					$('#idGalerie').animate({opacity:1} , 500 , 'linear');
					Positionning.arrow();
				});
				
			}//fin if
			else
			{
				isFullScreen = false;
				
				$('#header').animate({top:0 , opacity:1} , 500 , 'linear');
				$('#footer').animate({marginBottom:'+='+$('#footer').height() , opacity:1} , 500 , 'linear');
				
				$('#idGalerie').animate({opacity:0} , 500 , 'linear' , function()
				{
					$j('.tableauContenu').css({marginTop:0 , height:'100%'});
					$('.btnSavoirPlusInLine').css({opacity:1});
					
					$('#idGalerie').css({width:$(window).width() , height:$(window).height() , top:0});
					Positionning.galerie();
					Positionning.arrow();
					$('#idGalerie').animate({opacity:1} , 500 , 'linear');
					$j('#idConteneurGalerie').css({height:$j('#idGalerie').height() , top:0});
					$j('#coda-slider , .coda-slider-wrapper , .slider ').css({height:$j('#idGalerie').height() + 100 , top:0});
				});
				
			}//fin else
		}
	}
	
	var ResizeImg = 
    {
        to:function(objParams)
        {
            var imgWidth = 0;
            var imgHeight = 0;
            var widthMax = 0;
            var heightMax = 0;
            var proportional = true;
            
            var newWidthImg = 0;
            var newHeightImg = 0;
            var array = [];
            var scale = 0;
            
            (objParams.imgWidth) ? imgWidth = objParams.imgWidth : imgWidth = 800;
            (objParams.imgHeight) ? imgHeight = objParams.imgHeight : imgHeight = 600;
            (objParams.widthMax) ? widthMax = objParams.widthMax : widthMax = 80;
            (objParams.heightMax) ? heightMax = objParams.heightMax : heightMax = 60;
            proportional = objParams.proportional
            
            var scale = imgWidth / imgHeight;
            
            if(!proportional)
            {
                newWidthImg = widthMax;
                newHeightImg = heightMax;
            }//fin if
            else
            {
                if(scale > 1)
                {
                    newWidthImg = widthMax;
                    newHeightImg = widthMax / scale
                    
                    if(newHeightImg > heightMax)
                    {
                        newHeightImg = heightMax;
                        newWidthImg = heightMax * scale;
                    }//fin if
                }//fin if
                else if(scale < 1)
                {
                    newHeightImg = heightMax;
                    newWidthImg = heightMax * scale;
                    
                    if(newWidthImg > widthMax)
                    {
                        newWidthImg = widthMax;
                        newHeightImg = widthMax / scale;
                    }//fin if
                }//fin else if
                else if(scale == 1)
                {
                    newWidthImg = newHeightImg = widthMax;
                }//fin else
                
            }//fin else
            
            array = {width:Math.round(newWidthImg) , height:Math.round(newHeightImg)};
            return array;
        }
    }
	
	$.fn.laa = function(methode)
	{
		// Method calling logic
		if(PublicMethodes[methode]) 
		{
			return PublicMethodes[methode].apply(this, Array.prototype.slice.call(arguments , 1 ));
		}
		else if (typeof methode === 'object' || ! methode ) 
		{
			return PublicMethodes.init.apply(this , arguments);
		}
		else 
		{
			$.error( 'Method ' +  methode + ' does not exist on jQuery.laa.monPlugin' );
		}
	};
})(jQuery);
