var isMSNVideoClickEnabled = false;

function updateVideoData(videoItem, hlCatId)
{
	var manualLinksData = window["vidManualLinks_" + hlCatId];
	for(var i = 0; i < manualLinksData.length; i++)
	{
		var link = manualLinksData[i];
		if(link.uuid.toLowerCase() == videoItem.uuid.$.toLowerCase())
		{
			link.title = link.title ? link.title : videoItem.title.$;
			link.imgUrl = link.imgUrl ? link.imgUrl : getThumbnailUrl(videoItem, hlCatId);
		}
	}
}

function renderManualVideoLinks(hlCatId)
{
	var manualLinksDiv = document.getElementById("vidManualLinks_" + hlCatId);
	var manualLinksData = window["vidManualLinks_" + hlCatId];
	var innerHTML = "";
	for(var i = 0; i < manualLinksData.length; i++)
	{
		var link = manualLinksData[i];
		innerHTML += "<div class='videoItem'>";
		innerHTML += "	<div class='image'><img src='" + link.imgUrl + "' alt='" + link.title + "' /></div>";
		innerHTML += "	<div class='description'>";
		innerHTML += "		" + link.title;
		innerHTML += "		<span class='playIcon'>Play</span>";
		innerHTML += "	</div>";
		innerHTML += "</div>";
	}
	
	manualLinksDiv.className = "loaded";
	manualLinksDiv.innerHTML = innerHTML;

	$(".videoManualPlayer .videoItem").click(function(){
		var indexPosition = $(this).parent().find(".videoItem").index(this);
		document.getElementById("MSNVideoPlayer_pobj").vidPlayId(manualLinksData[indexPosition].uuid);
	});
}

/**
 * Returns the correct thumbnail url to use for the specified video.  Will return the image with the 2009 formatCode th
 * Author: Domagoj Filipovic
 * Date: 28/04/08
 * Notes:	The image with a formatCode of "2009" is the 300 x 400 image supplied by ninemsn and should be used in
 *			conjunction with the ninemsn resizer if found.  If not found, the "2007" format should be used.  The "2007"
 *			has 2 variants:
 *				1. Ninemsn supplied image (this should be passed through the ninemsn resizer)
 *				2. MSN resized thumbnail (this SHOULD NOT be passed through the ninemsn resizer).
 */
function getThumbnailUrl(videoData, hlCatId)
{
	var videoThumbnailSrc = "";
	var resizerUrl = "";
	var strImageSize = window["strImageSize_" + hlCatId];
		
	if (!strImageSize)
	{
		resizerUrl = "http://images.ninemsn.com.au/resizer.aspx?width=92&url=";
	}
	else
	{
		resizerUrl = "http://images.ninemsn.com.au/resizer.aspx?width=" + strImageSize + "&url=";
	}
	
	if(videoData && videoData.files && videoData.files.file)
	{
		for(var i = 0; i < videoData.files.file.length; i++)
		{
			var imgFile = videoData.files.file[i];
			if(imgFile.$formatCode == "2001")
			{//if 2001 format is found, use it with the resizer
				videoThumbnailSrc = resizerUrl + escape(imgFile.uri.$);
				break;
			}
			else if((imgFile.$formatCode == "2009" || imgFile.$formatCode == "2007") && (imgFile.uri.$.toLowerCase().indexOf("catalog.video.msn.com") == -1))
			{//if 2007 or 2009 format is found and the location is on netshow, use it with the resizer
				videoThumbnailSrc = resizerUrl + escape(imgFile.uri.$);
				break;
			}
		}

		if(!videoThumbnailSrc && videoData.providerId && videoData.providerId.$)
		{//couldn't find suitable thumbnail, construct it manualy
			videoThumbnailSrc = resizerUrl + buildVideoPath(videoData);
		}
	}

	return videoThumbnailSrc;
}

//Gets additional information for the video logo once it is loaded
Msn.Video.OnVideoTitle = function(text, id)
{
	//first, set the title for the player
	if(text && id)
	{
		$("#" + id + "_t").html(text);
		$("#" + id + "_mail").html("E-mail '" + text + "' video to your friends and family");
		$("#" + id + "_mail").show();
		if(text == "Advertisement")
		{
			$("#" + id + "_mail").hide();
		}
		if(!isMSNVideoClickEnabled)
		{
			$("#" + id + "_mail").click(function()
			{
				var vidObj = document.getElementById(id + "_pobj");
				if(vidObj)
				{
					var link = (window.location.protocol + "//" + window.location.hostname + window.location.pathname + window.location.search).toLowerCase();

					//first, get rid of any existing videoid QS parameter
					var startVideoId = link.indexOf("videoid=");
					if(startVideoId > -1)
					{
						var endVideoId = link.indexOf("&", startVideoId);
						var qsToRemove = link.substring(startVideoId - 1, (endVideoId == -1 ? link.length : endVideoId));
						link = link.replace(qsToRemove, "");
						
						//Next, fix the url to have a leading ? before any & in the QS
						var firstInstanceOfAnd = link.indexOf("&");
						var firstInstanceOfQuestionMark = link.indexOf("?");
						if(firstInstanceOfAnd < firstInstanceOfQuestionMark || (firstInstanceOfQuestionMark == -1 && firstInstanceOfAnd > -1))
						{
							link = link.replace("&", "?");
						}
					}
					
					//Construct new URL
					link = escape(link + (link.indexOf("?") > -1 ? "&" : "?") + "videoid=" + vidObj.vidGetId());
					window.location = "mailto:?subject=" + vidObj.vidGetTitle() + "&body=This video appears in news.ninemsn.com.au and I thought you should see it. " + link + "%0A%0AIf you cannot click on the link above, copy and paste the entire address below into your web browser." + link + "%0A%0AFor all the latest news around the globe, check out ninemsn.%0Ahttp%3A//ninemsn.com.au";
				}
			});
			isMSNVideoClickEnabled = true;
		}
	}

	var vidObj = document.getElementById(id + "_pobj");
	if(vidObj)
	{
		//fetch the details for this video to get the source
		$.getJSON("http://edge1.catalog.video.msn.com/videoByUuid.aspx?uuid=" + vidObj.vidGetId() + "&responseEncoding=json&callbackName=?", function(videoData){
			var logoImg = document.getElementById("srcManVidImgLogo");
			if(logoImg)
			{
				logoImg.src = "http://img.video.msn.com/video/i/src/" + videoData.source.$ + ".gif";
				logoImg.alt = videoData.source.$friendlyName;
				logoImg.style.visibility = "hidden";
			}

			//next get the logoUrl if one exists
			$.getJSON("http://edge1.catalog.video.msn.com/getcontentsource.aspx?csid=au_ninemsn&name=" + videoData.source.$ + "&responseEncoding=json&callbackName=?", function(results){
				var logoImg = document.getElementById("srcManVidImgLogo");
				if(logoImg)
				{
					if(results && results.contentSource && results.contentSource.$logoUrl && results.contentSource.$logoUrl != "http://img.video.msn.com/video/i/src/AU_ninemsn.gif")
					{//get image src from results. If it contains anything, update the image. Ignore the default ninemsn image
						logoImg.src = results.contentSource.$logoUrl;
						logoImg.alt = results.contentSource.$friendlyName;
					}

					//Make image visable
					logoImg.style.visibility = "visible";
				}
			});
		});
	}
}

//Returns the value for a given key (qsKey) in the query string
function getQueryParam(qsKey)
{
	var queryParams = window.location.href.split("?");

	if (queryParams.length<2)
	{
		return null;
	}

	var paramPairs = queryParams[1].split("&");
	for (var i = 0; i < paramPairs.length; i++)
	{
		var keyValue = paramPairs[i].split("=");
		if (keyValue[0].toUpperCase() == qsKey.toUpperCase())
		{
			return keyValue[1];
		}
	}

	return null;
}