﻿/* gallery used by t4 (star-lounge-gallery) and t5 (trophy-tour-region)
    these two began with indentical functionality, hence the code sharing,
    but have since diverged slightly - but not enough to warrant separation
*/
var hdp = hdp || {};
hdp.gallery = function(){
    var _this = {
        fadeSpeed : 1300,
        imgWidth : 996,
        imgHeight: 426,
        lgeImgFolder : 'fullsize',
        smlImgFolder : 'previews',
        imgIDPrefix : 'img-',
        
        init : function() {
            hdp.gallery.setImageTotal();

            $('ul.items a').live('click', function(e){hdp.gallery.swapImage(e);e.preventDefault();});

            var re = new RegExp(/[?&]img=(\d+)($|&)/);
            var srch = window.location.search;
            var m = re.exec(srch);
            if(m && m[1]) {
                hdp.gallery.currentImgId = hdp.gallery.imgIDPrefix + m[1];
                $('#'+hdp.gallery.currentImgId).trigger('click');
            }
            else { hdp.gallery.currentImgId = $('div.ucl-gallery-img-lge img').attr("id"); }
        },
        swapImage : function(e) {
            $('ul.items li.active').removeClass('active');
            $(e.target).parents('li').addClass('active');
            var index = $('div.bar-carousel li a img').index($(e.target));
            if(index === 0) {
                $('div.ucl-content-header div.ucl-img-copy-holder').hide();
                $('div.ucl-content-header h1').fadeIn();
                $('div.ucl-ucl-logo-div').fadeIn();
            }
            else {
                $('div.ucl-content-header h1:visible').fadeOut('normal',function() { $('div.ucl-content-header div.ucl-img-copy-holder').show(); });
                $('div.ucl-ucl-logo-div:visible').fadeOut();
            }
            try {
                var newSrc = $(e.target).closest('a').attr('href');
                $('div.ucl-gallery-img-lge img').attr('src',newSrc);
                hdp.gallery.updateIndex($('div.bar-carousel img:not("#title-img")').index($(e.target)));
            }
            catch(err) {
                // probably haven't found e.target
            }
        },
        setImageTotal : function() {
            // add the number of images to the count
            var imgCount = $('div.carousel-window ul.items img:not("#title-img")').length;
            if(imgCount < 10) { imgCount = '0' + imgCount; }
            $('#ucl-img-total').text(imgCount);
            Cufon.replace('#ucl-img-total',Cufon.defaultTextCufon);
        },
        updateIndex : function(index) {
            index = (isNaN(index)) ? 1 : index; // number passed in is zero indexed
            if(index < 10) { index = "0" + index; }
            $('#ucl-current-img-index').html(index);
            Cufon.replace('#ucl-current-img-index',Cufon.defaultTextCufon);
        }
    };
    return _this;
}();

$(document).ready(function(){
   hdp.gallery.init();
});