// GLOBAL VARIABLES
//--------------------------------------------------------
var queryString = window.top.location.search.substring(1);

var cId = getParameter(queryString, "contestId");
if (CONTEST)
	cId = CONTEST;

var pType = getParameter(queryString, "pageType");
if (TYPE)
	pType = TYPE;

var vendorId = getParameter(queryString, "vendorId");
if (VENDOR)
	vendorId = VENDOR;

var homeId = getParameter(queryString, "vendorHomeTeam");
if (VENDORHOME)
	homeId = VENDORHOME;

var visitId = getParameter(queryString, "vendorVisitTeam");
if (VENDORVISIT)
	visitId = VENDORVISIT;

var viewcastPath = PATH.replace('scoreboards','viewcast');
if (VIEWCAST_PATH)
	viewcastPath = VIEWCAST_PATH;
	
var viewcastMap = null;
photoPage = 1;
var pollingTimerList = new Array();

// Flag that tells mainController that it's a viewcast page and not a scoreboard
isViewcast = true;

//--------------------------------------------------------
function setCurrentPhotoPage(pageNum)
{
    photoPage = pageNum;
}
function getCurrentPhotoPage()
{
    return photoPage;
}

// this function resets the defaultPhotoPage and redirects to correct photo page
function showPhotoPage (pageNum)
{
    setCurrentPhotoPage(pageNum);
    viewcastMap = getViewcastMap(cId, vendorId, homeId, visitId, pType);
    showContest(cId, vendorId, homeId, visitId, "photos");
}

function populateDivs()
{
    var pageConfig = viewcastMap[pType];
    
    if (pageConfig)
    {
        // populate the page with correct page grid
        getViewcastContent("viewcast_content", pageConfig.page, false);
        
        // get a list of ajaxed includes and populate them
        // set timers on those includes
        
        for (var container in pageConfig.includes)
        {
            // populate
            getViewcastContent(container, pageConfig.includes[container], true);
            
            // set polling timer
            var timerId = window.setInterval(createViewcastReloadClosure(container, pageConfig.includes[container]), INTERVAL);
                pollingTimerList[pollingTimerList.length] = timerId;
        }
        
        // run the function if there is anything to be done.
        pageConfig.afterload();
    }    
}

function createViewcastReloadClosure(container, content) {
    return function () { getViewcastContent(container, content); }
}


function viewcastPageLoad()
{
    scoreboardsInitialPageLoad(PATH);

    // Populate bottom of the page with the requested content for a particulat contest
    viewcastMap = getViewcastMap(cId, vendorId, homeId, visitId, pType);
    
    populateDivs();
}

function getViewcastContent(container, content, async)
{
    //alert ("content:" + content);
    if (async == undefined) async = true;
    var httpRequest;
    if (window.XMLHttpRequest)
    {
        try { httpRequest = new XMLHttpRequest(); }
        catch(e) { }
    }
    else if (window.ActiveXObject)
    {
        try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e)
        { 
                   try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
                   catch(e) {}
        }
    }
            
    if (!httpRequest) { return false; }

    var url = content  + (content.indexOf("?") > -1 ? "&" : "?") + new Date().getTime();
    httpRequest.open('GET', url, async);
    if (async) 
    {
        httpRequest.onreadystatechange = function() 
        {
            switch(httpRequest.readyState) 
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    try {
                        if (httpRequest.status  == 200) { populateViewcast(container, httpRequest.responseText);}
                        else if (httpRequest.status  == 404) { }
                        else { }
                    } catch(e) {  }
                    break;
                default:
                    break;
            }
        }
    }
    // make the Ajax request
    httpRequest.send("");
    if (!async)
    populateViewcast(container, httpRequest.responseText);
}

function populateViewcast (container, content)
{
    // find container, clear it out, and insert new content
    var elem = document.getElementById(container);
    if (elem)
    {
            elem.innerHTML = content;
    } else {
    }
}

function getParameter (queryString, parameterName)
{
    // parse the request string and find parameter by name
    var parameterName = parameterName + "=";
    if (queryString.length > 0) 
    {
        var begin = queryString.indexOf (parameterName);
        if (begin != -1)
        {
            begin += parameterName.length;
            var end = queryString.indexOf ("&" , begin);
            if (end == -1)
            {
                end = queryString.length;
            }
            return unescape (queryString.substring (begin, end));
       }
       return "null";
   }
}

function showContest(contestId, vId, hTeam, vTeam, pageType)
{
   // compare current contestId with the newly requested one
   // if they are different, reload the contest map
   
   if (parseInt(contestId) != parseInt(cId))
   {    
                //alert ("new contest requested: new=" + contestId + ", old=" + cId);
           cId = contestId;
           vendorId = vId;
           homeId = hTeam;
           visitId = vTeam;
           setCurrentPhotoPage(1);
           
           viewcastMap = getViewcastMap(contestId, vId, hTeam, vTeam, pageType);
    }
   // else { alert ("No changes in contest. Page Num is" + photoPage);}
   pType = pageType;

   // remove all polling timers (except the top games)
   clearTimers();

   // first insert initial content in the right slots
   populateDivs();
}

function clearTimers()
{
    // get rid of all the contest based timers one by one
    var tempId;
    for (var i = 0; i < pollingTimerList.length; i++)
    {
        tempId = pollingTimerList[i];
        window.clearInterval(tempId);
    }
    // clear array of contest based timers
    pollingTimerList = new Array();
}

function setClassName(elem, newClass)
{
    // reassign class name to an element by id
    var myElem = null;
    myElem = document.getElementById(elem);
    if (myElem != null)
        myElem.className = newClass;
}

