var t;
var timer_is_on=0;

// Work out url from a start page
function DecodeSP(start) {
    if (typeof start.split(":")[1] == "undefined") {
        pageID = 0;
    } else {
        pageID = start.split(":")[1]
    }
    
	if (start == "" || start.toUpperCase() == "SPORTS" || start.toUpperCase().split(":")[0] == "SPORTS") {
        return 'flSports.ashx';
    } else if (start.toUpperCase() == "LOGIN") {
        return 'flLogin.ashx';
    } else if (start.toUpperCase().split(":")[0] == "DIVISION") {
        return 'comp_view.ashx?DivisionID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "ALLFIXTURES") {
        return 'comp_all_fixtures.ashx?DivisionID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "ALLSEASONS") {
        return 'league_all_comps.ashx?LeagueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "TEAM") {
        return 'team_view.ashx?TeamID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "CLUBSTEAMS") {
        return 'clubs_teams.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "CLUBVENUES") {
        return 'venue_clubs.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "VENUE") {
        return 'venue.ashx?VenueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "VENUEUSAGE") {
        return 'pitch_usage.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "FLACCESS") {
        return 'fl_access.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "COMPSENTERED") {
        return 'comps_entered.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "ARCHIVEDCOMPSENTERED") {
        return 'comps_entered_archived.ashx?ClubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "LEAGUEFIXTURES") {
        return 'sport_comps_divs.ashx?LeagueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "LEAGURERESULTS") {
        pageID = start.split(":")[1]
        pageDate = start.split(":")[2]
        return 'sport_comp_results_date.ashx?LeagueID=' + pageID + '&FixtureDate=' + pageDate;
    } else if (start.toUpperCase().split(":")[0] == "TEAMRANKINGS") {
        return 'team_rankings.ashx?clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "CLUBINFO") {
        return 'club_info.ashx?clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "WHATSON") {
        return 'whats_on.ashx?clubID=' + pageID;
       } else if (start.toUpperCase().split(":")[0] == "WHATSONPLUS") {
        return 'whats_on_plus.ashx?clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "CLUBS") {
        if (typeof start.split(":")[2] == "undefined") {
            return 'sport_clubs.ashx?sportID=' + pageID;
        } else {
            pageID = start.split(":")[1]
            letter = start.split(":")[2]
            return 'sport_clubs.ashx?sportID=' + pageID + '&letter=' + letter;
        }	                
    } else if (start.toUpperCase().split(":")[0] == "USEROVERVIEW") {
        return 'user_overview.ashx?id=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "REGISTER") {
        if (pageID == 0) {
            return 'flRegister.ashx';
        } else {
            return 'flRegister.ashx?a=' + pageID;
        }
    } else if (start.toUpperCase().split(":")[0] == "MSO") {
        return 'mso.ashx?msoType=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOCLUBSTEAMS") {
        return 'mso_clubs_teams.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOTEAMRANKINGS") {
        return 'mso_team_rankings.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOWHATSON") {
        return 'mso_whats_on.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOCONTACTS") {
        return 'mso_contacts.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOCURRENTCOMPS") {
        return 'mso_current_comps.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOARCHIVEDCOMPS") {
        return 'mso_archived_comps.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MSOCLUBS") {
        return 'mso_clubs.ashx?mso=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "TEAM") {
        return 'team_view.ashx?TeamID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "WHATSON") {
        return 'whats_on.ashx?clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "ALLFIXTURES") {
        return 'comp_all_fixtures.ashx?DivisionID=' + pageID;        
    } else if (start.toUpperCase().split(":")[0] == "TOPSCORERS") {
		sportID = start.split(":")[1];
		teamID = start.split(":")[2];
		seasonID = start.split(":")[3];
		clubID = start.split(":")[4];
        return 'Scorer_tv.ashx?SportID=' + sportID + '&TeamID=' + teamID + '&SeasonID=' + seasonID + '&ClubID=' + clubID;
    } else if (start.toUpperCase().split(":")[0] == "PMDOVERVIEW") {
        return 'PMD_Overview.ashx?FixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "PMDMATCHREPORT") {
		pageID = start.split(":")[1];
		teamID = start.split(":")[2];
        return 'PMD_MatchReport.ashx?FixtureID=' + pageID + '&TeamID=' + teamID;
    } else if (start.toUpperCase().split(":")[0] == "PMDWHOTOOKPART") {
        return 'PMD_WhoTookPart.ashx?FixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "PMDPHOTO") {
        return 'PMD_Photo.ashx?PhotoID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "PMDPHOTOS") {
        return 'PMD_Photos.ashx?FixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "PMDSUBS") {
        return 'PMD_Substitutions.ashx?FixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "PMDSCORERS") {
        return 'PMD_Scorers.ashx?FixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "USEROVERVIEW") {
        return 'PMD_UserOverview.ashx?id=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "USERCARDS") {
		pageID = start.split(":")[1];
		sportID = start.split(":")[2];
        return 'PMD_UserCardsIssued.ashx?id=' + pageID + '&SportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "USERAPPEARANCES") {
		pageID = start.split(":")[1];
		sportID = start.split(":")[2];
        return 'PMD_UserAppearances.ashx?id=' + pageID + '&SportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "SCORERSUMMARY") {
		pageID = start.split(":")[1];
		sportID = start.split(":")[2];
        return 'user_scorersummary.ashx?id=' + pageID + '&SportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "SCORERSFIXTURES") {
        return 'PMD_Scorers.ashx?fixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBRANKINGSALL") {
        return 'stats_club.ashx?stats=rankings_all&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBHOMEAWAYCURRENT") {
        return 'stats_club.ashx?stats=home_away_current&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBHOMEAWAYALL") {
        return 'stats_club.ashx?stats=home_away_all&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBUNBEATENTEAMSCURRENT") {
        return 'stats_club.ashx?stats=unbeaten_teams_current&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBUNBEATENTEAMSHOME") {
        return 'stats_club.ashx?stats=unbeaten_teams_home&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBUNBEATENTEAMSAWAY") {
        return 'stats_club.ashx?stats=unbeaten_teams_away&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBMOSTSCORED") {
        return 'stats_club.ashx?stats=most_scored&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBMOSTCONCEDED") {
        return 'stats_club.ashx?stats=most_conceded&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBHIGHESTSCORES") {
        return 'stats_club.ashx?stats=highest_scores&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBWINMARGIN") {
        return 'stats_club.ashx?stats=win_margin&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBLOSEMARGIN") {
        return 'stats_club.ashx?stats=lose_margin&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBMOSTSUCCESSFULVENUE") {
        return 'stats_club.ashx?stats=most_successful_venue&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATCLUBMATCHREPORTS") {
        return 'stats_club.ashx?stats=match_reports&clubID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEPERFORMANCECURRENT") {
        return 'stats_league.ashx?leagueId=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEPERFORMANCEALL") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=performance_all';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUECLUBPERFORMANCE") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=club_performance';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEHOMERECORDCURRENT") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=home_record_current';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEAWAYRECORDCURRENT") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=away_record_current';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEWINMARGIN") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=win_margin';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEWINMARGINALL") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=win_margin_all';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEMOSTSCOREDCURRENT") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=most_scored_current';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEHIGHESTSCORING") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=highest_scoring';
    } else if (start.toUpperCase().split(":")[0] == "STATLEAGUEMATCHREPORTS") {
        return 'stats_league.ashx?leagueId=' + pageID +'&stats=match_reports';
    } else if (start.toUpperCase().split(":")[0] == "STATTEAMS") {
        return 'stats_team.ashx?teamId=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "COMPSETTINGS") {
        return 'comp_settings.ashx?divisionId=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "LEAGUESETTINGS") {
        return 'league_settings.ashx?LeagueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MATCHREPORT") {
        return 'match_report.ashx?partId=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "RESULTSDATE") {
        return 'sport_comp_results_date.ashx?LeagueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "USERSEASONS") {
        return 'user_seasons.ashx?id=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "LEAGUEVENUES") {
        return 'venue_league.ashx?leagueID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "BLALLTABLES") {
        return 'BoxLeague_AllTables_View.ashx?CompID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "BLDIV") {
        return 'BoxLeague_Div_View.ashx?DivID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "BLSTATSSUM") {
        clubID = start.split(":")[1]
        playerID = start.split(":")[2]
        sportID = start.split(":")[3]
        return 'BoxLeague_Stats_Summary.ashx?ClubID=' + clubID + '&playerID=' + playerID + '&sportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "BLSTATSHIST") {
        clubID = start.split(":")[1]
        playerID = start.split(":")[2]
        sportID = start.split(":")[3]
        return 'BoxLeague_Stats_LeagueHistory.ashx?ClubID=' + clubID + '&playerID=' + playerID + '&sportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "BLVIRTUAL") {
        clubID = start.split(":")[1]
        sportID = start.split(":")[2]
        return 'BoxLeague_VirtualTable.ashx?ClubID=' + clubID + '&sportID=' + sportID;
    } else if (start.toUpperCase().split(":")[0] == "AUDIT") {
        if (typeof start.split(":")[3] == "undefined") {
            return 'flSports.ashx';
        } else {
            teamID = start.split(":")[1]
            teamID2 = start.split(":")[2]
            f = start.split(":")[3]
            return 'audit.ashx?teamID=' + teamID + '&teamID2=' + teamID2 + '&f=' + f; 
        }
    } else if (start.toUpperCase().split(":")[0] == "HISTORY") {
        if (typeof start.split(":")[2] == "undefined") {
            return 'flSports.ashx';
        } else {
            teamID = start.split(":")[1]
            teamID2 = start.split(":")[2]
            return 'history.ashx?teamID=' + teamID + '&teamID2=' + teamID2; 
        }
    } else if (start.toUpperCase().split(":")[0] == "SCORERSFIXTURES") {
        return 'scorer_fixtures.ashx?fixtureID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "SCORERSDIVISION") {
        return 'scorer.ashx?divisionID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "SCORERSALL") {
        return 'scorer.ashx?stats=all&divisionID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "SCORERSSEASON") {
        return 'scorer.ashx?stats=season&divisionID=' + pageID;
    } else if (start.toUpperCase().split(":")[0] == "MYINFO") {
        return 'my_info.ashx';
	} else if (start.toUpperCase().split(":")[0] == "MYFRIENDSRIVALS") {
        return 'my_friends_rivals.ashx';
	} else if (start.toUpperCase().split(":")[0] == "PHOTOSFIX") {
        return 'photo_fixtures.ashx?FixtureID=' + pageID;
	} else if (start.toUpperCase().split(":")[0] == "TEAMPHOTO") {
		pageID = start.split(":")[1];
		teamID = start.split(":")[2];
		var albumID = 	start.split(":")[3];										
        return 'photo.ashx?PhotoID=' + pageID + '&teamID=' + teamID + '&albumID=' + albumID;
    } else if (start.toUpperCase().split(":")[0] == "FIXTUREPHOTO") {
		pageID = start.split(":")[1];
		var fixtureID = start.split(":")[2];
		var albumID = start.split(":")[3];							
        return 'photo.ashx?PhotoID=' + pageID + '&fixtureID=' + fixtureID + '&albumID=' + albumID;
       } else if (start.toUpperCase().split(":")[0] == "RSSFEED") {
        var rssFeedID = start.split(":")[1];
        return 'rssfeed.ashx?rssFeedID=' + rssFeedID;
    } else {
        return 'flSports.ashx';
    }
}

// fading
var TimeToFade = 250.0;

function fade(eid){
	var element = document.getElementById(eid);
	if (element == null) {
		return;
	}
	
	if(element.FadeState == null) {
		element.FadeState = -2;
	}
	
	if(element.FadeState == 1 || element.FadeState == -1) {
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
	} 
	else {
		element.FadeState = element.FadeState == 2 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade;
		setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
	}
}

function animateFade(lastTick, eid){	
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	
	var element = document.getElementById(eid);
	
	if (element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	if(element.FadeState == 1) {
		newOpVal = 1 - newOpVal;
	}

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}


// function to get various sizes out of the browser
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


// make sure the div that blurs out the main page covers it
function blanket_size() {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	// get height of popup
//	var popup = document.getElementById('popUpDiv');
//	alert(popup.style.pixelTop + ' , ' + popup.offsetHeight);
	var blanket = document.getElementById('blanket');
	blanket.style.top = f_scrollTop();
	blanket.style.width = f_clientWidth() + 'px';
	blanket.style.height = f_clientHeight() + 'px'; // blanket_height + 'px';
	
//	alert(blanket_height + 'px');
}


// check the height of the blanket
function checkHeight()
{
	if (timer_is_on) {
		blanket_size();
		window_pos('popUpDiv');
	}
	
//	t = setTimeout('checkHeight()', 250);
}


// work out where to put the popup
function window_pos(popUpDivVar) {
//	if (typeof window.innerWidth != 'undefined') {
//		viewportwidth = window.innerHeight;
//	} else {
//		viewportwidth = document.documentElement.clientHeight;
//	}
	viewportwidth = f_clientHeight();

	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width = (window_width / 2) - 300;//300 is half popup's width
	popUpDiv.style.top = f_scrollTop() + 50 + 'px';
	popUpDiv.style.left = window_width + 'px';
	popUpDiv.style.height = (viewportwidth - 100) + 'px';
}


// toggle the appearance of a div
function toggle(div_id) {
	var el = document.getElementById(div_id);
	
	if (el.style.display == 'none') {	
		el.style.display = 'block';
	}
	else {
		el.style.display = 'none';
	}
}


// Show the popup and greying out bit
function showFLPopup(windowname) {
	blanket_size();
	window_pos(windowname);
	toggle('blanket');
//	fade('blanket');
	toggle(windowname);		
}


// Hide popup
function hideFLPopup(windowname) {
	toggle(windowname);
//	fade('blanket');
	toggle('blanket');
}


// Fixtures live integration - show FL div
function ShowFL(start) {
	goto = DecodeSP(start);
	navigate(goto);
	
	showFLPopup('popUpDiv');
	
	if (!timer_is_on) {
		timer_is_on = 1;
//		checkHeight();
	}
}


// Hide FL div again
function HideFL() {
//	clearTimeout(t);
	timer_is_on = 0;
	
	hideFLPopup('popUpDiv');
}


