// initialize XMLHttpRequest object
var xmlobj=null;
var data=new Array();
// send http request
function sendRequest(doc){
    // check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Mozilla, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    // assign state handler
    xmlobj.onreadystatechange=stateChecker;
    // open socket connection
    xmlobj.open('GET',doc,true);
    // send GET request
    xmlobj.send(null);
}
// check request status
function stateChecker(){
    // if request is completed
    if(xmlobj.readyState==4){
        // if status == 200 display text file
        if(xmlobj.status==200){
            // create data container
            createDataContainer();
            // read XML data
            data=xmlobj.responseXML.getElementsByTagName('tournament');
 // display XML data
            displayData();
        }
        else{
            alert('Failed to get response :'+ xmlobj.statusText);
        }
    }
}
// editable variables - these can probbaly be extended but fine for now

// div id that you use to hold data
	var containerId= 'tournamentScheduleFeed'; 
// path to header image if used as part of container	
	//var imgUrl='/paddyPower/default/images/tournamentTitleNext.gif';


// create data container 
function createDataContainer(){
    var div=document.getElementById(containerId);
    if(div){return};
    var div=document.createElement('div');
    div.setAttribute('id',containerId);
    document.getElementsByTagName('body')[0].appendChild(div);
	 
}
// display data at a given time interval
function displayData(){
    // reset data container
    document.getElementById(containerId).innerHTML='';
	// creating header image for box otherwise will get overwritten - not needed for galacoral
	//var image = document.createElement('img');
	//image.setAttribute('src',imgUrl);
	//image.setAttribute('style','margin:10px 0 0 8px'); // we can set whatever you fancy here class inline style etc
	// end header creation - add or delete as needed
    var ul=document.createElement('ul');
	// change i<$number for number of future tournaments to show
    for(var i=0;i<6;i++){
        // create li and a elements
		var li=document.createElement('li');
        var a=document.createElement('a');
		// getting the time chopping off last 2 digits then reformatting with : 
		var myTime=data[i].getElementsByTagName('time')[0].firstChild.nodeValue;
		var myGame=data[i].getElementsByTagName('game')[0].firstChild.nodeValue;
		var newTime = myTime.substring(0, myTime.length-2);
	    var tournTime= newTime;
		var L= tournTime.length;
		L=(L%2) ? ':'+ tournTime.charAt(L-1) : '';
		var M= tournTime.match(/(.{2})/g) ;
		if(M) tournTime= M.join(':')+ L;
		
        // assign 'href' attribute - we are going to do a switch to create the correct link based on tournament
		switch (myGame)
		{
		case 'Backgammon':
		  a.setAttribute('href','javascript:openLobby(1)');
		  break;
		case 'Blackjack':
		  a.setAttribute('href','javascript:openLobby(7)');
		  break;
		case 'Gin Rummy':
		  a.setAttribute('href','javascript:openLobby(5)');
		  break;
		case 'Dominoes':
		  a.setAttribute('href','javascript:openLobby(39)');
		  break;

		default:
		  a.setAttribute('href','javascript:openLobby(1)');
		}
        
        // add link labels - basically what you want to display as a link - in this case time and game name seperated by -
        //adding time as a text node within a
		a.appendChild(document.createTextNode(tournTime));
		//adding - as a text node within a
		a.appendChild(document.createTextNode(' - '));
		//adding game as a text node within a
		a.appendChild(document.createTextNode(myGame));
		// closing a li
		li.appendChild(a);
        ul.appendChild(li);    }
	//document.getElementById(containerId).appendChild(image);	// not needed for galacoral
    document.getElementById(containerId).appendChild(ul);
    // update headlines each 1 hour
    //setTimeout("sendRequest('/feed/tournamentSchedule.xml')",5*1000);
}

function tournamentBuild(){
    // check if browser is DOM compatible
    if(document.getElementById&&document.getElementsByTagName&&document.createElement){
        // load XML file
        sendRequest('/feed/tournamentSchedule.xml');
    }
}