// JavaScript Document// JavaScript Document
(function($) {
	$.fn.portfolio = function(options) {
		var opts = $.extend({}, $.fn.portfolio.defaults, options);
		return this.each(function() {
			var $this 					= $(this);
			$this.opts 					= $.extend({}, opts);
			$this.opts.elementwidth		= $this.find('.slider li').outerWidth();
			$this.opts.currentpos 		= 0;
			$this.opts.speed 			= 0;
			$this.opts.space 			= 40;
			$this.opts.speedfactor 		= 11;
			$this.opts.width			= $this.width();
			$this.opts.totalwidth 		= -10;
			
			$this.find('.thumbs li').each(function(){
				$this.opts.totalwidth += $(this).width() + 10;
			});
			$this.find('.thumbs ul').css('width',$this.opts.totalwidth+'px');
			
			
			$this.find('.thumbs a').bind('click',function(e){
				e.preventDefault();
				$.fn.portfolio.loadzoom($this, $(this).attr('href'))
			});
			
			$this.find('.thumbs').bind('mousemove',function(e){
				mousex 	= e.pageX - $(this).offset().left;
				perc 	= Math.round(100*mousex/$this.opts.width);
				spacewidth = $this.opts.space/100*$this.opts.width;
				if(perc >= 0 && perc <= $this.opts.space){
					$this.opts.speed = (spacewidth - mousex) / spacewidth * $this.opts.speedfactor;
				}else if(perc >= (100-$this.opts.space) && perc <= 100){
					$this.opts.speed = (mousex-($this.opts.width-spacewidth)) / spacewidth * -$this.opts.speedfactor;
				}else{
					$this.opts.speed = 0;
				}
			});
			
			setInterval(function(){	$.fn.portfolio.mover($this) }, 30);
		});
		
	};
	
	$.fn.portfolio.defaults = {
	};
	
	$.fn.portfolio.mover = function($this){
		if($this.opts.totalwidth > $this.opts.width){
			$this.opts.currentpos = $this.opts.currentpos + $this.opts.speed;
			if($this.opts.currentpos > 0){
				$this.opts.currentpos = 0;
			}else if($this.opts.currentpos < $this.opts.width-$this.opts.totalwidth){
				$this.opts.currentpos = $this.opts.width-$this.opts.totalwidth;
			}
			$this.find('.thumbs ul').css('left', $this.opts.currentpos+'px')
		}
	}
	
	
	$.fn.portfolio.loadzoom = function($this, $url){
		$('.projectzoom').stop().animate({opacity : 0}, 200, function(){ // , marginTop : '40px'
			$.ajax({
				url : $url,
				type : 'get',
				dataType : 'html',
				success: function(msg){
					$('.projectzoom').html(msg).animate({opacity : 1},200); // , marginTop : 0
					$('.projectzoom').slideshow();
				}
			});
		});
	}
	
	
	
})(jQuery);
