﻿var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loadingText = '<center><font color="yellow"><b>Đang tải dữ liệu ...</b></font><br><img src="images/loading.gif" /></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_keyword() {
	var frm = document.frm_search;
	kw = frm.keyword.value;
	if (kw == "")
		alert('Bạn chưa nhập từ khóa');
	else {
		kw = kw;		
		for(var i = 0; i < frm.elements.length; i++) {
			if(frm.elements[i].type == 'radio' && frm.elements[i].checked == true) {
				type = frm.elements[i].value ;
				switch (type) {
					case 'key' : type = 1; break;
					case 'casi' : type = 2; break;
				}
				last_url = '';
				if (type==1)
				window.location.href = '#tim_kiem/key/'+kw;
				else if(type==2)
				window.location.href = '#tim_kiem/casi/'+kw;
						
			}		
		}
	}
	return false;
}

function setValue(obj)
{
		var str = 'Tìm là có - Mò là thấy';   
			if(obj.value == '')
			{
				obj.value = str;
				obj.style.color = '#848484';
			}
			else if(obj.value == str)
			{
				obj.value = '';
				obj.style.color = '#000000';
			}
}
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 ='Music68.net - Trang Âm Nhạc | Bài Hót : '+Replaces(song)+' - Ca Sĩ: '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='Music68.net - Trang Âm Nhạc | Ca Sĩ: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='Music68.net - Trang Âm Nhạc | Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='Music68.net - Trang Âm Nhạc - | Bài Hót: '+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 = 40;
//var siteURL = 'http://namdinh.org/media/nct';
var siteURL = 'http://www.nhaccuatui.com/m/';
//var siteURL = 'http://getlink.thienduongviet.org/nct';
if(type==1)
//url = siteURL+'/xml.php?id='+id+'&showeq=true';
//url = siteURL+'/'+id+'.mp3&showeq=true';
url = siteURL+'/'+id;
if(type==2)
{
//url = siteURL+'/xml.php?id='+id+'&logo=images/flash.png';
//url = siteURL+'/'+id+'.flv&logo=images/flash.png';
url = siteURL+'/'+id;
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="line-height:20px; width:100%;"><img src="images/cakhuc.png"> Ca khúc: <b>'+name+'</b><br><img src="images/trinhbay.png"> Trình bày: <b>'+singer+'</b></div>';
	var	player_f = '<div style="text-align:center;"><table style="border:4px solid #00CCFF; width:350px;" align="center" cellpadding="0" cellspacing="0"><tr><td><object height="'+h+'" width="350px"><param name="movie" value="'+url+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="flashvars" value="&autostart=true" /><embed src="'+url+'" quality="high" wmode="transparent" type="application/x-shockwave-flash" height="'+h+'" width="350px"></embed></object></td></tr></table></div>';
	var sender_f = '<div class="sender"><div class="play_sl"></div><div class="play_sr"><img src="images/icolink.png"> <input type="text" class="S_Input" onFocus="this.select()" value="http://music68.net/nct/#tim_kiem/key/'+name+'"></div>';
		sender_f+= '<div class="play_sl"></div><div class="play_sr"><img src="images/icoblog.png"> <input type="text" class="S_Input" onFocus="this.select()" value=\'<embed type="application/x-shockwave-flash" id="ATMediaplayer" wmode="transparent" name="ATMediaplayer" src="http://i134.photobucket.com/albums/q113/Zackuzt/mediaplayer.swf?file=http://nhac.anhtrang.org/nct/xml.php?id='+id+'&autostart=true&repeat=true&shuffle=true&logo=http://phim.music68.net/images/logo1.png" quality="high" allowfullscreen="true" allowscriptaccess="always" width="350" height="400">\'></div>';
		sender_f+= '<div class="play_sl"></div><div class="play_sr"><img src="images/ico4rum.png"> <input type="text" class="S_Input" onFocus="this.select()" value="[FLASH]http://i134.photobucket.com/albums/q113/Zackuzt/mediaplayer.swf?file=http://nhac.anhtrang.org/nct/xml.php?id='+id+'&autostart=true&repeat=true&shuffle=true&logo=http://phim.music68.net/images/logo1.png[/FLASH]"></div></div>';
		sender_f+= '<center><a href="http://nhac.anhtrang.org/nct/xml.php?id='+id+'" onclick="return alert(\'Click chuột phải chọn Save Target As...\');"><img src="images/download.png" alt="Tải bài hát về máy!" 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_setHomepage(obj)
{
    obj.style.behavior='url(#default#homepage)';
    if (document.all)
    {
        obj.setHomePage('http://music68.net');
    }
    else
    {
        alert("Kéo Logo DvCk vào nút Home để đặt DvCk 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 = '<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=music68.net&aboutlnk=http://www.music68.net&lightcolor=0xffffff&backcolor=0x000000&frontcolor=0xffffff&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{}
}
/*------------------------------------------------------*/

//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