/**
 * 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
 */
var autoInterval = null; // Placeholder; Used to turn off auto animation on button click
(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: 3350,

        // 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,
        
        // Total width (including padding and margin) of the li tag withing the ul tag
        liWidth: 46,

        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) : o.liWidth;   // Was 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(autoInterval);
                clearInterval(fading);
                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(autoInterval);
                clearInterval(fading);
                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(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);
            });

        if(o.auto)
            autoInterval = setInterval(function() {
                go(curr+o.scroll);
            }, o.auto+o.speed);

        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() {
            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 setOpacity(level,div_name) {
    element = document.getElementById(div_name);
    
    if (element.style.opacity != null)
        element.style.opacity = level;
    
    if (element.style.MozOpacity != null)
        element.style.MozOpacity = level;
    
    if (element.style.KhtmlOpacity != null)
        element.style.KhtmlOpacity = level;
    
    if (element.style.filter != null)
        element.style.filter = "alpha(opacity=" + (level * 100) + ")";
}

function fade_in(div_name,clear_timeout) {
    display_block(div_name);
    
    for (i = 0; i <= 1; i += (1 / steps)) {
        setTimeout("setOpacity(" + i + ",\"" + div_name +"\")", i * duration);
    }
}

function fade_out(div_name,clear_timeout) {
    for (i = 0; i <= 1; i += (1 / steps)) {
        setting_opacity = setTimeout("setOpacity(" + (1 - i) + ",\"" + div_name +"\")", i * duration);
    }
    
    setTimeout("setOpacity(0,\"" + div_name +"\")", duration);
    setTimeout("display_none(\"" + div_name + "\")", duration);
}

function lets_fade() {
    fadeBlock_number = document.getElementById("fadeBlock_counter").value++;
    
    if (fadeBlock_number >= number_of_divs) {
        fade_out("fadeBlock" + number_of_divs);
        li_name = "liBlock" + number_of_divs;
        li_name = "liBlock1";
        fade_in("fadeBlock1");
        document.getElementById("fadeBlock_counter").value=1;
    }
    else {
        add_1_fadeBlock_number = fadeBlock_number + 1;
        fade_out("fadeBlock" + fadeBlock_number);
        li_name = "liBlock" + (fadeBlock_number);
        li_name = "liBlock" + (fadeBlock_number + 1);
        fade_in("fadeBlock" + add_1_fadeBlock_number);
    }
    if (autoscroll){
        $(".liBlock" + fadeBlock_number).css({"background-color":"transparent"});
        if (fadeBlock_number >= number_of_divs){
            $(".liBlock" + 1).css({"background-color":"#777"});
        }
        else{
            $(".liBlock" + (fadeBlock_number + 1)).css({"background-color":"#777"});
        }
    }

}

function previous_fade() {
    if(autoscroll == true){
      clearInterval(fading);
    }
    fadeBlock_number = document.getElementById("fadeBlock_counter").value;

    if (use_fade != "no")
        if (fadeBlock_number <= 1) {
            fade_out("fadeBlock1");
            fade_in("fadeBlock" + number_of_divs);
            document.getElementById("fadeBlock_counter").value=number_of_divs+1;
        }
        else {
            add_1_fadeBlock_number = fadeBlock_number - 1;
            fade_in("fadeBlock" + add_1_fadeBlock_number);
            fade_out("fadeBlock" + fadeBlock_number);
        }
    else if (fadeBlock_number <= 1) {
        div_name = "fadeBlock" + number_of_divs;
        setOpacity("100","fadeBlock" + number_of_divs);
        display_block(div_name);
        setOpacity("0","fadeBlock1");
        div_name = "fadeBlock1";
        display_none(div_name);
        clearTimeout(displaying_none);
        clearTimeout(setting_opacity);
        document.getElementById("fadeBlock_counter").value=number_of_divs+1;
    }
    else {
        minus_1_fadeBlock_number = fadeBlock_number - 1;
        div_name = "fadeBlock" + minus_1_fadeBlock_number;
        setOpacity("100","fadeBlock" + minus_1_fadeBlock_number);
        display_block(div_name);
        setOpacity("0","fadeBlock" + fadeBlock_number);
        div_name = "fadeBlock" + fadeBlock_number;
        display_none(div_name);
        clearTimeout(displaying_none);
        clearTimeout(setting_opacity);}
        document.getElementById("fadeBlock_counter").value--;
        fading = setInterval ("lets_fade()", restart_speed);
}

function next_fade() {
    if(autoscroll == true){
      clearInterval(fading);
    }
    if (use_fade != "no")
        lets_fade();
    else {
        fadeBlock_number = document.getElementById("fadeBlock_counter").value++;
        if (fadeBlock_number >= number_of_divs){
            setOpacity("1","fadeBlock1");
            div_name = "fadeBlock1";
            display_block(div_name);
            div_name = "fadeBlock" + number_of_divs;
            display_none(div_name);
            setOpacity("0","fadeBlock" + number_of_divs);
            document.getElementById("fadeBlock_counter").value=1;
        }
        else {
            add_1_fadeBlock_number = fadeBlock_number + 1;
            div_name = "fadeBlock" + add_1_fadeBlock_number;
            display_block(div_name);
            setOpacity("1","fadeBlock" + add_1_fadeBlock_number);
            setOpacity("0","fadeBlock" + fadeBlock_number);
            div_name = "fadeBlock" + fadeBlock_number;
            display_none(div_name);
        }
    }
    fading = setInterval ("lets_fade()", restart_speed);
}

function display_none(div_name){
  if(qToggle == 1){
    fade_in('fadeBlock' + qBlock);
    qToggle = 0;
  }
  else{
    document.getElementById(div_name).style.display = "none";
  }
    
}

function display_block(div_name){
    document.getElementById(div_name).style.display = "block";
}
//function timing_out(){timingout = setTimeout ("start_fade()", restart_speed);}

function start_fade(){
    fading = setInterval ("lets_fade()", speed);
}

var fadeBlock_number = 0,
    number_of_divs = 0,
    add_1_fadeBlock_number = 0,
    counter = 0,
    displaying_none,
    setting_opacity,
    block_number,
    li_name,
    id,
    qBlock, /* used as a block_number placeholder to prevent images from disappearing when clicking back and forth quickly */
    qToggle = 0, /* used as a toggle to prevent images from disappearing when clicking back and forth quickly */
    autoscroll = true;
    manual = 0;
var duration = 375; /* 1000 millisecond fade = 1 sec */
var steps = 21; /* number of opacity intervals */
var speed = 3650; /*speed of slide show; if working in tandem with jCarousel the value should be o.auto + (o.speed / 2) */
var restart_speed = 3650; /* speed to trigger restart slideshow */
var use_fade = "no"; /* use fade when next/previous button is triggered */

$(function() {
  if ($("#fadeBlock_counter").is('*')) {
    document.getElementById("fadeBlock_counter").value=1;

    for (x=1;x<=100;x++){
      fadeBlock = document.getElementById("fadeBlock"+x);
      if (fadeBlock!=null){
        if (fadeBlock.innerHTML !="" && fadeBlock.innerHTML !="dont use")
          number_of_divs = x;
        }
      else x=101;
    }

    document.getElementById("pager").style.display = "block";

    if(autoscroll == true){
      start_fade();
    }
  }
});

function fade_to(block_number) {
        if (autoscroll){
          clearInterval(autoInterval);
          $(".liBlock" + (fadeBlock_number + 1)).css({"background-color":"transparent"});
        }
        if (manual == 0){
            if (fadeBlock_number == 0 || fadeBlock_number == number_of_divs){
                div_name = "fadeBlock1";
                li_name = "liBlock1";
            }
            else{
                div_name = "fadeBlock" + (fadeBlock_number + 1);
                li_name = "liBlock" + (fadeBlock_number + 1);
            }
            $(".liBlock1").css({"background-color":"transparent"});
            $(".liBlock" + block_number).css({"background-color":"#777"});
            manual = 1;
            if(autoscroll == true){
              clearInterval(fading);
            }
        }
        else{
            div_name = "fadeBlock" + fadeBlock_number;
            li_name = "liBlock" + fadeBlock_number;
            $(".liBlock" + fadeBlock_number).css({"background-color":"transparent"});
            $(".liBlock" + block_number).css({"background-color":"#777"});
        }
        if (div_name == "fadeBlock" + block_number){
            return;
        }        
        fade_out(div_name);
        li_name = "liBlock" + block_number;
        if($('#fadeBlock' + block_number).css('opacity') > 0 && $('#fadeBlock' + block_number).css('opacity') < 1) {
          qBlock = block_number;
          qToggle = 1;
        }
        else{
          fade_in("fadeBlock" + block_number);
        }

        fadeBlock_number = block_number;
}

$(function() {
    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        onLoadAnim: false,
        speed: 300,
        visible: 4
    });
});

cm_TrackImpressions = "";
