/**
 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.2 or above
 *
 * 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: 1.0.1
 * Note: Requires jquery 1.2 or above from version 1.0.1
 */
(function($) {                                          // Compliant with jquery.noConflict()
$.fn.jCarouselLite = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,

        // Determine the speed of the auto slide function (in ms); Ex. '2500'; To turn off set to 'null'
        auto: null,

        // Placeholder; Used to turn off auto animation on button click
        autoInterval: null,

        // If set to 'true' the slide will automatically animate on load
        onLoadAnim: true,

        // Determine speed of animation on load; Does not have an effect if onLoadAnim is false
        onLoadSpeed: 1400,

        // Determine the speed (in ms) of the animation after load; Does not have an effect if onLoadAnim is false
        afterLoadSpeed: 600,

        // Determine the speed (in ms) of the animation; Does not have an effect if onLoadAnim is true
        speed: 600,

        // Determine how many slides to animate on load; Does not have an effect if onLoadAnim is false
        onLoadHowMany: 2,

        // Placeholder; Used to simulate turning on highlighting of nav links (if nav links are enabled)
        goWhere: null,

        // Placeholder; Used to simulate turning off highlighting of nav links (if nav links are enabled)
        goFrom: null,

        easing: 'swing',
        vertical: false,
        visible: 1,
        start: 0,
        scroll: 1,
        beforeStart: null,
        afterEnd: null
    }, o || {});

    return this.each(function() {                           // Returns the element collection. Chainable.

        var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

        div.after("<div class='" + o.btnPrev.replace(".","") + "'></div><div class='" + o.btnNext.replace(".","") + "'></div>");

        ul.prepend(tLi.slice(tl-v-1+1).clone())
            .append(tLi.slice(0,v).clone());
        o.start += v;

        var li = $("li", ul), itemLength = li.size(), curr = o.start;

        li.css({overflow: "hidden", "float": o.vertical ? "none" : "left"});
        ul.css({margin: "0", position: "relative", "list-style-type": "none"});
        div.css({overflow: "hidden", position: "relative"});

        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 = liSize * v;                           // size of entire div(total length for just the visible items)

        li.css({width: li.width(), height: li.height()});
        ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

        if(o.btnGo){
            attrAdd = $("#liTab" + o.start + " img").attr('src').replace("OFFbutton.jpg", "ONbutton.jpg");
            $("#liTab" + o.start + " img").attr({src: attrAdd});
        }

        if(o.btnPrev)
            $(o.btnPrev).click(function() {
                clearInterval(o.autoInterval);
                if(o.btnGo){
                    if(curr>tl){
                        o.goFrom=1;
                    }
                    else if(curr<1){
                        o.goFrom=tl;
                    }
                    else{
                        o.goFrom=curr;
                    }
                    if(curr==1){
                        o.goWhere = tl;
                    }
                    else if(curr<1){
                        o.goWhere = (tl - 1);
                    }
                    else{
                        o.goWhere = (curr-o.scroll);
                    }
                }
                return go(curr-o.scroll);
            });

        if(o.btnNext)
            $(o.btnNext).click(function() {
                clearInterval(o.autoInterval);
                if(o.btnGo){
                    if(curr>tl){
                        o.goFrom=1;
                    }
                    else if(curr<1){
                        o.goFrom=tl;
                    }
                    else{
                        o.goFrom=curr;
                    }
                    if(curr==tl){
                        o.goWhere = 1;
                    }
                    else if(curr>tl){
                        o.goWhere = 2;
                    }
                    else{
                        o.goWhere = (curr+o.scroll);
                    }
                }
                return go(curr+o.scroll);
            });

        if(o.btnGo)
            $(o.btnGo).click(function() {
                clearInterval(o.autoInterval);
                o.goWhere = parseInt($(this).attr('id').replace("liTab", ""))
                if(curr>tl){
                    o.goFrom=1;
                    ul.css(animCss, -(o.goFrom*liSize));
                }
                else if(curr<1){
                    o.goFrom=tl;
                    ul.css(animCss, -(o.goFrom*liSize));
                }
                else{
                    o.goFrom=curr;
                }
                return go(o.goWhere);
            });

        function vis() {
            return li.slice(curr).slice(0,v);
        };

        if(o.onLoadAnim){
            ul.css(animCss, -((curr*liSize)*(o.onLoadHowMany+1)));
            o.speed = o.onLoadSpeed;
            return go(1);
        }

        function autoLoaded() {
            o.autoInterval = setInterval(function() {
            if(o.btnGo){
                if(curr>tl){
                    o.goFrom=1;
                }
                else if(curr<1){
                    o.goFrom=tl;
                }
                else{
                    o.goFrom=curr;
                }
                if(curr==tl){
                    o.goWhere = 1;
                }
                else if(curr>tl){
                    o.goWhere = 2;
                }
                else{
                    o.goWhere = (curr+o.scroll);
                }
            }
            return go(curr+o.scroll);
            }, o.auto+o.speed);
        };

        function go(to) {
            var attrRemove;                                     // Placeholder for removal of ONbutton.jpg
            var attrAdd;                                        // Placeholder for assigning of ONbutton.jpg

            if(!running) {

                if(o.btnGo){
                    if(!o.onLoadAnim){
                        attrRemove = $("#liTab" + o.goFrom + " img").attr('src').replace("ONbutton.jpg", "OFFbutton.jpg");
                        $("#liTab" + o.goFrom + " img").attr({src: attrRemove});

                        attrAdd = $("#liTab" + o.goWhere + " img").attr('src').replace("OFFbutton.jpg", "ONbutton.jpg");
                        $("#liTab" + o.goWhere + " img").attr({src: attrAdd});
                    }
                }

                if(o.beforeStart)
                    o.beforeStart.call(this, vis());

                if(to<=o.start-v-1) {           // If first, then goto last
                    ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
                    // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
                    curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
                } else if(to>=itemLength-v+1) { // If last, then goto first
                    ul.css(animCss, -( (v) * liSize ) + "px" );
                    // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
                    curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
                } else curr = to;

                running = true;

                ul.animate(
                    animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
                    function() {
                        if(o.afterEnd)
                            o.afterEnd.call(this, vis());
                        running = false;
                    }
                );

                if(o.onLoadAnim){
                    o.speed = o.afterLoadSpeed;
                    o.onLoadAnim = false;
                    if(o.auto)
                        autoLoaded();
                }
            }
            return false;
        };
    });
};

function css(el, prop) {
    return parseInt($.css(el[0], 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);

$(function() {
    $(".home_carousel").jCarouselLite({
        btnNext: ".home_next",
        btnPrev: ".home_prev",
        btnGo: "#carouselTabs ul li"
    });
    $(".hover_block li").append("<div class='shadow'></div>");
    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        onLoadAnim: false,
        speed: 400,
        visible: 3
    });
});

cm_TrackImpressions = "";
