/**
 * wSlide 0.1 - 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){
        h = jQuery.extend({
        	uniqid: '',
            width: 150,
			height: 150,
			pos: 1,
			col: 1,
			cnt: 0,
			effect: 'swing',
			horiz: false,
			duration: 1500,
			span: 5000,
		    cache_timers : [],
		    setInterval  : function(func, ms) { this.cache_timers.push(setInterval(func, ms)) },
		    clearInterval: function() { jQuery.each(this.cache_timers, function(i,o){ clearInterval(o) }) }
        }, h);
        function swidx(a, o, t, eft){
			o.parents('td').removeClass("wactive");
			t.parents('td').addClass("wactive");
			var tri = t.attr('href');
			tri=tri.split('#');
			tri='#'+tri[1];
			p = tri.split('-');
			h.pos = Number(p[1]);
			var decal = $(tri).attr('rel');
			decal = decal.split(':');
			var decal2 = decal[1];
			decal2 = -decal2;
			decal = decal[0];
			decal = -decal;
			a.find('ul').animate({ top: decal2+'px',left: decal+'px' }, h.duration, eft);
			return;
		}
        function gogogo(g){
            g.each(function(i){
                var a = $(this);
				h.uniqid = a.attr('id');
				if(h.uniqid == undefined){
					h.uniqid = 'wslide'+i;
				}
				$(this).wrap('<div class="wslide-wrap" id="'+h.uniqid+'-wrap"></div>');
				a = $('#'+h.uniqid+'-wrap');
				var b = a.find('ul li');
				if(jQuery.easing.easeInQuad == undefined && (h.effect!='swing' || h.effect!='normal')){
					h.effect = 'swing';
				}
				var typex = h.width;
				var typey = h.height;
				function resultante(prop){
					var tempcalc = prop;
					tempcalc = tempcalc.split('px');
					tempcalc = tempcalc[0];
					return Number(tempcalc);
				}
				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')));
				var col = h.col;
				if(h.horiz){
					col =  Number(b.length+1);
				}
				var ligne = Math.ceil(Number(b.length)/col);
				a.css('overflow','hidden').css('position','relative').css('text-align','left').css('height',typey+'px').css('width',typex+'px').css('margin','0').css('padding','0');
				a.find('ul').css('position','absolute').css('margin','0').css('padding','0').css('width',Number((col+0)*typex)+'px').css('height',Number(ligne*typey)+'px');
				b.css('display','block').css('overflow','hidden').css('float','left').css('height',litypey+'px').css('width',litypex+'px');
				b.each(function (i) {
					h.cnt++;
					var offset = a.offset();
					var thisoffset = $(this).offset();
					$(this).attr('id',h.uniqid+'-'+Number(i+1)).attr('rel', Number(thisoffset.left-offset.left)+':'+Number(thisoffset.top-offset.top));
				});
				var start = '#'+h.uniqid+'-';
				var stoccurent = "";
				$('a[href*="'+start+'"]').click( function() {
		        	h.clearInterval();
					swidx(a, $('a[href$="'+start+h.pos+'"]') ,$(this), h.effect);
					h.setInterval( function(){
						var i1 = h.pos;
						var i2 = (i1+1>h.cnt) ? 1 : i1+1;
						swidx($('#'+h.uniqid+'-wrap'), $('a[href$="#'+h.uniqid+'-'+i1+'"]') ,$('a[href*="#'+h.uniqid+'-'+i2+'"]'), h.effect);
						return false;
					} , h.span);
					return false;
				} );
				h.pos = Math.floor(Math.random() * h.cnt) + 1;
				$('a[href$="'+start+h.pos+'"]').parents('td').addClass("wactive");
				var tri = $('a[href*="'+start+'"]:eq('+Number(h.pos-1)+')').attr('href');
				tri=tri.split('#');
				tri='#'+tri[1];
				stoccurent = tri;
				var decal = $(tri).attr('rel');
				decal = decal.split(':');
				var decal2 = decal[1];
				decal2 = -decal2;
				decal = decal[0];
				decal = -decal;
				a.find('ul').css('top',decal2+'px').css('left',decal+'px');

            })
        }
        gogogo(this);
		h.setInterval( function(){
			var i1 = h.pos;
			var i2 = (i1+1>h.cnt) ? 1 : i1+1;
			swidx($('#'+h.uniqid+'-wrap'), $('a[href$="#'+h.uniqid+'-'+i1+'"]') ,$('a[href*="#'+h.uniqid+'-'+i2+'"]'), h.effect);
			return false;
		} , h.span);
		return this;
    }
})(jQuery);
