﻿var http = createRequestObject();
var field = '';
var url = 'http://phimnhanh.net';
var loadingText = "<center><img src='"+ url +"/images/ajax-loader.gif'> <b>Đang tải dữ liệu ...</b></center>";
var loadingfilm = "<center><img src='"+ url +"/images/ajax-loader.gif'><br>Loading.. ...</center>";
           var RATE_OBJECT_IMG = ""+ url +"/images/rate/full.gif";
           var RATE_OBJECT_IMG_HOVER = ""+ url +"/images/rate/full.gif";
           var RATE_OBJECT_IMG_HALF = ""+ url +"/images/rate/half.gif";
           var RATE_OBJECT_IMG_BG = ""+ url +"/images/rate/none.gif";
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 handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}
//#######################################
//# SEARCH
//#######################################
function do_search() {
	kw = document.getElementById("keyword").value;
	
	if (!kw || (kw.length < 3)) alert('Từ khóa phải nhiều hơn 2 ký tự');
	else {
		kw = encodeURIComponent(kw);
		kw = kw.replace("%20", '+');
		window.location.href = ''+ url +'/movie/search/'+kw+'';	
	}
  return false;
}

function locdau(str) {  
  str= str.toLowerCase();  
  str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");  
  str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");  
  str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");  
  str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
  str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
  str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");  
  str= str.replace(/đ/g,"d");  
  str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-"); 
/* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */ 
  str= str.replace(/-+-/g,"-"); //thay thế 2- thành 1- 
  str= str.replace(/^\-+|\-+$/g,"");  
//cắt bỏ ký tự - ở đầu và cuối chuỗi  
  return str;  
  }   

//#######################################
//# ADD EMOTIONS
//#######################################
function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['add'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['add'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['add'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
		//txtarea.selectionStart = s1.length;
		//txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		//txtarea.scrollTop = oldScrollTop;
		$.scrollTo( $('#comment_content'), 800);
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}
//#######################################
//# COUNT WORDS
//#######################################
var submitcount=0;
   function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
   }


function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
      if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}
      if (wordcounter > 250) {field.value = field.value.substring(0, x);}
      else {countfield.value = maxlimit - wordcounter;}
      }
   }

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
//#######################################
//# COMMENT
//#######################################
function showComment(num,film_id,page) { 
	field = document.getElementById("comment_field");
	field.innerHTML = loadingText;
	http.open('POST',  '/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showcomment=1&num='+num+'&film_id='+film_id+'&page='+page); 
  return false; 
} 

function comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				film_id = encodeURIComponent(document.getElementById("film_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
				showComment(num,film_id,1);
			}
			else document.getElementById("comment_loading").innerHTML = response;

		}
  	}
	catch(e){}
	finally{}
}

function comment_check_values_old() {
	film_id = encodeURIComponent(document.getElementById("film_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	try {
	    document.getElementById("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  '/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = comment_handleResponse;
		http.send('comment=1&film_id='+film_id+'&num='+num+'&comment_poster='+comment_poster+'&comment_content='+comment_content);
		document.getElementById("submit").disabled=true;
		document.getElementById("submit").value="Đã gửi Comment...";
	}
	catch(e){}
	finally{}
  return false;
}

function comment_check_values() {
	var film_id = $('#film_id').val();
	var num = $('#num').val();
	var poster = $('#comment_poster').val();
	var comment_content = $('#comment_content').val();
	
	$('#comment_loading').html(loadingText).show();
	$.post('/index.php', {comment: '1', film_id: film_id, num: num, comment_poster: poster, comment_content: comment_content}, function (data) {
		if(data == 'OK') {
			var appendContent = '<div id="lastcomment" class="bloc02 bgd03"><div class="cont18"><div class="cont18desc"><div class="bgd05"><img height="24px" width="24px" align="absmiddle" src="http://forum.phimnhanh.net/images/misc/unknown.gif"><span class="clr01"><strong>Tôi</strong></span> - mới đây</div><div class="cont18desa"><p style="float: left; width: 425px;">'+$('#comment_content').val()+'</p></div></div><br class="clear" /></div></div>';
			$('#comment_loading').fadeOut();
			$('#commentlist').prepend(appendContent);
			$.scrollTo( $('#lastcomment'), 800);
			$('#lastcomment').fadeIn();
		} else {
			$('#comment_loading').html(data);
		}
	});
}

//#######################################
//# RATING
//#######################################
function rating(film_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  '/index.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&film_id='+film_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}


	// pre-fetch image
	(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) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		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() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
//#######################################
//# REQUEST
//#######################################
function showRequest(num,page) {
	field = document.getElementById("request_field");
	field.innerHTML = loadingText;
	http.open('POST',  '/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showrequest=1&num='+num+'&page='+page); 
  return false; 
} 

function request_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				num = encodeURIComponent(document.getElementById("num").value);
				showRequest(num,1);
			}
			else document.getElementById("request_loading").innerHTML = response;
			
		}
  	}
	catch(e){}
	finally{}
}

function request_check_values() {
	request_name = encodeURIComponent(document.getElementById("request_name").value);
	try{
			document.getElementById("send").disabled=true;document.getElementById("send").value="Đang gửi yêu cầu...";
			//document.getElementById("request_loading").innerHTML = loadingText;
			//document.getElementById("request_loading").style.display = "block";
			http.open('POST',  '/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			//http.onreadystatechange = request_handleResponse;
			http.onreadystatechange = function() {
				if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("request-result").innerHTML = http.responseText;
				document.getElementById("r-form").style.display = "none";
				}
			}
			http.send('request=1&request_name='+request_name);
			document.getElementById("send").disabled=true;document.getElementById("send").value="Hoàn thành !";
		}
	  catch(e){}
	  finally{}
	return false;
}
//#######################################
//# BROKEN
//#######################################
function showBroken(film_id,episode_id) {
	try {
		document.getElementById("broken_field").innerHTML = loadingText;
		document.getElementById('broken_field').style.display='block';
		http.open('POST','/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("broken_field").innerHTML = http.responseText;
			}
		}
		http.send('broken=1&film_id='+film_id+'&episode_id='+episode_id);
	}
	catch(e){}
	finally{}
	return false;
}
//#######################################
//# SHOW FILM
//#######################################
function showFilm(num,page,number,apr,cat_id) {
	
    field = document.getElementById('box-'+num);
	field.innerHTML = loadingText;
	http.open('POST',  '/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showfilm=1&num='+num+'&page='+page+'&number='+number+'&apr='+apr+'&cat_id='+cat_id); 
  return false; 
}
//#######################################
//# SHOW TRAILER
//#######################################
function showTrailer(num,apr,page) { 
    field = document.getElementById("trailer_loading");
	field.innerHTML = loadingText;
	http.open('POST',  '/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showtrailer=1&num='+num+'&apr='+apr+'&page='+page); 
  return false; 
}
//#######################################
//# WATCH MOVIE
//#######################################
function player(episode_id) {
	try {
		field = document.getElementById("player_field");
		field.scrollIntoView();
		field.innerHTML = loadingfilm;
		http.open('POST','/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('watch=1&episode_id='+episode_id);
	}
	catch(e){}
	finally{}
	return false;
}




//AJAX CHANGE PLAYER
function playume(yumeid) {
	try {
		document.getElementById("pnplayer").innerHTML = loadingText;
		document.getElementById('pnplayer').style.display='block';
		http.open('POST','/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("pnplayer").innerHTML = http.responseText;
			}
		}
		http.send('yumeid='+yumeid);
	}
	catch(e){}
	finally{}
	return false;
}



function admin_comment(commentid) {
	try {
	$('#admin_comment').html(loadingText);
	$.post('/admin_control.php', {act:'comment', s:'del', commentid:commentid}, function(data) {
		$(this).parent().parent().parent().fadeOut();
		$('#admin_comment').html(data);
	});
	}	catch(e){}
	finally{}
	return false;
}


var my_base_url='http://phimnhanh.net';
var my_pligg_base='';
//var $j = jQuery.noConflict();


/* Load a box on sidebar using ajax */
function ajaxSidebarLoadBox(action,query,rid) {
	$("#"+rid).html("<div align='center' valign='middle'><img src='http://phimnhanh.net/images/fb.gif' align='center'/></div>");
	$.get(my_base_url+my_pligg_base+action, query,
		function(result) {
	    	$("#"+rid).html(result).fadeIn('slow');
	    }
	);
	return false;
}

function close_playeradv(divid) {
$('#'+divid).fadeOut('slow');
}
/*
if(getCookies('pncmlast')) {
refreshTimer = setInterval(function()
		{
		
			$.getJSON('/ajax/comment.php?lastid='+getCookies('pncmlast'), function(data) {
				$.each(data, function() {
					$('#pane2').prepend('<div><p class="clr04">Gửi bởi<span class="clr01"><strong>'+data.poster+'</strong></span> trong phim <span class="clr01"><strong><a href="'+data.url+'">'+data.name+'</a></strong></span></p><p>'+data.comment_content+'</p></div>');
				});
			});			
}*/

function closehelp() {
	createCookie('pnhelp', '1', 365);
	$('#help').fadeOut();
}
function closeAdsFloat() {
	createCookie('muadsaaa', '1', 365);
	$('.divAdsBanner').fadeOut();
}

$(document).ready(function() {
  //if(!readCookie('pnhelp')) {
	//$('body').append('<div id="help" style="background-color: #5F5F5F; position: fixed; text-align: center; top: 0; width: 100%; z-index: 100; border-bottom: 2px solid #000;"><strong>Hi!, Nếu đây là lần đầu bạn truy cập vào PhimNhanh, hãy xem hướng dẫn xem phim tại đây, hoặc <a href="javascript:;" onclick="closehelp();">đóng bảng này lại</a><br /><img src="http://upanh.phimnhanh.net/images/26295705841320213744.png" alt="help" /><br /><a href="javascript:;" onclick="closehelp();">Cám ơn, tôi biết rồi !</a></strong></div>');
  //}
  if(readCookie('muadsaaa')) {
		$('.divAdsBanner').hide();
		//$('body').append('<div id="fl813691" style="height: 152px;"><div align="right" style="padding-right: 10px; font-size: 10px"><a class="min" href="javascript:pf204652clickhide();" title="Ẩn đi">Đóng</a></div><a href="http://xdeal.vn/Thong-tin-deal/33/May-bay-dieu-khien-tu-xa-LS-209" rel="nofollow" target="_blank"><img src="http://upanh.phimnhanh.net/images/51774036911268178224.png" /></a></div>');
		
		//$('body').append('<div id="divBannerFloatLeft" style="float: left; position: fixed; top: 0; left: 0px;"><div style="cursor: pointer; width: 120px; "><img onclick="window.open(\'http://xdeal.vn/Thong-tin-deal/37/Headphone-Triple-S\')" width="120" alt="" src="http://upanh.phimnhanh.net/images/11593001472773667788.jpg"/><div align="center"><a href="javascript:;" onclick="closeAdsFloat()">Đóng quảng cáo</a></div></div><br style="clear: both;"/></div><div id="divBannerFloatRight" style="float: right; position: fixed; top: 0; right: 0px;"><div style="cursor: pointer; width: 120px; "><img onclick="window.open(\'http://xdeal.vn/Thong-tin-deal/23/Dong-ho-day-da\')" width="120" alt="" src="http://upanh.phimnhanh.net/images/16548496235589123048.jpg"/><div align="center"><a href="javascript:;" onclick="closeAdsFloat()">Đóng quảng cáo</a></div></div><br style="clear: both;"/></div>');
  }
  
  
  //screen detect
  
  /*if($(window).width() >= 1100) {
		$('#divBannerFloatLeft').attr('style', 'float: left; position: fixed; top: 0; left: 75px;');
		$('#divBannerFloatRight').attr('style', 'float: right; position: fixed; top: 0; right: 70px;');
  }*/
  
  
  if(readCookie('pnexpand') == 'true' && page == 'watch') {
		expand();
  }
  if(readCookie('pncomment') == 'facebook') {
  fbcomment();
  } else {
  pncomment();
  }
  
  if(!readCookie('pncomment') && LOGIN != '1') {
		fbcomment();
  }

});









function expand() {
	$('.col03').fadeOut('slow');
	$('.col02').attr('style', 'width:auto');
	$('.col02').css('width', 'auto');
	$('object, embed').attr({
	'width':'100%',
	'height':'440'
	});

	$('.cont18desc').attr('style', 'width:940px');
	$('.cont18desc').css('width', '940px');
	$('.cont18desa p').attr('style', 'float: left; width: 920px;');
	$('.cont03desc').attr('style', 'width: 800px');
	
	$('#expand').html('<a href="javascript:;" onclick="shrink();return;"><img align="absmiddle" src="http://static.phimnhanh.net/images/expand_16.png" alt="expand" /> Thu gọn</a>');

	createCookie('pnexpand', 'true', 7);
	return false;
}

function shrink() {

	$('.col03').fadeIn('slow');
	$('.col02').attr('style', 'width: 629px');
	$('.col02').css('width', '629px');
	$('object, embed').attr({
	'width':'630',
	'height':'380'
	});

	$('.cont18desc').attr('style', 'width:610px');
	$('.cont18desc').css('width', '610px');
	$('.cont18desa p').attr('style', 'float: left; width: 590px;');
	$('.cont03desc').attr('style', 'width: 470px');
	
	$('#expand').html('<a href="javascript:;" onclick="expand();return;"><img align="absmiddle" src="http://static.phimnhanh.net/images/expand_16.png" alt="expand" /> Mở rộng</a>');
	
	createCookie('pnexpand', 'false', 7);
	return false;
}

function fbcomment() {
	$('#fbcommentlist').fadeIn();
	$('#commentlist, #cmform, #cmpage, #cmhelp').fadeOut();
	$('#commentswitch').html('<a href="javascript:;" onclick="pncomment();">Xem và gửi bình luận qua PhimNhanh</a>');
	
	createCookie('pncomment', 'facebook', 7);
}

function pncomment() {
	$('#fbcommentlist').fadeOut();
	$('#commentlist, #cmform, #cmpage, #cmhelp').fadeIn();
	$('#comment_poster').fadeIn();
	$('#comment_content').fadeIn();
	$('#commentswitch').html('<a href="javascript:;" onclick="fbcomment();">Xem và gửi bình luận qua Facebook</a>');
	
	createCookie('pncomment', 'phimnhanh', 7);
}

function trailerpost(forumid) {
	var r=confirm("Phim này chưa có trailer, bạn có muốn đóng góp ở diễn đàn không?");
	if (r==true)
	  {
			window.open('http://forum.phimnhanh.net/newthread.php?do=newthread&f='+forumid, '_blank');
	  }
	return;
}

function biography_translate() {
	var biography_en = $('#_fullSInfoShwn').text();
	$('#_fullSInfoShwn').html('<img src="http://static.phimnhanh.net/images/fb-loading.gif" align="absmiddle" /> Đang dịch, vui lòng chờ trong giây lát');
	var p = {};
	p.appid = 'C9EB702E386873890667A64ACFF9E98F3018142B';
	p.text = biography_en;
	p.from = 'en';
	p.to = 'vi';

	$.ajax({
    url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
    data: p,
    dataType: 'jsonp',
    jsonp: 'oncomplete',
    jsonpCallback: 'ajaxTranslateCallback',
    success: function(data, status) {
       $('#_fullSInfoShwn').html(data+'   <strong>Dịch bằng Bing</strong>');
    }
});

}



pf204652bottomLayer = document.getElementById('fl813691');
var checkcca = readCookie('muadsaa');

var pf204652IntervalId = 0;
var pf204652maxHeight = 150;//Chieu cao khung quang cao
var pf204652minHeight = 20;
var pf204652curHeight = 0;
function pf204652show(){
  pf204652curHeight += 2;
  if (pf204652curHeight > pf204652maxHeight){
	clearInterval ( pf204652IntervalId );
  }
  
  pf204652bottomLayer.style.height = pf204652curHeight+'px';
}
function pf204652hide(){
  pf204652curHeight -= 3;
  if (pf204652curHeight < pf204652minHeight){
	clearInterval ( pf204652IntervalId );
  }
  pf204652bottomLayer.style.height = pf204652curHeight+'px';
}

if(!checkcca) {
$('#fl813691').show();
pf204652IntervalId = setInterval ( 'pf204652show()', 5 );
} else {
$('#fl813691').hide();
}

function pf204652clickhide(){
	$('#fl813691').fadeOut();
	createCookie("muadsaa", "1", 1);
	pf204652IntervalId = setInterval ( 'pf204652hide()', 5 );
}
function pf204652clickshow(){
	document.getElementById('pf204652hide').style.display='inline';
	document.getElementById('pf204652show').style.display='none';
	pf204652IntervalId = setInterval ( 'pf204652show()', 5 );
}
function pf204652clickclose(){
	document.body.style.marginBottom = '0px';
	pf204652bottomLayer.style.display = 'none';
}
