/**
 * @author Thomas Sømoen
 */
(function($){
	
	$.fn.delay = function(millis,callBack){
		var object = $(this);
		$.extend(object,{callBack:callBack});
		return window.setTimeout(function() {
			object.callBack();
			return object;
		}, millis);
	}
	
	$.fn.endAnimation = function(rewind){
		rewind = (rewind == undefined)?false:rewind;
		return $(this).each(function(){
			if(parseFloat($(this).css('opacity')) > 0.5 || rewind){
				$(this).stop(true).css('opacity','1');
			}else{
				$(this).stop(true,true);
			}
		});
	}
	
	$.extend({
		carousel:{
			elm:null,
			settings:{
				delay:4000,
				duration:{
					fast:200,
					slow:500
				}
			},
			timer:null,
			flash:null,
			showNext:function(loop){
				var delay = (loop)?$.carousel.settings.delay:1;
				var duration = (loop)?$.carousel.settings.duration.slow:$.carousel.settings.duration.fast;
				var list = $.carousel.elm.find('ul');
				
				if($.carousel.timer != null){
					window.clearTimeout($.carousel.timer);
				}
				
				var next = function(I){
					if(loop || I == 0){
						$.carousel.timer = window.setTimeout(function(){
							var elm = $.carousel.elm.find('li:last');
							elm.stop(true,true);
							elm.css('opacity','');
							elm.prev().css('opacity','');
							elm.find('a.play').css('opacity','').hide();
							elm.animate(
								{opacity:'0'},
								{
									duration: duration,
									step:function(fx){
										if(fx < 0.5){
											elm.prev().find('a.play').css('opacity','0.4').show();
										}
									},
									complete: function(){
										$(this).prependTo(list).css('opacity','').show();
										if(loop){
											next(++I);
										}
									}
								}
							);
							
							
						},delay);
						
						
					}
				}
				next(0);
			},
			showPrev:function(){
				var list = $.carousel.elm.find('ul');
				 $.carousel.elm.find('li:first').css('opacity','0').show().appendTo(list).animate(
					{opacity:'1'},
					{
						duration: $.carousel.settings.duration.fast,
						
						queue: 'carousel',
						scope: 'loop',
						step:function(fx){
							if(fx < 0.5){
								 $.carousel.elm.find('a.play').hide();
								 $.carousel.elm.find('li:last').find('a.play').show();
							}
						}
					}
				);
			},
			init:function(){
				return $(this).each(function(){
					$.carousel.elm = $(this); 
					var visible = $.carousel.elm.find('li:last');
					
					var buttons =  $.carousel.elm.find('a.next, a.prev, a.play');
					var prevButton = $.carousel.elm.find('a.prev');
					var nextButton = $.carousel.elm.find('a.next');
					var play = $.carousel.elm.find('a.play');
					var desc = $.carousel.elm.find('div.desc');
					var timer = null;
					
					buttons.data('animate',true);
					
					$.carousel.elm.find('li.hidden').hide().removeClass('hidden');
					$.carousel.elm.find('li:not(:last) a.play').hide();
					
					var stopLoops = function(){
						window.clearTimeout(timer);
						$.carousel.elm.find('li').stop(true,true);
					}
					
					buttons.hover(
						function(e){
							if($.carousel.elm.data('hoverOn')){
								if($(this).data('animate')){
									$(this).hoverFlow(e.type, { opacity: 0.7 }, {
										duration: 500, 
										complete: function() {
											stopLoops();
										}
									});	
								}else{
									stopLoops();
								}
							}
						},
						function(e){
							if ($.carousel.elm.data('hoverOn')) {
								$.carousel.elm.data('loop', true);
								
								if($(this).data('animate')){
									$(this).hoverFlow(
										e.type, 
										{opacity: 0.3}, 
										{
										duration: 500,
											complete: function(){
												stopLoops();
												$.carousel.showNext(true);
											}
										}
									);	
								}else{
									stopLoops();
									$.carousel.showNext(true);
								}
							}
						}
					);
					
					
					
					nextButton.click(function(e){
						e.preventDefault();
						$.carousel.elm.data('hoverOn',true);
						stopLoops();
						$.carousel.showNext(false);
					});
					
					prevButton.click(function(e){
						e.preventDefault();
						$.carousel.elm.data('hoverOn',true);
						stopLoops();
						$.carousel.showPrev();
					});
					
					play.click(function(e){
						e.preventDefault();
						$.carousel.elm.data('hoverOn',false);
						stopLoops();
					});
					
					$('ul#links ul').parents('li').each(function(){
						var anchor = $(this).find('>a');
						var submenu = $(this).find('>div');
						var height = submenu.height();
						
						submenu.css('bottom',(-1*height)).removeClass('hidden').hide();
						
						
						anchor.toggle(
							function(e){
								e.preventDefault();
								anchor.addClass('open');
							    buttons.fadeOut('fast');
								desc.animate({
									bottom:(-1*desc.height())
								},'fast');
								
								
								$('ul#links > li > div').hide();
								submenu.css('bottom',(-1*height)).show().animate(
									{
										bottom:110
									},
									'fast'
								);
							},
							
							function(e){
								e.preventDefault();
								anchor.addClass('remove');
								submenu.hide();
								desc.animate(
									{
										bottom:0
									},
									'fast',
									function(){
										buttons.fadeIn('fast');
									}
								);
							}
						);
					});
					
					$.carousel.elm.data('hoverOn',true);
					$.carousel.showNext(true);
				});	
			}
		}
	});
	
	$.fn.carouselStart = $.carousel.init;
	
	$(document).ready(function(){
		$('.carousel').carouselStart();
	});
})(jQuery);
