//--------- Declaration of Variables -----
var main_Menu = new Array (
['/league/home.php', 'League', 'fc'],
['/team/home.php', 'Team', 'fc'],
['/exteam/home.php', 'Exteam', 'e'],
['/game/archive.php', 'Game', 'fnice'],
['/players/search.php', 'Players', 'fnice'],
['/footytipsredirect.php', 'Tipping', 'fnice'],
['/draft/preseason.php', 'Draft', 'fc'],
['/trade/tradingroom.php', 'Trade', 'fc'],
['/salary/home.php', 'FanCoach', 's'],
['/help/tutorial.php', 'Help', 'fnice'],
['/accountdetail.php?mode=options', 'Account', 'fnice']
);

var menu_League = new Array(
['/league/home.php','Home', 'fc'],
['/league/rules.php','Rules', 'fc'],
['/league/teams.php','Teams', 'fc'],
['/league/draw.php','Draw', 'fc'],
['/league/ladder.php','Ladder', 'fc'],
['/account/createleague.php?mode=options','Commissioner', 'c']
);

var menu_Team = new Array(
['/team/home.php','Home', 'f'],
['/team/list.php','Team List', 'f'],
['/team/add.php','Add Player', 'f'],
['/team/drop.php','Drop Player', 'f'],
['/team/logo.php','Logo', 'f'],
['/team/prioritypicks.php','Priority Picks', 'fc'],
['/team/commissioner.php','Commissioner', 'c']
);

var menu_Exteam = new Array(
['/exteam/home.php','Home', 'e'],
['/exteam/list.php','Team List', 'e'],
['/exteam/status.php','My Teams', 'e'],
['/exteam/dreampop.php','DT Popularity', 'e'],
['/exteam/superpop.php','SC Popularity', 'e'],
['/exteam/dreamteam.php','Dream Teams', 'e']
);

var menu_Game = new Array(
['/game/scoreboard.php','Scoreboard', 'f'],
['/game/gamepreview.php','Preview', 'f'],
['/game/live.php','Your team LIVE!', 'e'],
['/game/head2head.php','Head-to-head', 'e'],
['/game/archive.php','Archive', 'fnice'],
['/game/teamsheets.php','AFL Team Sheets', 'fnice'],
['/game/ladder.php','AFL Ladder', 'fnice'],
['/game/goalkicking.php','AFL Goalkicking', 'fnice']
);

var menu_Players = new Array(
['/players/search.php','Search', 'fnice'],
['/players/news.php?type=All','FanFooty News', 'fnice'],
['/players/depthchart.php','Depth Charts', 'fnice'],
// ['/players/mediastreet.php','Media Street', 'fnice'],
// ['/players/snippets.php','News Snippets', 'fnice'],
// ['/players/aroundtheweb.php','Around the Web', 'fnice'],
['/tribunal.php','Tribunal', 'fnice'],
['/breakdown.php','Breakdown DT', 'fnice'],
['/scbreakdown.php','Breakdown SC', 'fnice'],
['/bubble-dream-team.php','Bubble Boys DT', 'fnice'],
['/bubble-supercoach.php','Bubble Boys SC', 'fnice'],
['/rollercoaster-dream-team.php','Rollercoaster DT', 'fnice'],
['/rollercoaster-supercoach.php','Rollercoaster SC', 'fnice'],
//['/players/badnews.php?type=Injured','Injuries', 'fnice'],
//['/players/badnews.php?type=Sore','Sore', 'fnice'],
//['/players/badnews.php?type=Long-term','Long-term inj\'d', 'fnice'],
//['/players/badnews.php?type=All','All bad news', 'fnice'],
['/players/topscorers.php','Top Scorers', 'fnice'],
['/players/records.php','Records', 'fnice']
);

var menu_Tipping = new Array(
['/footytipsredirect.php', 'FanFooty comp', 'fnice'],
['/forum/index.php/board,39.0.html','Tipping forum', 'fnice']
);

var menu_Draft = new Array(
['/draft/preseason.php','Pre-season Draft', 'fc'],
['/draft/draftresults.php','Draft Results', 'fc'],
['/draft/commissioner.php','Commissioner', 'c']
);

var menu_Trade = new Array(
['/trade/tradingroom.php','Trading Room', 'fc'],
['/trade/baitshop.php','Bait Shop', 'fc']
);

var menu_FanCoach = new Array(
['/salary/home.php','Home', 'i'],
['/salary/list.php','Team List', 'i'],
['/salary/league.php','League', 'i'],
// ['/salary/logo.php','Team Logo'],
['/salary/details.php','Change Name', 'i']
);

var menu_Help = new Array(
['/help/tutorial.php','Tutorial', 'fnice'],
['/help/tutorial_join.php','How to Join', 'n'],
['/help/tutorial_teams.php','Teams', 'fc'],
['/help/tutorial_leagues.php','Leagues', 'fc'],
['/help/tutorial_scoring.php','Scoring', 'fc'],
['/help/tutorial_drafts.php','Drafts', 'fc'],
['/help/tutorial_adddrop.php','Add/drop', 'fc'],
['/help/tutorial_trades.php','Trading', 'fc'],
['/help/tutorial_finals.php','Finals', 'fc'],
['/help/tutorial_commissioner.php','Commissioner', 'c'],
['/help/aboutus.php','About Us', 'fnice'],
['/help/contactus.php','Contact Us', 'fnice']
);

var menu_Account = new Array(
['/accountdetail.php?mode=create','Create Account', 'n'],
['/accountdetail.php?mode=options','Options', 'fice'],
['/account/freeleague.php?mode=free','Start FF League', 'fice'],
['/joinleague.php','Join FF League', 'fice'],
['/createteam.php?mode=create','Enter Ext. Team', 'fice'],
['/exteam/list.php','Edit Ext. Team', 'e']
);


var cmTop = 0;
var cmLeft = 0;
var timer = null;

var IS_NS4 = (document.layers) ? 1 : 0;
var IS_IE4 = (document.all) ? 1 : 0;
var IS_NS6 = ((document.getElementById) && (!IS_IE4)) ? 1 : 0;

var PreseasonWindow;

function reDoIt() {
        if (innerWidth != origWidth || innerHeight != origHeight) {location.reload();}
}

// IS_NS4 redraw on resize
if (IS_NS4) {
        origWidth = innerWidth;
        origHeight = innerHeight;
	onresize = reDoIt;
}


var path = "/";
var domain = ".fanfooty.com.au";

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value ) {
	var today = new Date();
	today.setTime( today.getTime() );
	var expires = expires * 1000 * 60 * 60 * 6;
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' );
}

function removeCookie( name ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


var fanteamid = getCookie("FFFanTeamid");
var leaguesid = getCookie("FFLeaguesid");
var exteamid = getCookie("FFexTeamid");

var username = getCookie("FFLogin");
var membership = getCookie("FFMembership");
var isloggedin;

// Sign In part - table within a table
if (username == null) {
	isloggedin = false;
	
	document.write("<LINK rel=stylesheet type=text/css href=http://live.fanfooty.com.au/style/colour_DF.css>");
	
	document.write("<table class=topmenuleft style=\"font:0.66em/0.9em Verdana\" width=760>");
	document.write("<tr><td class=\"top left\" colspan=9>");
	document.write("<table class=\"top left nested\"><tr><td><FORM id=loginform name=loginform action=\"http://www.fanfooty.com.au/loginaction.php\" method=post style=\"margin:0px; padding:0px; border:0px\"></td>");

	document.write("<td>Coach name:</td>");
	document.write("<td><INPUT size=16 maxlength=16 name=id></td>");

	document.write("<td> Password: </td>");
	document.write("<td><INPUT type=password maxlength=16 size=16 name=password></td>");

	document.write("<td><input type=submit style=\"font-weight:bold\" value='Log in'>");

	document.write("</FORM></td><td> or <a href=http://www.fanfooty.com.au/accountdetail.php?mode=create><b>make an account</b></a>.</td></td></tr></table></td></tr>");
	}
else {
	isloggedin = true;
	
	var colourpref = getCookie("FFColour");	
	document.write("<LINK rel=stylesheet type=text/css href=http://live.fanfooty.com.au/style/colour_" + colourpref + ".css>");
	
	// Already Signed In part - table within a table
	document.write("<table class=nested width=760><tr><td class=topmenuleft colspan=9 nowrap>");
	
	// document.write("<span class=subheading>FanFooty Demo League - Round 5</span><br>");
	// Tai Fix document.write(" (<a href=placeholder>change leagues</a>)<br>");
	// var welcomemessage = "G'day, <b> " + username + "</b>. Your <b>Anglesea Anglers</b> won their last match by <b>34</b> points."

		var shortname = getCookie("FFShortName");
		var longname = getCookie("FFLongName");
		var leaguename = getCookie("FFLeague");		
		var exteamname = getCookie("FFexTeamName");		
		var planteamname = getCookie("FFplanTeamName");		
		var lethalteamname = getCookie("FFlethalTeamName");		

	if (longname == null)
		{ if (shortname == null) {
			if (exteamname != null) 
				{ var welcomemessage = "G'day, <b> " + username + "</b>. Your <a class=nounderline href=\"http://www.fanfooty.com.au/exteam/list.php\"><b>" + exteamname + "</b></a> team is in focus."; }
			else if (planteamname != null) 
				{ var welcomemessage = "G'day, <b> " + username + "</b>. Your <a class=nounderline href=\"http://www.fanfooty.com.au/planteam/list.php\"><b>" + planteamname + "</b></a> plan team is in focus."; }
			else if (lethalteamname != null) 
				{ var welcomemessage = "G'day, <b> " + username + "</b>. Your <a class=nounderline href=\"http://www.fanfooty.com.au/lethal/list.php\"><b>" + lethalteamname + "</b></a> Lethal League team is in focus."; }
			else
				{ var welcomemessage = "G'day, <b> " + username + "</b>."; } 
		}
		else { var welcomemessage = "G'day, <b>" + username + "</b>, commissioner of the <b>" + shortname + "</b> league."; }
		}
	else 
		{ if (shortname == null) 
			{ var welcomemessage = "G'day, <b>" + username + "</b>. Your <b>" + longname + "</b> team is not in a league yet."; }
		else { var welcomemessage = "G'day, <b>" + username + "</b>. Your <b>" + longname + "</b> team is in the <b>" + shortname + "</b> league."; }
		}
		
	document.write(welcomemessage);
	document.write("<br><span class=team><i>Check out our <a href=\"http://www.fanfooty.com.au/createteam.php?mode=create\"><b>Exteam feature</b><\/a> to get live scoring plus head2head for your DT, SC or other external teams</i>");
	//document.write("<br><span class=team><i>Check out our <a href=\"http://www.fanfooty.com.au/planteam.php\"><b>Fanplanner feature</b><\/a> to plan your Dream Team or Supercoach squad!</i>");
	document.write("</span></td><td class=\"topmenuleft\" style='padding:0px 0px 3px 0px'><span class=floatright><a class='nounderline' href='http://www.fanfooty.com.au/change.php'><b>Change&nbsp;team&nbsp;in&nbsp;focus</b></a><br><a class=nounderline href='http://www.fanfooty.com.au/logout.php'><span class=team>Logout from " + username + "</span></a></span>");
	document.write("</td></tr>");
	}

// Permissions
var permissions = "n"; // n == not logged in
if  ( isloggedin )
	{
	permissions = "i"; // i == is logged in
	if ( fanteamid != null )
		permissions = "f"; // f == fanteam in focus
	else if ( leaguesid != null )
		permissions = "c"; // c == league in focus + is commissioner
	else if ( exteamid != null )
		permissions = "e"; // e == exteam in focus
	}

// Main Menu System
document.write("<table class=topmenu width=760>");	
var strMenu = "<tr style=\"margin-bottom:-3px\">";
for ( i = 0; i < main_Menu.length; i++ )
	{
	if ( main_Menu[i][2].search(permissions) > -1 )
		strMenu = strMenu + "<TD class=\"topmenu left\"><b><A class=nounderline id=Menu" + main_Menu[i][1] + 
		"A onmouseover=ShowMenu('Menu" + main_Menu[i][1] + 
		"') onmouseout=HideMenu() name=Menu" + main_Menu[i][1] + "A " + 
		"href=http://www.fanfooty.com.au" + main_Menu[i][0] + ">" + main_Menu[i][1] + "</A></b></TD>";			
	}

if (isloggedin && fanteamid == null && leaguesid == null)
	strMenu = strMenu + "<TD class=topmenu><i>More menus for FF leagues!</i></td>";

if ( !isloggedin )
	strMenu = strMenu + "<TD class=topmenu><i>More menus when you log in!</i></td>";

strMenu = strMenu + "<TD class='topmenu right' style=\"margin-right:0px\"><form method=\"POST\" action=\"http://www.fanfooty.com.au/results.php\" style=\"margin:-1px -15px -3px 0px;font:9px Verdana, Arial;padding:0px\"><input id=playernamesearch name=playernamesearch style=\"height:16px;width:90px;font:9px Verdana, Arial;margin:0px;padding:0px\">";
strMenu = strMenu + "<input type=submit value=\"Player search\" style=\"height:16px;width:90px;font:9px Verdana, Arial;margin:0px;padding:0px\"></form></td></tr></table></tr></table>";

document.write(strMenu);

//--------------  Build FanFooty dropdown Menu --------------------
for ( i = 0; i < main_Menu.length; i++ ) {
	var menu = eval("menu_" + main_Menu[i][1]);
	
	var strDiv = "<div class=\"menu left\" id=Menu" + main_Menu[i][1] + " onMouseOver=OverMenu(); onMouseOut=HideMenu();>";
	
	strDiv = strDiv + "<table class=nested><tr><td class=topdropdownmenu>";
	
	for ( j = 0 ; j < menu.length; j++ ) 
		{
		if ( menu[j][2].search(permissions) > -1 )
			strDiv = strDiv + "&nbsp\;&#183\;&nbsp\;<a class=nounderline href=http://www.fanfooty.com.au" + menu[j][0] + 
				">" + menu[j][1] + "</a><br>";		
		}	

	strDiv = strDiv + "</td></tr></table></div>\n";
	document.write(strDiv);	
	}


// Tai Fix
if (IS_NS6) {
	for ( i = 0; i < main_Menu.length; i++ ) {
		var whichEl = document.getElementsByTagName("Menu" + main_Menu[i][1]);		
		// document.write("Name is " + whichEl);
		
		whichEl.onMouseOver = OverMenu;
		whichEl.onmouseout = HideMenu;
	}
}

function findy(item) {
	if (item.offsetParent) {return item.offsetTop + findy(item.offsetParent);}
	else {return item.offsetTop;}
}

function findx(item) {
	if (item.offsetParent) {return item.offsetLeft + findx(item.offsetParent);}
	else {return item.offsetLeft;}
}


function ShowMenu(ShowIt) {
	clearTimeout(timer);
	HideAllMenu();
	
	whichAnchor = ShowIt + "A";	
	
	if (IS_IE4) {		
		var whichMenu = document.all[ShowIt].style;	
		if (document.all[whichAnchor])
			{ 
			cmTop = findy(document.all[whichAnchor]) + 20;
			cmLeft = findx(document.all[whichAnchor]) - 20;
			}		
		whichMenu.visibility = "visible";
		whichMenu.top = cmTop;
		whichMenu.left = cmLeft;				
	}
	else {	
		if (document.anchors[whichAnchor]) {
			var whichEl = document.getElementById(ShowIt);

			var myObject = document.anchors[whichAnchor];
			while (myObject.offsetParent) {
				cmTop  = cmTop  + myObject.offsetTop;
				cmLeft = cmLeft + myObject.offsetLeft;
				myObject = myObject.offsetParent
			}
		whichEl.style.visibility = "visible";
		whichEl.style.top = cmTop + 20;
		whichEl.style.left = cmLeft;		
		}
	}	
		
	cmTop = 0; cmLeft = 0;	
}


function HideAllMenu() {
	if (IS_IE4) {
	var divTemp = document.all.tags("div");	
	for (i = 0; i < divTemp.length; i++) { 
		var divString = new String(divTemp[i].id);
		if (divString.indexOf('Menu') != -1) divTemp[i].style.visibility = "hidden";
		}
	}
	else	
	{
	var divTemp = document.getElementsByTagName('div');
	for (i = 0; i < divTemp.length; i++) { 
		var divString = new String(divTemp[i].id);
		if (divString.indexOf('Menu') != -1) divTemp[i].style.visibility = "hidden";
		}	
	}	
	
}


function OverMenu() { clearTimeout(timer);}

function HideMenu() {	
	// document.write("In Out Layer");
	clearTimeout(timer);
	timer = setTimeout("HideAllMenu()",300);
}


function ChangeCursor(ControlID, CursorType) {
	if (IS_IE4)
		document.body.style.cursor = CursorType;
	else
		if (CursorType == 'hand')
			document.getElementById(ControlID).style.cursor = 'pointer';
		else
			document.getElementById(ControlID).style.cursor = CursorType;		
}


