// JavaScript Document// JavaScript Document
(function($) {
	$.fn.slideshow = function(options) {
		var opts = $.extend({}, $.fn.slideshow.defaults, options);
		return this.each(function() {
			var $this 					= $(this);
			$this.opts 					= $.extend({}, opts);
			$this.opts.elementwidth		= $this.find('.slider li').outerWidth();
			$this.opts.position 		= 0;
			$this.opts.speedfactor 		= 15;
			$this.opts.moveregion		= 35;
			$this.opts.speed			= 1;
			$this.opts.title			= '';
			$this.opts.moveinterval		= null;
			$this.opts.totalwidth		= $this.find('.slider li').length * $this.opts.elementwidth;
			$this.opts.totalwidth		-= 5;
			$this.find('.slider li:first a').addClass('activeThumb');
			$this.find('.slider ul').css('width',($this.opts.totalwidth+5)+'px');
			/*
			$this.find('.navigation .slider ul a').each(function(){
				$(this).bind('click',function(event){
					event.preventDefault();
					$.fn.slideshow.show($this, $(this));
				});
			});
			*/
			
			$this.find('.navigation').bind('mousemove',function(event){
				position = event.pageX - $(this).offset().left;
				$vlak = $this.width() * ($this.opts.moveregion / 100);
				if(position <= $vlak){
					$this.opts.speed = ($vlak - position) / $this.opts.speedfactor;
				}else if(position >= $this.width() - $vlak){
					$this.opts.speed = (position - ($this.width() - $vlak)) / -$this.opts.speedfactor;
				}else{
					$this.opts.speed = 0;
				}
			});
			
			$this.find('.navigation').bind('mouseleave',function(event){
				$this.opts.speed = 0;
			});
			
			
			if($this.find('label').html() == ''){
				$this.find('label').css('opacity',0);
			}
			$this.opts.moveinterval = setInterval(function(){$.fn.slideshow.slide($this)},20);
		});
	};
	$.fn.slideshow.defaults = {
	};
	
	$.fn.slideshow.show = function($this, $el){
		$this.find('.activeThumb').removeClass('activeThumb');
		$el.addClass('activeThumb');
		$this.opts.title = $el.find('img').attr('title');
		$oldpic = $this.find('.bigpic img');
		$newpic = $('<img src="" />')
		$this.find('label').html($el.attr('title'));
		$newpic.load(function(){	
			$this.find('.bigpic').animate({height:$newpic.height()+'px'},100);
			$oldpic.css('position','absolute').fadeTo(400,0,function(){ $(this).remove(); })
			$newpic.fadeTo(400,1)
			$newpic.css('marginLeft',((610-$newpic.width())/ 2) + 'px');
			$this.find('label').stop().fadeTo(400, 0, function(){
				if($this.opts.title != ''){
					$(this).html($this.opts.title);	
					$this.find('label').fadeTo(400, 1);
				}
			})
		});
		$this.find('.bigpic').prepend($newpic);
		$newpic.hide();
		$newpic.attr('src',$el.attr('href'));
	}
	
	$.fn.slideshow.slide = function($this, $el){
		newpos = parseInt($this.find('.slider ul').css('left')) + $this.opts.speed;
		if(newpos > 0){
			newpos = 0;	
		}
		if(newpos < -$this.opts.totalwidth + $this.find('.navigation .slider').width()){
			newpos = -$this.opts.totalwidth + $this.find('.navigation .slider').width();	
		}
		$this.find('.slider ul').css('left',newpos+'px')
	}
	
})(jQuery);
