/*
 * 
 * 
 * 
 * 
 * 
 * Requires: jQuery v1.3+
*/
;(function($) {


    $.fn.gopics = function(options) {   
        
		var defaults = {
            navigation : true ,
		    mouseclose: true ,
			topclose: true,
            selectEvent: null,
			overlay: '#000000'
        };   

        var opts = $.extend(defaults, options); 	

		//Initialize plugin
		 function init(){
		 
			$('body').append('<div id="gopi"><div id="overplug"></div><div id="gopics" alt="1"><div id="miniplug"></div><div id="bigpreview"><div class="imgTable"></div></div></div><a class="plugprev" href="#">Previous</a><a class="plugnext" href="#">Next</a></div>');
			$('#overplug').css({'background' : opts.overlay })
			createPreviewList();
		};
		
		//Create small preview list
		function createPreviewList(){
			
			var i = 1 ;
			
			$('.gopics img').each(function(){
				var imageSrc = $(this).attr('src');
				$('#miniplug').append('<div class="preview"><img width="90" src="' + imageSrc + '" alt="' + i + '" /></div>');
				i++;
			});
			
			open();	
		}
		
		//Open div overlay
		function open(){
			var widthscreen 	= $(window).width();
			var heightscreen	= $(window).height();
			var startimg			= 'url(' + $('#miniplug:first img').attr('src') +')' ;		
			var img					= $('#miniplug:first img').attr('src') ;		
			$('#gopics').css({ 'width' : (widthscreen - 22) , 'height' : (heightscreen - 22) });
			$('#bigpreview').css({ 'width' : (widthscreen - 122)  });
			$.fn.gopics.calibrate(heightscreen , widthscreen , img);
		};
		
		//Interfaces fluide
        return this.unbind('click').click(init);
    };
		
		//Close div overlay
		$.fn.gopics.close = function () { 
				var $container 	 = $('#gopics');
				var $list			 = $('#miniplug');
				var $overlay	 	 = $('#overplug');
				var $nav	 	 	= $('a.plugnext, a.plugprev');
				var $gopi	 	 	= $('#gopi');
				$list.empty();
				$list.fadeOut();
				$nav.hide();
				$container.fadeOut();
				$overlay.fadeOut();
				$gopi.remove();
		};
		
		//Switch pic in overlay
		$.fn.gopics.switchpic = function (nextImgInt) {
			var $next				= $('.preview img[alt="' + nextImgInt + '"]');			
			var nextImg			= $next.attr('src') ;
			$('#gopics').attr( 'alt', nextImgInt );
			$('.imgTable img').attr('src' , nextImg );
			$.fn.gopics.waitevent;
		};
	
		//Switch pic to next one
		$.fn.gopics.nextpic = function () {
			var control	=	$('#miniplug .preview').length;
			var nextImgInt		= parseInt($('#gopics').attr('alt')) + 1;
			if((control-nextImgInt)<0 ){ nextImgInt = 1; }
			$.fn.gopics.switchpic(nextImgInt);
		};
		
		//Switch pic to previous one
		$.fn.gopics.prevpic = function () { 
			var control	=	$('#miniplug .preview').length;
			var nextImgInt		= parseInt($('#gopics').attr('alt')) - 1;
			if((nextImgInt -1) < 0 ){ nextImgInt = control ; }
			$.fn.gopics.switchpic(nextImgInt);
		};
	
		
		$.fn.gopics.waitevent =  function (){
			$('#bigpreview,a.plugprev , a.plugnext ').hover(function(){
				$('a.plugprev , a.plugnext ').show();
			},function(){
				$('a.plugprev , a.plugnext').hide();
			});
			$('a.plugprev').unbind('click').click($.fn.gopics.prevpic);
			$('a.plugnext').unbind('click').click($.fn.gopics.nextpic);
			$('.preview img').unbind('click').click($.fn.gopics.previewClick);
			$('#gopics #bigpreview').unbind('click').click($.fn.gopics.close);
		}
		
		$.fn.gopics.previewClick = function (){
			var nextImgInt =parseInt($(this).attr('alt'));
			$.fn.gopics.switchpic(nextImgInt);
		}
		
		$.fn.gopics.calibrate = function(height, width , srcImg){
			var widthB 	= width - 122;
			var heightB	= height- 22;
			$('.imgTable').css({ 'width' : widthB , 'height' : heightB });
			$('.imgTable').append('<img src="'+ srcImg +'" alt="gopics"/>');
			$('.imgTable img').css({ 'max-height' : heightB , 'max-width' : widthB });
			$.fn.gopics.show();
		}
		
		$.fn.gopics.show = function () {
			$('#overplug').fadeIn();
			$('#gopics').fadeIn();
			$('#miniplug').fadeIn();
			$('a.prev, a.next').show();
			$.fn.gopics.waitevent();
		}
			
	
})(jQuery); 
