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><embed id="ld" wmode="transparent" name="ld" src="http://nhachayvn.info/images/loading.swf" width="480" height="240" allowfullscreen="true" allowscriptaccess="always" /></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_search2() {
	kw = document.getElementById("keyword2").value;
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		s_type = document.getElementById("s_type");
		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 do_search() {
	kw = document.getElementById("keyword").value;
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		s = document.getElementsByName("searchType");
		if (s[0].checked) type = 1;
		if (s[1].checked) type = 2;
		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 ='Tìm là thấy|Song: '+Replaces(song)+' - Singer: '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='Tìm là thấy|Singer: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='Tìm là thấy|Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='Tìm là thấy|Song: '+Replaces(title);
}

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

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

function player(type,id,name,singer) {
var url = '';
var h = 140;
var siteURL = 'http://nhachayvn.info';
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 = 240;
}
	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 = '<embed type="application/x-shockwave-flash" id="Mediaplayer" wmode="transparent" name="Mediaplayer" src="'+siteURL+'/mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#000000" flashvars="file='+url+'&abouttxt=nhachayvn.info&aboutlnk=http://www.nhachayvn.info&lightcolor=0xdeefc2&backcolor=0x58bd37&frontcolor=0xffffff&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100" height="'+h+'" width="280">';
	var sender_f = 'Link nhạc: <input type="text" style="width: 210px" onFocus="this.select()" value="'+siteURL+'/#nghe/'+id+'/'+type+'">';
		sender_f+= '<br>Copy vào blog: <input type="text" style="width: 180px" 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: <input type="text" style="width: 160px" 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+= '<center><a href="download.php?id='+id+'" onclick="alert(\'Click chuột phải chọn save target as\')"><img src="images/download2.gif" alt="download" 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;
	document.getElementById("player_field").scrollIntoView();
	}
	catch(e){}
	finally{}
	return false;
}

function bb_bookmarksite(title,url)
{
	if (window.sidebar) {// firefox
		window.sidebar.addPanel(title, url, "");
	} 
	else if (window.opera && window.print) { // opera
			var elem = document.createElement('a');
			elem.setAttribute('href',url);
			elem.setAttribute('title',title);
			elem.setAttribute('rel','sidebar');
			elem.click();
			} 
	else if (document.all) {// ie
			window.external.AddFavorite(url, title);
	}
}

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

//Set tab to intially be selected when page loads:
//[which tab (1=first tab), ID of tab content to display]:
var initialtab=[1, "sc1"]

var previoustab=""

function expandcontent(cid, aobject){
if (document.getElementById){
highlighttab(aobject)
if (previoustab!="")
document.getElementById(previoustab).style.display="none"
document.getElementById(cid).style.display="block"
previoustab=cid
}
}

function highlighttab(aobject){
if (typeof tabobjlinks=="undefined")
collecttablinks()
for (i=0; i<tabobjlinks.length; i++)
tabobjlinks[i].className=""
aobject.className="current"
}

function collecttablinks(){
var tabobj=document.getElementById("tablist")
tabobjlinks=tabobj.getElementsByTagName("A")
}

function do_onload(){
collecttablinks()
expandcontent(initialtab[1], tabobjlinks[initialtab[0]-1])
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload