
/**
Description of an event

This code was taken from the TradeBetX.com homepage and so some
information may not be used on this site.

evID 	- Identity number for the event
evName	- Name of the Event
evStart	- The date and time the event opens for trading
numContracts- The number of contracts contained in the event
evClass	- The Event class of this event
isLive	- Is true if this event is an 'in-running' event
startTime- The 'kick off' time of the event if it's 'in-running'
evGrpName- The event group name
startTimeStamp  - The 'kick-off' time in a time stamp format
**/   
function EventListing(evID, evName, evStart, numContracts, evClass, isLive, startTime, evGrpName, startTimeStamp)
{
this.evID=evID;
this.evName= evName;
this.evStart = evStart;
this.numContracts = numContracts;
this.evClass = evClass; 
this.isLive = isLive;
this.startTime = startTime;
this.evGrpName = evGrpName;
this.startTimeStamp=startTimeStamp;
this.listAttributes=listAttributes;
}

function listAttributes()
{
	return 	this.evID + "," +			this.evName + "," +			this.evStart  + "," +			this.numContracts  + "," +			this.evClass  + "," +			this.isLive  + "," +			this.startTime  + "," +			this.evGrpName  + "," +			this.startTimeStamp	
	
}


/**
Description of an Event Group

Contains an array of events in the event group

grpID	- Identity number for the group
grpName	- Name of the Group
evAry	- Array of the events in this event group
**/
function EvGrp(grpID, grpName, evAry)
{
this.grpID=grpID;
this.grpName= grpName;
this.evAry=evAry;
}

/**
Description of an Event Class

clsName	- The name of the class
grpAry	- An array containing all the event groups in this event class
**/
function EvCls(clsName, grpAry)
{
this.clsName = clsName;
this.grpAry = grpAry;
}

function Coupon(id, name, classID, className){
	this.id=id;
	this.name=name;
	this.classID=classID;
	this.className=className;
}


// And array of contract ids
var conIDAry = new Array();



//An array of event classes
var evClsAry = new Array();

// An array of all the event groups
var allGroupsAry = new Array();

// A group
var grp;

//An array of groups
var grpAry;

//An array of events
var eventAry;

var showallDisplay = 'block';
function createMenu()
{

	var marketDataFrame = document.getElementById("md");

	var menu = document.getElementById("sidemenu");
	while(menu.childNodes[0])
	{
		menu.removeChild(menu.childNodes[0]);
	}
	var submenu = document.createElement("UL");

	var showAll = document.createElement("LI");
	showAll.innerHTML = "<a href=\"#\" onclick=\"createMenu();document.getElementById('show-all').style.display = 'none'; return false;\">All Markets</a><ul><li></li></ul>";
	showAll.id= "show-all";

	
	submenu.appendChild(showAll);


	var showAll = document.createElement("LI");
	if(typeof ajax != 'undefined' && ajax == true)
	{
		showAll.innerHTML = '<a href="/aav2/trading/ajax/marketData.jsp" onclick="showMostActive(); return false;">Most Active Today</a><ul><li></li></ul>';
	}
	else
	{
		showAll.innerHTML = '<a href="/aav2/trading/tradingHTML.jsp">Most Active Today</a><ul><li></li></ul>';
	}
	showAll.id= "mostActive";
	showAll.className = "selected";

	
	submenu.appendChild(showAll);

	var featuredEventsList = document.createElement("LI");
	featuredEventsList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">In-Running Events</a>";
	featuredEventsList.id="featuredEventsMenu";
	featuredEventsList.className = "selected";

	inrunningList = document.createElement("UL");
	for(var inrunningIndex = 0; inrunningIndex < featuredEvents.length; inrunningIndex++)
	{
		var inrunningItem = document.createElement("LI");
		inrunningItem.className = "eventGroup";
		if(marketDataFrame == null)
		{
			inrunningItem.innerHTML = '<a href="/aav2/trading/tradingHTML.jsp?evID='
				+ featuredEvents[inrunningIndex].evID
				+'&eventSelect='
				+ featuredEvents[inrunningIndex].evID				
				+'&updateList=true'
				+'&showExpired=false'
				+'" title="Event ID = inrunning'
				+ '">' 
				+ '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>'
				+ featuredEvents[inrunningIndex].evName 
				+ '</a>';
		}
		else
		{
			inrunningItem.innerHTML = '<a href="#" onclick="tradeEvent('
				+ featuredEvents[inrunningIndex].evID
				+ ','
				+ featuredEvents[inrunningIndex].evID
				+',false);return false;" title="Event ID = inrunning'
				+ '">' 
				+ '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>'
				+ featuredEvents[inrunningIndex].evName 
				+ '</a>';	
		}
		inrunningList.appendChild(inrunningItem);

	}
	featuredEventsList.appendChild(inrunningList);	
	submenu.appendChild(featuredEventsList);

	if(loggedIn == true)
	{

		createPortfolioMenu(submenu);

	}

	if(typeof ajax != 'undefined' && ajax == false)
	{
	var visitedList = document.createElement("LI");

	visitedList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">Visited Events</a>";
	visitedList.className = "selected";
	
	visitedMenu = document.createElement("UL");
	visitedMenu.id = "visitedMenu";
	if(visEvAry.length > 0)
	{		
		createVisitedMenu(visitedMenu);
	}
	else
	{
		var blankLine = document.createElement("LI");
		blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
		blankLine.className = "blankline";
		visitedMenu.appendChild(blankLine);
	
	}
	visitedList.appendChild(visitedMenu);
	submenu.appendChild(visitedList);
	}

	var blankLine = document.createElement("LI");
	blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
	blankLine.className = "blankline";
	submenu.appendChild(blankLine);
	if(window.location.href.match(/showExpired=true/))
	{
		var showExpired = true;
	}
	else
	{
		var showExpired = false;
	}
	
	for(var i = 0; i < evClsAry.length; i++)
	{
		var listing = document.createElement("LI");

		// this.ParentNode refers to the <li> which contains the link
		listing.className = "selected";
		listing.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">" + evClsAry[i].clsName + "</a>";
		if(evClsAry[i].clsName == "2006 Soccer World Cup Markets")
		{
			listing.className = listing.className  + " worldCup";
		}
		listing.id = "eventClassMenu" + i;
		var groupMenu = document.createElement("UL");
		
		createEventGroupMenu(groupMenu, evClsAry[i], showExpired);

		listing.appendChild(groupMenu);


		
		submenu.appendChild(listing);
	}

	var blankLine = document.createElement("LI");
	blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
	blankLine.className = "blankline";

	submenu.appendChild(blankLine);

	menu.appendChild(submenu);


	for(var i = 0; i < coupons.length; i++)
	{		
		couponsClassMenu = getCouponClassMenu(coupons[i].className);
		if(couponsClassMenu != null)
		{
	
			var couponListing = document.createElement("LI");
			couponListing.backgroundImage = "none";
			couponListing.className = "eventGroup";
			couponListing.id = "coupon_" + coupons[i].id;

			if(marketDataFrame == null)
			{
				couponListing.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?eventSelect=coupon_"
					+ coupons[i].id
					+"&evID=coupon_"
					+ coupons[i].id				
					+"&updateList=true"
					+"&showExpired="
					+ showExpired
					+"\" title=\"Event ID = "
					+ coupons[i].id					
					+ "\">" 
					+ coupons[i].name
					+ "</a>";
			}
			else
			{
				couponListing.innerHTML = '<a href="#" onclick=tradeEvent('
					+ coupons[i].id
					+ ','
					+ '"coupon_'
					+ coupons[i].id	
					+ '",'
					+ showExpired
					+');return false;" title="Event ID = '
					+ coupons[i].id
					+ '">' 
					+ coupons[i].name 
					+ '</a>';	
			}

			couponsClassMenu.appendChild(couponListing);
		}
	}

}

function createEventGroupMenu(groupMenu, evClsAry, showExpired)
{
		for(var j = 0; j < evClsAry.grpAry.length; j++)
		{
			var groupListing = document.createElement("LI");
			groupListing.className = "eventGroup";
			groupListing.innerHTML = "<a href=\"#\" id=\"group"+ evClsAry.grpAry[j].grpID +"\" onclick=\"getEvent('"+evClsAry.clsName+"','"+evClsAry.grpAry[j].grpID+"', "+ showExpired +");showNext(this.parentNode);return false;\">" + evClsAry.grpAry[j].grpName + "</a>";
			
			createEventMenu(groupListing, evClsAry.grpAry[j]);

			groupMenu.appendChild(groupListing);
		}
		if(evClsAry.clsName.match(/HorseRacing/) != null) //Adding poolbet link
		{
			var groupListing = document.createElement("LI");
			groupListing.className = "eventGroup";
			groupListing.innerHTML = "<a href=\"/poolbet/\" style=\"color: red;\">Horseracing Pools</a>";	
			
			groupMenu.appendChild(groupListing);
//			listing.appendChild(groupMenu);		
		}
}

function getEvent(className, groupID, showExpired, showThisMenu)
{
	if(typeof showExpired == 'undefined')
	{
		showExpired = false;
	}

	var frame = window.frames['getEventsFrame'];
	if(frame != null)
	{
		frame.location.replace("/aav2/menu/getEvents.jsp?className=" + className + "&groupID="+groupID +"&showExpired="+ showExpired+ "&showThisMenu="+showThisMenu);
	}
}
function createEventMenu(groupListing, grpAry)
{
	var marketDataFrame = document.getElementById("md");

	var groupElements = groupListing.getElementsByTagName("UL");
	while(groupElements.length > 0)
	{
		groupListing.removeChild(groupElements[groupElements.length - 1]);
	}

	if(window.location.href.match(/showExpired=true/))
	{
		var showExpired = true;
	}
	else
	{
		var showExpired = false;
	}
	var eventMenu = document.createElement("UL");
	for(var k = 0; k < grpAry.evAry.length; k++)
	{
		var eventListing = document.createElement("LI");
		eventListing.className = "event";


		var featuredIcon = "";
		if(isFeatured(grpAry.evAry[k].evID))
		{
			featuredIcon = '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>';
		}

		if(typeof ajax != 'undefined' && ajax == true)
		{
			eventListing.innerHTML = '<a href="#" onclick="selectEvent('
				+grpAry.evAry[k].evID
				+')">'
				+ featuredIcon
				+ grpAry.evAry[k].evName 
				+ '</a>';
		}
		else if(marketDataFrame == null)
		{
			eventListing.innerHTML = '<a href="/aav2/trading/tradingHTML.jsp?evID='
				+ grpAry.evAry[k].evID
				+'&eventSelect='
				+ grpAry.evAry[k].evID				
				+'&updateList=true&showExpired='
				+ showExpired
				+'" title="Event ID = '
				+ grpAry.evAry[k].evID					
				+ '">' 
				+ featuredIcon
				+ grpAry.evAry[k].evName 
				+ '</a>';
		}
		else
		{

			eventListing.innerHTML = '<a href="#" onclick=tradeEvent('					
					+ grpAry.evAry[k].evID
					+','
					+ grpAry.evAry[k].evID
					+','					
					+ showExpired
					+');return false;" title="Event ID = '
				+ grpAry.evAry[k].evID					
				+ '">' 
				+ featuredIcon
				+ grpAry.evAry[k].evName 
				+ '</a>';
		}

		eventMenu.appendChild(eventListing);
		groupListing.appendChild(eventMenu);
	}
}

function createPortfolioMenu(submenu)
{
	var marketDataFrame = document.getElementById("md");

	var portfolioList = document.createElement("LI");
	portfolioList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">My Portfolios</a>";
	portfolioList.className = "selected";

	portfolioMenu = document.createElement("UL");

	var portfolioItem = document.createElement("LI");
	portfolioItem.className = "eventGroup";
	
	if(marketDataFrame == null)
	{
		portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?portfolio=true&evID=pf1"
					+"&eventSelect=pf1"
					+"&updateList=true"
					+"&showExpired=false"
					+"\" title=\"Event ID = pf1\">" 
					+ "Portfolio 1"
					+ "</a>";
	}
	else
	{
		portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf1\',\'pf1\',false);return false;" title="Event ID = pf1">'
					+ 'Portfolio 1'
					+ '</a>';
	
	}
	
	portfolioMenu.appendChild(portfolioItem);
	var portfolioItem = document.createElement("LI");
	portfolioItem.className = "eventGroup";
	if(marketDataFrame == null)
	{
		portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?portfolio=true&evID=pf2"
					+"&eventSelect=pf2"
					+"&updateList=true"
					+"&showExpired=false"
					+"\" title=\"Event ID = pf2"
					+ "\">" 
					+ "Portfolio 2"
					+ "</a>";
	}
	else
	{
		portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf2\',\'pf2\',false);return false;" title="Event ID = pf2">'
					+ 'Portfolio 2'
					+ '</a>';

	}

					
	portfolioMenu.appendChild(portfolioItem);

	var portfolioItem = document.createElement("LI");
	portfolioItem.className = "eventGroup";
	if(marketDataFrame == null)
	{
		portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?portfolio=true&evID=pf3"
					+"&eventSelect=pf3"
					+"&updateList=true"
					+"&showExpired=false"
					+"\" title=\"Event ID = pf3"
					+ "\">" 
					+ "Portfolio 3"
					+ "</a>";
	}
	else
	{
		portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf3\',\'pf3\',false);return false;" title="Event ID = pf3">'
					+ 'Portfolio 3'
					+ '</a>';

	}
	portfolioMenu.appendChild(portfolioItem);

	var portfolioItem = document.createElement("LI");
	portfolioItem.id = "addToPortfolio";
	portfolioItem.className = "eventGroup";
	portfolioItem.style.background = "white";
	portfolioItem.innerHTML = "<form method=\"get\" action=\"/aav2/search.jsp\"><span class=\"form-label\">Add Contract:</span><input id=\"portfolioInput\" name=\"searchText\"><button>OK</button></form>";
	portfolioMenu.appendChild(portfolioItem);
	
	portfolioList.appendChild(portfolioMenu);	
	submenu.appendChild(portfolioList);

}

function createVisitedMenu(visitedMenu)
{
	var marketDataFrame = document.getElementById("md");
	while(visitedMenu != null && visitedMenu.childNodes[0])
	{
		visitedMenu.removeChild(visitedMenu.childNodes[0]);
	}

	for(i = 0; i < visEvAry.length; i++)
	{
		var visitedItem = document.createElement("LI");
		visitedItem.className = "selected";

		visitedItem.innerHTML = '<a href="#">'+visEvAry[i].evName + '</a>';

		if(marketDataFrame == null)
		{
			visitedItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID="
				+ visEvAry.evID
				+"&eventSelect="
				+ visEvAry.evID				
				+"&updateList=true"
				+"&showExpired=false"
				+"\" title=\"Event ID = Visited Event\">" 
				+ visEvAry[i].evName
				+ "</a>";
		}
		else
		{
			visitedItem.innerHTML = '<a href="#" onclick="tradeEvent(\''
				+ visEvAry[i].evID
				+'\',\''
				+ visEvAry[i].evID
				+'\',false);return false;" title="Event ID = Visited Event">'
				+ visEvAry[i].evName
				+ '</a>';
		}		

		if(visitedMenu != null)
		{
			visitedMenu.appendChild(visitedItem);
		}
	}
}
function getGroupListing(groupID)
{
	for(var i = 0; i < evClsAry.length; i++)
	{

		for(var j = 0; j < evClsAry[i].grpAry.length; j++)
		{
			if(evClsAry[i].grpAry[j].grpID == groupID)
			{
				return evClsAry[i].grpAry[j];
			}
		}
	}
	return null;
}
function tradeEvent(eventId, eventSelect, showExpired)
{
	var md = document.getElementById("md");
	if(md != null)
	{

		md.src = "/aav2/trading/marketDataScreen.jsp?evID="+eventId+"&eventSelect="+eventSelect+"&updateList=true&showExpired="+showExpired;
	}
}

function getCouponClassMenu(className)
{
	if(className != "")
	{
		var links = document.getElementsByTagName("A");
		for(var i = 0; i < links.length; i++)
		{
			var regex = new RegExp(className);
			if( links[i].innerHTML.match(regex) )
			{
				return links[i].parentNode.getElementsByTagName("UL")[0];
			}
		}
	}
	return null;
}

var couponReturnArray = new Array();
function getCouponIndices(className)
{
/*
var arrayIndex = 0;
	for(var i = 0; i < coupons.length; i++)
	{
		if(coupons[i].className == className)
		{
			return couponReturnArray[arrayIndex++] = i;
		}
	}
	return couponReturnArray;
	*/
	return -1;
}
function isFeatured(eventID)
{

	if(typeof featuredEvents != 'undefined')
	{
		for(var i = 0; i < featuredEvents.length; i++)
		{
			if(featuredEvents[i].evID == eventID)
			{
				return true;
			}
		}	
	}
}

/**
	Hides and shows different levels in the menu

	parentElement			- 	The parent li of the link that was 
							clicked to call this function
**/
function showNext(parentElement)
{	
	elements = parentElement.childNodes;

	for(i = 0; i < elements.length; i++)
	{
		// We have to check for <ul>s as the parent <li> 
		// also contains the <a>nchor link that was clicked 
		if(elements[i].nodeName == "UL")
		{
			//If the lower menu items are visible hide them
			if((elements[i].style.display == "block" || parentElement.id == "show-all"))
			{
				//Set the parent nodes' class to be selected
				setSiblingNodesClassName(parentElement, "selected", "block");

				elements[i].style.display = "none";

				//Set all menu items below this to display none
				items = parentElement.getElementsByTagName("UL");
				for(j = 0; j < items.length; j++)
				{
					items[j].style.display = "none";
				}
			}
			else
			{
				//Set all nodes on the parent node's level to be unselected
				setSiblingNodesClassName(parentElement, "unselected", "none");

				//Set all the nodes on the lower level to be selected
				setSiblingNodesClassName(elements[i].childNodes[0],"selected", "block");
				elements[i].style.display = "block";
			}
		}

	}
	document.getElementById("featuredEventsMenu").style.display = "none";
}

/**
	Set the class and display value of nodes on a particular level

	node			- 	A node on the level
	className		- 	The css class you wish to set the nodes to
	displayValue	- 	The display style you wish to set the nodes
						to, the node specified in node will not have
						it's display style set
**/
function setSiblingNodesClassName(node, className, displayValue)
{
	//All nodes on the same level as node
	siblingNodes = node.parentNode.childNodes;
	
	// In order to tell which node node is in the loop
	// we set it's id to be current, it's original id 
	// will be restored at the end of the function
	savedId = node.id;
	node.id = "current";
	
	for(j = 0; j < siblingNodes.length; j++)
	{
		if(siblingNodes[j].nodeName == "LI")
		{
			if(!siblingNodes[j].className.match(/special-event/) && 
				!siblingNodes[j].className.match(/blankline/) )
			{
				siblingNodes[j].className = className;
			}
			
			//We don't want to set the display value of the current node
			//(It'd get hidden when we want it to be the only node 
			//displayed on the level)
			if(siblingNodes[j].id != "current" && displayValue == "none" || displayValue == "block")
			{
				if(siblingNodes[j].id != "show-all" && !siblingNodes[j].className.match(/special-event/))
				{
					siblingNodes[j].style.display = displayValue;
				}
			}
		}
	}

	//Restore the node's id
	node.id = savedId
}

/*function getGroupFromArray(className, groupID)
{	
	var group = allGroupsAry[groupID];
	
	//Due to a bug in the MenuBuilder class only groups 
	//with more than one event get added to allGroupsAry
	if(group != null)
	{
		return group;
	}
	
	for(var i = 0; i < evClsAry.length; i++)
	{
		
	}

	
	
}*/

