/*
* $Id: videoportal.js 23032 2009-04-06 09:47:31Z jhickford $
*/
$(document).ready(function(){
	var createItem = function(text, elemType, className){
		var ttElem = $(document.createElement(elemType));
		ttElem.className = className;
		ttElem.text(text);
		return ttElem;
	}

	var generateTooltip = function(elem){
		var selfElem = $(elem);
		var promoText = selfElem.children(".vp-promo-text");
		
		var vpTitle = promoText.children(".vp-promo-title").html();
		var vpTime = promoText.children(".vp-promo-subtitle").children(".vp-promo-subtitle-time").html();
		var vpSubtitle = promoText.children(".vp-promo-subtitle").children(".vp-promo-subtitle-title").html();
		var vpDescription = selfElem.children(".vp-tooltip-text").html();
		
		vpTitle = createItem(vpTitle, "strong", "title");
		vpTime = createItem(vpTime, "b", "time");
		vpSubtitle = createItem(vpSubtitle, "em", "subtitle");
		vpDescription = createItem(vpDescription, "span", "description");	
		
		var tooltipElem = $(document.createElement("p"))
				.append(vpTitle)
				.append(vpTime)
				.append(vpSubtitle)
				.append(vpDescription)[0];
				
		return tooltipElem;
	}

	$(".tooltip").each(function(){
		var self = this;
		$(this).tooltip({
			track: true,
			delay: 400,
			showURL: false,
			bodyHandler: function(){
				if (!self.tooltip) self.tooltip = generateTooltip(self);
				return self.tooltip;
			}
		});
	});
	
	$(".vp-promo-hover").each(function(){
		var self = this;
		var selfElem = $(self);
		var promoArea = selfElem.children();
		var promoImage = selfElem.children(".vp-promo-image");
		$(promoArea).hover(function () {
	      $(promoImage).addClass("vp-promo-image-hover");
	    }, function () {
	      $(promoImage).removeClass("vp-promo-image-hover");
	    });
	});
	
	$("#vp-perpage-videotabs-list").children("li").each(function(){
		var itemClass = "listitem-hover";
		var self = $(this);
		var selfhref = self.children("a");
		self.hover(
			function(){	self.addClass(itemClass)},
			function(){ self.removeClass(itemClass)}
		);
		self.hover(
			function(){	selfhref.addClass(itemClass)},
			function(){ selfhref.removeClass(itemClass)}
		);
	});
	
	// Create social bookmark links
	try {
		if($('#vp-bookmark-links').length > 0) {
			var video_url =  location.href;
			video_url = video_url.replace('/video/truveo/','/video/')
			$('#vp-reddit-url').attr({ href: "http://reddit.com/submit?url=" + video_url });
			$('#vp-stumbleupon-url').attr({ href: "http://www.stumbleupon.com/submit?url=" + video_url });
			$('#vp-facebook-url').attr({ href: "http://www.facebook.com/share.php?u=" + video_url });
			$('#vp-delicious-url').attr({ href: "http://del.icio.us/post?url=" + video_url });
			$('#vp-digg-url').attr({ href: "http://digg.com/submit?phase=2&url=" + video_url });
		}
	}
	catch(err){}
	
	// read the two cookies, the variables will be null if no cookie exisits
	var hub_type = readCookie("hubType");
	var hub_name = readCookie("hubName");
	
	
	
	// make sure both cookies are set
	if (hub_name && hub_type) {
		hub_name = hub_name.replace(/\/$/,'');
		hub_name = hub_name.split(/\//).pop();
		
		// find  the breadcrumb div with both the correct hub type and hub name
		var crumb = $("#vp-breadcrumb span." + hub_type + "." + hub_name)
		// ensure 1 and only 1 breadcrumb is found and that the selected element is not empty
		if(crumb.length == 1 && ! crumb.is(':empty')) {
			// hide all breadcrumbs, then show the selected breadcrumb
			$("#vp-breadcrumb span").hide();
			crumb.show();
		}
	}
	$('.js-hidden').show(); 
});


var addBCListeners = function addBrightcoveListeners(_BCModMenu) {
	$("#vp-perpage-videotabs-list .showBCEmail").click(function() {
		_BCModMenu.showMenuPage("Email");
	});
	$("#vp-perpage-videotabs-list .showBCEmbed").click(function() {
		_BCModMenu.showMenuPage("Embed");
	});
	$("#vp-perpage-videotabs-list .showBCLink").click(function() {
		_BCModMenu.showMenuPage("Link");
	});
	$("#vp-perpage-videotabs-list .vp-clipinfo").click(function() {
		_BCModMenu.closeMenuPage();
		$('.vp-perpage-share').hide();
		$('.vp-perpage-clipinfo').show('fast');
	});
}

var addSharedListeners = function addSharedListeners() {
	$("#vp-perpage-videotabs-list .vp-clipinfo").click(function() {
		$('.vp-perpage-share').hide();
		$('.vp-perpage-clipinfo').show('fast');
	});
	$("#vp-perpage-videotabs-list .vp-share").click(function() {
		$('.vp-perpage-clipinfo').hide();
		$('.vp-perpage-share').show('fast');
	});
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function backLink() {
	history.go(-1);
}

function newWindow(ids) {
	  $(ids).attr('target','_parent');
}
