/**
 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.1.2
 *
 * http://gmarwaha.com/jquery/jcarousellite/
 *
 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 0.4.0
 */

(function($) {                                          // Compliant with jquery.noConflict()
$.fn.jCarouselLite = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,
        mouseWheel: false,
        auto: null, 

        speed: 200,
        easing: null,

        vertical: false,
        circular: true,
        visible: 3,
        start: 0,
        scroll: 1,

        beforeStart: null,
        afterEnd: null
    }, o || {});

    return this.each(function() {                       // Returns the element collection. Chainable.
        var curr = o.start, running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = div.find("ul.carousel-ul"), li = div.find("li.carousel-li"), itemLength = li.size();                       
        div.css("visibility", "visible");
        var highest = 100;
        jQuery("li.carousel-li").each(function() {
            var teste = jQuery(this).height();
            if (teste > highest) { highest = teste; }
            jQuery(this).css("width","460px")
        });
        

        li.css("overflow", "hidden")                        // If the list item size is bigger than required
            .css("float", o.vertical ? "none" : "left")     // Horizontal list
            .children().css("overflow", "hidden");          // If the item within li overflows its size, hide'em

        ul.css("margin", "0")                               // Browsers apply default margin 
            .css("padding", "0")                            // and padding. It is reset here.
            .css("position", "relative")                    // IE BUG - width as min-width
            .css("list-style-type", "none")                 // We dont need any icons representing each list item.
            .css("z-index", "1");                           // IE doesnt respect width. So z-index smaller than div

        div.css("overflow", "hidden")                       // Overflows - works in FF
            .css("position", "relative")                    // position relative and z-index for IE
            .css("z-index", "2")                            // more than ul so that div displays on top of ul
            .css("left", "0px");                            // after creating carousel show it on screen
                
        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
        var divSize = 520;                                  // size of entire div(total length for just the visible items)

        li.css("width", li.width())                         // inner li width. this is the box model width
            .css("height", highest);                    // inner li height. this is the box model height

        ul.css(sizeCss, ulSize+"px")                        // Width of the UL is the full length for all the images
            .css(animCss, -(curr*liSize));                  // Set the starting item

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

        if(o.btnPrev) {
            $(o.btnPrev).click(function() { 
                return go(curr-o.scroll); 
            });
        }
        
        if(o.btnNext) {
            $(o.btnNext).click(function() { 
                return go(curr+o.scroll); 
            });
        }
        
        if(o.btnGo) {
            $.each(o.btnGo, function(i, val) {
                $(val).click(function() {
                    return go(i);
                });
            });
        }
        
        if(o.mouseWheel && div.mousewheel) {
            div.mousewheel(function(e, d) {
                return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
            });
        }    
        
        if(o.auto){ 
            setInterval(function() { 
                go(curr+1); 
            }, o.auto+o.speed);
        }        

        function vis() {
            return li.gt(curr-1).lt(o.visible);
        };

        function go(to) {
            if(!running) {
                running = true;
                if(o.beforeStart) o.beforeStart.call(this, vis());

                if(to<0 && curr==0) {                                               // If first, then goto last
                    if(o.circular) curr=itemLength-o.visible; 
                    else return;
                } else if(to>=itemLength-o.visible && curr+o.visible>=itemLength) { // If last, then goto first
                    if(o.circular) curr = 0; 
                    else return;
                } else curr = to;

                ul.animate(
                    animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
                    function() {
                        ul.css(animCss, -(curr*liSize)+"px");    // For some reason the animation was not making left:0
                        if(o.afterEnd) o.afterEnd.call(this, vis());
                        running = false;
                    }
                );
            }
            return false;
        };
    });
};

function css(el, prop) {
    return parseInt($.css(el.jquery ? el[0] : el,prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

})(jQuery);
