var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loading=0;
var update = '001';

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 == 'DocTruyenTranh'){
			//self.scrollTo(0, 750);
			field = document.getElementById("playing_field");
			var vitri_top = document.getElementById("playing_field").offsetTop + 200;
			//alert(vitri_top);
			self.scrollTo(0, vitri_top);			
		}
		else if (act == 'Download')
			field = document.getElementById("download_field");
		else if (act !== 'DanhSachTruyenTranh') {
			field = document.getElementById("data_field");
			//self.scrollTo(0, 400);
			var vitri_top = document.getElementById("data_field").offsetTop + 200;
			//alert(vitri_top);
			self.scrollTo(0, vitri_top);
		}
		else field = document.getElementById("data_field");
		//document.getElementById("loading").innerHTML = loadingText;
		//document.getElementById("loading").style.display = "block";
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		current_url = encodeURIComponent(current_url);
		http.open('POST',  'truyentranh.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";
			hide_Loading();
			loading=0;
			response = http.responseText;
			if (current_url.indexOf('DocTruyenTranh') != -1 || current_url.indexOf('Gift') != -1) {
				field.style.marginBottom = "10";
				/*document.getElementById("show_hide").style.display = "block";*/
			}
			field.innerHTML = response;
			field.style.display = "";
		}
  	}
	catch(e){}
	finally{}
}

// NDK Loading
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=400;
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[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}

function show_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}

function hide_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nhide()",1);
}

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('LoadingDiv');
	newy = cury+((pY-cury)/16)+12;
	moveobj(obj,curx,newy);
}

function nshow() {
	obj = getobj('LoadingDiv');
	newx = curx+((mid_w-curx)/16)-7;
	moveobj(obj,newx,cury);
}
function nhide() {
	obj = getobj('LoadingDiv');
	newx = curx+((0-curx)/16)-15;
	moveobj(obj,newx,cury);
}
// End

function getVar(url,cnt)
{
	url=url+'#';
	url=url.split('#');
	if (!url[1]) {
		window.location.href = '#DanhSachTruyenTranh';
	}
	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;
	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 'all' : type = 999; break;
			case 'song' : type = 1; break;
			case 'ebook' : type = 5; break;
			case 'file' : type = 6; break;
			case 'singer' : type = 2; break;
			case 'album' : type = 3; break;
			case 'news' : type = 4; break;			
			case 'esnips' : type= 9;break;
			case 'internet' : type = 8; break;
			case 'video' : type = 7; break;
			case 'NCTget' : type = 10; break; 
			
		}
		last_url = '';	
		//[Mod] Search,Play from Esnips.com
		if (type==7) 
		window.location.href = '#Yget,'+kw;
		else if (type==8) 
		window.location.href = '#Zget,'+kw;		
		else if (type==9)
        window.location.href = '#Esget,'+kw;
		else if (type==10)		
		window.location.href = '#NCTget,'+kw;
		//[Mod] Search,Play from Esnips.com  
		else
		window.location.href = '#Search,'+type+','+kw;
	}
	return false;

}

function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function show_relate(type,id,page) {
    try {
        document.getElementById("relate_"+type+"_field").innerHTML = loadingText;
        document.getElementById("relate_"+type+"_field").style.display = "block";
        http.open('POST',  'truyentranh.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById("relate_"+type+"_field").innerHTML = http.responseText;
            }
        }
        http.send('relate=1&'+type+'='+id+'&pg='+page);
        }
    catch(e){}
    finally{}
    return false;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

