// Contains the javascript for the tournament central page,
// the view over all holes as well as the extended hole information,
// the performance alerts and the explore course image part



//Define Globals
var cycle = 150;
var groupInfo;
var courseData;
var holeDataPY;
var plyrHole;
//var fav_plyr="";
//var prfrd_plyrs = ;
var selectedPlyr=readCookie('selectedPlyr');
var selectedHole;
var holeDtlTitles = new Array('Player','Location','Position','Today','Total','Details');

//--------------------------------------------------------------------//
//--------------------------------------------------------------------//
//Define Object AJAX
var http = new getHTTPObject();
//--------------------------------------------------------------------//
//--------------------------------------------------------------------//

/** Get & Parse Player Data **/
function parse_playerData(argObj) {
     //All the data
     //var data = http.responseText;
     var data = argObj.data;
     if( !data ) return;
     //Split the data by line, each new line = new hole number
     holeData =  data.split('\n');
     //Where everything will eventually be stored
     groupInfo = new Array();
     //Get hole, next hole, and holeStorage which saves current hole after run through
     var hole; 
     var nextHole;
     var holeStorage;
     //Object to store each player's info
     plyrHole = new Object();
     //Story Players in there unique groups
     var group = new Array();

     //Loop through each hole
     for(i=0;i<holeData.length;i++){
       //Make sure of valid data
       var cleanup = /^<!-.*/;

       if (holeData[i] && !cleanup.test(holeData[i]) ){

           //Split the specific hole we are on by the pipe seperating key parts
           specHoleData = holeData[i].split('|');
           //Current Hole
           var holeKV = specHoleData[0].split('=');
           hole = holeKV[1];
           //Next Hole
           if (holeData[i+1] && !cleanup.test(holeData[i+1])){
                nextHoleData = holeData[i+1].split('|');
                var nextHoleKV = nextHoleData[0].split('=');  
                nextHole = nextHoleKV[1];
            }else{
                nextHole = null;
            } 

           var grpKV = specHoleData[1].split('=');
           grpKey = grpKV[1];

           grpPlyr = specHoleData[2].split('^^');
           //Store players for a group in the player array
           var player = new Array();
           //Unique object for player
           var plyrObj = plyrObj+i;
           for (j=0;j<grpPlyr.length;j++){
                //Split again, by ^ - this give me key=value
                var plyrKeys = grpPlyr[j].split('^');
                plyrObj = new Object();
                //Loop through each key=value, and split by =
                for (jj = 0; jj<plyrKeys.length; jj++){
                   var plyrKV = plyrKeys[jj].split('=');
                   plyrObj[plyrKV[0]] = plyrKV[1]; 
                }
                //Associate plyrid w/ hole number
                plyrHole[plyrObj.plyrid] = hole; 
                player.push(plyrObj);
            }

           //Push into hole array
           if ( holeStorage != hole ){
                 group.push(player);
                 if (hole != nextHole){ 
                    groupInfo.push(group);
                    group = new Array();
                }
           }else{
                group.push(player);
                if(hole != nextHole){
                    groupInfo.push(group);
                    group = new Array();
                }
           }
                
           holeStorage = hole;
        }

    }
    return groupInfo;
}

function get_playerData(argObj) {
    http.open('get',argObj.url + '?r=' + Math.floor(Math.random()*10000),false);
    http.send(null);
    return parse_playerData({data:http.responseText});
}

//--------------------------------------------------------------------//

/** Get & Parse Course Data **/
function parse_courseData(argObj){
    var course_id = courseId;
    var data = argObj.data;
    if ( !data ) return;
    //Split the data: COURSEID|COURSECODE|COURSENAME|ROUND 
    courseInfo =  data.split('\n');
    var cleanup = /^<!-.*/;
    var course = new Object();
    for (i=0;i<courseInfo.length;i++){
        if ( courseInfo[i] && !cleanup.test(courseInfo[i])){
                var courseInfoKV = courseInfo[i].split('|');
                if ( courseInfoKV[0] == parseFloat(course_id)){
                    course['name'] = courseInfoKV[2];
                    course['round'] = courseInfoKV[3];
                }
        }
    }

    return course;
}

function get_courseData(argObj) {
    http.open('get',argObj.url + '?r=' + Math.floor(Math.random()*10000),false);
    http.send(null);
    return parse_courseData({data:http.responseText});
}
//--------------------------------------------------------------------//

/** Get & Performance Alert**/
function parse_PAlertData(argObj){
    var data = argObj.data;
    //Split the data: \n
    var prfrmObj = document.getElementById('prfrmAlrt');
    var prfrmTxtObj = document.getElementById('prfrmTxt');
    var p_alert =  data.split('\n');
    var cleanup = /^<!-.*/;
    if (!cleanup.test(p_alert[0])){
        p_alert.pop();
        p_alert=p_alert.reverse();
        var time = p_alert[0].match(/^\w{2}\:\w{2}\w{2}\s\w{2}/)
        var remainder = p_alert[0].replace(/^\w{2}\:\w{2}\w{2}\s\w{2}\s/,'')
        prfrmObj.style.display = 'block';
        prfrmTxtObj.innerHTML = time + ": <b>"+remainder+"</b>";
    }else{
        prfrmObj.style.display = 'none';
    }
    return;
}

function get_PAlertData(argObj) {
    var cancel_req = 0;
    if (cancel_req) return;
    http.open('get',argObj.url + '?r=' + Math.floor(Math.random()*10000),false);
    http.send(null);
    return parse_PAlertData({data:http.responseText});
}


//--------------------------------------------------------------------//

/** Get & Parse Hole Data **/
function parse_holeData(argObj){
    var data = argObj.data;
    if( !data ) return;
    //Split the data by line, each new line = new hole number
    //HOLE|PAR|YARDAGE
    var hpyInfo =  data.split('\n');
    var hpySorted = new Array();
    var cleanup = /^<!-.*/;
    for (i = 0; i < hpyInfo.length; i++){
        var hpyObj=hpyObj+1;
        if ( hpyInfo[i] && !cleanup.test(hpyInfo[i])){
            hpyObj = new Object();
            holeSpecs = hpyInfo[i].split('|');
            hpyObj['par'] = holeSpecs[1]; 
            hpyObj['yards'] = holeSpecs[2]; 
        }
        hpySorted.push(hpyObj);
    }
    return hpySorted;
}

function get_holeData(argObj) {
    http.open('get',argObj.url + '?r=' + Math.floor(Math.random()*10000),false);
    http.send(null);
    return parse_holeData({data:http.responseText});
}

//--------------------------------------------------------------------//

/** Get message **/

function get_messageHtml(argObj) {
    http.open('get',argObj.url + '?r=' + Math.floor(Math.random()*10000),false);
    http.send(null);
    return http.responseText;
}
//--------------------------------------------------------------------//
//--------------------------------------------------------------------//

/** Function builds small group tables **/
function holeTable_bldr(argObj) {
    if (!argObj.groupInfo) return;
    mybody = document.getElementById(argObj.holeId);
    // creates an element whose tag name is table
    holeTable = document.createElement('table');
    holeTable_body = document.createElement('tbody');
    holeTable.cellSpacing = 1;
    holeTable.cellPadding = 0;
    holeTable.border = 0;
    holeTable.width = '100%';

    ttlHoleRow = document.createElement('tr');
    ttlHoleCell = document.createElement('td');
    holeNumber = argObj.holeId.split('hole');

    ttlHoleCell.innerHTML = '<b><span class="fauxlink" onmouseover=\'this.className=\"fauxlinkover\"\' onmouseout=\'this.className=\"fauxlink\"\' onclick="javascript:get_Hole(\'hole'+holeNumber[1]+'\')">Hole '+holeNumber[1]+'</span></b> '+ argObj.holeData[holeNumber[1]-1].par;
    ttlHoleCell.colSpan = 2;
    ttlHoleCell.className = 'holeTitle';
    ttlHoleRow.appendChild(ttlHoleCell);
    holeTable_body.appendChild(ttlHoleRow);

    // creating all cells
    //If no hole data - return;
    if( argObj.groupInfo[0][0].plyrid != null ){
        for(group=0;group<argObj.groupInfo.length;group++) {
            //creates an element whose tag name is tr
            loc_row = document.createElement('tr');
            loc_cell = document.createElement('td');
            loc_cell.innerHTML = '<b>' + argObj.groupInfo[group][0].loc + '</b>'; 
            loc_cell.colSpan = 2;
            
            //Get Specific class from location
            cssClass = get_locClass(argObj.groupInfo[group][0].loc);            
            loc_cell.className = cssClass;
            loc_row.appendChild(loc_cell);

            holeTable_body.appendChild(loc_row);

            for(player=0;player<argObj.groupInfo[group].length;player++) {
                current_row=document.createElement('tr');
                current_row.setAttribute('id', argObj.groupInfo[group][player].plyrid);
                // creates an element whose tag name is td
                cell1=document.createElement('td');
                cell2=document.createElement('td');
                // creates a Text Node
                //cell1.innerHTML = "<span class='fauxlink' onmouseover='this.className=\"fauxlinkover\"' onmouseout='this.className=\"fauxlink\"' onclick='javascript:set_Plyr("+argObj.groupInfo[group][player].plyrid+")'>"+argObj.groupInfo[group][player].name+"</span>";
                
                // URL: player page
                cell1.innerHTML = '<a href="' + getPlayerUrl(argObj.groupInfo[group][player].plyrid) + '">' + argObj.groupInfo[group][player].name+"</a>";                
                //cell1.innerHTML = "<a href='/players/scorecard/"+argObj.groupInfo[group][player].plyrid+"/r475/2006' target='_blank'>"+argObj.groupInfo[group][player].name+"</a>";
                cell2Txt = document.createTextNode(argObj.groupInfo[group][player].tscr);
                cell2.appendChild(cell2Txt);
                cell2.align='center';
                cell2.className = cssClass;
                // appends the cell td into the row tr
                current_row.appendChild(cell1);
                current_row.appendChild(cell2);
                // appends the row tr into tbody
                holeTable_body.appendChild(current_row);
            }
        }        
    }else{
        noplayers_row = document.createElement('tr');
        noplayers_cell = document.createElement('td');
        noplayers_cell.innerHTML = 'No Players';
        noplayers_row.appendChild(noplayers_cell);
        holeTable_body.appendChild(noplayers_row);
    }

    holeTable.appendChild(holeTable_body);
    document.getElementById(argObj.holeId).innerHTML = '';
    mybody.appendChild(holeTable);

    //Highlight selected player 
    defineSelected(holeNumber[1]);
}

function getPlayerUrl(playerId) {
    while (playerId.length < 6) {
        playerId = '0' + playerId;
	}
	
	return '/players/' + playerId.substring(0, 2) + '/' +
                         playerId.substring(2, 4) + '/' +
                         playerId.substring(4, 6) + '/scorecards/' +
                         currentYear + '/' + tourCode + tournId + '.html';
}


//--------------------------------------------------------------------//

/** Larger Detail Hole w/img Table Builder **/
function holeDetailTable_bldr(argObj) {
    if(!argObj.groupInfo) return;
    //argObj.holeCall, if set - skips selected player defs when building
    //No highlighting
    if( selectedPlyr && !argObj.holeCall){
        argObj.holeNumber = plyrHole[selectedPlyr];
        argObj.holeNumber = ( argObj.holeNumber ) ? argObj.holeNumber : 1;
        argObj.groupInfo = groupInfo[argObj.holeNumber-1];
    }
    
    //Set Image
    holeImg = document.getElementById('hdImg');
    holeImgNum = ( argObj.holeNumber < 10 ) ? "0"+argObj.holeNumber : argObj.holeNumber;
    
	holeImg.src = "http://i.pgatour.com/pgatour/courses/" + tourCode + courseId + "/hole-images/" + currentYear + "/hole-" + argObj.holeNumber + "-full-220x100.jpg"; 
    holeImg.alt = "Hole "+holeImgNum;
    holeDetailTxt = document.getElementById('hdTxt');
    holeDetailTxt.innerHTML = '';
    
    // URL: explore the course
    //holeDetailTxt.innerHTML = "<a target='_blank' href='/course/hole/r475/665/"+argObj.holeNumber+"'>Click here for Explore the Course</a>";
    holeDetailTxt.innerHTML = "<a href='/courses/" + tourCode + courseId + "/index.html'>Click here for Explore the Course</a>";
    holeDetailLink = document.getElementById('hdLink');
    
	// URL: explore the course
    //holeDetailLink.setAttribute("href","/course/hole/r475/665/"+argObj.holeNumber );
    holeDetailLink.setAttribute("href","/courses/" + tourCode + courseId + "/index.html" );
    holeDetailLink.setAttribute("target", "_top"); // currently set to _blank in xsl
    
    //If no hole data - return;
    mybody = document.getElementById('holeDetail');
    // creates an element whose tag name is table
    holeTable = document.createElement('table');
    holeTable_body = document.createElement('tbody');
    holeTable.cellSpacing = 1;
    holeTable.cellPadding = 0;
    holeTable.border = 0;
    holeTable.width = '100%';

    ttlHoleRow = document.createElement('tr');
    ttlHoleCell = document.createElement('td');
    ttlHoleCell.innerHTML = '<b>Hole '+argObj.holeNumber+'</b> '+ argObj.holeData[argObj.holeNumber-1].par+' - '+argObj.holeData[argObj.holeNumber-1].yards;
    ttlHoleCell.colSpan = holeDtlTitles.length;
    ttlHoleCell.className = 'detailTitle';
    ttlHoleRow.appendChild(ttlHoleCell);
    holeTable_body.appendChild(ttlHoleRow);

    subTtlRow = document.createElement('tr');
    subTtlRow.className = 'subTitles';
    //In global - defines column titles
    for (i=0;i<holeDtlTitles.length;i++){
        subTtlCell = document.createElement('td');
        if (holeDtlTitles[i] == 'Location' || holeDtlTitles[i] == 'Position' || holeDtlTitles[i] == 'Today' || holeDtlTitles[i] == 'Total'){
            subTtlCell.setAttribute('align','center');
        }
        subTtlCell.innerHTML = holeDtlTitles[i];
        subTtlRow.appendChild(subTtlCell);
    }
    holeTable_body.appendChild(subTtlRow);

    // creating all cells
    if( argObj.groupInfo[0][0].plyrid != null){
        for(group=0;group<argObj.groupInfo.length;group++) {
            //Get Specific class from location
            cssClass = get_locClass(argObj.groupInfo[group][0].loc);            

            for(player=0;player<argObj.groupInfo[group].length;player++) {
                current_row=document.createElement('tr');
                current_row.setAttribute('id', 'sh'+argObj.groupInfo[group][player].plyrid);
                current_row.className = cssClass;
                for (i=0;i<holeDtlTitles.length;i++){
                    current_cell = document.createElement('td');
    
                    if (holeDtlTitles[i] == 'Location' || holeDtlTitles[i] == 'Position' || holeDtlTitles[i] == 'Today' || holeDtlTitles[i] == 'Total'){
                        current_cell.setAttribute('align','center');
                    }

                    switch(holeDtlTitles[i]){
                        //case 'Player'   : specData = '<b>'+argObj.groupInfo[group][player].name+'</b>'; break;
                        case 'Player'   : specData = '<a href="' + getPlayerUrl(argObj.groupInfo[group][player].plyrid) + '">' + argObj.groupInfo[group][player].name+"</a>"; break;
                        case 'Position' : specData = '<b>'+argObj.groupInfo[group][player].pos+'</b>';  break;
                        case 'Location' : specData = argObj.groupInfo[group][player].loc;               break;
                        case 'Today'    : specData = '<b>'+argObj.groupInfo[group][player].scr+'</b>';  break;
                        case 'Total'    : specData = '<b>'+argObj.groupInfo[group][player].tscr+'</b>'; break;
                        case 'Details'  : specData = argObj.groupInfo[group][player].dtl;               break;
                        default: specData = '';
                    }
                    current_cell.innerHTML = specData; 
                    current_row.appendChild(current_cell);
                }
                // appends the row tr into tbody
                holeTable_body.appendChild(current_row);
            }
        }        
    }else{
        //No player on hole logic
        noplayers_row = document.createElement('tr');
        noplayers_cell = document.createElement('td');
        noplayers_cell.colSpan = holeDtlTitles.length;
        noplayers_cell.className = 'noplayers';
        noplayers_cell.innerHTML = 'No Players';
        noplayers_row.appendChild(noplayers_cell);
        holeTable_body.appendChild(noplayers_row);
    }

    holeTable.appendChild(holeTable_body);
    document.getElementById('holeDetail').innerHTML = '';
    mybody.appendChild(holeTable);
    //Hightlight Selected Player
    defineSelected(argObj.holeNumber);
}

//--------------------------------------------------------------------//

/** Shared table builder functions **/
function defineSelected(holeNumber){
    /*
    //Preferred and Favorite
    if( fav_plyr ){
        if (document.getElementById(fav_plyr)) document.getElementById(fav_plyr).className = 'favselected';
        if (document.getElementById('sh'+fav_plyr))  document.getElementById('sh'+fav_plyr).className = 'favselected';
    }

    if( prfrd_plyrs ){
        if ( prfrd_plyrs.length ){
            for(i=0;i<prfrd_plyrs.length;i++){
                 if (document.getElementById(prfrd_plyrs[i])) document.getElementById(prfrd_plyrs[i]).className = 'prfdselected';
                 if (document.getElementById('sh'+prfrd_plyrs[i]))  document.getElementById('sh'+prfrd_plyrs[i]).className = 'prfdselected';
            }
        }else{
             if (document.getElementById(prfrd_plyrs)) document.getElementById(prfrd_plyrs).className = 'prfdselected';
             if (document.getElementById('sh'+prfrd_plyrs))  document.getElementById('sh'+prfrd_plyrs).className = 'prfdvelected';
        }
    }
    */

    if ( selectedPlyr && plyrHole[selectedPlyr] == holeNumber && !selectedHole ){
        document.getElementById(selectedPlyr).className = 'selected';
        if (document.getElementById('sh'+selectedPlyr)) document.getElementById('sh'+selectedPlyr).className = 'selected';
        selectedHole = 'hole'+holeNumber;
    }else if( selectedPlyr && plyrHole[selectedPlyr] == holeNumber && selectedHole){
         document.getElementById(selectedHole).firstChild.style.border = 'none';
         document.getElementById(selectedPlyr).className = 'selected';
         if (document.getElementById('sh'+selectedPlyr)) document.getElementById('sh'+selectedPlyr).className = 'selected';
         //If get here always make sure selected hole is set
         selectedHole = 'hole'+holeNumber;
         document.holeDD.holeNum.value=plyrHole[selectedPlyr];
    }
}
function get_locClass(location){
    //Get Specific class from location
    switch(location){
        case 'Fairway' : cssClass = 'fw'; break;
        case 'Green'   : cssClass = 'gr'; break;
        case 'Tee Box' : cssClass = 'tee'; break;
        case 'Tee'     : cssClass = 'tee'; break;
        default: cssClass = 'fw';
    }
    return cssClass;
}

function set_Course(){
    document.getElementById('courseName').innerHTML = courseData.name;
}

function set_Round(){
    document.getElementById('courseRound').innerHTML = courseData.round;
}


// PENDING: what does this cookie do?
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name)
{
    createCookie(name,"",-1);
}

//--------------------------------------------------------------------//
//--------------------------------------------------------------------//

/** Set plyr you select **/
function set_Plyr(plyrId){
    var agree = (!plyrHole[plyrId])? confirm("Player not on course. Click OK to have the player highlighted when he starts to play.") : true;
    if (!agree) return;

    //Match dd to selected plyr
    document.plyrDD.player_id.value=plyrId;   
    if ( plyrHole[plyrId] ) document.holeDD.holeNum.value=plyrHole[plyrId];

    var selectedPlyr_bak = ( selectedPlyr ) ? selectedPlyr : 0;
    selectedPlyr = plyrId
    //Set new plyr
    createCookie('selectedPlyr', plyrId, 7);
    //Set back to gray around image just in case
    if (plyrHole[plyrId]) document.getElementById('holeDetailImg').style.border = "1px solid #ccc";
    //Get plyr
    get_Plyr(plyrId,selectedPlyr_bak);
}

/** Get plyr, i.e., set UI **/
function get_Plyr(plyrId,plyrId_bak){
    //If previous player != null
    if ( plyrId_bak ){
        //Set previous hole
        var holeNumber_prev = plyrHole[plyrId_bak];
        //Check first if previous hole is set - i.e., if prev selected player is not on course - no hole would be set
        if ( holeNumber_prev ){
            holeTable_bldr({groupInfo:groupInfo[holeNumber_prev-1],holeData:holeDataPY,holeId:'hole'+ holeNumber_prev});
            if ( plyrHole[plyrId] ){
                //Player exists on board
                holeTable_bldr({groupInfo:groupInfo[plyrHole[plyrId]-1],holeData:holeDataPY,holeId:'hole'+ plyrHole[plyrId]});
                holeDetailTable_bldr({groupInfo:groupInfo[plyrHole[plyrId]-1],holeData:holeDataPY,holeNumber:plyrHole[plyrId]});
            }else{
                //If you have a selected player - and you select someone not on the course - set current selected hole as default hole
                selectedHole = (selectedHole) ? selectedHole : 'hole'+holeNumber_prev;
                get_Hole(selectedHole);
            }
        }else{
            //Previous player does not exist on board
            holeTable_bldr({groupInfo:groupInfo[plyrHole[plyrId]-1],holeData:holeDataPY,holeId:'hole'+ plyrHole[plyrId]});
            holeDetailTable_bldr({groupInfo:groupInfo[plyrHole[plyrId]-1],holeData:holeDataPY,holeNumber:plyrHole[plyrId]});
        }
        

    }else{
        //Set new player
        var holeNumber =  plyrHole[plyrId];
        holeTable_bldr({groupInfo:groupInfo[holeNumber-1],holeData:holeDataPY,holeId:'hole'+ holeNumber});
        holeDetailTable_bldr({groupInfo:groupInfo[holeNumber-1],holeData:holeDataPY,holeNumber:holeNumber});
    }

}

/** Get Hole Info, set hole dtl table **/
function get_Hole(holeNum){
    selectedPlyr_bak = ( selectedPlyr ) ? selectedPlyr : 0;
    selectedPlyr_hole = ( selectedPlyr ) ? 'hole'+plyrHole[selectedPlyr] : 0;
    selectedHole_prev = ( selectedHole ) ? selectedHole : 0;
    selectedHole = holeNum;


    holeOnly = selectedHole.split('hole');

    document.getElementById('holeDetailImg').style.border = "1px solid #98200F";

    //Set drop down to match current hole
    document.holeDD.holeNum.value=holeOnly[1];

    //No previous hole, no previous sel plyr hole, and no selected plyr
    if( !selectedHole_prev && !selectedPlyr_hole && !selectedPlyr){
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
    //No sel prev hole, no sel plyr hole, but sel player - not on board 
    }else if(!selectedHole_prev && !selectedPlyr_hole && selectedPlyr){
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
    //No sel prev hole, sel plyr hole (on board), but doesn't equal hole clicked
    }else if(!selectedHole_prev && selectedPlyr_hole != selectedHole){
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";

        selectedPlyr = null;
        holeTable_bldr({groupInfo:groupInfo[plyrHole[selectedPlyr_bak]-1],holeData:holeDataPY,holeId:'hole'+plyrHole[selectedPlyr_bak]});
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
        selectedPlyr = selectedPlyr_bak;
    //Sel prev hole, but not sel plyr hole and no sel plyr
    }else if(selectedHole_prev && !selectedPlyr_hole && !selectedPlyr){
        document.getElementById(selectedHole_prev).firstChild.style.border = "none";
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
    //Sel prev hole, but no sel plyr hole(not on board)
    }else if( selectedHole_prev && !selectedPlyr_hole && selectedPlyr){
        document.getElementById(selectedHole_prev).firstChild.style.border = "none";
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";

        selectedPlyr = null;
        holeTable_bldr({groupInfo:groupInfo[plyrHole[selectedPlyr_bak]-1],holeData:holeDataPY,holeId:'hole'+plyrHole[selectedPlyr_bak]});
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
        selectedPlyr = selectedPlyr_bak;
    //Sel prev hole, but selected plyr hole not equal to sel hole
    }else if( selectedHole_prev && selectedPlyr_hole != selectedHole ){
        document.getElementById(selectedHole_prev).firstChild.style.border = "none";
        document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";

        selectedPlyr = null;
        holeTable_bldr({groupInfo:groupInfo[plyrHole[selectedPlyr_bak]-1],holeData:holeDataPY,holeId:'hole'+plyrHole[selectedPlyr_bak]});
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
        selectedPlyr = selectedPlyr_bak;
    //All else
    }else{
        document.getElementById('holeDetailImg').style.border = "1px solid #ccc";
        if ( selectedHole_prev) document.getElementById(selectedHole_prev).firstChild.style.border = "none";
        holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1]});
    }
}

//--------------------------------------------------------------------//
//--------------------------------------------------------------------//

var initialCall;
function init(){
	// load the information about the groups
    groupInfo = get_playerData({url:'/.element/ssi/sect/1.0/tournament-central/' + tourCode + tournId + '/data/' + courseId + '/group-info.txt'});
    
    // PENDING: adjust url to where we get the course information
    //courseData = get_courseData({url:'/tc/get-course-info?current_year=2006&tid=r475&course_id=665'});
    courseData = get_courseData({url:'/.element/ssi/sect/1.0/tournament-central/' + tourCode + tournId + '/data/' + courseId + '/course-info.txt'})
	set_Round();
	set_Course();

    if ( !initialCall ){ 
        //Only call once
        // PENDING: adjust url to where we get hole information
        //holeDataPY = get_holeData({url:'/tc/get-hole-info?current_year=2006&tid=r475&course_id=665'});
        holeDataPY = get_holeData({url:'/.element/ssi/sect/1.0/tournament-central/' + tourCode + tournId + '/data/' + courseId + '/hole-info.txt'});
        initialCall = 1;
    } else {
    	// omniture
		s.pageName = s.getPageName() + ":refresh" 
		void(s.t());
		
		// tournament central message
		var msgContent = get_messageHtml({url:'/.element/ssi/sect/1.0/tournament-central/' + tourCode + tournId + '/body/' + courseId + '/generatedMessage.html'});    
		document.getElementById('tournamentCentralMessageBox').innerHTML = msgContent;
    }

    //Page Loaded?
    if (document.getElementById('hole1') ){
        for (i = 0; i < groupInfo.length; i++){
            holeTable_bldr({groupInfo:groupInfo[i],holeData:holeDataPY,holeId:'hole'+ eval(i+1)});
        }
        //This is a check to see if there is a selected player - if not - keep on selected hole
        if ( selectedHole && !selectedPlyr ){
            document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";
            holeOnly = selectedHole.split('hole');
            holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
        //Selected hole - but sel plyr not on board
        }else if(selectedHole && !plyrHole[selectedPlyr]){
            document.getElementById(selectedHole).firstChild.style.border = "1px solid #98200F";
            holeOnly = selectedHole.split('hole');
            holeDetailTable_bldr({groupInfo:groupInfo[holeOnly[1]-1],holeData:holeDataPY,holeNumber:holeOnly[1],holeCall:1});
        //All else
        }else{ 
            document.getElementById('holeDetailImg').style.border = "1px solid #ccc";
            holeDetailTable_bldr({groupInfo:groupInfo[0],holeData:holeDataPY,holeNumber:1});
        }

        //Player Alerts
        //get_PAlertData({url:'/tc/get-palert-info?current_year=2006&tid=r475'});
        get_PAlertData({url:'/.element/ssi/sect/1.0/tournament-central/' + tourCode + tournId + '/data/' + courseId + '/p.txt'});
    }
}


/*
Removed inlined code - now everything is loaded once the page loading has finished

function setInitialGroupInfo() {
       holeTable_bldr({groupInfo:groupInfo[0],holeData:holeDataPY,holeId:"hole1"});
       holeTable_bldr({groupInfo:groupInfo[1],holeData:holeDataPY,holeId:"hole2"});
       holeTable_bldr({groupInfo:groupInfo[2],holeData:holeDataPY,holeId:"hole3"});
       holeTable_bldr({groupInfo:groupInfo[3],holeData:holeDataPY,holeId:"hole4"});
       holeTable_bldr({groupInfo:groupInfo[4],holeData:holeDataPY,holeId:"hole5"});
       holeTable_bldr({groupInfo:groupInfo[5],holeData:holeDataPY,holeId:"hole6"});
       holeTable_bldr({groupInfo:groupInfo[6],holeData:holeDataPY,holeId:"hole7"});
       holeTable_bldr({groupInfo:groupInfo[7],holeData:holeDataPY,holeId:"hole8"});
       holeTable_bldr({groupInfo:groupInfo[8],holeData:holeDataPY,holeId:"hole9"});
       holeTable_bldr({groupInfo:groupInfo[9],holeData:holeDataPY,holeId:"hole10"});
       holeTable_bldr({groupInfo:groupInfo[11],holeData:holeDataPY,holeId:"hole12"});
       holeTable_bldr({groupInfo:groupInfo[12],holeData:holeDataPY,holeId:"hole13"});
       holeTable_bldr({groupInfo:groupInfo[13],holeData:holeDataPY,holeId:"hole14"});
       holeTable_bldr({groupInfo:groupInfo[14],holeData:holeDataPY,holeId:"hole15"});
       holeTable_bldr({groupInfo:groupInfo[15],holeData:holeDataPY,holeId:"hole16"});
       holeTable_bldr({groupInfo:groupInfo[16],holeData:holeDataPY,holeId:"hole17"});
       holeTable_bldr({groupInfo:groupInfo[17],holeData:holeDataPY,holeId:"hole18"});
                        
    //Initialize alerts
    get_PAlertData({url:'palert.txt'});
}*/




function initTournamentCentral() {
    init();
    
    // PENDING: is this part required
    holeDetailTable_bldr({groupInfo:groupInfo[0],holeData:holeDataPY,holeNumber:1});
    if ( selectedHole ) {
        holeValue=selectedHole.split('hole');
        document.holeDD.holeNum.value=holeValue[1];
    }
     
    eraseCookie('selectedPlyr');
    setInterval('init()',cycle*1000);
}

// register initTournamentCentral so the function gets called when the page finished loading
addLoadListener(initTournamentCentral);

