﻿var hdp = hdp || {};

hdp.headerCarousel = {
    scrollSpeed : 1300,
    fadeDelay : 2500,
    fadeSpeed : 1500,
    init : function() {
        setTimeout(function() { hdp.headerCarousel.fades(); },hdp.headerCarousel.fadeDelay);
        hdp.headerCarousel.setImageTotal();
        $('img#title-img').closest('li').addClass('hdp-active-item');
        $("div.ucl-carousel-window").scrollable({
            speed:hdp.headerCarousel.scrollSpeed,
            size:1,
            onSeek: function(e) {
                $('li.hdp-active-item').removeClass('hdp-active-item');
                $(this.getVisibleItems()[0]).addClass('hdp-active-item');
                if(this.getPageIndex() === 0) { 
                    $('div.ucl-carousel-right a').css('text-indent','0');
                    $('div.ucl-content-header h1').fadeIn(hdp.headerCarousel.fadeSpeed);
                    $('p.ucl-img-count').fadeOut(hdp.headerCarousel.fadeSpeed);
                    $('p.ucl-image-title').html('');
                }
                else {
                    $('div.ucl-carousel-right a').css('text-indent','-5000em');
                    $('div.ucl-content-header h1').fadeOut();
                    $('p.ucl-img-count').fadeIn(hdp.headerCarousel.fadeSpeed);
                    $('p.ucl-image-title').html($(this.getVisibleItems()[0]).find('img').attr('title'));
                    hdp.headerCarousel.updateIndex(this.getPageIndex());
                }
            }
        });
    },
    fades : function() {
        $('div.ucl-carousel-right a').css('text-indent','0');
        $('div.ucl-header-carousel').fadeIn(hdp.headerCarousel.fadeSpeed);
    },
    setImageTotal : function() {
        // add the number of images to the count
        var imgCount = $('ul.items img:not("#title-img")').length;
        if(imgCount < 10) { imgCount = '0' + imgCount; }
        $('#ucl-img-total').text(imgCount);
        Cufon.replace('#ucl-img-total,p.ucl-image-title',Cufon.defaultTextCufon);
    },
    updateIndex : function(index) {
        index = (isNaN(index)) ? 1 : index; // number passed in is not zero indexed
        if(index < 10) { index = "0" + index; }
        $('#ucl-current-img-index').html(index);
        Cufon.replace('#ucl-current-img-index,p.ucl-image-title',Cufon.defaultTextCufon);
    }
};

$(document).ready(function(){
   hdp.headerCarousel.init();
});
