var items = $('.slideshow .slideshow_item'),
	delay = 6500,
	speed = 2000,
	anim  = 1.5,
	cur   = 0,
	last  = items.size() - 1,
	animation = {};

	
$(function(){
	/*
	$('.slideshow .image').fancybox({
		'hideOnContentClick': 	false,
		'overlayOpacity': 		0.7,
		'padding':				3,
		'imageScale':			true,
		'centerOnScroll':		true
	});

*/

	$('.fb .link').fancybox({
		'hideOnContentClick': 	false,
		'overlayOpacity': 		0.7,
		'padding':				3,
		'imageScale':			true,
		'centerOnScroll':		true
	});
	
	$('.slideshow .next').click( next );
	$('.slideshow .prev').click( prev );
	rotate();
	
	
	
	$('.view').view();
	
	
	
});

function rotate()
{	
	var img = $(items[cur]).find('.image'),
		pos = img.position(), 
		max = items.parents('.slideshow');

	img2 = new Image();
	img2.src = img.attr('src');

	animation.top  = ( Math.floor(Math.random()*2) ? '-' : '' ) + ( Math.abs( img2.height - max.height() ) - Math.abs(pos.top) );
	animation.top  = ( animation.top > 0 ? 0 : animation.top ) + 'px';
	animation.left = ( Math.floor(Math.random()*2) ? '-' : '' ) + ( Math.abs( img2.width - max.width() ) - Math.abs(pos.left) );
	animation.left = ( animation.left > 0 ? 0 : animation.left ) + 'px';
	delete img2;

	if( cur == 0 )
	{
		$(items[last]).fadeOut(speed, resetPos).find('.image').stop();
		$(items[cur]).fadeIn(speed).find('.image').animate( animation, delay*anim, 'linear' );
	}
	else
	{
		$(items[cur-1]).fadeOut(speed, resetPos).find('.image').stop();
		$(items[cur]).fadeIn(speed).find('.image').animate( animation, delay*anim, 'linear' );
	}
	
	if( cur == last )
		cur = 0;
	else
		cur++;
	
	setTimeout( 'rotate()', delay );
}

function resetPos()
{
	$(this).find('.image').css({top: '0px', left: '0px'});
}

function next()
{
	var parent = $(this).parents('.slideshow_item');
	
	if( cur == last )
		cur = 0;
	else
		cur++;
	
	parent.fadeOut(speed, resetPos).find('.image').stop();
	$(items[cur]).fadeIn(speed).find('.image').animate( animation, delay*anim, 'linear' );
	$(this).blur();
	return false;
}

function prev()
{
	var parent = $(this).parents('.slideshow_item');
	
	if( cur == 0 )
		cur = last;
	else
		cur--;
	
	parent.fadeOut(speed, resetPos).find('.image').stop();
	$(items[cur]).fadeIn(speed).find('.image').animate( animation, delay*anim, 'linear' );
	$(this).blur();
	return false;
}


(function($) {
	$.fn.view = function() 
	{
		return $(this).each(function(){
			var $this = $(this), center = Math.floor(($this.children('.view_item').size()/2));
			$this.find('.view_item:last .next').remove();	
			$this.find('.view_item:first .prev').remove();	

			$this.children('.view_item').each(function( i, v ){
				if( i == center )
				{
					$(this).removeClass('hide');
				}
				else
				{
					$(this).addClass('hide');
				}
			});
			
			$this.find('.next').click(function(){
				var btn = $(this),
					cur = btn.parents('.view_item'),		
					next = cur.next();
				
				cur.addClass('hide');
				next.removeClass('hide');
				btn.blur();
				return false;
			});	
			
			$this.find('.prev').click(function(){
				var btn = $(this),
					cur = btn.parents('.view_item'),		
					prev = cur.prev();
				
				cur.addClass('hide');
				prev.removeClass('hide');
				btn.blur();
				return false;
			});	
		});
	}
})(jQuery);
