/* utility methods */

function openWin(theURL, Name, popW, popH, scroll) 
{ // V 1.0
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable'
	Win = window.open(theURL, Name, winProp)
	if (parseInt(navigator.appVersion) >= 4) 
	{ 
		Win.window.focus(); 
	}
}

function GetParam(strParamName)
{
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1)
    {
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++)
        {
            if(aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1)
            {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
} 


/* methods for handling validation */

function EnableValidator(validatorId, enable)
{
    var validator = document.getElementById(validatorId);
    validator.enabled = enable;
//    ValidatorEnable(validator, enable); 
}

/* methods for showing and hiding elements */

function Show(element)
{
    element.setAttribute("class", "");
    element.setAttribute("className", "");
}

function Show(element, classes)
{
    element.setAttribute("class", classes);
    element.setAttribute("className", classes);
}

function Hide(element)
{
    element.setAttribute("class", "hide");
    element.setAttribute("className", "hide");
}

function SetClass(elementId, classes)
{
    var element = document.getElementById(elementId);
    element.setAttribute("class", classes);
    element.setAttribute("className", classes);
}

/* form submit methods */

function DisableButton(btnSubmitUniqueId)
{
    //disables button on form submit to prevent multiple form submissions.
    //check if form is valid.
    if (typeof(Page_ClientValidate) == 'function') 
    {
        if (Page_ClientValidate() == false) 
        { 
            return false; 
        }
    }
    //hide button.
    var buttons = document.getElementById("buttons");
    if(buttons != null)
    {
        Hide(buttons);
    }
    //hide error message.
    var error = document.getElementById("error");
    if(error != null)
    {
        Hide(error);
    }
//    //postback page to submit form request.
//    __doPostBack(btnSubmitUniqueId,'');
}

/* game box updates */

function FadeOut(element, fadeTime, callbackFunction) {
    //fade out.
    var fadeArgs = {
        node: element,
        duration: fadeTime
    };
    dojo.fadeOut(fadeArgs).play();
    //call callback function after fade is complete.
    if (typeof (callbackFunction) != 'undefined') {
        window.setTimeout(callbackFunction + "();", fadeTime);
    }
}

function FadeIn(element, fadeTime) {
    //fade in.
    dojo.style(element, "opacity", "0");
    var fadeArgs = {
        node: element,
        duration: fadeTime
    };
    dojo.fadeIn(fadeArgs).play();
}

function GetGameStats(gameId) {
    //get latest score.
    PageMethods.GetGameStats(gameId, DisplayGameStats, AjaxError);
}
function DisplayGameStats(json) {
    var game = eval('(' + json + ')');
    if (game != null) {
        //check if games stats have changed.
        var spnCurrentTimeInGame = dojo.byId("spnCurrentTimeInGame_" + game.Id);
        if (game.CurrentTimeInGame != spnCurrentTimeInGame.innerHTML) {
            FadeOut(spnCurrentTimeInGame, 500);
            window.setTimeout(function() { FadeInGameStat(spnCurrentTimeInGame, game.CurrentTimeInGame); }, 500);
        }
        var spnHomeTeamScore = dojo.byId("spnHomeTeamScore_" + game.Id);
        if (game.HomeTeam.Score != spnHomeTeamScore.innerHTML) {
            FadeOut(spnHomeTeamScore, 500);
            window.setTimeout(function() { FadeInGameStat(spnHomeTeamScore, game.HomeTeam.Score); }, 500);
        }
        var spnAwayTeamScore = dojo.byId("spnAwayTeamScore_" + game.Id);
        if (game.AwayTeam.Score != spnAwayTeamScore.innerHTML) {
            FadeOut(spnAwayTeamScore, 500);
            window.setTimeout(function() { FadeInGameStat(spnAwayTeamScore, game.AwayTeam.Score); }, 500);
        }
        //call again in 30 seconds.
        window.setTimeout(function() { GetGameStats(game.Id); }, 30000);
    }
}
function FadeInGameStat(control, value) {
    //set value.
    control.innerHTML = value;
    //fade in control and show in red for 5 seconds.
    FadeIn(control, 500);
    Show(control, "red bold");
    window.setTimeout(function() { Show(control, ""); }, 5500);
}

function AjaxError(error) {
    var errorMessage = error.get_message();
    console.debug("ASP.NET Ajax Extensions error: " + errorMessage);
}