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><br><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 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 = '#tim_kiem/key/'+kw;
		else
		window.location.href = '#tim_kiem/casi/'+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 ='MuzikBox.Info | Song: '+Replaces(song)+' - Singer: '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='ithubt.com | Singer: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='ithubt.com | Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='ithubt.com | Song: '+Replaces(title);
}

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

function player(type,id,name,singer) {
var url = '';
var h = 160;
var siteURL = 'http://ithubt.com/music';
if(type==1)
url = siteURL+'/xml.php?id='+id+'&showeq=true';
if(type==2)
{
url = siteURL+'/xml.php?id='+id+'&logo=images/flash.png';
h = 300;
}
	try {
	document.getElementById("player_field").innerHTML = loadingText;
    document.getElementById("player_field").style.display = "block";
	document.getElementById("sender_field").style.display = "block";
	document.getElementById("info_field").style.display = "block";
	var	info_f = '<div style="margin-top:12"><i>Ca khúc: </i>'+name+'<br><i>Trình bày: </i> '+singer+'</div>';
	var	player_f = '<table border=4 cellpadding=0 cellspacing=0 bordercolor="#e6e6e6" style="margin-top:12"><tr><td><embed type="application/x-shockwave-flash" id="Mediaplayer" wmode="transparent" name="Mediaplayer" src="'+siteURL+'/mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#FFFFFF" flashvars="file='+url+'&abouttxt=MuzikBox.Info&aboutlnk=http://www.MuzikBox.Info&lightcolor=0x368df8&backcolor=0x69aaf9&amp;frontcolor=0x000000&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100" height="'+h+'" width="350"></td></tr></table>';
	var sender_f = '<br>Link nhạc: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" style="width: 210px" onFocus="this.select()" value="'+siteURL+'/#nghe/'+id+'/'+type+'">';
		sender_f+= '<br>Copy vào blog: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" style="width: 210px" onFocus="this.select()" value=\'<embed type="application/x-shockwave-flash" id="Mediaplayer" wmode="transparent" src="'+siteURL+'/mediaplayer.swf" name="Mediaplayer" flashvars="file='+url+'&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#000000" height="'+h+'" width="350">\'>';
		sender_f+= '<br>Copy vào diễn đàn: &nbsp;&nbsp;<input type="text" style="width: 210px" onFocus="this.select()" value="[FLASH]'+siteURL+'/mediaplayer.swf?file='+url+'&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100[/FLASH]">';
		sender_f+= '<br><br><center><a target="_blank" href="nhac/ithubt.com_'+id+'.mp3"><img src="images/download.jpg" title="Click chuột phải chọn Save Link As | Save Target As" border=0></a></center>';
	document.getElementById("info_field").innerHTML = info_f;
	document.getElementById("player_field").innerHTML = player_f;
	document.getElementById("sender_field").innerHTML = sender_f;
	}
	catch(e){}
	finally{}
	return false;
}

function bb_setHomepage(obj)
{
    obj.style.behavior='url(#default#homepage)';
    if (document.all)
    {
        obj.setHomePage('http://ithubt.com');
    }
    else
    {
        alert("Kéo Logo MB vào nút Home để đặt MB làm 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 = '<br><center><a onclick="Playlist(0,\'all\')"><b>Xóa tất cả</b></a></center>';
	}
	
	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{}
}
/*------------------------------------------------------*/