/**
 * wSlide 2.0 béta - http://www.webinventif.fr/wslide-plugin/
 * 
 * Rendez vos sites glissant !
 *
 * Copyright (c) 2008 Julien Chauvin (webinventif.fr)
 * Licensed under the Creative Commons License:
 * http://creativecommons.org/licenses/by/3.0/
 *
 * Date: 2008-01-27
 */
(function($){
    $.fn.wslide = function(h){
	
		//_________( Options )_________//
        h = jQuery.extend({
            width: 150,
			height: 150,
			pos: 1,//defaut start position
			col: 1,//Number of columns
			effect: 'swing',//Move effects
			fade: false,//Fading ?
			horiz: false,//Horizontal or vertical sliding ?
			automenu: true,//Generate or attribute  "1-2-3-..." menu ?
			navmenu: 'auto',//Prev/next nav: true, false or auto
			duration: 1500,//Duration of transitions
			forcecss: 'all',//all=max style | good=less style | min=no style !
			autoplay: true,//Slideshow ?
			pause: 2000,//Duration of each slide (without duration of transitions)
			autopause: true,//Pause the slideshow on mouseover
			showbloc: 1,//Number of "blocs" to display (it's multyply the height or width)
			caption: false,//Display a caption if "rel" attribute is available on each children
			prevText: 'Prev',//text previous
			nextText: 'Next',//text next
			captionOpacity: 0.8, // Universal caption opacity
			captionPos: 'bottom', // Top or bottom
        }, h);
		

		//next/prev universel !
		//check nocss
		
		//_________( Slideshow function )_________//
		function goNext(ele){
			//ele.trigger('click', [true]);
			//timeOut = setTimeout(goNext,h.pause);
			//timeOut = setTimeout("goNext("+ele+")",h.pause);
			var x = setInterval(function() { ele.trigger('click'); }, Number(h.pause+h.duration));    
			return x;
			//console.log('goNext'+' - timeOut id: '+timeOut);
		}
		//_________( resultante function )_________//
		function resultante(prop){
			var tempcalc = prop;
			tempcalc = tempcalc.split('px');
			tempcalc = tempcalc[0];
			return Number(tempcalc);
		}
		//_________( gotoele function )_________//
		function gotoele(pos,ele,contenair){
			jQuery.data(contenair, "currentpos", pos);
			var decal = '-'+jQuery.data(contenair, "stock"+pos).wrell;
			var decal2 = '-'+jQuery.data(contenair, "stock"+pos).wrelh;
			
			
			
			if(h.caption) $('#'+ele+'-caption').fadeOut(h.duration/2);
		
			
			
			if(h.fade){
				$('#'+ele+'').animate({ opacity: 0 }, h.duration/2, h.effect, function(){
						$(this).css('top',decal2+'px').css('left',decal+'px'); 
						$(this).animate({ opacity: 1 }, h.duration/2, h.effect)
						 if(h.caption){ if($('#'+ele+' :nth-child('+pos+')').attr(h.caption))	{$('#'+ele+'-caption').html($('#'+ele+' :nth-child('+pos+')').attr(h.caption)).fadeIn(h.duration/2);}}
					}  );
			}else{
				$('#'+ele+'').animate({ top: decal2+'px',left: decal+'px' }, h.duration, h.effect , function() { if(h.caption){ if($('#'+ele+' :nth-child('+pos+')').attr(h.caption))	{$('#'+ele+'-caption').html($('#'+ele+' :nth-child('+pos+')').attr(h.caption)).fadeIn('fast');}} });
			}
		}
		
		//_________( Main function )_________//
        function gogogo(g){
		
			//_________( Loop for several instances of plugin effect )_________//
            g.each(function(i){
			
				//_________( Many variables )_________//
                var a = $(this); //Target element
				var uniqid = a.attr('id'); //Id of the element
				if(uniqid == undefined){uniqid = 'wslide'+i;} //If no Id, define a unique value
				$(this).wrap('<div class="wslide-wrap" id="'+uniqid+'-wrap"></div>'); //Wrap the element in a div
				a = $('#'+uniqid+'-wrap'); //Stock wrap element
				var b = $('#'+uniqid+'').children(); //Stock chidrens of the element
				var c = $('#'+uniqid+''); //Stock the element
				var idautomenu = 1; //Boolean for check display menu
				var incrtemp = 0; //for i
				var manip = ''; //for html code of the menu
				var start = '#'+uniqid+'-'; //prefix of menu links
				var col = h.col; //columns
				var ligne = Math.ceil(Number(b.length)/col);//Number of lines
				
				b.addClass('wslide-childs');
				c.addClass('wslide-ele');
				
				if(h.showbloc >1) col = 1;
				if(col>1) h.horiz = false;
				if(!h.autoplay) h.autopause = false;
				
				
				if(h.caption == true) h.caption = 'rel';
				a.append('<div class="wslide-caption" id="'+uniqid+'-caption"">&nbsp;</div>');
				
				a.append('<a class="wslide-nav wslide-nav-next" href="#">'+h.nextText+'</a> <a class="wslide-nav wslide-nav-prev" href="#">'+h.prevText+'</a>');
				
				//Effect (if jQuery asing plugin available)
               	var effets = h.effect; 
				if(jQuery.easing.easeInQuad == undefined && (effets!='swing' || effets!='normal')){effets = 'swing';}
				

				//_________( Calcul positions and sizes )_________//
				var typex = h.width;
				var typey = h.height;
				var litypex = typex-(resultante(b.css('padding-left'))+resultante(b.css('padding-right')));
				var litypey = typey-(resultante(b.css('padding-top'))+resultante(b.css('padding-bottom')));
				if(h.showbloc >=2){
					if(h.horiz){ typex = typex*h.showbloc; }else{typey = typey*h.showbloc;}
				}
				if(h.horiz){col =  Number(b.length+1);}
				
		
				
				//_________( Apply defauts CSS style to: wrap element, element, child element )_________//
				
				//min
				if(h.forcecss == 'min' || h.forcecss == 'good' || h.forcecss == 'all'){
					a.css({'height' : typey+'px', 'width' : typex+'px'});
					$('#'+uniqid+'').css({'width' : Number((col+0)*typex)+'px', 'height' : Number(ligne*typey)+'px'});
					b.css({'height' : litypey+'px', 'width' : litypex+'px'});
					
				}
				
				//good
				if(h.forcecss == 'good' || h.forcecss == 'all'){
					a.css({'overflow' : 'hidden', 'position' : 'relative', 'margin':'0', 'padding' : '0'});
					$('#'+uniqid+'').css({'position' : 'absolute', 'margin' : '0', 'padding' : '0'});
					b.css({'display' : 'block', 'position' : 'relative', 'overflow' : 'auto', 'float' : 'left'});
					$('#'+uniqid+'-caption').css({'position' : 'absolute', 'display' : 'none', 'opacity' : h.captionOpacity}).hide();
					a.find('a.wslide-nav').css({
						'position' : 'absolute',
						'top' : Number(typey-a.find('a.wslide-nav').outerHeight())/2+'px'
					}).hide();
				}
				
				//all
				if(h.forcecss == 'all'){
					a.css({'text-align' : 'left'});
					$('#'+uniqid+'').css({});
					b.css({'overflow' : 'hidden'});
					$('#'+uniqid+'-caption').css({'margin' : '0', 'padding' : '5px 10px', 'width' : '100%', 'bottom' : '0', 'background' : 'black', 'color' : 'white'}).hide();
					if(h.captionPos == 'top') $('#'+uniqid+'-caption').css({'top' : '0', 'bottom': 'auto'});
					
					a.find('a.wslide-nav').css({
						'color' : 'white',
						'background' : 'white',
						'color': 'black',
						'opacity': 0.85,
						'padding' : '5px 10px',
						'margin' : '0'
					}).hide();
					a.find('a.wslide-nav-next').css({'right' : '0'});
				}
				
				//tout-fonctionnel-minimum-nothing
				
				
				
				//_________( Loop: stock coordonates and prepare menu )_________//
				b.each(function (i) {
					var offset = a.offset();
					var thisoffset = $(this).offset();
					jQuery.data(c, "stock"+(i+1), { wrell: Number(thisoffset.left-offset.left), wrelh: Number(thisoffset.top-offset.top) });
					if(h.showbloc >=2){
						if(Number(i+1)%h.showbloc == 1) { 
							manip += ' <a href="#'+uniqid+'-'+Number(i+1)+'">'+Number(incrtemp+1)+'</a>'; 
							incrtemp++;
						}
					}else{
						manip += ' <a href="#'+uniqid+'-'+Number(i+1)+'">'+Number(i+1)+'</a>';
					}
					
				});
				 
				 
				 
				//_________( Menu generation )_________//
				if(typeof h.automenu == 'boolean'){
					if(h.automenu){
						a.after('<div class="wslide-menu wslide-menu-auto" id="'+uniqid+'-menu">'+manip+'</div>');
					}else{if(!$('a[href*="'+start+'"]').length){idautomenu=0 }}
				}else if (typeof h.automenu == 'string'){
					if($('#'+h.automenu).length){
						$('#'+h.automenu).html(manip);
					}else{
						a.after('<div class="wslide-menu" id="#'+h.automenu+'">'+manip+'</div>');
					}
				}else{if(!$('a[href*="'+start+'"]').length){idautomenu=0 }}

				
				
				//_________( Menu actions )_________//
				if(idautomenu){
					$('a[href*="'+start+'"]').click(function () { 
							$('a[href*="'+start+''+jQuery.data(c, 'currentpos')+'"]').removeClass("wactive");
							$(this).addClass("wactive");
							var tri = $(this).attr('href');
							tri=tri.split('#');
							tri='#'+tri[1];
							incrtemp=tri.substring((tri.lastIndexOf("-")+1));
							gotoele(incrtemp,uniqid,c);
							return false;
					});
					if(h.autopause){
						$('a[href*="'+start+'"]').hover(
						  function () {clearInterval(thisinterval);}, 
						  function () {thisinterval = goNext(a.find('a.wslide-nav-next'));}
						);
					}
				}
				

				//_________( Nav Next/Prev )_________//
				

				

				a.find('a.wslide-nav-next').click(function () { 
						var navmax = b.length;
						var navcurrent = jQuery.data(c, "currentpos");
						if(idautomenu) $('a[href*="'+start+''+navcurrent+'"]').removeClass("wactive");
						navcurrent = Number(navcurrent*1+h.showbloc);
						if(navcurrent > navmax) navcurrent = 1;
						if(idautomenu) $('a[href*="'+start+''+navcurrent+'"]').addClass("wactive");
						gotoele(navcurrent,uniqid,c);
						return false;
				});
				
				a.find('a.wslide-nav-prev').click(function () { 
						var navmax = b.length;
						var navcurrent = jQuery.data(c, "currentpos");
						if(idautomenu) $('a[href*="'+start+''+navcurrent+'"]').removeClass("wactive");
						navcurrent = Number(navcurrent*1-h.showbloc);
						if(navcurrent < 1){
							navcurrent = navmax;
							if(navmax % h.showbloc != 0) navcurrent = (Number(navmax)-Number(navmax % h.showbloc)+1);
						}
						if(idautomenu) $('a[href*="'+start+''+navcurrent+'"]').addClass("wactive");
						gotoele(navcurrent,uniqid,c);
						return false;
				});
				

				if(h.navmenu == 'auto') {
					a.hover(
					  function () { a.find('a.wslide-nav').fadeIn();  }, 
					  function () { a.find('a.wslide-nav').fadeOut();  }
					);
				}else if(h.navmenu == true){
					a.find('a.wslide-nav').show();
				}

				
				
				//_________( At start )_________//
				if(h.pos <= 1){h.pos = 1;}
				if(idautomenu){$('a[href$="'+start+h.pos+'"]').addClass("wactive");}
				gotoele(h.pos,uniqid,c);
				
				
				//_________( Slideshow )_________//
				if(h.autoplay){
					var thisinterval = goNext(a.find('a.wslide-nav-next'));
					if(h.autopause){
						a.hover(
						  function () {clearInterval(thisinterval);}, 
						  function () {thisinterval = goNext(a.find('a.wslide-nav-next'));}
						);
					}

					
				}				
            })
        }
		
		//_________( Execute main function )_________//
        gogogo(this);
		return this;
    }
})(jQuery);
