var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var myplayer = '';
var player_field = 0;
var player_form = '';
var loading = 0;
var web_url = 'http://mp3.nhac.pro';
var loadingText = '<span style="text-align:center; font-weight:bold;"><img src="http://mp3.nhac.pro/files/loading.gif" alt="Loading" />&nbsp;{loading} ...</span>';


function createRequestObject() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            oXmlHttp=null;
        }
      }
    }
    throw new Error("XMLHttp object could be created.");
}

function sendRequest(current_url,act) {
	try{
		if (act == 'Gift' || act == 'Play' || act == 'Play_Album' || act == 'Play_Singer' || act == 'Play_Playlist'){
			field = document.getElementById("playing_field");
			if (act != 'Play') player_field = 1;
		}
		else field = document.getElementById("data_field");
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		current_url = encodeURIComponent(current_url);
		http.open('POST',  web_url+'/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)){
			response = http.responseText;
			if (current_url.indexOf('Play') != -1 || current_url.indexOf('Gift') != -1) {
				field.style.marginBottom = "10";
				/*document.getElementById("show_hide").style.display = "block";*/
			}
			field.innerHTML = response;
			hide_Loading();
			loading=0;
			field.style.display = "";
			field.scrollIntoView();
			if(player_field==1) {
				delay_getplayer();
				player_field = 0;
			}
			document.title = WEBTITLE;
		}
  	}
	catch(e){}
	finally{}
}

// + ---------------------- +
// |  BEGIN Loading Alpha   |
// + ---------------------- +
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;

var timershow=false;
var curx=-200;
var cury=200;
var win_w=window.innerWidth ? window.innerWidth : document.body.offsetWidth;
var mid_w=win_w/2;
var timershow1=window.setInterval("stayMiddle()",1);

function getobj(id) {
el = document.all ? document.all['LoadingDiv'] :   dom ? document.getElementById('LoadingDiv') :   document.layers['LoadingDiv'];
return el;
}

function show_Loading() {
	layout = document.getElementById('Loading_layout');
	layout.style.width = '100%';
	layout.style.height = document.body.offsetHeight+'px';
	obj = getobj();
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}

function hide_Loading() {
	obj = getobj();
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nhide()",1);
	layout = document.all ? document.all['Loading_layout'] :   dom ? document.getElementById('Loading_layout') :   document.layers['Loading_layout'];
	layout.style.width = '0px';
	layout.style.height = '0px';
}

function moveobj(obj,x,y) {
	obj.style.left=x + "px";
	obj.style.top=y+ "px";
	curx=x;
	cury=y;
}

function stayMiddle() {
	if (document.documentElement && document.documentElement.scrollTop)
		var pY =  document.documentElement.scrollTop;
	else if (document.body)
		var pY =  document.body.scrollTop;

	obj = getobj();
	newy = cury+((pY-cury)/16)+12;
	moveobj(obj,curx, newy);
}

function nshow() {
	obj = getobj();
	newx = curx+((mid_w-curx)/16)-7;
	moveobj(obj,newx, cury);
}
function nhide() {
	obj = getobj();
	newx = curx+((0-curx)/16)-15;
	moveobj(obj,newx, cury);
}
// + ---------------------- +
// |    END Loading Alpha   |
// + ---------------------- +

function getVar(url,cnt)
{
	url=url+'#';
	url=url.split('#');
	if (!url[1]) window.location.href = '#Home/1/Page.html';
	url=url[1];
	url=url+'/';
	url=url.split('/');
	if (url[0] == 'Logout')
		window.location.href = '?refresh=1';
	if (cnt != -1) {
		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;
	dot_html = url.split('.');
	if (dot_html[(dot_html.length-1)] != 'html') window.location.href = '#Home/1/Page.html';
	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 alertBrokenLink(id) {
	if (confirm("{report_broken_link}")) {
		try{
			http.open('POST',  web_url+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = BrokenResponse;
			http.send('url=Broken,'+id);
		}
		catch(e){}
		finally{}
	}
}

function BrokenResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			if (response == 1) alert("{report_successfully}");
			else alert("{error_try_again}");
		}
  	}
	catch(e){
		alert("{error_try_again}");
	}
	finally{}
}

function do_search() {
	kw = document.getElementById("keyword").value;
	if (!kw) alert('{keyword_empty}');
	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;
			case 'album' : type = 3; break;
		}
		last_url = '';
		window.location.href = '#Search/'+type+'/'+kw+'/1/Page.html';
	}
	return false;

}


function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function login_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("login_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("login_loading").innerHTML = response;
				document.getElementById("login_loading").style.display = "block";
			}
			else{
				window.location.href = '?refresh=1';
			}
		}
  	}
	catch(e){}
	finally{}
}

function login(form) {
	name = encodeURIComponent(document.getElementById("login_name").value);
	pwd = encodeURIComponent(document.getElementById("login_pwd").value);
	if(	trim(name) == "" ||	trim(pwd) == "")
		alert("Chưa nhập đủ dữ liệu");//{missing_info}
	else {
		try{
			document.getElementById("login_loading").innerHTML = loadingText;
			document.getElementById("login_loading").style.display = "block";
			http.open('POST',  web_url+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = login_handleResponse;
			http.send('login=1&name='+name+'&pwd='+pwd);
			
		}
		catch(e){}
		finally{}
	}
	return false;
}

// + ---------------------- +
// |        REQUEST        |
// + ---------------------- +
function request_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("request_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("request_loading").innerHTML = response;
				document.getElementById("request_loading").style.display = "block";
			}
			else {
				alert("{request_successfully}");
				window.location.href = '#Home/1/Page.html';
			}
		}
  	}
	catch(e){}
	finally{}
}
function request_check_values() {
    ok = false;
    title_request = encodeURIComponent(document.getElementById("title_request").value);
    singer_request = encodeURIComponent(document.getElementById("singer_request").value);
    author_request = encodeURIComponent(document.getElementById("author_request").value);
    ym_request = encodeURIComponent(document.getElementById("ym_request").value);
    email_request = encodeURIComponent(document.getElementById("email_request").value);
    info_request = encodeURIComponent(document.getElementById("info_request").value);
   
    if(    trim(title_request) == "" ||    trim(email_request) == "" )
        alert("Chưa nhập đủ dữ liệu");//{missing_info}
    else {
        try{
            document.getElementById("request_loading").innerHTML = loadingText;
            document.getElementById("request_loading").style.display = "block";
            http.open('POST',  web_url+'/index.php');
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            http.onreadystatechange = request_handleResponse;
            http.send('request=1&title_request='+title_request+'&author_request='+author_request+'&singer_request='+singer_request+'&ym_request='+ym_request+'&email_request='+email_request+'&info_request='+info_request);
           
        }
        catch(e){}
        finally{}
    }
    return ok;
}
// + ---------------------- +
// |        REGISTER        |
// + ---------------------- +

function reg_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("reg_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("reg_loading").innerHTML = response;
				document.getElementById("reg_loading").style.display = "block";
			}
			else {
				alert("Đăng ký thành công. Bạn có thể trở lại trang chủ và đăng nhập ngay bây giờ");//{register_successfully}
				window.location.href = '#Home/1/Page.html';
			}
		}
  	}
	catch(e){}
	finally{}
}


function reg_check_values() {
	ok = false;
	name = encodeURIComponent(document.getElementById("reg_name").value);
	pwd = encodeURIComponent(document.getElementById("reg_pwd").value);
	pwd2 = encodeURIComponent(document.getElementById("reg_pwd2").value);
	date = encodeURIComponent(document.getElementById("reg_date").value);
	month = encodeURIComponent(document.getElementById("reg_month").value);
	year = encodeURIComponent(document.getElementById("reg_year").value);
	email = encodeURIComponent(document.getElementById("reg_email").value);
	avatar = encodeURIComponent(document.getElementById("reg_avatar").value);
	blog = encodeURIComponent(document.getElementById("reg_blog").value);
	security = encodeURIComponent(document.getElementById("reg_security").value);
	agree = document.getElementById("agree").checked;
	
	s = document.getElementsByName("reg_sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	
	if(	trim(name) == "" ||	trim(pwd) == "" ||	trim(pwd2) == "" ||	trim(email) == "" ||	trim(security) == "" )
		alert("Chưa nhập đủ dữ liệu");//{missing_info}
	else
		if (pwd != pwd2) alert("Xác nhận mật khẩu không chính xác");//{wrong_confirm_pass}
		else if (!agree) alert("Bạn phải đồng ý với các quy định của website");
		else {
			try{
				document.getElementById("reg_loading").innerHTML = loadingText;
				document.getElementById("reg_loading").style.display = "block";
				http.open('POST',  web_url+'/index.php');
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http.onreadystatechange = reg_handleResponse;
				http.send('reg=1&name='+name+'&pwd='+pwd+'&date='+date+'&month='+month+'&year='+year+'&email='+email+'&avatar='+avatar+'&blog='+blog+'&sex='+sex+'&security='+security);
				
			}
			catch(e){}
			finally{}
		}
	return ok;
}

// + ------------------------- +
// |        CHANGE INFO        |
// + ------------------------- +

function change_info_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("change_info_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("change_info_loading").innerHTML = response;
				document.getElementById("change_info_loading").style.display = "block";
			}
			else window.location.href = '?refresh=1';
		}
  	}
	catch(e){}
	finally{}
}

function change_info() {
	email = encodeURIComponent(document.getElementById("u_email").value);
	avatar = encodeURIComponent(document.getElementById("u_avatar").value);
	blog = encodeURIComponent(document.getElementById("u_blog").value);
	oldpwd = encodeURIComponent(document.getElementById("u_oldpwd").value);
	newpwd_1 = encodeURIComponent(document.getElementById("u_newpwd_1").value);
	newpwd_2 = encodeURIComponent(document.getElementById("u_newpwd_2").value);
	h_sex = document.getElementById("hide_sex").checked;
	h_email = document.getElementById("hide_email").checked;
	s = document.getElementsByName("u_sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	if(	trim(email) == "" )
		alert("{missing_info}");
	else if (newpwd_1 != newpwd_2)
		alert("{wrong_confirm_pass}");
	else {
		try{
			document.getElementById("change_info_loading").innerHTML = loadingText;
			document.getElementById("change_info_loading").style.display = "none";
			http.open('POST',  web_url+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = change_info_handleResponse;
			http.send('change_info=1&email='+email+'&avatar='+avatar+'&blog='+blog+'&oldpwd='+oldpwd+'&newpwd='+newpwd_1+'&sex='+sex+'&hide_sex='+h_sex+'&hide_email='+h_email);
		}
		catch(e){}
		finally{}
	}
	return false;
}

// + ----------------------------- +
// |        FORGOT PASSWORD        |
// + ----------------------------- +

function forgot_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("forgot_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("forgot_loading").innerHTML = response;
				document.getElementById("forgot_loading").style.display = "block";
			}
			else alert('{forgot_successfully}');
		}
  	}
	catch(e){}
	finally{}
}

function forgot() {
	email = encodeURIComponent(document.getElementById("u_email").value);
	if(	trim(email) == "" )	alert("{missing_email}");
	else {
		try{
			document.getElementById("forgot_loading").innerHTML = loadingText;
			document.getElementById("forgot_loading").style.display = "block";
			http.open('POST',  web_url+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = forgot_handleResponse;
			http.send('forgot=1&email='+email);
		}
		catch(e){}
		finally{}
	}
	return false;
}

function popup(url,wdname,width,height)
{
	if (width == null)  { width  = 200; }   // default width
	if (height == null) { height = 400; }   // default height
	newwin=window.open(url,wdname,'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}
/*
function showComment(media_id) {
		try {
		//document.getElementById("comment_field").innerHTML = loadingText;
		document.getElementById("comment_field").style.display = "block";
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("comment_field").innerHTML = http.responseText;
				hide_Loading();
				loading=0;
				document.title = WEBTITLE;
			}
		}
		http.send('show_comment=1&media_id='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
*/
// + ---------------------- +
// |        COMMENT         |
// + ---------------------- +
function showComment(media_id,pg) {
 try {
  //document.getElementById("loading_comment").innerHTML = loadingText;
  document.getElementById("loading_comment").style.display = "block";
  if (loading==0) {
			loading=1;
			show_Loading();
		}
  http.open('POST',  web_url+'/comment.php');
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = function() {
   if((http.readyState == 4)&&(http.status == 200)){
    //document.getElementById("loading_comment").style.display = "none";
    document.getElementById("comment_field").innerHTML = http.responseText;
    			hide_Loading();
				loading=0;
   		}
  }
  http.send('lvcomment=1&media_id='+media_id+'&pg='+pg);
 }
 catch(e){}
 finally{}
 return false;
}
function comment_handleResponse() {
 try {
 	if((http.readyState == 4)&&(http.status == 200)){
   	document.getElementById("comment_loading").style.display = "none";
   	hide_Loading();
   	loading=0;
   	var response = http.responseText;
   	if (response != 1) {
   		media_id = encodeURIComponent(document.getElementById("media_id").value);
		showComment(media_id,'1');
   		}
   		//else document.getElementById("comment_loading").innerHTML = response;
   		else alert("Bạn hãy đợi vài giây nữa mới có thể viết tiếp");
   	  }
  }
 catch(e){}
 finally{}
}
function comment_check_values() {
 media_id = encodeURIComponent(document.getElementById("media_id").value);
 comment_content = encodeURIComponent(document.getElementById("comment_content").value);
 if(trim(comment_content) == "")
  alert("Bạn chưa nhập cảm nhận");
 else if (comment_content.length > 250)
  alert("Nội dung cảm nhận quá 250 ký tự.");
 else {
  try {
   //document.getElementById("loading_comment").innerHTML = loadingText;
   //document.getElementById("loading_comment").style.display = "block";
   		if (loading==0) {
		loading=1;
		show_Loading();
		}
   		http.open('POST',  web_url+'/comment.php');
  		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = comment_handleResponse;
	   	http.send('comment=1&media_id='+media_id+'&comment_content='+comment_content);
  }
  catch(e){}
  finally{}
 }
 return false;
}
function comment_delete(media_id,comment_id) {
 		if (confirm("Bạn có muốn xóa cảm nhận này không ?")) {
 			if (loading==0) {
			loading=1;
			show_Loading();
		}
  //document.getElementById("comment_loading").innerHTML = loadingText;
  //document.getElementById("comment_loading").style.display = "block";
  		http.open('POST',  web_url+'/comment.php');
  		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  		http.onreadystatechange = comment_handleResponse;
  		http.send('delete=1&media_id='+media_id+'&comment_id='+comment_id);
 	}
 return false;
}
function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
function show_download(media_id) {
	try{
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				DragHandler.attach(document.getElementById('field_download'));
				document.getElementById("field_download").style.display = 'block';
				document.getElementById("field_download").innerHTML = http.responseText;
				hide_Loading();
				loading=0;
			}
		}
		http.send('Download='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
function show_download_list(media_id,media_id) {
	try{
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				DragHandler.attach(document.getElementById('field_download_'+media_id));
				document.getElementById("field_download_"+media_id).style.display = 'block';
				document.getElementById("field_download_"+media_id).innerHTML = http.responseText;
				hide_Loading();
				loading=0;
			}
		}
		http.send('Download='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
var playertimeout = 150; //delay 30s
//var wmptimerID;
function delay_getplayer(){
	var timerID = setTimeout("delay_getplayer();", 100);
	if (playertimeout > 0) playertimeout -= 1;
	else{
		clearTimeout(timerID);
		if(myplayer = document.getElementById('myplayer')) {
			myplayer.addModelListener('STATE','auto_change_song_flash');
		}
	}
}

function auto_change_song_flash(obj) {
	if(obj.newstate=='COMPLETED') {
		myplayer.sendEvent('STOP');
		var i=0;
		var check = document.getElementById('m_list_song');
		var cur = document.getElementById("current_id").value;
		while(cur !=check[i].value && i<(check.length-1)) i++;
		if(check[check.length-1].value == cur) next_id = check[0].value;
		else next_id = check[i+1].value;
		manual_change_song(next_id);
	}
	return false;
}

function manual_change_song(id) {
	try{
		var i=0;
		var x=0;
		var cur = document.getElementById("current_id").value;
		var check = document.getElementById('m_list_song');
		//clearInterval(wmptimerID);
		while(cur !=check[i].value && i<=(check.length-1)) i++;
		document.getElementById('m_list_song')[i].selected = '';
		document.getElementById('m_list_song')[i].style.color = '';
		while(id !=check[x].value && x<=(check.length-1)) x++;
		document.getElementById('m_list_song')[x].selected = 'selected';
		document.getElementById('m_list_song')[x].style.color = 'blue';
		document.getElementById("current_id").value = id;
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById("m_play_list_song").innerHTML = http.responseText;
				hide_Loading();
				loading=0;
				document.title = WEBTITLE;
				playertimeout = 150; //delay 30s
				delay_getplayer();
            }
        }
	    http.send('quick_play='+id);
	}
	catch(e){}
	finally{}
	return false;	
}

function Quick_Play(type,media_id) {
    try {
		current_id = document.getElementById(type+"_current_quickplay").value;
		if(current_id) Close_Quick_Play(type,current_id);
		document.getElementById(type+"_current_quickplay").value = media_id;
		if (navigator.appName=="Microsoft Internet Explorer")
			document.getElementById(type+"_show_hide_quickplay_"+media_id).style.display = "block";
		else
			document.getElementById(type+"_show_hide_quickplay_"+media_id).style.visibility = "visible";
        document.getElementById(type+"_quickplay_field_"+media_id).innerHTML = loadingText;
        document.getElementById(type+"_quickplay_field_"+media_id).style.display = "block";
        http.open('POST',  web_url+'/index.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById(type+"_quickplay_field_"+media_id).innerHTML = http.responseText;
                document.getElementById(type+"_quickplay_control_"+media_id).src = web_url+'/files/pls.gif';
				document.getElementById(type+"_quickplay_control_"+media_id).onclick = function() {Close_Quick_Play(type,media_id);}
				document.title = WEBTITLE;
            }
        }
    http.send('quick_play='+media_id);
    }
    catch(e){}
    finally{}
    return false;
}

function Close_Quick_Play(type,media_id) {
	document.getElementById(type+"_quickplay_field_"+media_id).innerHTML = '';
	if (navigator.appName=="Microsoft Internet Explorer")
		document.getElementById(type+"_show_hide_quickplay_"+media_id).style.display = "none";
	else
		document.getElementById(type+"_show_hide_quickplay_"+media_id).style.visibility = "collapse";
	document.getElementById(type+"_quickplay_control_"+media_id).src = web_url+'/files/quick_play.png';
	document.getElementById(type+"_current_quickplay").value = '';
	//alert('Quick_Play(\''+type+'\','+media_id+')');
	document.getElementById(type+"_quickplay_control_"+media_id).onclick = function() {Quick_Play(type,media_id);}
	return false;
}

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

function guestPlaylist(add_id,remove_id) {
	try{
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = playlist_handleResponse;
		http.send('guestPlaylist=1&add_id='+add_id+'&remove_id='+remove_id);
	}
	catch(e){}
	finally{}
}

function playlist_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("playlist_field").innerHTML = response;
			hide_Loading();
			loading=0;
		}
  	}
	catch(e){}
	finally{}
}
function add_del_playlist(add_name,del_id) {
	try {
		if(add_name) name = document.getElementById("add_playlist_name").value;
		if (!name && !del_id) alert('{missing_name_playlist}');
		else{
			if (loading==0) {
					loading=1;
					show_Loading();
			}
        	http.open('POST',  web_url+'/index.php');
        	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        	http.onreadystatechange = playlist_handleResponse;
    		http.send('add_del_playlist=1&add_name='+name+'&remove_id='+del_id);
		}
    }
    catch(e){}
    finally{}
    return false;
}

function loggedPlaylist(add,remove,pl) {
	try{
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if(!pl) { http.onreadystatechange = function() {
				if((http.readyState == 4)&&(http.status == 200)){
					document.getElementById("show_playlist_select_field_"+add).style.display = 'block';
					document.getElementById("show_playlist_select_field_"+add).innerHTML = http.responseText;
					hide_Loading();
					loading=0;
				}
			}
		} else {
			http.onreadystatechange = function() {
				if((http.readyState == 4)&&(http.status == 200)){
					document.getElementById("playlist_field").innerHTML = http.responseText;
					if(add) document.getElementById("show_playlist_select_field_"+add).style.display = 'none';
					hide_Loading();
					loading=0;
				}
			}
		}
		http.send('loggedPlaylist=1&add_id='+add+'&remove_id='+remove+'&pl_id='+pl);
	}
	catch(e){}
	finally{}
}

function addPlaylist(){add_del_playlist(1,0);}

function delPlaylist(pl_id){add_del_playlist(0,pl_id);}

function addToPlaylist(login,song_id,pl_id)
{
	if(!pl_id) pl_id = 0;
	if(login)
		loggedPlaylist(song_id,0,pl_id);
	else
		guestPlaylist(song_id,0);
}
function removeFromPlaylist(login,song_id,pl_id)
{
	if(!pl_id) pl_id = 0;
	if(login)
		loggedPlaylist(0,song_id,pl_id);
	else
		guestPlaylist(0,song_id);
}
function gift(id,width,height) {
	popup(web_url+'/gift.php?id='+id,'gift',width,height);
}


function receive_gift(id) {
	window.location.href = '#Gift,'+id;
}
function changeplayer(type,id,w,h,s) {
	document.getElementById("old_player_field").innerHTML = '';
	document.getElementById("new_player_field").innerHTML = loadingText;
    document.getElementById("new_player_field").style.display = "block";
	var player = '<OBJECT id="Mediaplayer" name="Mediaplayer" WIDTH='+w+' HEIGHT='+h+' classid=clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6>';
	player += '<PARAM NAME="url" VALUE="?play=asx&type='+type+'&id='+id+'">';
	player += '<PARAM NAME="ShowControls" VALUE="-1">';
	player += '<PARAM NAME="ShowDisplay" VALUE="0">';
	player += '<PARAM NAME="ShowStatusBar" VALUE="-1">';
	player += '<PARAM NAME="AutoSize" VALUE="-1">';
	player += '<PARAM NAME="PlayCount" VALUE="5">';
	player += '<PARAM NAME="AutoStart" VALUE="1">';
	player += '<PARAM NAME="AnimationAtStart" VALUE="-1">';
	player += '<PARAM NAME="AutoRewind" VALUE="0">';
	player += '<PARAM NAME="Balance" VALUE="0">';
	player += '<PARAM NAME="BaseURL" VALUE="">';
	player += '<PARAM NAME="BufferingTime" VALUE="5">';
	player += '<PARAM NAME="ClickToPlay" VALUE="-1">';
	player += '<PARAM NAME="DisplayBackColor" VALUE="0">';
	player += '<PARAM NAME="DisplayForeColor" VALUE="16777215">';
	player += '<PARAM NAME="Enabled" VALUE="-1">';
	player += '<PARAM NAME="EnablePositionControls" VALUE="-1">';
	player += '<PARAM NAME="EnableFullScreenControls" VALUE="0">';
	player += '<PARAM NAME="EnableTracker" VALUE="-1">';
	player += '<PARAM NAME="InvokeURLs" VALUE="-1">';
	player += '<PARAM NAME="Language" VALUE="-1">';
	player += '<PARAM NAME="Mute" VALUE="0">';
	player += '<PARAM NAME="Volume" VALUE="100">';
	player += '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/ " src="?play=asx&type='+type+'&id='+id+'" width="'+w+'" height="'+h+'" autostart="1" align="middle" volume="100" stretchtofit="100%" windowlessvideo="fit" enabled="-1" SHOWSTATUSBAR=1  ENABLECONTEXTMENU=0></embed>';
	player += '</OBJECT>';
	document.getElementById("new_player_field").innerHTML = player;
}
function returnplayer(type,id,w,h,s) {
var backcolor="000000";
var frontcolor="FFFFFF";
var lightcolor="FFFFFF";
if(s==1)
{
backcolor="9966CC";
frontcolor="FFE7FF";
lightcolor="7B5DA5";
}
else
{
lightcolor="95caff";
backcolor="007dfc";
frontcolor="95caff";
}
	document.getElementById("new_player_field").innerHTML = '';
	document.getElementById("old_player_field").innerHTML = loadingText;
    document.getElementById("old_player_field").style.display = "block";
	var player = '<embed src="files/mediaplayer.swf" width="'+w+'" height="'+h+'" allowfullscreen="true" allowscriptaccess="always" flashvars="&file=%3Fplay=xml%26type=1%26id='+id+'&abouttxt=NoNhac.Com&aboutlnk=http://www.nonhac.com&backcolor=0x'+backcolor+'&frontcolor=0x'+frontcolor+'&lightcolor=0x'+lightcolor+'&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showeq=true&showdigits=true;volume=100;" />';
	document.getElementById("old_player_field").innerHTML = player;
}
// BEGIN RATING
function Rating(media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  web_url+'/rating.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("rating_field").innerHTML = http.responseText;
			}
		}
		http.send('rating=1&media_id='+media_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}


	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

	function show_star(starNum,rate_text) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		full_star(starNum);
	}
	
	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
	}
	function remove_star() {
		for (var i=0; i < 5; i++)
			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
	}
	function remove_all_star() {
		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
		document.getElementById("rate_text_d").innerHTML = 'Bình Chọn';
	}
	function show_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
// END RATING
/*------------------------------------------------------*/