(function($){
	var Peek = function(element, options){
		var defaults = {
			'prevClass': 'left-arrow',
			'nextClass': 'right-arrow',
			'goTo': false
		};
		var options = $.extend(defaults,options);
		var obj = $(element);
		var w = obj.width() || 1080;
		var h = obj.height() || 450;
		var clickable = true;
		var ul = $('ul',obj);
		var li = $('li',obj);
		var s = li.length;
		var cs = 1;
		var sw = (s>3)?w/3:w/s;
		var prevBtn = $('.'+options.prevClass);
		var nextBtn = $('.'+options.nextClass);
		var expanded = false;
		if (s>1){
			ul.width(sw*s).height(h);
			li.animate({'width':sw+'px'}, 1000, 'easeOutCubic', function(){
				adjust();
			});
			prevBtn.click(function(){
				hideDetails();
				animate('prev',true);
			});
			nextBtn.click(function(){
				hideDetails();
				animate('next',true);
			});
		}
		else
		{
			ul.width(w).height(h);
			li.width(w).height(h);
			adjust();
		}
		$('a',li).css('cursor','default').click(function(){
			return false;
		});
		li.addClass('peek-item').click(function(){
			showItem($(this));
		}).hover(
			function(){
				if (!expanded) $('.overlay',$(this)).stop(true,true).fadeIn(200);
			},
			function(){
				if (!expanded) $('.overlay',$(this)).stop(true,true).fadeOut(200);
			}
		).height(h);
		$('.details,.detailwrap',li).hide();
		
		if (options.goTo) {
			var item = $('#'+options.goTo, ul);
			if (item.length) showItem(item);
		}
		
		function animate(dir,clicked){
			if (clickable){
				clickable = false;
				switch(dir){
					case 'prev':
						cs--;
						break;
					case 'next':
						cs++;
						break;
				};
				ul.animate({'margin-left':(cs-1)*li.width()*-1},{'duration':1000,'easing':'easeInOutCubic','complete':adjust});
			}
		};
		function adjust(){
			if (cs<2) prevBtn.hide();
			else prevBtn.show();
			if (!expanded && cs>s-3) nextBtn.hide();
			else if (cs>=s) nextBtn.hide();
			else nextBtn.show();
			if (expanded) showDetails();
			clickable = true;
		};
		function showItem(elm){
			if (!expanded){
				expanded = true;
				$('.overlay',elm).remove();
				$(document).attr('title', 'Papapalheta | '+$('a',elm).attr('rel'));
				if (s>1){
					var i = elm.index();
					cs = i+1;
					ul.width(w+sw*(s-1));
					elm.stop().animate({'width':w},{'duration':1000,'easing':'easeInOutCubic'});
					elm.parent('ul').animate({'margin-left':sw*i*-1},{'duration':1000,'easing':'easeInOutCubic','complete':expandAll});
				} else expandAll();
			}
		};
		function expandAll(){
			ul.css('margin-left',(cs-1)*w*-1);
			ul.width(w*s);
			li.width(w).removeClass('peek-item');
			adjust();
		};
		function showDetails(){
			var details = $('li:nth-child('+cs+') .details',ul);
			if (details.length){
				details.slideDown({'duration':500, 'easing':'easeInOutCubic', 'complete':function(){
					$('.detailwrap',$(this)).fadeIn(250, function(){
						$(this).parent('.details').jScrollPane({'showArrows':true});
					});
				}});
			}
		};
		function hideDetails(){
			$('li:nth-child('+cs+') .details, li:nth-child('+cs+') .detailwrap',ul).fadeOut(250);
		};
	};
	$.fn.peek = function(options){
		return this.each(function(){
			var element = $(this);
			if (element.data('peek')) return;
			var peek = new Peek(this, options);
			element.data('peek', peek);
		});
	};
})(jQuery);
