/**
  * History Channel Photo Gallery
 **/
HC.gallery = {
	photoHolderId 	: 'largePhotoHolder .largePhoto',
	init: function() {
		$images = $('.items .subpanel');
		jQuery.each($images, function(i, val) {
			$(val).bind('click', function(e){
				$('.subpanel').removeClass('active');
				$(e.currentTarget).addClass('active');
				var image = $(e.currentTarget).find('a').attr('href');
				var title = $(e.currentTarget).find('.cItemTitle').html();
				var caption = $(e.currentTarget).find('.cPhotoCaption').html();
				//var enlImage = (image.replace(".jpg","-large.jpg"));
				var enlImage = $(e.currentTarget).find('em').html();
				HC.gallery.renderImage(image, caption, title, enlImage);
				return false;
			});
		});
		// get the first image in the list and display as default
		defaultImage = $('.items .subpanel:first').find('a').attr('href');
		defaultCaption = $('.items .subpanel:first').find('.cPhotoCaption').html();
		defaultTitle = $('.items .subpanel:first').find('.cItemTitle').html();
		//defaultEnlImage = (defaultImage.replace(".jpg", "-large.jpg"));
		defaultEnlImage = $('.items .subpanel:first').find('em').html();
		$('.items .subpanel:first').addClass('active');
		$('.topicCarousel .items .subpanel:first').removeClass('active');
		$('.items em').hide();
		this.renderImage(defaultImage, defaultCaption, defaultTitle, defaultEnlImage);
		this.bindEventHandlers();
	},
	bindEventHandlers: function() {
		// select the next/prev thumb and move the scroller (uses the carousel api prev() and next() methods)
		$('#galleryPrevHotspot').bind('click', function(e){ $('.items .active').prev('.subpanel').click();HC.carousel.api.prev();return false; });
		$('#galleryNextHotspot').bind('click', function(e){ $('.items .active').next('.subpanel').click();HC.carousel.api.next();return false; });
	},
	renderImage: function(image, caption, title, enlImage){
		$('#'+HC.gallery.photoHolderId).fadeOut("fast");
		$('#'+HC.gallery.photoHolderId).empty();
		$('#'+HC.gallery.photoHolderId).append("<img src=\""+image+"\" alt=\"\" title=\""+title+"\" />");
		//$('#'+HC.gallery.photoHolderId).append("<h4 id=\"largePhotoTitle\"><span>"+title+"</span></h4>");
		//$('#'+HC.gallery.photoHolderId).append("<a id=\"largePhotoEmbed\"><span>embed</span></a>");
		//$('#'+HC.gallery.photoHolderId).append("<a id=\"largePhotoShare\"><span>share</span></a>");
		$('#'+HC.gallery.photoHolderId).append("<p class=\"ceebox\"><a rel=\"example1\" href="+enlImage+" id=\"largePhotoEnlargeIcon\" title=\""+title+"\" class=\"enlarge\"></a></p>");
		$('#'+HC.gallery.photoHolderId).append("<a id=\"largePhotoHolderCaptionIcon\" class=\"info\"><span>info</span></a>");
		$('#'+HC.gallery.photoHolderId).append("<div id=\"largePhotoCaption\"><p>"+caption+"</p></div>");
		$('#'+HC.gallery.photoHolderId).fadeIn("fast");
		//$(".ceebox").ceebox({boxColor:"#fff"});
		// $("a[rel='example1']").colorbox();
		$("#largePhotoHolderCaptionIcon.info").hover(
			function() {
				$("#largePhotoCaption").fadeIn("fast");
			},function() {
				$("#largePhotoCaption").fadeOut("fast");
			});
		$("#largePhotoEnlargeIcon.info").hover(
			function() {
				$("#largePhotoCaption").fadeIn("fast");
			},function() {
				$("#largePhotoCaption").fadeOut("fast");
			});
	}
}
