// JavaScript Document
var lastplayingsong = '';
function GetXmlHttpObjectCheck() {
	var objXMLHttp = null;
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function checkfornewsong() {
	var psongxml = GetXmlHttpObjectCheck();
	if(psongxml == null) return;
	psongxml.open('GET', '/ostehaps.php?' + new Date().getTime(), true);
	psongxml.onreadystatechange = function() { 
	   	if (psongxml.readyState == 4) {
			var content = psongxml.responseText;
	     	if(content != '' && content != lastplayingsong ) {
				lastplayingsong = content;
				var radiodata = content.split('||', 2);
				document.getElementById('nowplaying').innerHTML = radiodata['1'];
				document.getElementById('listners').innerHTML = radiodata['0'];
			}
	   	}
	}
	psongxml.send(null);
	setTimeout('checkfornewsong()', 10000);
}

$(document).ready(function(){
	checkfornewsong();
});



//Generic updates #userbox with info retrieved
//from services
function update_userbox(name, image, url, logout) {

  //populate the data in #userbox and show it
  $('#userbox').html( "<a href='"+url+"'>"
                    + "<img alt='"+name+"' src='"+image+"' />"
                    + "Logged in as " + name + "</a> "
                    + "(<a href='./index.php' onclick='" + logout + "'>logout</a>)" ).show();

  //hide name input and service
  //login buttons
  $('#userinfo').hide();

  //populate the values of the inputs
  //using data from service
  $('#name').val(name);
  $('#url').val(url);
  $('#image').val(image);

}
//Facebook Connect
function auth_using_fb() {
  //get the users data from FB
  var viewer  = FB.Facebook.apiClient.fql_query(

      'SELECT name, pic_square_with_logo,profile_url FROM user WHERE uid='+FB.Facebook.apiClient.get_session().uid,

      function(results) {
        update_userbox( results[0].name,
                        results[0].pic_square_with_logo,
                        results[0].profile_url,
                        'FB.Connect.logoutAndRedirect("./");return false;')
      }
  );
}
