var curStatId = 0;

function startPage(){
    loadGenres('House');
    var ajaxRequest;
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            ajaxFunction('House', ajaxRequest.responseText, 1);
        }
        else if(ajaxRequest.readyState == 1){
            //document.getElementById('genres').innerHTML = '<img src="images/Please_wait.gif" height="100" width="100" alt="Please wait" />';
        }
    }
    ajaxRequest.open("GET", "getFirstStation.php?gen=House", true);
    ajaxRequest.send(null);
}

function loadGenres(genre){
    var ajaxRequest;  
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.getElementById('genres').innerHTML = ajaxRequest.responseText;
        }
        else if(ajaxRequest.readyState == 1){
            document.getElementById('genres').innerHTML = '<div align="center"><img src="images/Please_wait.gif" height="100" width="100" alt="Please wait" /></div>';
        }
    }
    //alert('adsasd');
    ajaxRequest.open("GET", "getGenreStations.php?gen=" + genre, true);
    ajaxRequest.send(null); 
}

function ajaxFunction(genre, stationId, isNew){
if (isNew !== undefined){
    curStatId = stationId;
    updatePlayer(stationId);
}
if (curStatId == stationId){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.getElementById('DIVINFO').innerHTML = ajaxRequest.responseText;     
        }
    }
    //alert("GetStationInfoWithAjax.php?gen=" + genre + "&station=" + stationId);
    ajaxRequest.open("GET", "GetStationInfoWithAjax.php?gen=" + genre + "&station=" + stationId, true);
    ajaxRequest.send(null); 
    //alert(inst);
    setTimeout("ajaxFunction('" + genre + "', '" + stationId + "')", 10000);
}
}

function updatePlayer(stationId){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.getElementById('player').innerHTML = ajaxRequest.responseText;
        }
    }
    //alert("GetStationInfoWithAjax.php?gen=" + genre + "&station=" + stationId);
    ajaxRequest.open("GET", "updatePlayer.php?stationId=" + stationId, true);
    ajaxRequest.send(null); 
}

function goAndTellAFriend(){
    var http = new XMLHttpRequest();
    var url = "tellAFriend.php";
    var yourName = document.getElementById('tfYourName').value;
    var friendName = document.getElementById('tfFriendName').value;
    var friendEmail = document.getElementById('tfFriendEmail').value;
    var params = "yourName=" + yourName + "&friendName=" + friendName + "&friendEmail=" + friendEmail;
    alert(params);
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            document.getElementById('response').innerHTML = http.responseText;
        }
    }
    http.send(params);
    changeDisplay('tellAFriend', 'hide');
}

function changeDisplay(id, action){
    if (action=="hide"){
        document.getElementById(id).style.display = "none";
    }
    else document.getElementById(id).style.display = "block";
}