/*
**
**  mGal 1.0 27/10/09
**  Author: Arnaud AUBRY
**  
*/
$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};

(function($){

var $$;

$$ = $.fn.mGal = function($options) 
{

	// set default options
	var $defaults = {
		insert      : '.mGal_container',
		playIcon	: 'img/mGal/play.png',
		pauseIcon	: 'img/mGal/pause.png',
		autoPlay	: false,
		title		: false,
		comments	: false,
		autoPlayNC  : true,/*Autoplay without comment*/
		speed		: 2,
		defaultcolor: "#00FFFF",
		onComplete	: function() {},
		duration	: 3000 //how long do we look at each image?
	};
	
	$.mGal.curIndex=0;
	$.mGal.timerIMG=0;
	$.mGal.srcTab= new Array();
	$.mGal.titleTab= new Array();
	$.mGal.commentTab= new Array();
	
	$(document).keydown(function(e){
		switch(e.keyCode){
			case 37:
				$$.stopSlideShow();
				$$.previousSlideShow();
				break;
			case 39:
				$$.stopSlideShow();
				$$.playSlideShow();
				break;
		};
	});
		
	// extend the options (erase defaults with options)
	var $opts = $.extend($defaults, $options);
	
	// bring the options to the mGal object
	for (var i in $opts) {
		if (i) {
			$.mGal[i]  = $opts[i];
		}
	}
	
	// if no insert selector, create a new division and insert it before the ul
	var _insert = ( $($opts.insert).is($opts.insert) ) ? $($opts.insert) : jQuery(document.createElement('div')).insertBefore(this);
		
	var _divLeft = $(document.createElement('div')).addClass('mGal_left');
		var _divTopMove = $(document.createElement('div')).addClass('mGal_topMove disabled');
		var _divBottomMove = $(document.createElement('div')).addClass('mGal_bottomMove');
		var _divImg = $(document.createElement('div')).addClass('mGal_image');
			var _divImgComments = $(document.createElement('div')).addClass('mGal_imageComments');
			var _divImgNext = $(document.createElement('div')).addClass('mGal_next');
			var _divImgPrevious = $(document.createElement('div')).addClass('mGal_previous');
			var _Img = $(document.createElement('img'));
			
		var _divControlerButton = $(document.createElement('div')).addClass('mGal_controlerButton');
		
	var _divRight = $(document.createElement('div')).addClass('mGal_right');
		var _divThumbsContainer = $(document.createElement('div')).addClass('mGal_thumbsContainer');
		
	// create the disposition
	_insert.addClass('mGal_container');
	_insert.append(_divLeft).append(_divRight);
	
	_divImg.append(_divImgComments);
	_divImg.append(_divImgNext);
	_divImg.append(_divImgPrevious);
	_divLeft.append(_divImg);
	_divLeft.append(_divTopMove);
	_divLeft.append(_divBottomMove);
	_divImg.append(_Img);
		
	_divLeft.append(_divControlerButton);

		
	//_divRight.append(_divTopMove);
	//_divRight.append(_divBottomMove);
	_divRight.append(_divThumbsContainer);
		
	var _playImg = new Image();
	_playImg.src = $.mGal.playIcon;
	_playImg.id="mGal_Play";
	
	_divControlerButton.append(_playImg);
	_divControlerButton.click(function(){
		if($.mGal.timerIMG == 0)
		{	
			$$.playSlideShow(true);
			$.mGal.timerIMG=setInterval(function(){$$.playSlideShow(true)},$.mGal.duration);
			$("#mGal_Play").attr("src",$.mGal.pauseIcon);
		}
		else
		{
			$$.stopSlideShow();
			if($.mGal.autoPlayNC)
				if($.mGal.title || $.mGal.comments)
					$$.showComments();
			
		}
	});
	
	_divImgNext.attr("title","Photo suivante");
	_divImgNext.click(function(){
		$$.stopSlideShow();
		$$.playSlideShow();
	});
	
	_divImgPrevious.attr("title","Photo précédente");
	_divImgPrevious.click(function(){
		$$.stopSlideShow();
		$$.previousSlideShow();
	});
	
	
	if($.mGal.autoPlay)
	{
		$("#mGal_Play").attr("src",$.mGal.pauseIcon);
		$.mGal.timerIMG=setInterval(function(){$$.playSlideShow(true)},$.mGal.duration);
	}
	
	var timer;
		
	$(".mGal_topMove").mousedown(function(){timer=setInterval(function(){$$.moveThumbsContainer("Top")},1); }).mouseup(function(){clearInterval(timer);});
	$(".mGal_bottomMove").mousedown(function(){timer=setInterval(function(){$$.moveThumbsContainer("Bottom")},1); }).mouseup(function(){clearInterval(timer);});
	
	var index=0;
	
	 this.each(function(){
		// add the mGal class
		$(this).addClass('mGal');
		
		//Append UL to the container 
		_divThumbsContainer.append(this);
		
		// loop through list
		$(this).children('li').each(function(i) 
		{
			// bring the scope
			var _container = $(this);
							
			// reference the original image as a variable and hide it
			var _img = $(this).children('img').addClass("thumb");
			
					
			_img.wrap('<div class="thumb-img"><div class="thumb-inner">' + '</div></div>');
			_img.css('position','absolute');

			var __resLeft = parseInt(_img.width())/2 - parseInt($(this).width())/2;
			var __resTop =  parseInt(_img.height())/2 - parseInt($(this).height())/2;
			
			if(__resLeft > 0 && __resTop > 0)
			{
				_img.css("left", '-' + parseInt(__resLeft) + 'px' );
				_img.css("top", '-' + parseInt(__resTop) + 'px' );
			}
		 
			// extract the original source
			var _srcBig = _img.attr('Rel');
			var _title = _img.attr('title');
			var _comments= _img.attr('alt');
	
			_img.fadeTo("fast",0.3);
			
			$.mGal.srcTab[index]=_srcBig;
			$.mGal.titleTab[index]=_title;
			$.mGal.commentTab[index]=_comments;
			
			if(index==0)
			{
				if($.mGal.autoPlayNC)
					$.mGal.activate(_srcBig);
				else
					$.mGal.activate(_srcBig,_title,_comments);
			}
			index++;
							
			$(this).click(function() 
			{
				//disable the timer
				$$.stopSlideShow();
				//Set the big image
				$.mGal.activate(_srcBig,_title,_comments);
			});
			
			//hover Style
			_img.hover(function(){$(this).stop().fadeTo("fast",1)},function(){$(this).stop().fadeTo("fast",0.3);});			
		});
	});
	
	$.mGal.onComplete();
	
	return this;
};

$$.moveThumbsContainer = function(direction)
{
	var reg=/^(-?\d+)(px)?$/i ;
	var __top = $(".mGal_thumbsContainer").css("top");
	var __topInt = __top.replace(reg,"$1");
	
	var sign = "+=";
	
	if(direction == "Bottom")
	{
		$(".mGal_topMove").removeClass("disabled");
		sign="-=";
	}
	else
	{
		$(".mGal_bottomMove").removeClass("disabled");
	}
		
	//limit Top
	if(direction == "Top" && __topInt >= 0)
	{
		$(".mGal_topMove").addClass("disabled");
		return;
	}
	
	//limit bottom
	if( direction == "Bottom" && ( Math.abs(__topInt) >= ( $(".mGal_thumbsContainer").height() + $(".mGal_topMove").height() + $(".mGal_bottomMove").height() - $(".mGal_right").height() ) ) )
	{
		$(".mGal_bottomMove").addClass("disabled");
		return;
	}
	
	$(".mGal_thumbsContainer").animate({ top:sign+$.mGal.speed},0);
};

$$.stopSlideShow = function()
{
	clearInterval($.mGal.timerIMG);
	$.mGal.timerIMG=0;
	$("#mGal_Play").attr("src",$.mGal.playIcon);
};

$$.previousSlideShow = function()
{
	$.mGal.curIndex--;
	if($.mGal.curIndex<0)	
		$.mGal.curIndex=$.mGal.srcTab.length;
	var localIndex = $.mGal.curIndex%$.mGal.srcTab.length;
	$.mGal.activate($.mGal.srcTab[localIndex],$.mGal.titleTab[localIndex],$.mGal.commentTab[localIndex]);	
};

$$.playSlideShow = function(autoplay) 
{
	$.mGal.curIndex++;
	var localIndex = $.mGal.curIndex%$.mGal.srcTab.length;
	
	if(autoplay && $.mGal.autoPlayNC)
		$.mGal.activate($.mGal.srcTab[localIndex]);	
	else
		$.mGal.activate($.mGal.srcTab[localIndex],$.mGal.titleTab[localIndex],$.mGal.commentTab[localIndex]);	

}

$$.showComments = function()
{
	var localIndex = $.mGal.curIndex%$.mGal.srcTab.length;
	
	if($.mGal.title)
		$(".mGal_imageComments").html("<h3>"+$.mGal.titleTab[localIndex]+"</h3>");
	if($.mGal.comments)
		$(".mGal_imageComments").append($.mGal.commentTab[localIndex]);
	
	if(($.mGal.comments && $.mGal.commentTab[localIndex]) || ($.mGal.titleTab[localIndex] && $.mGal.title))
		$(".mGal_imageComments").pause(500).slideDown("slow");
}

$$.setImg = function(_src,_title,_comments) 
{
	var theImg = $(".mGal_image").children("img");
	
	if(theImg.attr("src")!=_src) //avoid to do it again if the same picture
	{
		$(".mGal_left").hover(function(){$(".mGal_controlerButton").show()},function(){$(".mGal_controlerButton").hide()});
		$(".mGal_imageTitle").hide().empty();
		$(".mGal_imageComments").hide().empty();
		$(".current").removeClass("current").find("img").fadeTo("fast",0.3).mouseout(function(){$(this).stop().fadeTo("fast",0.3);});
		$("[rel="+_src+"]").parent().parent().addClass("current").find("img").fadeTo("fast",1).mouseout(function(){$(this).stop();});
		var reg=/^(-?\d+)(px)?$/i ;
		
		var __top = $(".mGal_thumbsContainer").css("top").replace(reg,"$1");
		
		if( ($(".current").offset().top - $(".mGal_right").offset().top) +$(".current").outerHeight() > 410)
		{	
			var max = Math.max( parseInt(-($(".mGal_thumbsContainer").height() + $(".mGal_topMove").height() + $(".mGal_bottomMove").height() - $(".mGal_right").height())),parseInt(__top-$(".current").outerHeight())+(410-parseInt($(".current").offset().top - $(".mGal_right").offset().top)));
			$(".mGal_thumbsContainer").stop(true,true).animate({ top:max+"px"},200);
		}
		else
			if( ($(".current").offset().top - $(".mGal_right").offset().top)< 35 )
			{
				var min = Math.min( 0,parseInt(__top)-parseInt(($(".current").offset().top- $(".mGal_right").offset().top))+35);
				$(".mGal_thumbsContainer").stop(true,true).animate({ top:min+"px"},200);
			}
		
		theImg.stop().fadeTo("normal",0,function()
		{
			
			if(_title && $.mGal.title)
			{	
				$(".mGal_imageComments").html("<h3>"+_title+"</h3>");
				
			}
			if(_comments && $.mGal.comments)
			{
				$(".mGal_imageComments").append(_comments);
			}
			
			if(($.mGal.comments && _comments) || (_title && $.mGal.title))
				$(".mGal_imageComments").pause(500).slideDown("slow");
			
			var _img = new Image();
			
			_img.onload = function() 
			{
				var _width = _img.width;
				var _height = _img.height;
				
				$(".mGal_image").width(_width+2);
				$(".mGal_image").height(_height+2);
				
				
				var reg=/^(-?\d+)(px)?$/i ;
				var __paddingLeftInt = $(".mGal_imageComments").css("padding-left").replace(reg,"$1");
				var __paddingRightInt = $(".mGal_imageComments").css("padding-right").replace(reg,"$1");
				$(".mGal_imageComments").width(_width-__paddingLeftInt-__paddingRightInt);
				
					
				$(".mGal_image").css('margin-top',(($(".mGal_container").height() -_height)/2));
				theImg.attr("src",_src).fadeTo("normal",1);	
				_img.onload = function(){};
			};
			_img.src = _src;
		});
	}
};



$.extend({mGal : 
{
	activate : function(_src,_title,_comments) 
	{ 
		$$.setImg(_src,_title,_comments);
	}
}
});

})(jQuery);


