
/************************************************************************************
*
*	Package:	mGoalGUI.js
*	Version:	1.0
*	Author:		Jesper Svensson; jesper@pyramedia.se
*
*	Revision history
*	================================================================================
*	1.0			2007-12-30		Jesper Svensson		Initial version
*	1.1			2008-01-19		Jesper Svensson		Updated after customer review
*
*
*
************************************************************************************/



/*
	Global vars and objects *******************************************************
*/


var subtypes = new Array();
var teams = new Array();
var flags = new Array();
var headlines = new Array();

var selectedTab = 0;
var tabCont = null;
var contentCont = null;
var live = 0;

var cssTab = "";
var cssTabSelected = ""

var posX = 0;
var posY = 0;
var toolTipOffsetX = -120;
var toolTipOffsetY = 15;

var toolTipEnabled = false;

var intCount = 0;



/*
	Constructors ******************************************************************
*/

function headline (name, caption, width, selected, popup)
{
	this.name = name;
	this.caption = caption;
	this.width = width;
	this.selected = selected;
	this.popup = popup;
}

function subtype(st_nm, sf, start, stop, price, price_conf, prior, cov_info, add_info, price_info)
{
	this.st_nm 		= st_nm;
	this.sf 		= sf;
	this.start 		= start;
	this.stop 		= stop;
	this.price 		= price;
	this.price_conf = price_conf;
	this.prior 		= prior;
	this.cov_info 	= cov_info;
	this.add_info 	= add_info;
	this.price_info = price_info;

}

function team(leg_name, slg_id, slg_name, tm_id, tm_name)
{
	this.leg_name = leg_name;
	this.slg_id = slg_id;
	this.slg_name = slg_name;
	this.tm_id = tm_id;
	this.tm_name = tm_name;
}

function flag(leg_id, live, period, redcards, lineups, subst)
{
	this.leg_id = leg_id;
	this.live = live;
	this.period = period;
	this.redcards = redcards;
	this.lineups = lineups;
	this.subst = subst;
}



/*
	Rendering functions ***********************************************************
*/

function fillTabs ()
{
	try
	{
		live = liveTest();

		for (var i = 0;i< subtypes.length;i++)
		{
			var createTab = 0;

			switch (subtypes[i].sf)
			{
				case 'ENDRESULT':
				{
					createTab = 1;
					break;
				}
				case 'LIVE':
				{
					if (live == 1)
					{
						createTab = 1;
					}
					break;
				}
				case 'PERIOD':
				{
					if (flags[0].period == 1)
					{
						 createTab = 1;
					}
					break;
				}
				case 'LINEUPS':
				{
					createTab = 0;
					break;
				}
				case 'SUBST':
				{
					if (flags[0].subst == 1)
					{
						createTab = 1;
					}
					break;
				}
				case 'LIVE_LINEUPS':
				{
					createTab = 0;
					break;
				}
				case 'LIVE_REDCARDS':
				{
					if (live == 2)
					{
						createTab = 1;
					}
					break;
				}
			}

			if (createTab == 1)
			{
				headlines[headlines.length] = new headline (subtypes[i].st_nm, subtypes[i].st_nm.toUpperCase(), 120, "", false);
			}
		}

		headlines[headlines.length] = new headline ("Villkor", locConditions.toUpperCase(), 120, "", true);
		//headlines[headlines.length] = new headline ("Info", "<img src=\"images/information_24x28.gif\" border=0 width=24 height=28><br>", 30, "");
	}
	catch (e)
	{
		// Void;
	}

}


function renderTabs()
{
	try
	{
		if (headlines.length == 0)
		{
			fillTabs();
		}

	/*	var sHtml = "<ul class=\"tabs\">";


		for (var i=0; i<headlines.length;i++)
		{
			sHtml += "<li id=\""+ headlines[i].selected +"\"><a href=\"#\" onClick=\"renderContent("+ i +");\"><span style='width:"+ headlines[i].width +"px;height:30px;'>"+ headlines[i].caption +"</span></a></li>";
		}

		sHtml += "</ul>";
	*/


		var sHtml = "<table border=0 cellspacing=0 cellpadding=0><tr>";

		for (var i=0; i<headlines.length;i++)
		{
			if (headlines[i].selected.length > 0)
			{
				var imgLeft = "images/left_on.gif";
				var imgMid = "images/middle_on.gif";
				var cssName = "tabOn";
				var imgRight = "images/right_on.gif";
			}
			else
			{
				var imgLeft = "images/left_off.gif";
				var imgMid = "images/middle_off.gif";
				var cssName = "tabOff";
				var imgRight = "images/right_off.gif";
			}

			sHtml += "<td><img src=\""+ imgLeft +"\" height=30 border=0 alt=\"\"></td>";
			sHtml += "<td height=25 style=\"background-image:url('"+ imgMid +"');\"><a href=\"#\" class=\""+ cssName +"\" onClick=\"renderContent("+ i +");\">"+ headlines[i].caption +"</a></td>";
			sHtml += "<td><img src=\""+ imgRight +"\" height=30 border=0 alt=\"\"></td>";

		}

		sHtml += "</tr></table>"



		document.getElementById("tabcontainer").innerHTML = sHtml;
	}
	catch(e)
	{
		// Void;
	}
}




function renderContent(headLineIndex)
{
	var padding = "5px";
	var tableWidth = "100%";
	try
	{

		selectTab(headLineIndex);
		renderTabs();
		var subTypeIndex = getSubTypeIndex(headLineIndex);


		var sHtml = "";

		if (headlines[headLineIndex].name == "Villkor")
		{
			sHtml = "<table id='TeamsTable' cellpadding=4 cellspacing=4 width="+ tableWidth +"><tr><td>"+ locConditionContent.replace("\n", "<br>") +"</td></tr></table>";

		}
		else if (headlines[headLineIndex].name == "Info")
		{
			sHtml = "<table id='TeamsTable' cellpadding=4 cellspacing=4 width="+ tableWidth +"><tr><td>Informationstext</td></tr></table>";
		}
		else
		{
			sHtml = "<table id='TeamsTable' border=0 cellpadding=0 cellspacing=0 width="+ tableWidth +">";
			sHtml += "<tr><td colspan=5><img src=\"images/white_1x1.gif\" width=1 height=1><br></td></tr>";
			var slgId = 0;

			for (var i=0;i<teams.length;i++)
			{
				if (slgId != teams[i].slg_id)
				{
					slgId = teams[i].slg_id;
					sHtml += "<tr><td class='AT_LeagueName' colspan=5 style=\"padding:"+ padding +";\">" + teams[i].slg_name + "</td></tr>";
					sHtml += "<tr><td colspan=5><img src=\"images/white_1x1.gif\" width=1 height=1><br></td></tr>";
				}

						var tm_id      = teams[i].tm_id;
						var tm_name 	= teams[i].tm_name;
						var tm_start	= subtypes[subTypeIndex].start;
						var tm_stop		= subtypes[subTypeIndex].stop;

						tm_start		= tm_start.replace("Sms:a", "</b>Sms:a<b>");
						tm_start		= tm_start.replace("till", "</b>till<b>");
						tm_start 		= tm_start.replace("[tm_id]", tm_id);

						tm_stop			= tm_stop.replace("Sms:a", "</b>Sms:a<b>");
						tm_stop			= tm_stop.replace("till", "</b>till<b>");
						tm_stop 		= tm_stop.replace("[tm_id]", tm_id);


						var st_nm		= subtypes[subTypeIndex].st_nm;
						var prior		= subtypes[subTypeIndex].prior;
						var price		= subtypes[subTypeIndex].price;
						var price_conf	= subtypes[subTypeIndex].price_conf;
						var price_info	= subtypes[subTypeIndex].price_info;
						var cov_info	= subtypes[subTypeIndex].cov_info;
						var add_info	= subtypes[subTypeIndex].add_info;
						var mess		= add_info;

						cov_info 		= cov_info.replace("[TeamName]", tm_name);
						add_info 		= add_info.replace("[TeamName]", tm_name);



						sHtml += "<tr><td style=\"padding:"+ padding +";\">"+ tm_name +"</td>";
						sHtml += "<td style=\"padding:"+ padding +";\"><nobr><b>- "+ tm_start +"</b></nobr></td>";
						sHtml += "<td class='AT_AdditionalInfo' onMouseOver=\"tooltip('"+ locMoreInformation +"');\" onMouseOut=\"hidetip();\" style=\"padding:"+ padding +";\">";
						sHtml += "<img class=\"infoBtn\" src=\"images/information.png\" border=0 onClick='ShowAdditionalInfo(this.parentNode);'>";
						sHtml += "</td><td></td>";
						sHtml += "<td style=\"cursor:pointer;\" onMouseOver=\"tooltip('"+ tm_stop +"');\" onMouseOut=\"hidetip();\"  style=\"padding:"+ padding +";\">";
						sHtml += "<img class=\"deleteBtn\" src=\"images/delete.png\" border=0>";
						sHtml += "</td>";
						sHtml += "</tr>";
					    sHtml += "<tr style='display:none'>";
						sHtml += "<td colspan='5' class='AT_AdditionalInfoText' width="+ tableWidth +">";
						sHtml += ""+ cov_info + "<hr/>" + price_info + "<br>" + add_info +"</td></tr>";
						sHtml += "<tr><td colspan=5><img src=\"images/white_1x1.gif\" width=1 height=1><br></td></tr>";

			}

			sHtml += "</table>";

		}

		document.getElementById("teamcontainer").innerHTML = sHtml;
	}
	catch(e)
	{
		// Void;
	}
}



/*
	Navigation functions **********************************************************
*/


function selectTab(tabIndex)
{
	unselectTabs();

	headlines[tabIndex].selected = "current";

}


function unselectTabs()
{
	for (var i=0; i< headlines.length; i++)
	{
		headlines[i].selected = "";
	}
}




/*
	Tooltip functions **********************************************************
*/

function tooltip(sTip)
{

	var oTT = 	document.getElementById("ttContainer")

	if (sTip=="")
	{
		//hidetip();
		return;
	}

	intCount ++;

	toolTipEnabled = true;
	oTT.innerHTML = "<table border=0 cellpadding=0 cellspacing=0><tr><td width=100% class=tooltip>" +  sTip + "</td></tr></table>";
	//oTT.style.left = (posX + toolTipOffsetX);
	oTT.style.right = 25;
	oTT.style.left = "auto";
	oTT.style.top = (posY + toolTipOffsetY);
	oTT.style.visibility = "visible";


}

function hidetip()
{
	intCount ++;

	var oTT = 	document.getElementById("ttContainer")
	oTT.style.visibility = "hidden";
	toolTipEnabled = false;

}


/*
	Support functions ************************************************************
*/

function escapeQuote (toEscape)
{
	return toEscape.replace('"', '\"');
}

function liveTest ()
{
	var fl = flags[0];

	for (var i=0;i<subtypes.length;i++)
	{
		switch(subtypes[i].sf)
		{
			case 'LIVE':
			{
				if ((fl.live == 1) && (live == 0))
				{
					live = 1;
				}
				break;
			}
			case 'LIVE_LINEUPS':
			{
				if ((fl.live == 1) && (live == 0))
				{
					live = 1;
				}
				break;
			}
			case 'LIVE_REDCARDS':
			{
				if ((fl.live == 1) && (fl.redcards == 1))
				{
					live = 2;
				}
				break;
			}
		}
	}

	return live;

}


function getSubTypeIndex (inHeadLineIndex)
{

	sName = headlines[inHeadLineIndex].name;

	var iIndex = 0;

	for (var i= 0;i<subtypes.length;i++)
	{
		if (subtypes[i].st_nm == sName)
		{
			iIndex = i;
			break;
		}
	}

	return iIndex;

}

function capturemouse(e)
{
	if (toolTipEnabled)
	{
		return;
	}
		if (!e) var e = window.event;

		if (e.pageX || e.pageY) 	{
			posX = e.pageX;
			posY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posY = e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
		}
}


document.onmousemove = capturemouse;