var presentationCycle = {
    
    /*
     * Presentation Cycle - a jQuery Cycle extension
     * Author:  Gaya Kessler
     * URL:     http://www.gayadesign.com
     * Date:	03-11-09
     */
    
    //slide options
    slideTimeout: 10000,
    containerId: "home-teaser",
	appendId: "show-bar",
    
    //cycle options
    cycleFx: 'fade',
    cycleSpeed: 1000,  
	cyclePrev: '#prev2',
	cycleNext: '#next2',
    
    //progressbar options
    barHeight: 14,
    barDisplacement: 20,
    barImgLeft: "_js/cycle/pc_item_left.gif",
    barImgRight: "_js/cycle/pc_item_right.gif",
    barImgBarEmpty: "_js/cycle/pc_bar_empty.gif",
    barImgBarFull: "_js/cycle/pc_bar_full.gif",
    
    //variables this script need
    itemCount: 0,
    currentItem: 0,
    itemBarWidth: 0,
    barContainer: "",
    barContainerActive: "",
    barContainerOverflow: "",
    disableAnimation: false,
    
    init: function() {        
        presentationCycle.itemCount = $('#' + presentationCycle.containerId).children().length;

        presentationCycle.barContainer = $("<div></div>");
        $(presentationCycle.barContainer).addClass("pc_bar_container");
        
        var subtrackSpace = (presentationCycle.itemCount * presentationCycle.barHeight);
		var totalWidth = 493 - presentationCycle.barDisplacement;
        var fillWidth = Math.floor((totalWidth - subtrackSpace));
        presentationCycle.itemBarWidth = fillWidth+18;
        
        for (var i = 0; i < 2; i++) {
            var extra_bar = true;
            if (i == 0) {
				var item = $("<div>&nbsp;</div>").appendTo(presentationCycle.barContainer);
                $(item).addClass("left");
                $(item).css({
                    backgroundImage: "url(" + presentationCycle.barImgLeft + ")",
                    height: presentationCycle.barHeight + "px",
                    width: presentationCycle.barHeight + "px"
                });
            } else if (i == 1) {
				var item = $("<div>&nbsp;</div>").appendTo(presentationCycle.barContainer);
                $(item).addClass("right");
                $(item).css({
                    backgroundImage: "url(" + presentationCycle.barImgRight + ")",
                    height: presentationCycle.barHeight + "px",
                    width: presentationCycle.barHeight + "px"
                });
                extra_bar = false;
            }             
            if (extra_bar == true) {
                var item = $("<div>&nbsp;</div>").appendTo(presentationCycle.barContainer);
                $(item).addClass("bar");
                 $(item).css({
                    backgroundImage: "url(" + presentationCycle.barImgBarEmpty + ")",
                    height: presentationCycle.barHeight + "px",
                    width: fillWidth + "px"
                });
            }
        }
        
        var overflow = $("<div></div>");
        $(overflow).addClass("pc_bar_container_overflow");
        $(overflow).css({
            overflow: "hidden",
            width: totalWidth + "px"
        });
        var underflow = $("<div></div>");
        $(underflow).addClass("pc_bar_container_underflow").appendTo(overflow);
        
        presentationCycle.barContainerActive = $(presentationCycle.barContainer).clone().appendTo(underflow);
        $(presentationCycle.barContainerActive).removeClass("pc_bar_container");
        $(presentationCycle.barContainerActive).children().each(function () {
            $(this).css({
                backgroundPosition: "right"
            });
            if ($(this).css("background-image").match(presentationCycle.barImgBarEmpty)) {
                var newImg = $(this).css("background-image").replace(presentationCycle.barImgBarEmpty, presentationCycle.barImgBarFull);
                $(this).css("background-image", newImg);
            }
        });
        $(overflow).css({
            width: presentationCycle.barHeight + "px",
            height: presentationCycle.barHeight + "px"
        });
        
        presentationCycle.barContainerOverflow = overflow;
		
		$('#' + presentationCycle.containerId).hover(function(){
				$(this).cycle('pause');
				$(presentationCycle.barContainerOverflow).stop();
			},
			function(){
				presentationCycle.animateProcess($(presentationCycle.barContainerOverflow).width());
				$(this).cycle('resume');
			});
        
        $('#' + presentationCycle.containerId).cycle({
    		fx: presentationCycle.cycleFx,
            speed: presentationCycle.cycleSpeed,
            timeout: presentationCycle.slideTimeout,
			prev: presentationCycle.cyclePrev,
			next: presentationCycle.cycleNext, 
            before: function(currSlideElement, nextSlideElement) { presentationCycle.beforeSlide(currSlideElement, nextSlideElement); }
    	});
        
        presentationCycle.barContainer.appendTo($('#' + presentationCycle.appendId));
        overflow.appendTo($('#' + presentationCycle.appendId));
    },
    
    beforeSlide: function(currSlideElement, nextSlideElement) {
        presentationCycle.animateProcess(14);
    },
    
    animateProcess: function(width) {
		var startWidth = width;	
		var newWidth = presentationCycle.itemBarWidth;   
        
		$(presentationCycle.barContainerOverflow).css({
            width: startWidth + "px"
        });
        if (presentationCycle.disableAnimation == false) {
            $(presentationCycle.barContainerOverflow).stop().animate({
                width: newWidth + "px"
            }, (presentationCycle.slideTimeout-100));   
        }
    }
}
