var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loadingFlash = "<center><img src='images/loading.gif' alt='Đang tải dữ liệu ...'></center>";
var loadingText = "<center><img src='images/loading.gif' alt='Đang tải dữ liệu ...'></center>";

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function sendRequest(current_url,act) {
	try{
		if (act == 'nghe')
		field = document.getElementById("playing_field");
		else field = document.getElementById("data_field");
		document.getElementById("loading").innerHTML = loadingText;
		document.getElementById("loading").style.display = "block";
		current_url = encodeURIComponent(current_url);
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('url='+current_url);
	}
	catch(e){}
	finally{}
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
		document.getElementById("loading").style.display = "none";
			response = http.responseText;
			if (current_url.indexOf('nghe') != -1) {
				field.style.marginBottom = "10";
			}
			field.innerHTML = response;
			field.style.display = "";
			field.scrollIntoView();
		}
  	}
	catch(e){}
	finally{}
}

function getVar(url,cnt){
	url=url+'#';
	url=url.split('#');
	//if (!url[1]) window.location.href = '';
	url=url[1];
	url=url+'/';
	url=url.split('/');
	url=url[cnt];
	if (!url)
		return '';
	return url;
}

function loadPage() {
	act = getVar(window.location.href,0);
	if (act) {
		current_url=window.location.href;
		current_url=current_url+'#';
		current_url=current_url.split('#');
		current_url=current_url[1];
		if (current_url) sendRequest(current_url,act);
	}
}

function urlCheck()
{
	url=window.location.href;
	if (url.indexOf('?') != -1 ) {
		url_temp = url.split('?');
		clearInterval(interval);
		href = url_temp[0];
		
		url=url+'#';
		url=url.split('#');
		if (url[1]) window.location.href = href+'#'+url[1];
		return;
	}
	if (url != '' && url!=lastUrl)
	{
		loadPage();
		lastUrl=url;
	}
}

function Title(song,sing)
{
  document.title ='Tìm kiếm nhạc | Playing: '+Replaces(song)+' - '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='Tìm kiếm nhạc | Singer: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='Tìm kiếm nhạc | Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='Tìm kiếm nhạc | Playing: '+Replaces(title);
}


function startLoad() {
	interval = setInterval('urlCheck()',100);
}

function do_search() {
	kw = document.getElementById("keyword").value;
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		s_type = document.getElementById("searchType");
		type = s_type.options[s_type.selectedIndex].value;
		switch (type) {
			case 'song' : type = 1; break;
			case 'singer' : type = 2; break;
		}
		last_url = '';
		if (type==1)
		window.location.href = '#Search/song/'+kw;
		else
		window.location.href = '#Search/singer/'+kw;
	}
	return false;

}
function applyType(obj, value)
{
					var arrType = document.getElementsByName('searchType');
					for (var i = 0; i < arrType.length; i++) {
						arrType[i].className = '';
					}
					obj.className = 'curnav';
					//$('t').value = value;
					document.getElementById('t').value = value;
}
/*------------------------------------------------------*/
function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function Title(song,sing)
{
  document.title ='Nghe bài hát: '+Replaces(song)+' - Singer: '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='Ca sĩ: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='Song: '+Replaces(title);
}

function  Replaces(string) {
   string=string.replace(/'/g,"\'");
  return string
}

function killErrors() {
  return true;
}
window.onerror = killErrors;



function bb_setHomepage(obj)
{
    obj.style.behavior='url(#default#homepage)';
    if (document.all)
    {
        obj.setHomePage('http://inghe.info/music');
    }
    else
    {
        alert("Kéo logo của inghe vào biểu tượng Home Page trên trình duyệt để đặt trang chủ");
    }
}

// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +

function Playlist(add_id,remove_id) {
	try{
	document.getElementById("playlist_field").innerHTML = loadingFlash;
    document.getElementById("playlist_field").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = playlist_handleResponse;
		http.send('Playlist=1&add_id='+add_id+'&remove_id='+remove_id);
		document.getElementById("playlist_player").innerHTML = '<embed type="application/x-shockwave-flash" id="Mediaplayer1" wmode="transparent" name="Mediaplayer1" src="mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#000000" flashvars="file=play_playlist.php&abouttxt=inghe.info&aboutlnk=http://inghe.info&lightcolor=0x000000&backcolor=0xFFFFFF&frontcolor=0x000000&showfsbutton=true&autostart=false&repeat=true&shuffle=false&showdigits=true&volume=100&displayheight=64" height="200" width="155"><br><a onclick="Playlist(0,\'all\')">Xóa tất cả</a>';
	}
	catch(e){}
	finally{}
}

function playlist_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("playlist_field").innerHTML = response;
		}
  	}
	catch(e){}
	finally{}
}
/*------------------------------------------------------*/