﻿
var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loadingFlash = "<center><img src='http://nhac.tuoigi.com/@/images/loading.gif' alt='Đang tải dữ liệu ...'></center>";
var loadingText = '<center><embed id="ld" wmode="transparent" name="ld" src="http://phim.tuoigi.com/nhac/images/loading.gif" 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_search() {
	kw = document.getElementById("keyword").value;
	if (!kw || kw=='Tên bài hát hoặc ca sỹ') 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 ='Nhac.TuoiGi.Com | Song: '+Replaces(song)+' - Singer: '+Replaces(sing);
}
function TitleSing(title)
{
  document.title ='Nhac.TuoiGi.Com | Singer: '+Replaces(title);
}
function TitleAlbum(title)
{
  document.title ='Nhac.TuoiGi.Com | Album: '+Replaces(title);
}
function TitleSong(title)
{
  document.title ='Nhac.TuoiGi.Com | 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 isNav = navigator.appName == "Netscape";
	
var url = '';
var h = 160;

var siteURL = 'http://nhac.tuoigi.com/@';

document.getElementById("content_lyric").style.display = "none";
if(type==1){
url = siteURL+'/xml.php?id='+id+'&showeq=true';
}
if(type==2)
{
url = siteURL+'/xml.php?id='+id+'&logo=http://nhac.tuoigi.com/@/images/flash.png';
h = 300;
}
if(type==10)
{
url = siteURL+'/all_playlist.php?id='+singer+'&logo=http://nhac.tuoigi.com/@/images/flash.png';
h = 300;
}

	try {
		
		if(type!=10){	
	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:5; font-size:11px;"><i>Ca khúc: </i>'+name+'<br><i>Trình bày: </i> '+singer+'</div>';
	var	player_f = '<table border=0 cellpadding=0 cellspacing=0 bordercolor="#ffffff" style="margin-top:5;font-size:11px;"><tr><td><embed type="application/x-shockwave-flash" id="Mediaplayer" name="Mediaplayer" src="'+siteURL+'/mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#ffffff" flashvars="file='+url+'&abouttxt=nhac.tuoigi.com/@&aboutlnk=http://nhac.tuoigi.com/@&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100" height="202" width="434"></td></tr></table>';
	var sender_f = '<span style="font-size:11px;">Link nhạc: <input type="text" style="width: 209px" onFocus="this.select()" value="'+siteURL+'/#nghe/'+id+'/'+type+'"></span>';
		sender_f+= '<br><span style="font-size:11px;">Copy vào blog: <input type="text" style="width: 185px" 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">\'></span>';
		sender_f+= '<br><span style="font-size:11px;">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]"></span>';
		
		
		if(isNav){
			sender_f+= '<br><span style="font-size:11px; padding:2px;"><a href="download.php?id='+id+'" target="_blank"><img src="images/icodown.png" alt="download" border=0> <span style="color:#FF6600">Download</span></a> ca khúc này.</span>';
		}else{
			sender_f+= '<br><span style="font-size:11px; padding:2px;">Click chuột phải <a href="download.php?id='+id+'" target="_blank" onclick="alert(\'Click chuột phải và chọn Save Target As...  để tải (download) ca khúc này về!...\'); return false;"><span style="color:#FF6600"><img src="images/icodown.png" alt="download" border=0>vào đây</span></a> và chọn Save Target As...  để tải (download) ca khúc này về .</span>';
		}
		
		
		
		
	document.getElementById("info_field").innerHTML = info_f;
	document.getElementById("player_field").innerHTML = player_f;
	document.getElementById("sender_field").innerHTML = sender_f;
	
	document.getElementById("lyric").innerHTML = 'Bấm <a href="javascript:void(0)" onclick = "ajaxpage(\''+siteURL+'/lyric.php?id='+id+'\',\'content_lyric\');"> <span style="color:#FF6600">vào đây</span> </a> để xem lời bài hát.';
	
		document.getElementById("all_player_field").innerHTML = '';
	
		}else{
			
			document.getElementById("player_field").innerHTML = '';
			document.getElementById("all_player_field").innerHTML = loadingText;
    		document.getElementById("all_player_field").style.display = "block";
			
			 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:5; font-size:12px;"><i>Bạn đang nghe Playlist: </i><strong>'+name+'</strong></div>';
			var	sender_f= '<br><span style="font-size:12px;">Copy vào blog: <input type="text" style="width: 185px" onFocus="this.select()" value=\'<embed type="application/x-shockwave-flash" id="Mediaplayer2" wmode="transparent" name="Mediaplayer2" src="'+siteURL+'/mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#fff" flashvars="file='+url+'&abouttxt=nhac.tuoigi.com/@&aboutlnk=http://nhac.tuoigi.com/@&lightcolor=0xffffff&backcolor=0x000000&frontcolor=0xffffff&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100&displayheight=64" height="200" width="300">\'></span>';
				sender_f+= '<br><span style="font-size:12px;">Copy vào diễn đàn: <input type="text" style="width: 160px" onFocus="this.select()" value="[FLASH]'+siteURL+'/mediaplayer.swf?file='+url+'&allowfullscreen=true&quality=high&allowscriptaccess=always&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100&displayheight=155[/FLASH]"></span>';
			
			var	player_f = '<table border=0 cellpadding=0 cellspacing=0 bordercolor="#ffffff" style="margin-top:5;font-size:12px;"><tr><td><embed type="application/x-shockwave-flash" id="Mediaplayer2" name="Mediaplayer2" src="'+siteURL+'/mediaplayer.swf" quality="high" allowfullscreen="true" allowscriptaccess="always" bgcolor="#fff" flashvars="file='+url+'&abouttxt=nhac.tuoigi.com/@&aboutlnk=http://nhac.tuoigi.com/@&bgcolor=#fff&lightcolor=0xffffff&backcolor=0x000000&frontcolor=0xffffff&showfsbutton=true&autostart=true&repeat=true&shuffle=false&showdigits=true&volume=100&displayheight=64" height="202" width="434"></td></tr></table>';
			
			document.getElementById("all_player_field").innerHTML = player_f;
			document.getElementById("info_field").innerHTML = info_f;
			document.getElementById("sender_field").innerHTML = sender_f;
			document.getElementById("lyric").innerHTML = '';
			
			
		}
	//end type
	
	}
	catch(e){}
	finally{}
	return false;
}

function popup(url,wdname,width,height)
{
	if (width == null)  { width  = 350; }   // default width
	if (height == null) { height = 400; }   // default height
	newwin=window.open(url,wdname,'scrollbars=no,width='+width+',height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}


function bb_setHomepage(obj)
{
    obj.style.behavior='url(#default#homepage)';
    if (document.all)
    {
        obj.setHomePage('http://nhac.tuoigi.com/@');
    }
    else
    {
        alert("Kéo Logo Nhac.TuoiGi.Com vào nút Home để đặt Nhac.TuoiGi.Com website 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=nhac.tuoigi.com/@&aboutlnk=http://nhac.tuoigi.com/@&lightcolor=0xffffff&backcolor=0x000000&frontcolor=0xffffff&showfsbutton=true&autostart=false&repeat=true&shuffle=false&showdigits=true&volume=100&displayheight=64" height="202" width="296"><br><a onclick="Playlist(0,\'all\'); return false;" style="cursor:pointer">Xóa tất cả</a>';
		
		if(add_id==0 && remove_id=="all"){
			document.getElementById("playlist_player").innerHTML ='';
		}
		
	}
	catch(e){}
	finally{}
}
function copy(text2copy) {
  if (window.clipboardData) {
	window.clipboardData.setData("Text",text2copy);
  }
  alert("Đã copy xong\nBạn chỉ việc Ctrl+V để dán!");
}
function playlist_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("playlist_field").innerHTML = response;
		}
  	}
	catch(e){}
	finally{}
}
/* --- */

/*------------------------------------------------------*/

