(function($) {
 
	$.fn.wslider = function(settings) {
		var config = {
			'slideSmallWidth': 50,
			'delayTime':5000
		};
		
		var slideTimer=null;

		if (settings) $.extend(config, settings);

		setupDimensions=function(obj) {
			//get main div dimensions
			var sliderwidth=$(obj).width();
			var sliderheight=$(obj).height();
			
			var slides=$(obj).find('.wslider-slide');
			
			//calculate medium width
			var slidemediummargin = Math.floor(sliderwidth / slides.length);
			var slidemediummarginmod = sliderwidth % slides.length;
			var smmpos = 0;
			
			//calculate big width
			var slidebigwidth = sliderwidth - (config.slideSmallWidth * (slides.length-1));
			
			
			slides.each(function(i){
				$(this).width(slidebigwidth);

				var wsliderp=parseInt($(this).find('.wslider-panel').css('padding-left'));
				
				$.data(this, 'marginmedium', smmpos);
				$.data(this, 'marginsmall', config.slideSmallWidth*i);
				$.data(this, 'panelfullwidth', slidebigwidth-wsliderp);
				$.data(this, 'panelmediumwidth', slidemediummargin-wsliderp);
				$.data(this, 'height', sliderheight);
				
				var smw=slidemediummargin;
				if (slidemediummarginmod>0) {smw++;slidemediummarginmod--;}
				
				smmpos += smw;
				
			});
			
		}
		
		initializePositions = function(obj) {
			var slides=$(obj).find('.wslider-slide');
			
			var initsleep=1;
			
			slides.each(function(i){
				var smm = $.data(this, 'marginmedium');
				var smh = $.data(this, 'height');
				var pmw = $.data(this, 'panelmediumwidth');
				
				var slobj = $(this).css({height:smh+'px', display:'block','margin-left':smm+'px'}).find('.wslider-slidelink')
					.css({'display':'none', 'background-image':'url('+$(this).find('.wslider-smallimg').attr('src')+')'});
					
				//fade timer
				setTimeout(function(){slobj.fadeIn(500);},initsleep);
				setTimeout(function(){slobj.find('.wslider-panel').addClass('medium').width(pmw).fadeIn(500);}, 300);
				initsleep+=300;
			});
		}
		
		openSlide = function(obj) {
			//stop all slide animations
			$(obj).find('.wslider-slidelink').stop(true,true);
			$(obj).parents('.wslider').find('.wslider-slide').stop(true)
			//remove medium panels
			.find('.wslider-panel').css({opacity:0, bottom:'-70px'});
			
			//animate this to big width
			$(obj).animate({marginLeft:$.data(obj, 'marginsmall')},{queue:false, easing:'easeOutCirc', duration:800}).addClass('active')
				//init link
				.find('.wslider-slidelink').animate({opacity:0}, 100,'linear', function(){$(this).css({'background-image':'url('+$(obj).find('.wslider-bigimg').attr('src')+')'}).animate({opacity:1}, 200);}).end()
				//remove medium panels
				.find('.wslider-panel').removeClass('medium').width($.data(obj, 'panelfullwidth')).animate({opacity:1, bottom:0},{duration:1000, queue:false}).end()
				.prevAll().each(function(){
					var defmargin = $.data(this, 'marginsmall');
					if ((defmargin+'px') == $(this).css('margin-left')) return;
					$(this).animate({marginLeft:defmargin},{queue:false, easing:'easeOutCirc', duration:800})
				}).end()
				.nextAll().each(function(){$(this).animate({marginLeft:$.data(this, 'marginsmall')+$(obj).width()-config.slideSmallWidth},{queue:false, easing:'easeOutCirc', duration:800})});
		}
		
		slideGallery = function(obj) {
			$(obj).removeClass('active').find('.wslider-slidelink').animate({opacity:0}, 700, 'linear', function(){$(this).css({'background-image':'url('+$(obj).find('.wslider-smallimg').attr('src')+')'}).animate({opacity:1}, 300);}).end()
			.parent().find('.wslider-slide').each(function(){
				$(this).stop().animate({marginLeft:$.data(this, 'marginmedium')},{duration:1000, queue:false, easing:'easeOutCirc', complete:function(){
					$(this).find('.wslider-panel').css('opacity', 1);
				}});
			}).find('.wslider-panel').stop().addClass('medium').width($.data(obj, 'panelmediumwidth')).css({bottom:'0px'});
		}
		
		initEvents=function(obj) {
			$(obj).find('.wslider-slide').hover(function(event, tm) {
				if (tm!=true) { clearInterval(slideTimer); slideTimer=null; } else {
					if (slideTimer==null) slideTimer=setInterval(function(){timerFunc(obj)}, config.delayTime);
				}
				
				if($(this).hasClass('active')) return;
				openSlide(this);
			},function(event, tm){
				if (tm!=true && slideTimer==null) slideTimer=setInterval(function(){timerFunc(obj)}, config.delayTime);
			
				slideGallery(this);
			});
			
			slideTimer=setInterval(function(){timerFunc(obj)}, config.delayTime);
		}
		
		timerFunc=function(obj) {
			//find active
			var active=$(obj).find('.wslider-slide').filter('.active');
			if (active.length>0) active.trigger('mouseout', [true]);
			active=active.next();
			//find next
			if (active.length==0) active=$(obj).children().eq(0);
			
			//fire event
			active.trigger('mouseover', [true]);
		}
		

		this.each(function() {
			setupDimensions(this);
			initializePositions(this);
			initEvents(this);
		});

		return this;

	};

})(jQuery);


