// Callback interface
// Callback interface

var yhost = window.external;

function LaunchPlugin(pID, pName, pVersion, pLocale, pUsers)
{
	// alert("Launch or Open " + pID);
	yhost.LaunchPlugin (pID, pName, pVersion, pLocale, pUsers);
}

function StopPlugin(pID)
{
	alert("Stop " + pID);
	// yhost.StopPlugin (pID);
}

function RemovePlugin(pID)
{
	// alert("Remove " + pID);
	//yhost.RemovePlugin (pID, "version", "en-us");
	//LaunchPlugin(pID);
}

function GetType()
{
	SelectedType = yhost.GetType();
	// SelectedType = "content-tab";
	// SelectedType = "side-panel";
}

function GetSelectedTab()
{
	SelectedTab = yhost.GetSelectedTab();
	// SelectedTab = "published";
	// SelectedTab = "installed";
}

function GetColor()
{
	// SelectedColor = yhost.GetColor (ColorId);
}

function getXML()
{
	var xmlString = yhost.GetData (SelectedTab, SelectedType);
	var xmlObject=new ActiveXObject("Microsoft.XMLDOM");
	xmlObject.async="false";
	xmlObject.loadXML(xmlString);

	/*
	if (SelectedTab == "published")
	{
		var xmlObject = document.all("testData").XMLDocument;
	}
	else
	{
		var xmlObject = document.all("testData2").XMLDocument;
	} // end of if (SelectedTab == "published")
	*/

	if (SelectedType == "content-tab")
	{
		//alert("we are here.");
        var dataFirstNodes = xmlObject.getElementsByTagName("content-tabs");
	}
	else
	{
		var dataFirstNodes = xmlObject.getElementsByTagName("sidepanel-plugins");
	} // end of if (SelectedType == "content-tab")

	var dataFirstNodeFirst = dataFirstNodes[0];
	var dataSecondNode = dataFirstNodeFirst.getElementsByTagName("plug-in");

	if (SelectedTab == "published")
	{
		published2Note = dataSecondNode;
	}
	else
	{
		installed2Note = dataSecondNode;
	} // end of if (SelectedTab == "published")

	return true;
}

function calculateLeftOfGetItNowOrTrashCanImg()
{
	if (SelectedTab == "published")
	{
		var eachRowIDObject = document.getElementById("eachRowID");
		var getItNowSpanObject = document.getElementById("getItNowSpan");
		var leftOfGetItNowOrTrashCanImg = eachRowIDObject.offsetWidth - getItNowSpanObject.offsetWidth;
	}
	else
	{
		var eachRowIDObject = document.getElementById("eachRowID");
		var trashCanImgObject = document.getElementById("trashCanImg");
		var leftOfGetItNowOrTrashCanImg = eachRowIDObject.offsetWidth - trashCanImgObject.offsetWidth;
	} // end of if (SelectedTab == "published"))

	return leftOfGetItNowOrTrashCanImg;
}

function attachPluginInfoToDetailDiv(showThisID)
{
	var i, idData, getThisName, getThisDescription, getThisPreview;

	if (SelectedTab == "published")
	{
		var secondNodes = published2Note;
	}
	else
	{
		var secondNodes = installed2Note;
	}

	for (i=0; i<secondNodes.length; i++)
	{
		var eachOfSecondNodes = secondNodes[i];
		idData = eachOfSecondNodes.getElementsByTagName("Id")[0].firstChild.nodeValue;
		if (idData == showThisID)
		{

			if (eachOfSecondNodes.getElementsByTagName("Name")[0] != null && eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild.data != null)
				getThisName = eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild.data;

			if (eachOfSecondNodes.getElementsByTagName("Description")[0] != null && eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild.data != null)
				getThisDescription = eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild.data;

			if (eachOfSecondNodes.getElementsByTagName("Preview")[0] != null && eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild.data != null)
				getThisPreview = eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild.data;

			i = secondNodes.length;
		} // end of if (idData == showThisID)
	} // end of for (i=0; i<secondNodes.length; i++)

	var previewImage = new Image();
	previewImage.src = getThisPreview;
	previewImage.border = "0";
	previewImage.id = "previewIMG";
	previewImage.style.width = "60";
	previewImage.style.height = "80";
	previewImage.style.padding = "0";
	previewImage.style.margin = "0";
	previewImage.style.position = "absolute";
	previewImage.style.left = "10px";
	previewImage.style.top = "16px";
	previewImage.style.zIndex = "3";

	var pluginTitleText = document.createTextNode(getThisName);
	var pluginTitleTextDiv = document.createElement("span");
	pluginTitleTextDiv.id = "pluginTitleTextDivID";
	pluginTitleTextDiv.style.backgroundColor = highlightBKgroundColor;
	pluginTitleTextDiv.style.border = "0px solid #000000";
	pluginTitleTextDiv.style.position = "relative";
	pluginTitleTextDiv.style.padding = "0";
	pluginTitleTextDiv.style.margin = "0";
	pluginTitleTextDiv.style.zIndex = "3";
	pluginTitleTextDiv.style.whiteSpace = "normal";
	pluginTitleTextDiv.style.fontWeight = "bold";

	pluginTitleTextDiv.appendChild(pluginTitleText);

	var pluginDescriptionText = document.createTextNode(getThisDescription);
	var pluginDescriptionTextDiv = document.createElement("div");
	pluginDescriptionTextDiv.id = "pluginDescriptionTextID";
	pluginDescriptionTextDiv.style.backgroundColor = highlightBKgroundColor;
	pluginDescriptionTextDiv.style.border = "0px solid #000000";
	pluginDescriptionTextDiv.style.position = "relative";
	pluginDescriptionTextDiv.style.padding = "0";
	pluginDescriptionTextDiv.style.marginTop = "10px";
	pluginDescriptionTextDiv.style.marginBottom = "0";
	pluginDescriptionTextDiv.style.marginLeft = "0";
	pluginDescriptionTextDiv.style.marginRight = "0";
	pluginDescriptionTextDiv.style.zIndex = "3";
	pluginDescriptionTextDiv.style.whiteSpace = "normal";

	pluginDescriptionTextDiv.appendChild(pluginDescriptionText);

	var detailTitleDescriptionDiv = document.createElement("div");
	detailTitleDescriptionDiv.id = "detailTitleDescriptionDivID";
	detailTitleDescriptionDiv.style.backgroundColor = highlightBKgroundColor;
	detailTitleDescriptionDiv.style.border = "0px solid #000000";
	detailTitleDescriptionDiv.style.position = "absolute";
	detailTitleDescriptionDiv.style.left = "90";
	detailTitleDescriptionDiv.style.top = "16";
	detailTitleDescriptionDiv.style.paddingLeft = "0";
	detailTitleDescriptionDiv.style.paddingRight = "10px";
	detailTitleDescriptionDiv.style.paddingTop = "0";
	detailTitleDescriptionDiv.style.paddingBottom = "10px";
	detailTitleDescriptionDiv.style.margin = "0";
	detailTitleDescriptionDiv.style.zIndex = "3";
	detailTitleDescriptionDiv.style.whiteSpace = "normal";

	detailTitleDescriptionDiv.appendChild(pluginTitleTextDiv);
	detailTitleDescriptionDiv.appendChild(pluginDescriptionTextDiv);
	
	var detailDivObject = document.getElementById("detailDiv");
	if (document.getElementById("previewIMG"))
	{
		var pointerToPreviewIMG = document.getElementById("previewIMG");
		var parentOfPreviewIMG = pointerToPreviewIMG.parentNode;
		parentOfPreviewIMG.removeChild(pointerToPreviewIMG);
		// detailDivObject.item(0).removeChild(eraseThisNode);
	}

	if (document.getElementById("detailTitleDescriptionDivID"))
	{
		var pointerToDetailTitleDescriptionDiv = document.getElementById("detailTitleDescriptionDivID");
		var parentOfDetailTitleDescriptionDiv = pointerToDetailTitleDescriptionDiv.parentNode;
		parentOfDetailTitleDescriptionDiv.removeChild(pointerToDetailTitleDescriptionDiv);
		// detailDivObject.item(0).removeChild(eraseThisNode);
	}

	detailDivObject.appendChild(previewImage);
	detailDivObject.appendChild(detailTitleDescriptionDiv);
} // end of function

function GetData()
{
	var i, idData, localeData, versionData, nameData, descriptionData, iconData, previewData, ratingData, usersData, installedData, openData;

	if (SelectedTab == "published")
	{
		var secondNodes = published2Note;
	}
	else
	{
		var secondNodes = installed2Note;
	}
	
	var listDivNode = document.getElementById("listDiv");
	var arrayOfListDivChildNode = listDivNode.childNodes

	if (document.getElementById("eachRowID"))
	{
		var listDivKids = listDivNode.childNodes;
		for (m=listDivKids.length-1; m > 0; m--)
		{
			if (listDivKids[m].id == "eachRowID")
			{
				listDivNode.removeChild(listDivKids[m]);
			}
		}
	} // end of if (document.getElementById("eachRowID"))

	for (i=0; i<secondNodes.length; i++)
	{
		var eachOfSecondNodes = secondNodes[i];

		if (eachOfSecondNodes.getElementsByTagName("Id")[0] != null && eachOfSecondNodes.getElementsByTagName("Id")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Id")[0].firstChild.data != null)
			idData = eachOfSecondNodes.getElementsByTagName("Id")[0].firstChild.nodeValue;
		
		if (eachOfSecondNodes.getElementsByTagName("Locale"[0]) != null && eachOfSecondNodes.getElementsByTagName("Locale")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Locale")[0].firstChild.data != null)
			localeData = eachOfSecondNodes.getElementsByTagName("Locale")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Version")[0] != null && eachOfSecondNodes.getElementsByTagName("Version")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Version")[0].firstChild.data != null)
			versionData = eachOfSecondNodes.getElementsByTagName("Version")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Name")[0] != null && eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild.data != null)
			nameData = eachOfSecondNodes.getElementsByTagName("Name")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Description")[0] != null && eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild.data != null)
			descriptionData = eachOfSecondNodes.getElementsByTagName("Description")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Icon")[0] != null && eachOfSecondNodes.getElementsByTagName("Icon")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Icon")[0].firstChild.data != null)
			iconData = eachOfSecondNodes.getElementsByTagName("Icon")[0].firstChild.data;
			
		if (eachOfSecondNodes.getElementsByTagName("Preview")[0] != null && eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild.data != null)
			previewData = eachOfSecondNodes.getElementsByTagName("Preview")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Rating")[0] != null && eachOfSecondNodes.getElementsByTagName("Rating")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Rating")[0].firstChild.data != null)
			usersData = eachOfSecondNodes.getElementsByTagName("Rating")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Users")[0] != null && eachOfSecondNodes.getElementsByTagName("Users")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Users")[0].firstChild.data != null)
			usersData = eachOfSecondNodes.getElementsByTagName("Users")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Installed")[0] != null && eachOfSecondNodes.getElementsByTagName("Installed")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Installed")[0].firstChild.data != null)
			installedData = eachOfSecondNodes.getElementsByTagName("Installed")[0].firstChild.data;
		
		if (eachOfSecondNodes.getElementsByTagName("Open")[0] != null && eachOfSecondNodes.getElementsByTagName("Open")[0].firstChild != null && eachOfSecondNodes.getElementsByTagName("Open")[0].firstChild.data != null)
			openData = eachOfSecondNodes.getElementsByTagName("Open")[0].firstChild.data;

		var iconImageIdPrefix = "pIcon:";
		var iconImage = new Image();
		iconImage.src = iconData;
		iconImage.border = "0";
		iconImage.id = iconImageIdPrefix + idData;
		iconImage.style.width = "28";
		iconImage.style.height = "28";
		iconImage.style.padding = "0";
		iconImage.style.marginLeft = "10";
		iconImage.style.marginRight = "10";
		iconImage.style.marginTop = "4";
		iconImage.style.marginBottom = "4";
		iconImage.style.position = "absolute";
		iconImage.style.left = "0px";
		iconImage.style.top = "0px";
		iconImage.style.zIndex = "3";
		
		var deletePluginImg = new Image();
		deletePluginImg.src = "http://us.i1.yimg.com/us.yimg.com/i/us/msg/voice/el/aro_incoming_1.gif";
		deletePluginImg.style.backgroundColor = neturalBKgroundColor;
		deletePluginImg.border = "0";
		deletePluginImg.id = "trashCanImg";
		deletePluginImg.style.width = "16";
		deletePluginImg.style.height = "16";
		deletePluginImg.style.padding = "10";
		deletePluginImg.style.margin = "0";
		deletePluginImg.style.position = "absolute";
		deletePluginImg.style.right = "0px";
		deletePluginImg.style.top = "0px";

		var deletePluginLink = document.createElement("a");
		deletePluginLink.href = "javascript: RemovePlugin('" + idData + "');";
		deletePluginLink.appendChild(deletePluginImg);

		var getItNowText = document.createTextNode("Get it now");
		var getItNowLink = document.createElement("a");
		getItNowLink.className = "GetItNowCSS";
		getItNowLink.href = "javascript: LaunchPlugin('" + idData + "', '" + nameData + "', '" + versionData + "', '" + localeData + "', '" + usersData + "');";
		getItNowLink.appendChild(getItNowText);
		
		var getItNowTextSpan = document.createElement("span");
		getItNowTextSpan.id = "getItNowSpan";
		getItNowTextSpan.style.backgroundColor = neturalBKgroundColor;
		getItNowTextSpan.style.position = "absolute";
		getItNowTextSpan.style.margin = "0";
		getItNowTextSpan.style.paddingRight = "10";
		getItNowTextSpan.style.paddingLeft = "10";
		getItNowTextSpan.style.paddingTop = "0";
		getItNowTextSpan.style.paddingBottom = "0";
		getItNowTextSpan.style.textIndent = "0";
		getItNowTextSpan.style.top = "12px";
		getItNowTextSpan.style.right = "0px";
		getItNowTextSpan.style.whiteSpace = "nowrap";
		getItNowTextSpan.appendChild(getItNowLink);
		
		var eachText = document.createTextNode(nameData);
		var textSpan = document.createElement("span");
		textSpan.style.position = "absolute";
		textSpan.style.top = "12px";
		textSpan.style.left = "0px";
		textSpan.appendChild(eachText);

		var eachRow = document.createElement("div");
		eachRow.id = "eachRowID";
		eachRow.style.backgroundColor = neturalBKgroundColor;
		eachRow.style.borderTop = "1px solid #ffffff";
		eachRow.style.borderBottom = "1px solid #ffffff";
		eachRow.style.position = "relative";
		eachRow.style.padding = "0";
		eachRow.style.margin = "0";
		eachRow.style.width = "100%";
		eachRow.style.height = "35px";
		eachRow.style.zIndex = "2";
		eachRow.style.whiteSpace = "nowrap";
		eachRow.style.overflow = "hidden";
		eachRow.style.textIndent = "48px";
		eachRow.style.fontFamily = "tahoma, Arial";
		eachRow.style.fontSize = "11px";

		eachRow.appendChild(iconImage);
		eachRow.appendChild(textSpan);

		if (SelectedTab == "published")
		{
			eachRow.appendChild(getItNowTextSpan);
		}
		else
		{
			eachRow.appendChild(deletePluginLink);
		} // end of if (SelectedTab == "published")
		
		eachRow.onmouseover = function()
		{
			this.style.backgroundColor = highlightBKgroundColor;
			this.style.borderBottom = "1px Solid " + highlightBKgroundColor;
			this.style.borderTop = "1px Solid " + highlightBKgroundColor;
			
			var leftOfGetItNowOrTrashCanImg = calculateLeftOfGetItNowOrTrashCanImg();
			var mouseX = window.event.clientX;
			if (mouseX < leftOfGetItNowOrTrashCanImg)
			{
				this.style.cursor = "n-resize";
			}
			else
			{
				this.style.cursor = "default";
			} // end of if (mouseX < leftOfGetItNowOrTrashCanImg)
			
			if (SelectedTab == "published")
			{
				// this only occurs Get It Now text
				var listOfAllSpanElements = this.getElementsByTagName("span");
				for (k=0; k<listOfAllSpanElements.length; k++)
				{
					var thisSpanID = listOfAllSpanElements.item(k).id;
					if (thisSpanID == "getItNowSpan")
					{
						listOfAllSpanElements.item(k).style.backgroundColor = highlightBKgroundColor;
					} // end of if
				} // end of for
			}
			else
			{
				// this only occurs for trash can image
				var listOfAllDivElements = this.getElementsByTagName("IMG");
				for (k=0; k<listOfAllDivElements.length; k++)
				{
					var thisDivID = listOfAllDivElements.item(k).id;
					if (thisDivID == "trashCanImg")
					{
						listOfAllDivElements.item(k).style.backgroundColor = highlightBKgroundColor;
					} // end of if
				} // end of for
			} // end of if (SelectedTab == "published")
		} // end of eachRow.onmouseover = function()
		
		eachRow.onmousemove = function()
		{
			var leftOfGetItNowOrTrashCanImg = calculateLeftOfGetItNowOrTrashCanImg();
			var mouseX = window.event.clientX;
			if (mouseX < leftOfGetItNowOrTrashCanImg)
			{
				this.style.cursor = "n-resize";
			}
			else
			{
				this.style.cursor = "default";
			} // end of if (mouseX < leftOfGetItNowOrTrashCanImg)
		} // end of eachRow.onmouseover = function()
		
		eachRow.onmouseout = function()
		{
			this.style.backgroundColor = neturalBKgroundColor;
			this.style.borderBottom = "1px Solid " + neturalBKgroundColor;
			this.style.borderTop = "1px Solid " + neturalBKgroundColor;
			
			if (SelectedTab == "published")
			{
				// this only occurs for Get It Now text
				var listOfAllSpanElements = this.getElementsByTagName("span");
				for (k=0; k<listOfAllSpanElements.length; k++)
				{
					var thisSpanID = listOfAllSpanElements.item(k).id;
					if (thisSpanID == "getItNowSpan")
					{
						listOfAllSpanElements.item(k).style.backgroundColor = neturalBKgroundColor;
					} // end of if
				} // end of for
			}
			else
			{
				// this only occurs for trash can image
				var listOfAllDivElements = this.getElementsByTagName("IMG");
				for (k=0; k<listOfAllDivElements.length; k++)
				{
					var thisDivID = listOfAllDivElements.item(k).id;
					if (thisDivID == "trashCanImg")
					{
						listOfAllDivElements.item(k).style.backgroundColor = neturalBKgroundColor;
					} // end of if
				} // end of for
			} // end of if (SelectedTab == "published")
		} // end of function()
		
		eachRow.onmousedown = function()
		{ 
			this.style.borderTop = "1px dotted #8c8c8c";
			this.style.borderBottom = "1px dotted #8c8c8c";
			
			var leftOfGetItNowOrTrashCanImg = calculateLeftOfGetItNowOrTrashCanImg();
			var mouseX = window.event.clientX;
			
			if (mouseX < leftOfGetItNowOrTrashCanImg)
			{
				var imgArrayFromThis = this.getElementsByTagName("IMG");
				for (j=0; j<imgArrayFromThis.length; j++)
				{
					var iconIdValue = imgArrayFromThis.item(j).id;
					var searchIcondIdValueResult = iconIdValue.indexOf(iconImageIdPrefix);
					if (searchIcondIdValueResult > -1)
					{
						var idFromIcon = imgArrayFromThis.item(j).id.substr(iconImageIdPrefix.length, iconIdValue.length - 1);
					}  // end of if
				} // end of for (j=0; j<imgArrayFromThis.length; j++)
				
				expandDetailsDiv(true, idFromIcon);
				this.scrollIntoView(true); 
			} // end of if (mouseX < leftEdgeOfGetItNow)
		} // end of eachRow.onmousedown = function()
		
		eachRow.onmouseup = function()
		{ 
			this.style.borderTop = "1px solid " + highlightBKgroundColor;
			this.style.borderBottom = "1px Solid " + highlightBKgroundColor;
		} // end of eachRow.onmouseup = function()
		
		listDivNode.appendChild(eachRow);
	} // end of for
} // end of function GetData()

//  Javascript functions provided by the plug-in selector
//  Javascript functions provided by the plug-in selector
function $OnPluginInstalled(id)
{
}

function $OnPluginUninstalled(id)
{
}

function $OnPluginOpen(id)
{
}

function $OnPluginClose(id)
{
}

function $OnTabSelChanged()
{
	runTheseOnLoadAndOnFucus();
}

function $OnSkinChanged()
{
}
