var iptv_aniSpeed = 8;           // Animation speed - bigger==faster
var iptv_direction = "left";   // Can be "right" | "left" | "bottom"
var iptv_debug=false;
var iptv_dbgDiv=null;
var iptvCompanionDuration = 0;
var g_isIE = (window.navigator.userAgent.indexOf("MSIE")>0);

function iptvDbgMessage(strMessage)
{
	if (iptv_debug)
	{
		if (iptv_dbgDiv==null)
		{
			iptv_dbgDiv=document.createElement("div");
			iptv_dbgDiv.id="iptv_dbgDiv";
			iptv_dbgDiv.style.fontFamily="arial";
			iptv_dbgDiv.style.fontSize="8pt";
			iptv_dbgDiv.style.backgroundColor="#D0D0D0";
			iptv_dbgDiv.style.border="1px solid gray";
			iptv_dbgDiv.innerHTML="DEBUG";
			iptv_dbgDiv.style.position="absolute";
			iptv_dbgDiv.style.left="5px";
			iptv_dbgDiv.style.top="5px";
			iptv_dbgDiv.style.padding="5px";
			document.body.appendChild(iptv_dbgDiv);

			var adContainer=document.getElementById("iptvAdContainer");
			if (adContainer!=null)
				adContainer.style.border="2px solid red";
		}

		iptv_dbgDiv.innerHTML += " | " + strMessage;
	}
}

function iptvShowCompanions(playerX, playerY, companions)
{
	for(var i=0;i<companions.length;i++)
	{
		var adsize = companions[i][0];
		switch(adsize)
		{
		case "300x250":
			iptvShowFirstCompanion(playerX, playerY, companions[i]);
			if(iptvCompanionDuration>0)
				setTimeout("iptvHideCompanions(true)", iptvCompanionDuration*1000);
			break;
		}
	}
}

function iptvHideCompanions(hide)
{
	if(iptvCompanionDuration==0 || hide==true)
	{
		var tbl = document.getElementById("iptvAdTbl");
		if (tbl != null)
		{
			if (!g_isIE && document.getElementById("iptvIFrame") != null)
				document.getElementById("iptvIFrame").style.display="none";
	
			document.getElementById("iptvAdContainer").style.display="";
			switch(iptv_direction)
			{
			case "right":
				tbl.setAttribute("anim_destX", (-1 * tbl.offsetWidth));
				break;
			case "left":
				tbl.setAttribute("anim_destX", tbl.offsetWidth);
				break;
			default:
				tbl.setAttribute("anim_destY", (-1 * tbl.offsetHeight));
			}

			iptvStartAnimation(true);
		}
	}
}

function iptvShowFirstCompanion(playerX, playerY, companion)
{
	var adContainer = document.getElementById("iptvAdContainer");
	if (adContainer == null)
	{
		adContainer=document.createElement("div");
		adContainer.id="iptvAdContainer"
		adContainer.style.width="320px";
		adContainer.style.height="290px";
		adContainer.style.position="absolute";
		adContainer.style.overflow="hidden";
		document.body.appendChild(adContainer);

		var tbl = document.createElement("table");
		tbl.style.backgroundColor="#666666";
		tbl.id = "iptvAdTbl";
		tbl.cellPadding = "0";
		tbl.cellSpacing = "0";
		tbl.style.position = "absolute";
		tbl.style.top = "0px";
		tbl.style.width="320px";
		tbl.style.height="290px";
		var oRow1=tbl.insertRow(-1);
		var oCell = oRow1.appendChild(document.createElement("td"));
		oCell.style.width="100%";
		oCell.style.textAlign = "right";
		oCell.style.paddingRight="3px";
		oCell.innerHTML = "<a style='color:#FFFFFF;font-size:8pt;font-family:arial' href='javascript:iptvHideCompanions(true)'>CLOSE</a>";

		var oRow2=tbl.insertRow(-1);
		oRow2.style.height="100%";
		var oCell2 = oRow2.appendChild(document.createElement("td"));
		oCell2.style.padding="10px";
		oCell2.vAlign = "top";

		var oDiv = oCell2.appendChild(document.createElement("div"));
		oDiv.id = "iptvAdDiv";
		oDiv.style.width = "300px";
		oDiv.style.height = "250px";

		adContainer.appendChild(tbl);
	}

	var obj = document.getElementById("iptvembed");
	if(obj)
	{
		obj = obj.parentNode;
		// Position ad container
		var x_pos=0;
		var y_pos=0;

		var temp = obj;
		do
		{
			x_pos += temp.offsetLeft;
			y_pos += temp.offsetTop;
			temp = temp.offsetParent;
		}
		while(temp!=null)

		switch(iptv_direction)
		{
		case "right":
			adContainer.style.left = (x_pos + obj.offsetWidth) + "px";
			adContainer.style.top = y_pos + "px";
			break;
		case "left":
			adContainer.style.left = (x_pos - 320) + "px";
			adContainer.style.top = y_pos + "px";
			break;
		default:
			adContainer.style.left = x_pos + "px";
			adContainer.style.top = (y_pos + obj.offsetHeight) + "px";
		}


		// Set HTML for ad
		var strAdHTML = "";
		
		if("iframe"==companion[1] && companion[2]!=null)
		{
			strAdHTML = "<iframe id='iptvIFrame' width='300' height='250' frameborder='0' scrolling='no' src='"+companion[2]+"'></iframe>";
		}
		else if("swf"==companion[1] && companion[2]!=null)
		{
			strAdHTML = "<object width='300' height='250' id='iptvSWF' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'><param name='movie' value='"+companion[2]+"' /><embed name='iptvSWF' pluginspage='http://www.adobe.com/go/getflashplayer' src='"+companion[2]+"' type='application/x-shockwave-flash' width='300' height='250'></embed></object>";
		}
		else if("img"==companion[1] && companion[2]!=null)
		{
		
			if (companion[3] !=null && companion[3].length > 0)
				strAdHTML = "<a href='"+companion[3]+"' target='_blank'>";
			
			strAdHTML += "<img src='"+companion[2]+"' border='0' />";

			if (companion[3] !=null && companion[3].length > 0)
				strAdHTML += "</a>";
		}

		if (!g_isIE)
			document.getElementById("iptvAdDiv").style.display="none";
			
		document.getElementById("iptvAdDiv").innerHTML = strAdHTML;

		// Animate ad table within ad box
		var tbl = document.getElementById("iptvAdTbl");
		
		switch(iptv_direction)
		{
		case "right":
			document.getElementById("iptvAdContainer").style.display="";
			tbl.style.left = (-1 * tbl.offsetWidth) + "px";
			iptvDbgMessage("Setting Left:" + tbl.style.left);
			tbl.setAttribute("anim_destX", 0);
			break;
		case "left":
			document.getElementById("iptvAdContainer").style.display="";
			tbl.style.left = tbl.offsetWidth + "px";
			iptvDbgMessage("Setting Left:" + tbl.style.left);
			tbl.setAttribute("anim_destX", 0);
			break;
		default:
			document.getElementById("iptvAdContainer").style.display="";
			tbl.style.top = (-1 * tbl.offsetHeight) + "px";
			iptvDbgMessage("Setting Top:" + tbl.style.top);
			tbl.setAttribute("anim_destY", 0);
		}

		iptvStartAnimation(false);
	}

}
function iptvStartAnimation(bMoveIn)
{
	iptvDbgMessage("startAnimation: "+bMoveIn);
	document.getElementById("iptvAdContainer").style.display="";
	iptvAnimateItem(bMoveIn);
}

function iptvAnimateItem(bMoveIn)
{
	var obj=document.getElementById("iptvAdTbl");

	var currLeft = obj.offsetLeft;
	var currTop = obj.offsetTop;

	switch(iptv_direction)
	{
	case "right":
		var iTargetPosX = obj.getAttribute("anim_destX");
		if (iTargetPosX != null)
		{
			iptvDbgMessage(currLeft);
			if ((bMoveIn && (currLeft - iptv_aniSpeed <= iTargetPosX)) || (!bMoveIn && (currLeft + iptv_aniSpeed >= iTargetPosX)))
			{
				iptvDbgMessage("MoveIn: "+bMoveIn);
				iptvDbgMessage("CurrLeft: "+currLeft);
				iptvDbgMessage("Speed: "+iptv_aniSpeed);
				iptvDbgMessage("TargetXPos: "+iTargetPosX);

				obj.style.left = iTargetPosX + "px";  // Stop animating

				document.getElementById("iptvAdDiv").style.display="";
				if (bMoveIn)
					document.getElementById("iptvAdContainer").style.display="none";
			}
			else
			{
				if (bMoveIn)
				{
					if (document.getElementById("iptvAdContainer").style.display=="none")
						return;
					obj.style.left = currLeft - iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(true)", 1);
				}
				else
				{
					obj.style.left = currLeft + iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(false)", 1);
				}
			}
		}
		break;
	case "left":
		var iTargetPosX = obj.getAttribute("anim_destX");
		if (iTargetPosX != null)
		{
			iptvDbgMessage(currLeft);
			if ((bMoveIn && (currLeft + iptv_aniSpeed >= iTargetPosX)) || (!bMoveIn && (currLeft - iptv_aniSpeed <= iTargetPosX)))
			{
				iptvDbgMessage("MoveIn: "+bMoveIn);
				iptvDbgMessage("CurrLeft: "+currLeft);
				iptvDbgMessage("Speed: "+iptv_aniSpeed);
				iptvDbgMessage("TargetXPos: "+iTargetPosX);

				obj.style.left = iTargetPosX + "px";  // Stop animating

				document.getElementById("iptvAdDiv").style.display="";
				if (bMoveIn)
					document.getElementById("iptvAdContainer").style.display="none";
			}
			else
			{
				if (bMoveIn)
				{
					if (document.getElementById("iptvAdContainer").style.display=="none")
						return;
					obj.style.left = currLeft + iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(true)", 1);
				}
				else
				{
					obj.style.left = currLeft - iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(false)", 1);
				}
			}
		}
		break;
	default:
		var iTargetPosY = obj.getAttribute("anim_destY");
		if (iTargetPosY != null)
		{
			iptvDbgMessage(currTop);
			if ((bMoveIn && (currTop - iptv_aniSpeed <= iTargetPosY)) || (!bMoveIn && (currTop + iptv_aniSpeed >= iTargetPosY)))
			{
				iptvDbgMessage("MoveIn: "+bMoveIn);
				iptvDbgMessage("CurrTop: "+currTop);
				iptvDbgMessage("Speed: "+iptv_aniSpeed);
				iptvDbgMessage("TargetYPos: "+iTargetPosY);

				obj.style.top = iTargetPosY + "px";  // Stop animating
				
				document.getElementById("iptvAdDiv").style.display="";
				if (bMoveIn)
					document.getElementById("iptvAdContainer").style.display="none";
			}
			else
			{
				if (bMoveIn)
				{
					obj.style.top = currTop - iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(true)", 1);
				}
				else
				{
					obj.style.top = currTop + iptv_aniSpeed + "px";
					setTimeout("iptvAnimateItem(false)", 1);
				}
			}
		}
	}
}