HISTORY.videoPlayer = {

	isShow  : (HISTORY.body.attr('id') === 'show-videos') ? true : false,
	isTopic : (HISTORY.body.attr('id') === 'topic-videos') ? true : false,
	isTdih  : (HISTORY.body.attr('id') === 'tdih') ? true : false,

	init : function () {

		var href, media_type = $('#content > div.media-wrap').attr('id') == 'media-audio' ? 'audio' : 'video';
        var related_media_type =  $('#content > div.media-wrap').attr('id') == 'media-audio' ? 'video' : 'audio';

		$('#content > div.media-wrap > div.controller').prepend('<h4>Loading&hellip;</h4>');

		if (HISTORY.videoPlayer.isTdih === false) {

			// Clicks on video thumbs will determine what the slug of the video is by
			// inspecting the href of the link and performing a Flash callback.
			$('ul.media-thumbs li.' + media_type + ' a.thumb').live('click', function (e) {
				e.preventDefault();
				//var split_char = (this.href.indexOf('/topics/') != -1 || this.href.indexOf('/shows/') != -1) ? '#' : '/';
				//href = this.href.split(split_char);
                var split_char = (this.href.indexOf('#') != -1) ? '#' : '/';
			    href = this.href.split(split_char);
				$.scrollTo($('#media-videos'), 400);
				HISTORY.videoPlayer.flashCallback(href[href.length - 1]);
			});

            var related_thumbs = $('ul.media-thumbs li.' + related_media_type + ' a.thumb');
            related_thumbs.bind('click', function(e){
            document.location = this.href;
        });

		} else {

			$('#ad-box').after('<div id="ad-box-vid" class="ad" style="display:none;"></div>');

		}
	},

	// flashCallback is a function that calls flash and tells it to play the video
	// that corresponds with the provided slug.
	flashCallback : function (slug) {
		HISTORY.videoPlayer.getFlashMovie("flash-player").flashCallback(slug);
	},

	// getFlashMovie returns the object storing the flash movie for callback purposes.
	getFlashMovie : function (movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	},

	// updateThumb is called by Flash to update the thumbnails on the page to reflect
	// the currently selected movie.
	updateThumbs : function (slug) {
		var thumbs_lists = $('ul.media-thumbs');
		
		if (thumbs_lists.length > 0) {

			// Find the item that corresponds to the slug.
			var target = $('a.thumb[rel*="' + slug + '"]', thumbs_lists).parent();
			// Remove the playing indicator from the old playing item.
			$('li.playing', thumbs_lists).removeClass('playing');

			// Indicate that the target is playing and if it's inside a collapsed box, show it.
			target.addClass('playing');

			if (target.hasClass('truncated-video')) {
				$('#truncated-video').show();
			}

			// Update the URL hash.
			window.location.hash = slug;
		}
	},

	//	updatePage is called by Flash to update the appearance of the page and the information module.
	updatePage : function (slug, title, duration, overview, ecommerce, videoUrl) {

		var download_text, title, full_title, html, middle, i, j, controller, purchase, media_type;

		if (HISTORY.videoPlayer.isTdih === false) {

			controller = $('div.media-wrap > div.controller');
			purchase = $('#show-videos div.purchase > p');
			media_type = controller.parent().attr('id') == 'media-audio' ? 'audio' : 'videos';

			full_title = (duration.length > 0) ? title + ' <span>(' + HISTORY.secondsToHms(duration) + ')</span>' : title;
			html = '<h4>' + full_title + '</h4><div class="copy"><p>' + overview + '</p></div>';


			// Non-show video pages have the "similar" link their unique URLs are search landing pages.
			if (controller.length > 0) {
				html += '<p><a class="smaller more" href="' + HISTORY.context_path + '/' + media_type + '/' + slug + '"><span>';
				html += (media_type == 'videos') ? 'Watch Similar Videos' : 'Listen to Similar Speeches &amp; Audio';
				html += '</span></a></p>';
				middle = '/' + media_type + '/';

			// Shows have no audio and their unique URL structure is different.
			} else {
				middle = '/shows/' + HISTORY.show_slug + '/videos/';
			}

			html += '<div id="ad-box"></div>';

			// Update the info box.
			controller.empty().html(html);

            // Update the gigya sharebar.
            $('#gigya-video').empty().html('<div id="gigya-bar"></div>');
            $('#gigya-bar').shareBar({"contextPath": "http://www.history.com",
                                 "shareBarSize": "large",
                              "docLocationPath": document.location.pathname,
                                  "docLocation": document.location,
                                     "shareUrl": videoUrl,
                                   "shareTitle": title,
                                "documentTitle": document.title + " @The History Channel",
                              "facebookLikeUrl": "http://www.facebook.com/history",
                         "facebookRecommendUrl": videoUrl});

			// Update the citation link(s).
			$('ul.content-controls a.cite-this').attr('href', HISTORY.context_path + '/ajax/cite.jsp?title=' + escape(title) + '&path=' + escape(HISTORY.context_path + middle + slug));

			if (purchase.length > 0 && ecommerce.length > 0) {

				download_text = $('span.download-text', purchase).text();
				// Reset the html variable.
				html = '';

				// Construct the ecommerce module of the video hero.
				for (i = 0; i < ecommerce.length; i++) {

					if (ecommerce[i].type == 'downloadUrls') {
						html += '<span class="download-text">' + download_text + '</span> ';
					}

					for (j = 0; j < ecommerce[i].urls.length; j++) {

						// If on the last link in a set of 2 or more, add an "or" to the string..
						if (ecommerce[i].urls.length > 1 && j === ecommerce[i].urls.length - 1) {
							html += 'or ';
						}

						html += '<a href="' + ecommerce[i].urls[j].url + '" class="new-window">' + ecommerce[i].urls[j].label + '</a>';

						// Use proper grammatical structure for listed items.
						if (ecommerce[i].urls.length > 2 && j < ecommerce[i].urls.length - 1) {
							html += ', ';
						} else {
							html += ' ';
						}
					}

					// If not on the last item set of links, add a line break.
					if (i < ecommerce.length - 1) {
						html += '<br>';
					}
				}
				purchase.empty().html(html);
			}
		}
	},

	// adStart is called by Flash and triggers ad changes - replacing the default ads - on the page.
	adStart : function (banners) {
		var controller = $('div.media-wrap div.controller'), i;


		// Only do 300x250 ad switching in a video hero.
		if (controller.length > 0) {
			controller.children(':not(#ad-box)').hide();
            $('#ad-box').show();
			tmDisplayBanner(banners, "ad-box", 300, 250);
		}

		if (HISTORY.videoPlayer.isTdih === true) {
			$('#ad-box').hide();
			$('#ad-box-vid').show();
			tmDisplayBanner(banners, "ad-box-vid", 300, 250);
		}

		for (i = 0; i < banners.length; i++) {
			if (banners[i].width === 728 && banners[i].height === 90) {
				// Swap out the top leaderboard ads.
				$('#top-leaderboard').hide();
				$('#top-leaderboard-video').show();
				tmDisplayBanner(banners, "top-leaderboard-video", 728, 90);
			}
		}
	},

	// adComplete is called by Flash and triggers ad changes.
	adComplete : function () {
		var controller = $('div.media-wrap div.controller');

		// Only do 300x250 ad switching in a video hero.
		if (controller.length > 0) {
			controller.children().show();
			$('#ad-box').hide();
		}
	},

	trackMorpheusDART : function (src) {
		var iframe = '<iframe src=' + src + ' width="1" height="1" frameborder="0"></iframe>';
		HISTORY.body.append(iframe);
	},

	// This function is called by Flash to get the text entitlement contents.
	getTextEntitlement : function () {
		var entitlement = $('span.text-entitlement:eq(0) > a').text();
		return entitlement.length > 0 ? entitlement : 'NULL';
	},

	// This function is called by Flash to get Omniture account data.
	getOmnitureAccount : function () {
		if (omniture.s_account || omnitureSuiteName) {
			return omniture.s_account.length > 0 ? s_account : (omnitureSuiteName.length > 0 ? omnitureSuiteName : 'NULL');
		}
		return 'NULL';
	},

	// This function is called by Flash to get the Omiture "ord" variable.
	getOrd : function () {
		return ord;
	}
}

jQuery(function($) {
    //todo: remove 
    try{
        var console = new Object();
        console.log = function(args){}
        console.info = function(args){}
    }catch(error){}
	HISTORY.videoPlayer.init();
});
