var currentSong = 0;

function setSong(nowPlaying, flashSongID)
{
	//Show the loader
	showLoader();
	
	//Update Title
	document.title = "OpenRadio: " + nowPlaying;
	
	//Update current song ID
	currentSong = flashSongID;
	
	//Update last played (non jQuery)
	loadDataToDiv("format.php?mode=last5","last20");
	socialLink("social.php");
};

function socialLink(url)
{
	//Show the loader
	showLoader();
	
	//Act on it
	loadDataToDiv(url,"socialMain");
}

function likeCurrent()
{
	//Show the loader
	showLoader();
		
	//Send like statement (non jQuery)
	var request = getHTTPObject();
	request.open("GET", "like.php?songid=" + currentSong, true);
	request.send(null);
	socialLink("social.php");
}

//Ajax Stuff
function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

//Simple function to load a URL into an element
function loadDataToDiv(SourceFile, DestinationElement){
	var request = getHTTPObject();
	var el = document.getElementById(DestinationElement);
  	if (request) {
    	request.onreadystatechange = function() {
    	parseResponse(request, DestinationElement);
    };
    request.open("GET", SourceFile, true);
    request.send(null);
  }
}

//Send data, designed to handle form logins and what not
function loginSubmit() {

	//Get the data
	var request = getHTTPObject();
	var theForm = document.getElementById('login');
	var data = "";
	
	//Calculate the data string
	data+= "user="+escape(theForm.user.value);
	data+= "&";
    data+= "pass="+escape(theForm.pass.value);
	
	//Show the loader
	showLoader(); 
	 
  	//Setup the action to perform then open, set and send!
    request.onreadystatechange = function() {parseResponse(request, "socialMain");};
    request.open( "POST", "social.php", true );
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
}

function newUserSubmit() {
	
	//Get the data
	var request = getHTTPObject();
	var theForm = document.getElementById('newUserForm');
	var data = "";
	
	//Calculate the data string
	data+= "newUser="+escape(theForm.newUser.value);
	data+= "&";
    data+= "newPass="+escape(theForm.newPass.value);
    data+= "&";
    data+= "newEmail="+escape(theForm.newEmail.value);
    data+= "&";
    data+= "newName="+escape(theForm.newName.value);
    
    //Show the loader
	showLoader(); 
	 
  	//Setup the action to perform then open, set and send!
    request.onreadystatechange = function() {parseResponse(request, "socialMain");};
    request.open( "POST", "social.php", true );
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
}


function parseResponse(request, DestinationElement) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      var container = document.getElementById(DestinationElement);
      container.innerHTML = request.responseText;
      hideLoader();
    }
  }
}

function showLoader(){
	//Show loading image
	if (document.getElementById('loaderImage')) document.getElementById('loaderImage').style.visibility = 'visible'; 
}

function hideLoader(){
	//Hide loading image
	if (document.getElementById('loaderImage')) document.getElementById('loaderImage').style.visibility = 'hidden'; 
}