var array = new Array();

function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }
 
        function addHTML(elmId, value) {
          document.getElementById(elmId).innerHTML += value;
        }
 
        function loadNewVideo(id, startSeconds) {
          if (player) {
            player.loadVideoById(id, parseInt(startSeconds));
          }
        }
 
        function cueNewVideo(id, startSeconds) {
          if (player) {
            player.cueVideoById(id, startSeconds);
          }
        }
 
        function play() {
          if (player) {
            player.playVideo();
          }
        }
 
        function pause() {
          if (player) {
            player.pauseVideo();
          }
        }
 
        function stop() {
          if (player) {
            player.stopVideo();
          }
        }
 
        function getPlayerState() {
          if (player) {
            return player.getPlayerState();
          }
        }
 
        function seekTo(seconds) {
          if (player) {
            player.seekTo(seconds, true);
          }
        }
 
        function getBytesLoaded() {
          if (player) {
            return player.getVideoBytesLoaded();
          }
        }
 
        function getBytesTotal() {
          if (player) {
            return player.getVideoBytesTotal();
          }
        }
 
        function getCurrentTime() {
          if (player) {
            return player.getCurrentTime();
          }
        }
 
        function getDuration() {
          if (player) {
            return player.getDuration();
          }
        }
 
        function getStartBytes() {
          if (player) {
            return player.getVideoStartBytes();
          }
        }
 
        function mute() {
          if (player) {
            player.mute();
          }
        }
 
        function unMute() {
          if (player) {
            player.unMute();
          }
        }
        
        function getEmbedCode() {
          alert(player.getVideoEmbedCode());
        }
 
        function getVideoUrl() {
          alert(player.getVideoUrl());
        }
        
        function setVolume(newVolume) {
          if (player) {
            player.setVolume(newVolume);
          }
        }
 
        function getVolume() {
          if (player) {
            return player.getVolume();
          }
        }
 
        function clearVideo() {
          if (player) {
            player.clearVideo();
          }
        }
        
		function onYouTubePlayerReady(playerId) {
		  player = document.getElementById("player");
		  player.addEventListener("onStateChange", "onplayerStateChange");
		  //setInterval("nextSong()", 100);
		}
		
		function onplayerStateChange(newState) {
			if(newState == 0){
				//testPlay();
				
			}
			if(newState > 0 && newState != 5){
				//setInterval("loadbar()", 250);
			}
			
			
			//document.title = newState;
		}

function addslashes (str) {
    
	str.replace('"', '\"');
	
	return str;
}

function startSearch(){
		document.getElementById("dynamicHTML").innerHTML = "<img src='ajax-loader.gif' />";
	
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "getyoutube.php?q="+escape(document.getElementById("searchForm").value), true);
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById("dynamicHTML").innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send(null);
	
}

function changePage(query, page){
		document.getElementById("dynamicHTML").innerHTML = "<img src='ajax-loader.gif' />";
	
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "getyoutube.php?q="+query+"&pagina="+page, true);
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById("dynamicHTML").innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send(null);
	
}


function loadPage(page){
		document.getElementById("dynamicHTML").innerHTML = "<img src='ajax-loader.gif' />";
	
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', page+".php", true);
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById("dynamicHTML").innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send(null);
	
}

function playlistAdd(songName, songId){
	addHTML("playlistHTML","<a href=\"javascript:void(0);\" onclick=\"player.loadVideoById('"+songId+"')\">"+songName+"</a><br />");
	addHTML("playitems",","+songId);
	
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "playlistadd.php?name="+songName+"&id="+songId, true);
		self.xmlHttpReq.onreadystatechange = function() {
		}
		self.xmlHttpReq.send(null);
}

function playlistDel(songName){
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "deletefromplaylist.php?id="+songName, true);
		self.xmlHttpReq.onreadystatechange = function() {
		}
		self.xmlHttpReq.send(null);
}

function testPlay(){
	var b = document.getElementById('playitems').innerHTML; 
	var temp = new Array(); 
	temp = b.split(',');
	
	songId = temp[Math.floor(Math.random()*temp.length)];
	
	
	
	player.loadVideoById(songId);
	changeTitleFromPlaylist(songId);

}

function loadbar(){
	
	if(getPlayerState() == 1)
		document.getElementById('loadbar').style.width = Math.floor(getCurrentTime())/Math.floor(getDuration())*100+"%";
	
}

function nextSong(){
	
		document.title = Math.floor(getCurrentTime())+"/"+Math.floor(getDuration());
		
		if(getCurrentTime() == getDuration()){
			testPlay();
		}
}

function doLogin(){

		document.getElementById("login_error").innerHTML = "<img src='ajax-loader.gif' />";
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "checklogin.php?name="+escape(document.getElementById("login_name").value)+"&password="+escape(document.getElementById("login_password").value), true);
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				if(self.xmlHttpReq.responseText == "OK"){
					reloadMenu();
					loadPage("goodlogin");
				}
				else{
					document.getElementById("login_error").innerHTML = self.xmlHttpReq.responseText;
				}
			}
		}
		self.xmlHttpReq.send(null);

}
function reloadMenu(){
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		self.xmlHttpReq.open('GET', "menu.php", true);
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById("topmenu").innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send(null);	
}

function showHide(element){
	if(document.getElementById(element).style.visibility == "hidden"){
		document.getElementById(element).style.visibility = "";
		document.getElementById(element).style.height = "";
	}
	else{
		document.getElementById(element).style.visibility = "hidden";
		document.getElementById(element).style.height = "0px";
	}
}

function changeTitle(songName){
	document.title = decodeURI(songName);
	updateHTML("songNameDiv", songName);
}
function changeTitleFromPlaylist (songId){
	var xmlHttpReq = false;
	var self = this;
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
				
	self.xmlHttpReq.open('GET', "getsongnamefromplaylist.php?id="+songId, true);
	
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			songName = self.xmlHttpReq.responseText;
			document.title = songName;
			updateHTML("songNameDiv", songName);
		}
	}
	self.xmlHttpReq.send(null);


}

function colorSquare(color){
	document.write('<div style="float:right; width:15px; height:15px; background-color:'+color+'; margin:2px; border:1px #ccc solid;" onclick="document.body.style.backgroundColor=\''+color+'\';"></div>');
}

function goto_next(){
	if(Math.floor(getCurrentTime()-1) == Math.floor(getDuration()) && getCurrentTime() > 0){
		testPlay();
	}
	else if(getPlayerState() == 0){
		testPlay();
	}
}

function searchText(){
	if(document.getElementById('searchForm').value == "Search ..")
		document.getElementById('searchForm').value = "";
}

function timers(){
	setInterval("goto_next()", 1000);
	setInterval("loadbar()", 1000);
}