﻿// JScript File
//created by: Hoang Anh Quy
var RATE_OBJECT_IMG = 'images/icons/icon_star2.gif';
var RATE_OBJECT_IMG_HOVER = 'images/icons/icon_star2.gif';
var RATE_OBJECT_IMG_HALF = 'images/icons/icon_star1.gif';
var RATE_OBJECT_IMG_BG = 'images/icons/icon_star0.gif';
var RATE_OBJECT_IMG_REMOVED = 'images/icons/icon_star0.gif';
var re_rated = 0,movID_previous = -1,forComID = -1;
var re_rated_2 = 0,chaID_previous_2 = -1,forComID_2 = -1;
var re_rated_account = 0,userID_previous_account = -1;
//ajax function
// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var divContentId ="";
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try	{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
		}
	}
	if (!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
	}
	return xmlHttp;
}

function New_rate_object(ratingElementId, maxStars, objectName, ratingMessageId, size)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.ratingMessageId = ratingMessageId
	this.starTimer = null;
	this.star_count = 0;

	if (size=='S') {
		RATE_OBJECT_IMG      = 'images/rate/star2.gif'
		RATE_OBJECT_IMG_HALF = 'images/rate/star2_half.gif'
		RATE_OBJECT_IMG_BG   = 'images/rate/star2_bg.gif'
	}

	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;

	function show_star(starNum, skipMessageUpdate) {
		if (re_rated==0){
			this.clearStarTimer();
			this.remove_star();
			this.full_star(starNum);
			if (!skipMessageUpdate)
				this.setMessage(starNum);
		}
	}

	function setMessage(starNum) {
		messages = new Array("Your Rating", "Rating 1", "Rating 2", "Rating 3", "Rating 4", "Rating 5");
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}

	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
		    if (document.getElementById('star_' +"_"  + (i+1)))
			document.getElementById('star_'  + "_" + (i+1)).src = RATE_OBJECT_IMG;
	}

	function remove_star() {
		for (var i=0; i < this.maxStars; i++) {
			if (i <= this.star_count) {
			    if (document.getElementById('star_' +"_"  + (i+1)))
				document.getElementById('star_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
			}
			else {
			    if (document.getElementById('star_' +"_"  + (i+1)))
				document.getElementById('star_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG;
			}
		}
	}

	function set_star(starNum,is_member,is_rated,movID) {
		if (is_member=='false'){
			if (confirm("Để thực hiện thao tác này mời bạn đăng nhập, click Ok nếu bạn đồng ý!"))
			{
			  window.location.href="/users/notice_login";
			}
		}
		else{
			if ((is_rated=='false')&&(movID_previous!=movID)){
				//if user want to vote for another
				movID_previous = movID;
				mes = "Cám ơn bạn đã bình chọn cho Clip này!";
				this.star_count = starNum;
				this.draw_star(starNum);
				this.clearStarTimer();
				this.remove_star();
				this.full_star(starNum);
				document.getElementById(this.ratingMessageId).innerHTML = mes;
				var ratingElementId = this.ratingElementId;
				var url = "star_count=" + this.star_count +"&movID="+movID; 	//update database using ajax
				getHTML('/mov/ajax_clip_star_update', url,"star_update");
				if ($('ratenum'))
				{
					var totalRate = ($('ratenum').innerHTML)*1+1;
					$('ratenum').innerHTML = totalRate;
				}
			}
			else
				alert("Bạn đã bình chọn cho Clip này rồi");
		}
	}
	function set_star_mytv(starNum,is_member,is_rated,movID) {
		if (is_member=='false'){
			if (confirm("Để thực hiện thao tác này mời bạn đăng nhập, click Ok nếu bạn đồng ý!"))
			{
			  window.location.href="/users/notice_login";
			}
		}
		else{
			if ((is_rated=='false')&&(movID_previous!=movID)){
				//if user want to vote for another
				movID_previous = movID;
				mes = new Array("Cám ơn bạn đã bình chọn!");
				this.star_count = starNum;
				this.draw_star(starNum);
				this.clearStarTimer();
				this.remove_star();
				this.full_star(starNum);
				document.getElementById(this.ratingMessageId).innerHTML = mes[0];
				var ratingElementId = this.ratingElementId;
				var url = "star_count=" + this.star_count +"&movID="+movID; 	//update database using ajax
				getHTML('/mov/ajax_clip_star_update', url,"star_update");
				var totalRate = ($('totalRate').innerHTML)*1+1;
				$('totalRate').innerHTML = totalRate;
			}
			else
				alert("Bạn đã bình chọn cho Clip này rồi");
		}
	}

	function draw_star(starNum, skipMessageUpdate) {
		this.star_count=starNum;
		this.show_star(starNum, skipMessageUpdate);
	}

	function clear_star() {
		if (re_rated==0){
			this.starTimer = setTimeout(this.objectName + ".reset_star()", 3000000);
		}
		this.show_star(0);
		this.setMessage(0);
	}

	function reset_star() {
		this.clearStarTimer();
		if (this.star_count)
			this.draw_star(this.star_count);
		else
			this.remove_star();
		this.setMessage(0);
	}

	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}

	this.clear_star = clear_star;
	this.clearStarTimer = clearStarTimer;
	this.remove_star = remove_star;
	this.full_star = full_star;
	this.reset_star = reset_star;
	this.set_star = set_star;
	this.set_star_mytv = set_star_mytv;
	this.draw_star = draw_star;
	this.show_star = show_star;
	this.setMessage = setMessage;
}

function New_rate_object_account(ratingElementId, maxStars, objectName, ratingMessageId, size)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.ratingMessageId = ratingMessageId
	this.starTimer = null;
	this.star_count = 0;

	if (size=='S') {
		RATE_OBJECT_IMG      = '/images/rate/star2.gif'
		RATE_OBJECT_IMG_HALF = '/images/rate/star1.gif'
		RATE_OBJECT_IMG_BG   = '/images/rate/star0.gif'
	}

	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;

	function show_star_account(starNum, skipMessageUpdate) {
		if (re_rated==0){
			this.clearStarTimer_account();
			this.remove_star_account();
			this.full_star_account(starNum);
			if (!skipMessageUpdate)
				this.setMessage_account(starNum);
		}
	}

	function setMessage_account(starNum) {
		messages = new Array("Mời bạn bình chọn cho thành viên này", "Tẻ nhạt", "Bình thường", "Cũng hay", "Khá thú vị", "Rất tuyệt vời");
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}

	function full_star_account(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star_'  + "_" + (i+1)).src = RATE_OBJECT_IMG;
	}

	function remove_star_account() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.star_count)
				document.getElementById('star_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
			else
				document.getElementById('star_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG;
	}

	function set_star_account(starNum,is_member,is_rated,userID) {
		if (is_member=='false'){
			if (confirm("Để thực hiện thao tác này mời bạn đăng nhập, click Ok nếu bạn đồng ý!"))
			{
			  window.location.href="/users/notice_login";
			}
		}
		else{
			if ((is_rated=='false')&&(userID_previous_account!=userID)){
				//if user want to vote for another
				userID_previous_account = userID;
				mes = "Cám ơn bạn đã bình chọn cho thành viên này!";
				this.star_count = starNum;
				this.draw_star_account(starNum);
				this.clearStarTimer_account();
				this.remove_star_account();
				this.full_star_account(starNum);
				document.getElementById(this.ratingMessageId).innerHTML = mes;
				var ratingElementId = this.ratingElementId;
				var url = "is_member=true&star_count=" + this.star_count +"&userID="+userID; 	//update database using ajax
				getHTML('/mov/ajax_clip_star_update', url,"star_update");
			}
			else
				alert("Bạn đã bình chọn cho thành viên này rồi");
		}
	}

	function draw_star_account(starNum, skipMessageUpdate) {
		this.star_count=starNum;
		this.show_star_account(starNum, skipMessageUpdate);
	}

	function clear_star_account() {
		if (re_rated==0){
			this.starTimer = setTimeout(this.objectName + ".reset_star()", 3000000);
		}
		this.show_star_account(0);
		this.setMessage_account(0);
	}

	function reset_star_account() {
		this.clearStarTimer();
		if (this.star_count)
			this.draw_star_account(this.star_count);
		else
			this.remove_star();
		this.setMessage(0);
	}

	function clearStarTimer_account() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}

	this.clear_star_account= clear_star_account;
	this.clearStarTimer_account = clearStarTimer_account;
	this.remove_star_account = remove_star_account;
	this.full_star_account = full_star_account;
	this.reset_star_account = reset_star_account;
	this.set_star_account = set_star_account;
	this.draw_star_account = draw_star_account;
	this.show_star_account = show_star_account;
	this.setMessage_account = setMessage_account;
}
function New_rate_object_2(ratingElementId, maxStars, objectName, ratingMessageId, size)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.ratingMessageId = ratingMessageId
	this.starTimer_2 = null;
	this.star_count_2 = 0;

	if (size=='S') {
		RATE_OBJECT_IMG      = '/images/rate/star2.gif'
		RATE_OBJECT_IMG_HALF = '/images/rate/star1.gif'
		RATE_OBJECT_IMG_BG   = '/images/rate/star0.gif'
	}

	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;

	function show_star_2(starNum, skipMessageUpdate) {
		if (re_rated_2==0){
			this.clearStarTimer_2();
			this.remove_star_2();
			this.full_star_2(starNum);
			if (!skipMessageUpdate)
				this.setMessage_2(starNum);
		}
	}

	function setMessage_2(starNum) {
		messages_2 = new Array("Mời bạn bình chọn cho MyTV", "Tẻ nhạt", "Bình thường", "Cũng hay", "Khá thú vị", "Rất tuyệt vời");
		document.getElementById(this.ratingMessageId).innerHTML = messages_2[starNum];
	}

	function full_star_2(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('mytvstar_'  + "_" + (i+1)).src = RATE_OBJECT_IMG;
	}

	function remove_star_2() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.star_count_2)
				document.getElementById('mytvstar_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
			else
				document.getElementById('mytvstar_' +"_"  + (i+1)).src = RATE_OBJECT_IMG_BG;
	}

	function set_star_2(starNum_2,is_member,is_rated,chaID) {
		if (is_member=='false'){
			if (confirm("Để thực hiện thao tác này mời bạn đăng nhập, click Ok nếu bạn đồng ý!"))
			{
			  window.location.href="/users/notice_login";
			}
		}
		else{
			if ((is_rated=='false')&&(chaID_previous_2!=chaID)){
				//if user want to vote for another
				chaID_previous_2 = chaID;
				mes = new Array("Cám ơn bạn đã bình chọn!");
				this.star_count_2 = starNum_2;
				this.draw_star_2(starNum_2);
				this.clearStarTimer_2();
				this.remove_star_2();
				this.full_star_2(starNum_2);
				document.getElementById(this.ratingMessageId).innerHTML = mes[0];
				var ratingElementId = this.ratingElementId;
				var url = "is_channel=true&star_count=" + this.star_count_2 +"&chaID="+chaID; 	//update database using ajax
				getHTML('/mov/ajax_clip_star_update', url,"star_update");
			}
			else
				alert("Bạn đã bình chọn cho MyTv này rồi");
		}
	}

	function draw_star_2(starNum_2, skipMessageUpdate_2) {
		this.star_count_2=starNum_2;
		this.show_star_2(starNum_2, skipMessageUpdate_2);
	}

	function clear_star_2() {
		if (re_rated_2==0){
			this.starTimer_2 = setTimeout(this.objectName + ".reset_star_2()", 3000000);
		}
		this.show_star_2();
		this.setMessage_2(0);
	}

	function reset_star_2() {
		this.clearStarTimer_2();
		if (this.star_count_2)
			this.draw_star_2(this.star_count_2);
		else
			this.remove_star_2();
		this.setMessage_2(0);
	}

	function clearStarTimer_2() {
		if (this.starTimer_2) {
			clearTimeout(this.starTimer_2);
			this.starTimer_2 = null;
		}
	}

	this.clear_star_2 = clear_star_2;
	this.clearStarTimer_2 = clearStarTimer_2;
	this.remove_star_2 = remove_star_2;
	this.full_star_2 = full_star_2;
	this.reset_star_2 = reset_star_2;
	this.set_star_2 = set_star_2;
	this.draw_star_2 = draw_star_2;
	this.show_star_2 = show_star_2;
	this.setMessage_2 = setMessage_2;
}

function getXmlHttpRequest()
{
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try	{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
		}
	}
	if (!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
	}
	return xmlHttp;
}

// call a url
function getUrl(url, async, handleStateChange) {
	var xmlHttpReq = getXmlHttpRequest();

	if (!xmlHttpReq)
		return;


	if (handleStateChange)
	{
		xmlHttpReq.onreadystatechange = function()
			{
				handleStateChange(xmlHttpReq);
			};
	}
	else
	{
		xmlHttpReq.onreadystatechange = function() {;}
	}

	xmlHttpReq.open("GET", url, async);
	xmlHttpReq.send(null);
}
function mmvc_point_update(point){
	if (document.getElementById('mmclipoint'))
	{
		var p = (document.getElementById('mmclipoint').innerHTML)*1+point;
		document.getElementById('mmclipoint').innerHTML = p;
	}	
}
//end created by Hoang Anh Quy



