﻿var hdp = hdp || {};

hdp.Carousels = {
	active: false,
	inc: 0,
	barScrollSpeed: 1300,
	barItemsPerPage: 5		/* this is how many images there are */,
	fullItemsPerPage: 1		/* this is how many GROUPS of 15 images there are! */ ,
	init : function() {
		hdp.Carousels.inc = 0;
		$("div.bar-carousel").each(function(index, item) {
			 hdp.Carousels.initBar(hdp.Carousels.inc, this);
		});
		$("div.full-carousel").each(function(index, item) {
			 hdp.Carousels.initFull(this);
		});
		this.active = true;
	},
	initEach : function(el) {
		hdp.Carousels.initBar(hdp.Carousels.inc, this);
		hdp.Carousels.initFull(this);
	},
	initBar : function(i, el) {
		hdp.Carousels.makeScroller(el, hdp.Carousels.barItemsPerPage);
		var jqEl = $(el);
		jqEl.find("div.more-tab").click(hdp.Carousels.openFull);
		if(jqEl.find("li").length<=hdp.Carousels.barItemsPerPage) {
			jqEl.find("div.more-tab").hide();
			jqEl.find("div.buttons").hide();
		}
		jqEl.before(("<div class='bar-overlay' id='bar-overlay-"+i+"'></div>"));
		thisOverlay = "#bar-overlay-"+i;
		$(thisOverlay).hide();
		if(jqEl.find('div.more-tab').length === 1) {
			$(thisOverlay).load(jqEl.find("div.more-tab a")[0].href + " div.full-carousel", function() {
			        var carEl = $(this).find("div.full-carousel");
					hdp.Carousels.initFull(carEl);
					carEl.find("#back").css("display", "none");
					carEl.find("#close").css("display", "block").find("a").click(hdp.Carousels.closeFull);
					Cufon.replace('span.title',Cufon.smlHeaderTextCufon);
					Cufon.replace('#close a',Cufon.linkTextCufon);
					Cufon.replace('div.gallery-title p',Cufon.headerNoGradientCufon);
			});
			hdp.Carousels.inc++;
		}
	},
	initFull : function(el) {
		hdp.Carousels.makeScroller(el, hdp.Carousels.fullItemsPerPage);
		var jqEl = $(el);
		if (jqEl.find("ul.items ul").length <= hdp.Carousels.fullItemsPerPage) {
			jqEl.find("div.buttons").hide();
		}
	},
	makeScroller : function(el, pageSize) {
	    var jqEl = $(el);
		jqEl.find("div.buttons").css("display", "block").click(function(e) {
			e.preventDefault();
		});
		// made public so it can reload dynamic content
		hdp.Carousels.api = jqEl.find("div.carousel-window").scrollable({
			onSeek: function() {
			    // this is not used in UCL
				if (jqEl.find("div.pagination-counter span.index").length > 0) {
					jqEl.find("div.pagination-counter span.index").html(this.getPageIndex() + 1);
					Cufon.replace('span.index',Cufon.smlHeaderTextCufon);
				}
			},
			speed: hdp.Carousels.barScrollSpeed,
			size: pageSize,
			easing: 'easeInOutQuad',
			api: true
		});

// IMPORTANT
// if($(el).find("div.pagination-counter .total")) will pass as true because it returns an object
// you need the .length test, or something similar, to ensure it's not empty
		if (jqEl.find("div.pagination-counter .total").length) {
			if(hdp.Carousels.api.getSize() > 1) {
				jqEl.find("div.pagination-counter span.total").html(hdp.Carousels.api.getSize());
				jqEl.find("div.pagination-counter span.index").html(hdp.Carousels.api.getPageIndex() + 1);
			} else {
				jqEl.find("div.pagination-counter").hide();
			}
		}
		hdp.Carousels.activateLinks(jqEl.find("div.carousel-window"));
	},
	openFull : function(e) {
		thisBar = $(this).parent();
		thisOverlay = thisBar.prev();
		thisOverlay.parent().parent().find("div.hdp-main-content").fadeOut();
		thisBar.animate({height:1, width:996, top:260}, function() {
			thisBar.hide();
			thisOverlay.show().css("width", "996").animate({height:'100%', width:996, top:0}, function() {});
		});
		$('div.ucl-signup-div').fadeOut();
		e.preventDefault();
	},
	closeFull : function(e) {
		//this will do the reverse of above!
		thisOverlay = $(this).closest('div.bar-overlay');
		thisOverlay.animate({height:1, width:996, top:280}, 800, function() {
			thisOverlay.hide();
			thisOverlay.parent().parent().find("div.hdp-main-content").fadeIn(800);
			thisOverlay.next().css("width", "996").animate({height:160, width:996, top:90}, function() {});
		});
		hdp.signUp.showSignUpPanelIfAppropriate();
		e.preventDefault();
	},
	activateLinks : function(el) {
		if(typeof(MediaPlayer) != 'undefined') {
			$(el).find("a").click(function(e) {
				hdp.Carousels.launchPlayer($(this), e);
			});
		}
	},
	launchPlayer : function(el,e) {
		/* NOTES ON PREVIEW IMAGES
		any media player videos can have their own preview images. the paths of which are generated from the
		thumbnail image location. So if thumbnails are [location]/thumbs/xxxxx_thumbnail.jpg
		then correspoding preview image should be at [location]/thumbs/xxxxx_preview.jpg
		if their is no preview it will appear transparent/black depending on opacity. */

		hdp.Carousels.topElement = $('#hdp-header').next();
		e.preventDefault();
		$('div.ucl-signup-div').fadeOut();
		playerHref = $(el)[0].href;
		trackingName = $(el)[0].name;	// Used to send a 'friendly name' to the Google Analytics code
		twain = $(el).find("img").attr("src").split('/thumbs/');
		prevRef = twain[0] + "/images/" + twain[1].split("_thumbnail.jpg")[0] + "_preview.jpg";
		thisOverlay = $('<div id="media-overlay"></div><div id="media-player"><div id="close-btn"><a href="#">CLOSE</a></div><div id="media-MediaPlayer1"></div></div>').appendTo(hdp.Carousels.topElement).hide();
		$("#close-btn").click(hdp.Carousels.closePlayer);
		$("#close-btn").hide();
		// IE6 doesn't set the height properly
	        if($('#media-overlay').height() === 0) {
	            $('#media-overlay').height($('#media-overlay').parent().height());
	        }
		//'fadeTo' doesn't de-activate display:none left from 'hide()'
		$('#media-overlay').css('display','block').fadeTo(900,0.8);
		$("#media-player").fadeIn(400, function() {
			var dynamicMediaPlayer = new MediaPlayer ({
				id: 'media-MediaPlayer1',
				url: playerHref,
				previewImage: prevRef,
				width: '770',
				height: '435',
				useCustomControls: false,
				skin: '../assets/global/flash/heineken.swf',
				onLoad: function() {
				    $("#close-btn").show();
				},
				trackingName: trackingName
			});
		});
	},
	closePlayer : function(e) {
		if (typeof(e) != 'undefined') { e.preventDefault(); }
		if ($("#media-overlay")) {
			hdp.signUp.showSignUpPanelIfAppropriate();
			$("#media-overlay").remove();
			$("#media-player").remove();
		}
	},
	reload : function(e) {
		hdp.Carousels.api.reload().begin();
	}
};
