/**
 * Infinate Carousel with automation (0.3)
 * by Kyle Welsby (www.mekyle.com)
 * kyle@liquid24.com
 *
 * Copyright (c) 2010 Liquid 24 (www.liquid24.com)
 * 
 * Thanks to:
 * Remy Sharp (http://www.jqueryfordesigners.com)
 * 
 * NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
 *
 */
(function ($) {
    $.fn.extend({
        fluidShow: function(options){
			var defaults = {
				easing: 'swing',
				duration: 1000,
				delay: 5000,
				auto: false,
				timer: null,
				horizontal: true
			}
			var options = $.extend(defaults, options);
            return this.each(function(){
				function repeat(str, n) {
			  		return new Array( n + 1 ).join( str );
				};
				var o =options;
				
				var self = $(this);
                self.css({
                    position: 'relative'
                });
				self.wrapInner('<div class="wrapper"></div>');
				self.append('<ul class="hud"></ul>');
				var $wrapper = $(".wrapper", self).css({
                    overflow: 'hidden',
					position:'absolute',
					top:0,
					left:0,
					width:self.width(),
					height:self.height()
                }), 
				$hud = $('.hud', self).css({
					// position:'absolute',
					// zIndex:100
				}),
				$slider = $('> ul', $wrapper).css({
					position:'absolute'
				}), 
				$items = $('> li.item', $slider).css({
					width: self.width(),
					height: self.height()
				}).show(), 
				$single = $items.filter(':first'), 
				singleWidth = $single.outerWidth(),
				singleHeight = $single.outerHeight();
				if(o.horizontal) { 
					var visible = Math.ceil($wrapper.outerWidth() / singleWidth);
				}else {
					var visible = Math.ceil($wrapper.outerHeight() / singleHeight);
				}
				var currentPage = 1, 
				pages = Math.ceil($items.length / visible);
				
				if (($items.length % visible) != 0) {
                    $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                    $items = $slider.find('> li');
                }
                $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
                $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
				if(o.horizontal){
					$slider.css({
						width: singleWidth * (Math.ceil($items.length)+3),
						height:'100%'
					});
					$('> li.item', $slider).css('float','left').show();
					$wrapper.scrollLeft(singleWidth * visible);
				}else{
					$slider.css({
						height: singleHeight * (Math.ceil($items.length)+3),
						width:'100%'
					});
					$wrapper.scrollTop(singleHeight * visible);
				}
                function gotoPage(page){
                    var dir = page < currentPage ? -1 : 1, 
						n = Math.abs(currentPage - page), 
						top = singleHeight * dir * visible * n,
						left = singleWidth * dir * visible * n;
					if(o.horizontal){
						left = left;
						top = 0;
					}else{
						left = 0;
						top = top;
					}
                    $wrapper.filter(':not(:animated)').animate({
                        scrollTop: '+=' + top,
						scrollLeft: '+=' + left
                    }, o.duration, o.easing, function(){
                        if (page == 0) {
							if(o.horizontal){
								$wrapper.scrollLeft(singleWidth * visible * pages);
							}else{
								$wrapper.scrollTop(singleHeight * visible * pages);
							}
							page = pages;
						} else {
							if (page > pages) {
								if(o.horizontal){
									$wrapper.scrollLeft(singleWidth * visible);
								}else{
									$wrapper.scrollTop(singleHeight * visible);
								}
								page = 1;
							}
						}
                        currentPage = page;
						$('> li', $hud).animate({
							// opacity: $('li', $hud).filter(':first').css('opacity')
						},500, o.easing, function(){
							// $(this).css('opacity','');
							$(this).removeClass('selected');
						}).slice(page, page+1).animate({
							// opacity:1
						},500, o.easing, function(){
							$(this).addClass("selected");
						});
                    });
                    return false;
                }	
				$items.each(function(i){
					page = i+1;
					if(page == 1){
						title = "H";
					}else{
						title = page - 1;
					}
					$hud.append($('<li class="goto_' + i + '">' + title + '</li>').click(function(){
						return gotoPage(i+1);
					}));
				});
				$hud.append(
					$('<li class="forwards">&gt;</li>').click(function(){
						automatic();
						return gotoPage(currentPage+1);
					})
				).prepend(
					$('<li class="backwards">&lt;</li>').click(function(){
						automatic();
						return gotoPage(currentPage-1);
					})
				).find('li.goto_0').addClass("selected");
				$(this).bind('next', function () {
  					gotoPage(currentPage + 1);
					automatic();
				});
				self.mouseenter(function(){
					clearTimeout(o.timer);
				}).mouseleave(function(){
					automatic();
				});
				automatic = function(){
					clearTimeout(o.timer);
					if (o.auto == true) {
						o.timer = setTimeout(function(){
							self.trigger('next');
						}, o.delay);
					}
				}
				automatic();
            });
        }
    });
})(jQuery);