﻿var hdp = hdp || {};

hdp.tabs = function(){
    var _this = {
        init : function() {
            $('ul.ucl-t5-tabs a').bind('click', function(e){e.preventDefault();hdp.tabs.changeTabContent(e,$(this).attr('href'));});
        },
        changeTabContent : function(e,href) {
            // add tracking
            var query = href.split('?')[1];
            try {
                var year = query.match(/year=(\d+)($|&)/)[1];
            }
            catch(e) {
                var year = query; // malformed url - should never happen, but pass the whole thing
            }
            $('.ucl-t5-tabs li.active').removeClass('active');
            $(e.target).parents('li').addClass('active');
            // track event
            var city = $('.ucl-t5-tabs li.active').text().replace(/^\s*/,'').split(",")[0];
            hdp.tracking.trackEvent('trophy_tour',year,city);

            if(hdp.gallery) { hdp.gallery.updateIndex(); }
            // load the ajax data once and put into a dummy element
            $('#ucl-ajax-container').load('trophy-tour-ajax.aspx?'+query, '', function() {
                    // large image html
                    var contentDiv = $('div.hdp-content');
                    contentDiv.find('div.ucl-gallery-img-lge').html($('#ucl-ajax-container #default-image').html());
                    // image title
                    contentDiv.find('p.ucl-image-title').html($('#ucl-ajax-container #image-header').html());
                    // carousel
                    contentDiv.find('ul.items').html($('#ucl-ajax-container #tab-images .items').html());
                    // tab text
                    contentDiv.find('div.ucl-t5-tab-text').html($('#ucl-ajax-container #tab-content').html());
                    // reload carousel
                    hdp.Carousels.reload();
                    hdp.gallery.init();
                    // refresh cufon text display
                    Cufon.replace('div.ucl-t5-tab-text',Cufon.defaultTextCufon);
                    Cufon.replace('p.ucl-image-title',Cufon.defaultTextCufon);
		            if(contentDiv.find('ul.items li').length<=hdp.Carousels.barItemsPerPage) {
			            contentDiv.find("div.buttons").hide();
		            }
		            else { contentDiv.find("div.buttons").show(); }
                    // clear dummy element
                    $('#ucl-ajax-container').empty();
                });
        }
    };
    return _this;
}();

$(document).ready(function(){
   hdp.tabs.init();
});