﻿/////////////////////////////////////////////////////////////////////////// GLOBALS
var g_sVideoID					= "";

var	g_aTabs						= new Array();
var	g_oCommentViewer			= null;

var g_sTabsContainerId			= "eRowTabsContainer";
var	g_sTabsContentContainerId	= "eCellContentContainer";
var g_sTabsContentTitleId		= "eCellTabContentTitle";
var g_sInfoBoxContainerId		= "eCellInfoBox";
var g_sIconsContainerId			= "eCellBookmarks";

var g_aBookmarkSites			= new Array(// Link, %0 is replaced with url of this page, %1 with title						// Site name	// Site img
											["http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=%0&Title=%1",	"BlinkList",	"images/bookmark/blinklist.gif"],
											["http://del.icio.us/post?url=%0&title=%1",											"Del.icio.us",	"images/bookmark/delicious.gif"],
											["http://digg.com/submit?phase=2&url=%0&title=%1",									"Digg",			"images/bookmark/digg.gif"],
											//["http://cgi.fark.com/cgi/fark/submit.pl?new_url=%0",								"Fark",			"images/bookmark/fark.gif"],
											["http://www.furl.net/storeIt.jsp?u=%0&t=%1",										"Furl",			"images/bookmark/furl.gif"],
											["http://ma.gnolia.com/beta/bookmarklet/add?url=%0&title=%1",						"Ma.gnolia",	"images/bookmark/magnolia.gif"],
											["http://www.newsvine.com/_tools/seed&save?u=%0&h=%1",								"Newsvine",		"images/bookmark/newsvine.gif"],
											["http://reddit.com/submit?url=%0&title=%1",										"Reddit",		"images/bookmark/reddit.gif"],
											["http://www.simpy.com/simpy/LinkAdd.do?href=%0&title=%1",							"Simpy",		"images/bookmark/simpy.gif"],
											["http://www.spurl.net/spurl.php?url=%0&title=%1",									"Spurl",		"images/bookmark/spurl.gif"],
											["http://technorati.com/cosmos/search.html?url=%0",									"Technorati",	"images/bookmark/technorati.gif"],
											["http://myweb2.search.yahoo.com/myresults/bookmarklet?u=%0&t=%1",					"Yahoo",		"images/bookmark/yahoo.gif"],
											["http://www.stumbleupon.com/submit?url=%0&title=%1",								"Stumble Upon",	"images/bookmark/stumble.gif"]
											);



/////////////////////////////////////////////////////////////////////////// EVENT HANDLERS
function OnPageLoad(oEvent)
{
	// Retrieve tab content divs for later usage (done because mozilla counts textNodes)
	var eCellTabContentContainer = window.document.getElementById(g_sTabsContentContainerId);
	for (var nChildIndex = 0; nChildIndex < eCellTabContentContainer.childNodes.length; nChildIndex++)
	{
		var eCurrentChild = eCellTabContentContainer.childNodes[nChildIndex];
		if (eCurrentChild.nodeName.toLowerCase() == "div") g_aTabContentDivs.push(eCurrentChild);
	}
	
	// Create tabs
	g_aTabs.push(new CTab("comment", "Comments"));
	g_aTabs.push(new CTab("join trooker", "", JoinTrooker));
	var eCellTabsSpacer			= CreateElement(window.document.getElementById(g_sTabsContainerId), "td", "CellTabEnd");
	eCellTabsSpacer.style.width	= "100%";
	eCellTabsSpacer.innerHTML	= "&nbsp;";
	
	// Create comment viewer
	g_oCommentViewer			= new CCommentViewer(g_aTabContentDivs[0], g_sVideoID);
	
	// Initialize the xml-loading cascade (done this way because of asynchronous nature)
	LoadTagCloud();
	
	// Misc.
	g_aTabs[0].Select();
	window.document.getElementById("eTextFindIt").value = "";
	window.document.getElementById("eTextFindIt").focus();
}

function OnCheckAllSources()
{
	var eCellParent = window.document.getElementById("eCellSources");
	
	// Go through all child nodes, make sure it's the proper input element and select it
	for (var nChildIndex = 0; nChildIndex < eCellParent.childNodes.length; nChildIndex++)
	{
		var eCurrentChild = eCellParent.childNodes[nChildIndex];
		if (eCurrentChild.nodeName.toLowerCase() == "input" && eCurrentChild.type == "checkbox")
		{
			eCurrentChild.checked			= "checked";
			eCurrentChild.nextSibling.src	= eCurrentChild.nextSibling.src.replace("0.png", "1.png");
		}
	}
}

function OnFindItKeyDown(oEvent)
{
	if (typeof(oEvent) == "undefined")	oEvent		= window.event;
	if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
	if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
	
	if (oEvent.keyCode == 13) FindIt();
}

function OnClickSource(oEvent)
{
	if (typeof(oEvent) == "undefined")	oEvent		= window.event;
	if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
	if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
	
	if (eSrcElement.previousSibling.checked)
	{
		eSrcElement.previousSibling.checked = false;
		eSrcElement.src = eSrcElement.src.replace("1.png", "0.png");
	}
	else
	{
		eSrcElement.previousSibling.checked = true;
		eSrcElement.src = eSrcElement.src.replace("0.png", "1.png");
	}		
}

function OnSourceChecked(oEvent)
{
	if (typeof(oEvent) == "undefined")	oEvent		= window.event;
	if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
	if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
	
	if (eSrcElement.checked)	eSrcElement.nextSibling.src = eSrcElement.nextSibling.src.replace("0.png", "1.png");
	else						eSrcElement.nextSibling.src = eSrcElement.nextSibling.src.replace("1.png", "0.png");
}



/////////////////////////////////////////////////////////////////////////// LINKS
function FindIt()
{
	var eInputFindIt		= window.document.getElementById("eTextFindIt");
	var sURL				=  g_sURLHome + "q=" + eInputFindIt.value;
	
	if (eInputFindIt.value == "")
	{
		alert("Please enter some keywords before submitting a search.");
		eInputFindIt.focus();
		return;
	}
	
	var sSourcesArgs = "";
	if (window.document.getElementById("eCheckYouTube").checked)		sSourcesArgs += "&yt=1";
	else																sSourcesArgs += "&yt=0";
	
	if (window.document.getElementById("eCheckMySpace").checked)		sSourcesArgs += "&mp=1";
	else																sSourcesArgs += "&mp=0";
	
	if (window.document.getElementById("eCheckLiveVideo").checked)		sSourcesArgs += "&lv=1";
	else																sSourcesArgs += "&lv=0";
	
	if (window.document.getElementById("eCheckMetaCafe").checked)		sSourcesArgs += "&mc=1";
	else																sSourcesArgs += "&mc=0";
	
	if (window.document.getElementById("eCheckDailyMotion").checked)	sSourcesArgs += "&dm=1";
	else																sSourcesArgs += "&dm=0";
	
	if (sSourcesArgs.indexOf("=1") == (-1))
	{
		alert("You must select at least one source to search.");
		return;
	}
	
	window.location.href	= sURL + sSourcesArgs;
}

function JoinTrooker()
{
	window.location.href	= g_sURLHome + "r=1";
}

function LogIn()
{
	window.location.href	= g_sURLHome + "l=1";
}



/////////////////////////////////////////////////////////////////////////// REQUESTS
function LoadTagCloud()
{
	var sURL	= g_sTagCloudURL;
	XMLHTTPRequest(sURL, PopulateTagCloud);
}

function LoadInfoBox()
{
	var sURL	= g_sSetInfoURL + g_sVideoID;
	XMLHTTPRequest(sURL, PopulateInfoBox);
}

function LoadComments()
{
	g_oCommentViewer.LoadComments();
}



/////////////////////////////////////////////////////////////////////////// XMLHTTP CALLBACKS
function PopulateTagCloud()
{
	if (g_oXMLHTTPRequestObject.readyState != 4) return;	// Cannot do anything if transmission is not complete
	
	window.document.getElementById("eCellTagCloud").innerHTML = g_oXMLHTTPRequestObject.responseText;
	
	LoadInfoBox();
}

function PopulateInfoBox()
{
	if (g_oXMLHTTPRequestObject.readyState != 4) return;	// Cannot do anything if transmission is not complete
	
	if (g_oXMLHTTPRequestObject.responseXML != null && g_oXMLHTTPRequestObject.responseXML.firstChild != null)
	{
		var oRequestedXML = g_oXMLHTTPRequestObject.responseXML.firstChild.getElementsByTagName("Table")[0];
		
		// Create HTML elements for InfoBox
		if (g_bIE)
		{
			var sTitle		= oRequestedXML.getElementsByTagName("Title")[0].text;
			var sAuthor		= oRequestedXML.getElementsByTagName("PublishedBy")[0].text;
			var sViews		= oRequestedXML.getElementsByTagName("ViewCount")[0].text;
			var sRawDate	= oRequestedXML.getElementsByTagName("Date_Created")[0].text;
		}
		else
		{
			var sTitle		= oRequestedXML.getElementsByTagName("Title")[0].textContent;
			var sAuthor		= oRequestedXML.getElementsByTagName("PublishedBy")[0].textContent;
			var sViews		= oRequestedXML.getElementsByTagName("ViewCount")[0].textContent;
			var sRawDate	= oRequestedXML.getElementsByTagName("Date_Created")[0].textContent;
		}
		sAuthor				= "by " + sAuthor;
		sViews				= "views: " + sViews;
		var sDate			= "updated: " + sRawDate.substr(0, sRawDate.indexOf("T")) + " " + sRawDate.substr(sRawDate.indexOf("T") + 1, 8);
		
		
		var eCellInfoContainer	= window.document.getElementById(g_sInfoBoxContainerId);
		var eTableInfoBox		= CreateElement(eCellInfoContainer, "table");
		eTableInfoBox.style.width	= "100%";
		var eTBodyInfoBox		= CreateElement(eTableInfoBox, "tbody");
		
		var eRowTitle			= CreateElement(eTBodyInfoBox, "tr");
		var eCellTitle			= CreateElement(eRowTitle, "td", "CellInfoTitle");
		eCellTitle.colSpan		= 3;
		eCellTitle.textContent	= sTitle;
		eCellTitle.innerText	= sTitle;
		
		var eRowInfo			= CreateElement(eTBodyInfoBox, "tr");
		var	eCellAuthor			= CreateElement(eRowInfo, "td", "CellInfoAuthor");
		eCellAuthor.textContent	= sAuthor;
		eCellAuthor.innerText	= sAuthor;
		var eCellDate			= CreateElement(eRowInfo, "td", "CellInfoDate");
		eCellDate.textContent	= sDate;
		eCellDate.innerText		= sDate;
		var eCellViews			= CreateElement(eRowInfo, "td", "CellInfoViews");
		eCellViews.textContent	= sViews;
		eCellViews.innerText	= sViews;
		
		
		// Create HTML elements for Bookmarks
		var eCellIconsContainer	= window.document.getElementById(g_sIconsContainerId);
		var eTableIcons			= CreateElement(eCellIconsContainer, "table");
		eTableIcons.style.width	= "100%";
		var eTBodyIcons			= CreateElement(eTableIcons, "tbody");
		var eRowIcons			= CreateElement(eTBodyIcons, "tr");
		
		for (var nBookmarkIndex = 0; nBookmarkIndex < g_aBookmarkSites.length; nBookmarkIndex++)
		{
			var eCellBookmark	= CreateElement(eRowIcons, "td");
			eCellBookmark.style.textAlign	= "center";
			
			var sFinalURL		= g_aBookmarkSites[nBookmarkIndex][0].replace("%0", window.location.href).replace("%1", sTitle);
			
			var eLinkBoomark	= CreateElement(eCellBookmark, "a");
			eLinkBoomark.href	= sFinalURL;
			var eImgBookmark	= CreateElement(eLinkBoomark, "img");
			eImgBookmark.border	= "0";
			eImgBookmark.src	= g_aBookmarkSites[nBookmarkIndex][2];
			eImgBookmark.title	= "Share this set on " + g_aBookmarkSites[nBookmarkIndex][1];
		}
	}
	
	
	LoadComments();
}
