var presentationCycle = {
    
    /*
     * Presentation Cycle - a jQuery Cycle extension
     * Author:  Gaya Kessler
     * URL:     http://www.gayadesign.com
     * Date:    03-11-09
     */
    
    //slide options
    slideTimeout: 5000,
    containerId: "presentation_container",
    
    //cycle options
    cycleFx: 'scrollHorz',
    cycleSpeed: 1000,  
    
    //progressbar options
    barHeight: 14,
    barDisplacement: 20,
    barImgBarFull: "/js/images/pc_bar_full.gif",
    
    //variables this script need
    itemCount: 0,
    currentItem: 0,
    itemBarWidth: 0,
    barContainer: "",
    barContainerActive: "",
    barContainerOverflow: "",
    disableAnimation: false,
    
    init: function() {
        
        presentationCycle.itemCount = j$('#' + presentationCycle.containerId).children().length;

        presentationCycle.barContainer = j$("<div></div>");
        j$(presentationCycle.barContainer).addClass("pc_bar_container");
        
        var subtrackSpace = (presentationCycle.itemCount * presentationCycle.barHeight);
        var totalWidth = j$('#' + presentationCycle.containerId).innerWidth() - presentationCycle.barDisplacement;
        var fillWidth = Math.floor((totalWidth - subtrackSpace) / (presentationCycle.itemCount - 1));
        presentationCycle.itemBarWidth = fillWidth;
        
        var overflow = j$("<div></div>");
        j$(overflow).addClass("pc_bar_container_overflow");
        j$(overflow).css({
            overflow: "hidden",
            width: totalWidth + "px"
        });
        var underflow = j$("<div></div>");
        j$(underflow).addClass("pc_bar_container_underflow").appendTo(overflow);
        
        presentationCycle.barContainerActive = j$(presentationCycle.barContainer).clone().appendTo(underflow);
        j$(presentationCycle.barContainerActive).removeClass("pc_bar_container");
        j$(presentationCycle.barContainerActive).children().each(function () {
            j$(this).css({
                backgroundPosition: "right"
            });
            if (j$(this).css("background-image").match(presentationCycle.barImgBarEmpty)) {
                var newImg = j$(this).css("background-image").replace(presentationCycle.barImgBarEmpty, presentationCycle.barImgBarFull);
                j$(this).css("background-image", newImg);
            }
        });
        j$(overflow).css({
            width: presentationCycle.barHeight + "px",
            height: presentationCycle.barHeight + "px"
        });
        
        presentationCycle.barContainerOverflow = overflow;
        
        j$('#' + presentationCycle.containerId).cycle({
            fx: presentationCycle.cycleFx,
            speed: presentationCycle.cycleSpeed,
            timeout: presentationCycle.slideTimeout,
            before: function(currSlideElement, nextSlideElement) { presentationCycle.beforeSlide(currSlideElement, nextSlideElement); }
        });
        
        presentationCycle.barContainer.appendTo(j$('#' + presentationCycle.containerId));
        overflow.appendTo(j$('#' + presentationCycle.containerId));
        
        var i = 0;
        j$(".pc_bar_container_overflow .left, .pc_bar_container_overflow .center, .pc_bar_container_overflow .right").each(function () {
            j$(this).attr('itemNr', (i + 1));
            j$(this).css('cursor', 'pointer');
            j$(this).click(function() {
                presentationCycle.gotoSlide(j$(this).attr('itemNr'));
            });
            i++;
        });
    },
    
    beforeSlide: function(currSlideElement, nextSlideElement) {
        if (presentationCycle.currentItem == 0) {
            presentationCycle.currentItem = 1;
        } else {
            presentationCycle.currentItem = (presentationCycle.itemCount - (j$(nextSlideElement).nextAll().length)) + 2;
        }
        presentationCycle.animateProcess();
    },
    
    animateProcess: function() {
        var startWidth = (presentationCycle.itemBarWidth * (presentationCycle.currentItem - 1)) + (presentationCycle.barHeight * presentationCycle.currentItem);
        if (presentationCycle.currentItem != presentationCycle.itemCount) {
            var newWidth = (presentationCycle.itemBarWidth * (presentationCycle.currentItem)) + (presentationCycle.barHeight * (presentationCycle.currentItem + 1));   
        } else {
            var newWidth = presentationCycle.barHeight;
        }
        
        j$(presentationCycle.barContainerOverflow).css({
            width: startWidth + "px"
        });
        if (presentationCycle.disableAnimation == false) {
            j$(presentationCycle.barContainerOverflow).stop().animate({
                width: newWidth + "px"
            }, (presentationCycle.slideTimeout - 100));   
        }
    },
    
    gotoSlide: function(itemNr) {
        j$(presentationCycle.barContainerOverflow).stop();
        presentationCycle.disableAnimation = true;
        j$('#' + presentationCycle.containerId).cycle((itemNr - 1));
        j$('#' + presentationCycle.containerId).cycle('pause');
    }
    
}
