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

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 == 'play' || act == 'play_zing' || act == 'play_youtube' || act == 'play_clip' || act == 'play_nct' || act == 'play_nhacso' || act == 'play_blog' || act == 'play_tamtay')
		field = document.getElementById("playing_field");
		else field = document.getElementById("data_field");
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		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)){
			hide_Loading();
			loading=0;
			response = http.responseText;
			if (current_url.indexOf('play') != -1) {
				field.style.marginBottom = "10";
			}
			field.innerHTML = response;
			field.style.display = "";
			field.scrollIntoView();
		}
  	}
	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=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[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 = '#cnews/ngs/News/Choi-blog/';
	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.indexOf('cnews') ) {
		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 = encodeURIComponent(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 'zing' : type = 1; break;
					case 'youtube' : type = 2; break;
					case 'music' : type = 3; break;
					case 'nhacso' : type = 4; break;
					case 'clip' : type = 5; break;
					case 'nct' : type = 6; break; 
					case 'blog' : type = 7; break; 
					case 'tamtay' : type = 8; break; 
				}
				last_url = '';
				if (type==1) 
                window.location.href = '#search_zing/'+kw;
                else
				if (type==2) 
		        window.location.href = '#search_youtube/'+kw;
                else
				if (type==3) 
		        window.location.href = '#search/'+kw;
		        else
				if (type==4) 
		        window.location.href = '#search_nhacso/'+kw;
		        else
				if (type==5) 
		        window.location.href = '#search_clip/'+kw;
				else if (type==6)
				window.location.href = '#search_nct/'+kw;  
				else if (type==7)
				window.location.href = '#search_blog/'+kw;
				else if (type==8)
				window.location.href = '#search_tamtay/'+kw; 
				}		
		}
	}
	return false;
}
/*------------------------------------------------------*/
function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function Title(song,sing)
{
  document.title ='––– ChipLove.Info –––» Song: '+Replaces(song)+' - Singer: '+Replaces(sing)+' –––';
}
function TitleSing(title)
{
  document.title ='––– ChipLove.Info –––» Singer: '+Replaces(title)+' –––';
}
function TitleAlbum(title)
{
  document.title ='––– ChipLove.Info –––» Album: '+Replaces(title)+' –––';
}
function TitleSong(title)
{
  document.title ='––– ChipLove.Info –––» Song: '+Replaces(title)+' –––';
}

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

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

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,'scrollbars=yes,width='+width+',height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}