// JavaScript Document

/* Variabels */
webplayerContent = "";
nowplaying_song = "";
webcamplayerContent = "";
camcount = 0;

function init() 
{
	//fieldFocus();
	//updateNowPlaying();
	//alert("hallo!");
}

function updateNowPlaying()
{
	
	
	if(getObj('nowplaying_song') != null)
	{
		//alert("id gevonden");
		//set current content
		nowplaying_song = getObj('nowplaying_song').innerHTML;
		refreshNowPlaying();
	}	
}

function refreshNowPlaying()
{
	//alert("refresh nowplaying wordt geroepen");
    var xmlhttp = getHTTPObject(); 
    var d = new Date();  
    var url = "http://www.gigantfm.nl/ajax/nowplaying_song.php?date="+d;  
    xmlhttp.open("GET",url,true);  
    xmlhttp.onreadystatechange= 
    function()  
    {  
    	if (xmlhttp.readyState==4)  
      	{        		
			var answer = xmlhttp.responseText;
			//als antwoord niet leeg is
			if(answer != "")
			{
				//als er nieuwe inhoud is (kijken naar eerste 15 tekens ivm BR conversie)
				if(answer.substring(0, 15) != nowplaying_song.substring(0, 15))
				{
					//alert("verschil gevonden");
					subst = answer.substring(0, 8);
					if(subst == "<strong>") // check if it is the expected content
					{							
						//ververs content
						nowplaying_song = answer;
						
						getObj('nowplaying_song').innerHTML = nowplaying_song;
					}
				}
			}
		}
	}
	xmlhttp.send(null);
	
	setTimeout("refreshNowPlaying()",10000); 
}

//Focus first field
function fieldFocus() 
{
   // var textElements = 0;
	var focusField = -1;
	
	if(document.forms[0])
	{
		for (i = 0; i < document.forms[0].elements.length; i++)
		{
			if (document.forms[0].elements[i].type == "text" || document.forms[0].elements[i].type == "password") 
			{
				if (focusField < 0) 
				{
						focusField = i;
				}
			}
		}
		//put focus in first text or password field
		if(focusField > -1)
		{
			document.forms[0].elements[focusField].focus();
		}
	}
}

function webplayer() 
{
	width = 400	
	heigth = 250;
	URL = "/webplayer.php";
	
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height=" + heigth + ",left = 100,top = 100');");
}

function webcam() 
{
	width = 350	
	heigth = 350;
	URL = "/webcam.php";
	
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height=" + heigth + ",left = 100,top = 100');");
}

function popup(URL, width, height) 
{
	width = parseInt(width);	
	heigth = parseInt(height);
	
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height=" + heigth + ",left = 100,top = 100');");
}


function insertEmo(emo)
{
	getObj("message").value = getObj("message").value+" "+emo+ " ";
	getObj("message").focus();
}

function refreshWebcam()
{
	refreshWebcamImage();
	refreshWebcamplayer();
	startWebcamplayerSlider();
}

function refreshWebcamImage()
{
	//alert("wordt geroepen");
    var xmlhttp = getHTTPObject(); 
    var d = new Date();  
    var url = "ajax/webcam_popup.php?date="+d;  
    xmlhttp.open("GET",url,true);  
    xmlhttp.onreadystatechange= 
    function()  
    {  
    	if (xmlhttp.readyState==4)  
      	{        		
			var answer = xmlhttp.responseText;
			//als antwoord niet leeg is
			if(answer != "")
			{
				subst = answer.substring(0, 3);
				if(subst == "<im") // check if it is the expected content
				{						
					if(getObj('cam_content') != null)
						getObj('cam_content').innerHTML = answer;
					
					setTimeout("refreshWebcamImage()",10000); 
				}				
			}
		}
	}
	xmlhttp.send(null);
}

function startWebplayerSlider()
{
	$('#webplayerSlider').cycle({
		fx: 'scrollUp',
		timeout: 3000, 
		speed: 500,
		after: refreshWebplayer
	});
}

function refreshWebplayer()
{
	//alert("wordt geroepen");
    var xmlhttp = getHTTPObject(); 
    var d = new Date();  
    var url = "ajax/webplayer.php?date="+d;  
    xmlhttp.open("GET",url,true);  
    xmlhttp.onreadystatechange= 
    function()  
    {  
    	if (xmlhttp.readyState==4)  
      	{        		
			var answer = xmlhttp.responseText;
			//als antwoord niet leeg is
			if(answer != "")
			{
				//als er nieuwe inhoud is
				subst1 = answer.substring(0, 10);
				subst2 = webplayerContent.substring(0, 10);
				if(subst1 != subst2)
				{
					subst = answer.substring(0, 3);
					if(subst == "<p>") // check if it is the expected content
					{					
						//stop slideshow
						$('#webplayerSlider').cycle('stop');
							
						//ververs content
						webplayerContent = answer;
						
						getObj('webplayerSlider').innerHTML = webplayerContent;
							
						//start slideshow
						startWebplayerSlider();
					}
				}
			}
		}
	}
	xmlhttp.send(null);
}

function startWebcamplayerSlider()
{
	//alert("starten wordt geroepen");
	$('#webcamplayerSlider').cycle({
		fx: 'scrollUp',
		timeout: 5000, 
		speed: 500,
		after: refreshWebcamplayer
	});
}

function refreshWebcamplayer()
{
	//alert("ajax wordt geroepen");
    var xmlhttp = getHTTPObject(); 
    var d = new Date();  
    var url = "ajax/webplayer.php?date="+d;  
    xmlhttp.open("GET",url,true);  
    xmlhttp.onreadystatechange= 
    function()  
    {  
    	if (xmlhttp.readyState==4)  
      	{        		
			var answer = xmlhttp.responseText;
			//als antwoord niet leeg is
			if(answer != "")
			{
				//als er nieuwe inhoud is
				if(answer != webcamplayerContent)
				{
					subst = answer.substring(0, 3);
					if(subst == "<p>") // check if it is the expected content
					{					
						//stop slideshow
						$('#webcamplayerSlider').cycle('stop');
							
						//ververs content
						webcamplayerContent = answer;
						
						getObj('webcamplayerSlider').innerHTML = webcamplayerContent;
							
						//start slideshow
						startWebcamplayerSlider();
					}
				}
			}
		}
	}
	xmlhttp.send(null);
}


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; 
}

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;
}

function getObj(elementID)
{
	if (typeof elementID == "string") 
	{
		return document.getElementById(elementID);
	}
	else
	{
		return elementID;
	}
}

