// JavaScript Document

var nowplaying = "";
var title = "";
var artist = "";

function init() 
{
	nowPlayingLoop();
	getObj("now_playing_song").innerHTML = "";
}

function nowPlayingLoop()
{
	updateNowplaying();
   	setTimeout("nowPlayingLoop()",1000); 
}

function updateNowplaying()
{
    var xmlhttp = getHTTPObject(); 
    var d = new Date();  
    var url = "http://www.gigantfm.nl/titelstream/ajax_nowplaying.php";  
    xmlhttp.open("GET",url,true);  
    xmlhttp.onreadystatechange= 
    function()  
    {  
    	if (xmlhttp.readyState==4)  
      	{        		
			var answer = xmlhttp.responseText;
			//als antwoord niet leeg is
			if(answer != nowplaying)
			{
				nowplaying = answer; // new nowplaying
				
				//Get separate artist and title
				//var explode = nowPlaying.split(" - "); 
				//artist = trim(explode[0]);
				//title = trim(explode[1]);
				
				fadeOut("now_playing_song",100); // fadeout nowplaying on site
			}
		}
	}
	xmlhttp.send(null);
}


function fadeIn(objId,opacity)
{
	if (document.getElementById) 
	{
		obj = document.getElementById(objId);
		if (opacity > 90) 
		{
			setOpacity(obj, 100);
		}else
		{
			opacity += 20;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);	
		}
		setOpacity(obj, opacity);		
	}
}

function fadeOut(objId,opacity)
{
	//alert(opacity);
	if (document.getElementById) 
	{
		obj = document.getElementById(objId);
		if (1==1) 
		{
			setOpacity(obj, opacity);
			opacity -= 20;
			if(opacity > -10)
			{
				window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 50);
			}else
			{
				getObj(objId).style.visibility = "hidden";
				getObj("now_playing_song").innerHTML = nowplaying;
				getObj(objId).style.visibility = "visible";
				fadeIn("now_playing_song",0);
			}
		}
	}
}

function setOpacity(obj, opacity)
{
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

//Reference to Object Id
function getObj(elementID)
{
	if (typeof elementID == "string") 
	{
		return document.getElementById(elementID);
	}
	else
	{
		return elementID;
	}
}

//Create Ajax object
function getHTTPObject()  
{
	var xmlhttp; 
    /*@cc_on 
        @if (@_jscript_version >= 5) 
          try  
        { 
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
            } 
        catch (e)  
        { 
              try  
          { 
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
              }  
          catch (E)  
          { 
                xmlhttp = false; 
              } 
            } 
        @else 
        xmlhttp = false; 
        @end @*/ 
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined')  
        { 
          try  
      { 
            xmlhttp = new XMLHttpRequest(); 
          }  
      catch (e)  
      { 
            xmlhttp = false; 
          } 
        }  
        return xmlhttp; 
}