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 loadingText = '<span style="text-align:center; font-weight:bold;"><img src="css/images/loading.gif" alt="" />&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{
		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)){
			//document.getElementById("loading").style.display = "none";
			response = http.responseText;
			if (current_url.indexOf('Play') != -1 || current_url.indexOf('Gift') != -1) {
				field.style.marginBottom = "10";

			}
			field.innerHTML = response;
			hide_Loading();
            loading=0;
			field.style.display = "";
		}
  	}
	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[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 Loading Alpha   |
// + ---------------------- +  
function getVar(url,cnt)
{
	url=url+'#';
	url=url.split('#');
	if (!url[1]) window.location.href = '?act=home';
	url=url[1];
	url=url+',';
	url=url.split(',');
	if (url[0] == 'Logout')
		window.location.href = '/movie/?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 trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function Get_Player(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").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("load_playing").style.display = "none";
				document.getElementById("playing").innerHTML = http.responseText;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_'+(i+1)).className = 'cl_part';
				}
				document.getElementById('epi_'+cur).className = 'cl_part_cur';
			}
		}
	http.send('ajaxtype=player&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}
function Get_Player_New(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").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("load_playing").style.display = "none";
				document.getElementById("playing").innerHTML = http.responseText;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_new_'+(i+1)).className = 'cl_part_new';
				}
				document.getElementById('epi_new_'+cur).className = 'cl_part_new_cur';
			}
		}
	http.send('ajaxtype=player&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}
function Get_Player_New1(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").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("load_playing").style.display = "none";
				document.getElementById("playing").innerHTML = http.responseText;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_new1_'+(i+1)).className = 'cl_part_new1';
				}
				document.getElementById('epi_new1_'+cur).className = 'cl_part_new1_cur';
			}
		}
	http.send('ajaxtype=player&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}
function showcat(id_cat,pg) {
 try {
	  //document.getElementById("loading_cat").innerHTML = loadingText;
	  document.getElementById("loading_cat").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("loading_cat").style.display = "none";
	  document.getElementById("cat_field").innerHTML = http.responseText;
	  hide_Loading();
	  loading=0;
	  }
  }
  http.send('cat=1&id_cat='+id_cat+'&pg='+pg);
 }
 catch(e){}
 finally{}
 return false;
}
// + ---------------------- +
// |        COMMENT         |
// + ---------------------- +
function showComment(media_id,pg) {
 try {
  	document.getElementById("loading_comment").style.display = "block";
  	if (loading==0) {
			loading=1;
			show_Loading();
	}
  	http.open('POST',  'comment.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;   		}
  }
  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 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);
 name_mail = encodeURIComponent(document.getElementById("name_mail").value);
 if(trim(comment_content) == "")
  alert("Comment !");
if(trim(name_mail) == "")
  alert("Name or Email !");
 else if (comment_content.length > 500)
  alert("Contents exceed 500 characters.");
 else {
  try {
   		if (loading==0) {
			loading=1;
			show_Loading();
		}
   		http.open('POST',  'comment.php');
  		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = comment_handleResponse;
	   	http.send('comment=1&media_id='+media_id+'&name_mail='+name_mail+'&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();
		}
  		http.open('POST',  '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 get_download(obj,type,m_id,encrypt_id) {
	try {
		if(obj==''){
			type=encodeURIComponent(type);
			m_id=encodeURIComponent(m_id);
			encrypt_id=encodeURIComponent(encrypt_id);
			multi_id='';
		}
		else{
		var type=encodeURIComponent(type);	
		var m_id=encodeURIComponent(obj.select_down.value);
		var encrypt_id=encodeURIComponent(obj.id_down.value);
		var multi_id=encodeURIComponent(obj.movie_id.value);	
		}
   		document.getElementById("field_download").innerHTML = loadingText;
   		document.getElementById("field_download").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("field_download").innerHTML = http.responseText;
			}
		}
		http.send('ajaxtype=download&type='+type+'&multi_id='+multi_id+'&m_id='+m_id+'&encrypt_id='+encrypt_id);
	}
	catch(e){}
	finally{}
	return false;
}
// + ---------------------------------- +
// |            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',  '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;
}
	(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) {
		document.getElementById("rate_text_d").innerHTML = rate_text;
		remove_star();
		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() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
/*--------------------------------------------*/
function Rating_dvd(media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  'rating_dvd.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_dvd=1&media_id='+media_id+'&star='+star);

	}
	catch(e){}
	finally{}
	return false;
}
function alertBrokenLink(id) {
	if (confirm("This video broken link?")) {
		try{
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = BrokenResponse;
			http.send('ajaxtype=Broken&id='+id);
		}
		catch(e){}
		finally{}
	}
}

function BrokenResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			if (response == 1) alert("Report was send to Administator. Thank you!!!");
			else alert("Report was send to Administator. Thank you!!!");
		}
  	}
	catch(e){
		alert("Thank you report!!!");
	}
	finally{}
}
