(function($){
	$(document).ready(function(){
		$('.js-images').each(function(){
			var $images = $(this);
			var $items = $images.find('li');
			var $visible = $(this).find('li:first');
            var $next = null
			var $controlpane = $images.find('.js-controls').removeClass('full-hidden').hide().css('z-index',2);
			var $controls = $controlpane.find('a').css('opacity','0');
			var $play =  $controlpane.find('a.js-play').data('state','play');
			var $bw = $controlpane.find('a.js-bw');
			var $fw = $controlpane.find('a.js-fw');
			var t = null;
			var speed = 1000;
			$images.find('li:not(:first)').hide();

			var next = function(){
				$next = $visible.next();
                if($next.length < 1){
                    $next = $images.find('li:first');
                }

				$next.hide().css('z-index',1).fadeIn(speed,function(){
					$visible.hide();
					$visible = $next.css('z-index',0);
				});
			}

            var play = function(){
                speed = 1000;
                clearInterval(t);
                t = setInterval(next,5000);
            };
			
			var prev = function(){
				$next = $visible.prev();
                if($next.length < 1){
                    $next = $images.find('li:last');
                }
				$next.hide().css('z-index',1).fadeIn(speed,function(){
					$visible.hide();
					$visible = $next.css('z-index',0);
				});
			}
			
			var stop = function(){
				speed = 200;
				clearInterval(t);
                if($next != null){
                    $next.stop(true,true);
                    $visible = $next.css('z-index',0);
                }
			};
			
			var showControls = function(){
				$controlpane.show();
				$controls.stop(true,true).fadeTo(200,0.75);
			};
			
			var hideControls = function(){
				$controls.stop(true,true).fadeTo(200,0,function(){
					$controlpane.hide();
				});
			};
			
			if($items.length > 1){
				$items.hide();
				$visible.show();
				$images.hover(showControls,hideControls);
				
				$controls.hover(
					function(e){
                        stop();
						$(this).hoverFlow(e.type,{opacity:1},200);
					},
					function(e){
                        if($play.data('state') == 'play'){
                            play();
                        }
						$(this).hoverFlow(e.type,{opacity:0.75},200);
					}
				);
				
				$play.click(function(e){
					e.preventDefault();
                    stop();
					if($(this).data('state') == 'play'){
                        $(this).removeClass('pause').addClass('play').data('state','pause');
					}else{
						hideControls();
						$(this).removeClass('play').addClass('pause');
						$(this).data('state','play');
					}
				});
				
				$fw.click(function(e){
					e.preventDefault();
					stop();
					next();
				});
				
				$bw.click(function(e){
					e.preventDefault();
					stop();
					prev();
				});
				
				play();
			}
		});
	});
})(jQuery);
