﻿/// <reference path="Lib/Lib.js" />

window.onerror = function()
{
	return true;
};

function logout(hpl)
{
	Window.confirm
	(
		Language.Logout_Confirm,
		function() { exec(decodeURI(get(hpl).href)); }
	);
}

var PageExAjax =
{
	reCaptcha: function(token, img)
	{
		var value = WEB_Ajax.ReCaptcha(token).value;
		if (value) get(img).src = value;
	},

	openResource: function()
	{
		if (!window.resourceWindow)
		{
			var w = (window.innerWidth || window.screen.availWidth) - 100 + "px";
			var h = (window.innerHeight || (window.screen.availHeight - 150)) - 100 + "px";
		
			var content = create
			(
				[
					["DIV", { ID: "CONTENT" }, [
						["IFRAME", { frameBorder: 0, src: "VNVN_Web/Resource.aspx", style: { width: w, height: h}}]
					]]
				],
				document.body
			).CONTENT;

			window.resourceWindow = new Window
			({
				content: content,
				useMask: true,
				background: "#000",
				opacity: 0.4,
				usingForm: true,
				centerScreen: true
			});
		}
		if (window.resourceWindow)
			window.resourceWindow.open();
	}
};

/* ============================== Module_NewsModule_Other_ContactForm ============================== */
var Module_NewsModule_Other_ContactForm =
{
	count: function(txt, max)
	{
		get("Module_NewsModule_Other_ContactForm_Count").value = max - txt.value.length;
	}
}

/* ============================= Module_NewsModule_Other_Search_Google ============================= */
function Module_NewsModule_Other_Search_Google(s_Domain)
{
	var s_Search = get("Google_SiteSearch_Content").value;
	window.open('http://www.google.com/search?ie=utf-8&oe=utf-8&vnType=0&domains=' + s_Domain + '&sitesearch' + '=' + s_Domain + '&q=' + s_Search, '_blank');
	return false;
}

/* ============================= Module_NewsModule_News_UC_PrintBox ============================= */
var Module_NewsModule_News_UC_PrintBox =
{
	print: function()
	{
		window.open("VNVN_Web/Print.htm", "_blank");
	},
	
	_print: function(obj)
	{
		var c_1 = get("Article_Detail_Content_1");
		var c_2 = get("Article_Detail_Content_2");
		obj.innerHTML = (c_1 ? c_1.innerHTML : "") + (c_2 ? c_2.innerHTML : "");
	},
	
	Undelete: function()
	{
		Window.alert(Language.Delete_Unsuccessful);
	},

	email: function()
	{
		if (!this.sendMailWin)
		{
			this.sendMailWin = new Window
			({
				content: get("pnl_Module_NewsModule_News_UC_SendToFriend"),
				useMask: true,
				background: "#000",
				opacity: 0.4,
				usingForm: true,
				centerScreen: true
			});
		}
		if (this.sendMailWin)
			this.sendMailWin.open();
	},

	close: function()
	{
		if (this.sendMailWin)
			this.sendMailWin.close();
		return false;
	},

	back: function()
	{
		history.back();
	},
	
	count: function(txt, max)
	{
		get("Module_NewsModule_News_UC_SendToFriend_Count").value = max - txt.value.length;
	}
}

/* ============================= Module_NewsModule_Box_Parent_News_Box_1 (TabBox) ============================= */
var Module_NewsModule_Box_Parent_News_Box_1_TabBox = new Class();
Module_NewsModule_Box_Parent_News_Box_1_TabBox.prototype =
{
	content: null,
	curTab: null,
	tab: null,

	init: function()
	{
		var active = 0;
		this.content = get(this.content);
		for (var i = 0; i < this.tab.length; i++)
		{
			var but = get(this.tab[i][0]);
			if (but != null)
			{
				but.content = get(this.tab[i][1]);
				if (Element.text(but.content) == "")
				{
					Element.hide(but);
					if (i == active) active++;
				}
				else
					but.onmousedown = this.changeTab.handle(this, but);
			}
		}
		if (this.tab.length > 0)
			this.changeTab(get(this.tab[active][0]), this.tab[active][1]);
	},

	changeTab: function(but)
	{
		if (but != this.curTab)
		{
			if (this.curTab)
			{
				this.curTab.className = "TabBox_Title";
				Element.hide(this.curTab.content);
			}

			but.className = "TabBox_Title_Select";
			if (but.content.parentNode != this.content)
			{
				but.content.parentNode.removeChild(but.content);
				this.content.appendChild(but.content);
			}
			Element.show(but.content);
			this.curTab = but;
		}
	}
}

/* ============================= Module_NewsModule_User_Login ============================= */
var Module_NewsModule_User_Login =
{
	redirect: function(url)
	{
		window.setTimeout(function(url)
		{
			window.location.href = url;
		}.handle(null, url), 2500);
	}
}

/* ============================= Module_NewsModule_News_MediaList ============================= */
var Module_NewsModule_News_MediaList =
{
	hiddenSubmit: function(id, hdf_ID, lbt_Submit)
	{
		get(hdf_ID).value = id;
		exec(decodeURI(get(lbt_Submit).href));
	}
}

/* ============================= Module_NewsModule_Advertise_UC_RandomAdvertise ============================= */
var Module_NewsModule_Advertise_UC_RandomAdvertise =
{
	random: function(div, delay)
	{
		if (window.__isLoaded)
		{
			div = get(div);
			if (div)
			{
				var arr = [];
				for (var i = 0; i < div.childNodes.length; i++)
				{
					var tag = div.childNodes[i];
					if (tag.tagName == "DIV")
						arr.push(tag);
				}
				if (arr.length > 1)
					window.setTimeout(this._random.handle(this, arr, 0, delay), delay);
			}
		}
		else
			loadQueue.push(this.random.handle(this, div, delay));
	},

	_random: function(arr, index, delay)
	{
		Element.hide(arr[index++]);
		if (index > arr.length - 1) index = 0;
		Element.show(arr[index]);
		window.setTimeout(this._random.handle(this, arr, index, delay), delay);
	},

	marquee: function(div, style)
	{
		if (window.__isLoaded)
		{
			div = get(div);
			if (div)
			{
				div.scrollLeft = 0;
				var html = div.innerHTML;
				switch (style)
				{
					case 11002: //ngang
						div.innerHTML = "";
						var width = div.offsetWidth;
						div.style.width = width + "px";
						div.style.whiteSpace = "nowrap";
						div.style.overflow = "hidden";

						var div_1 = document.createElement("SPAN");
						var div_2 = document.createElement("SPAN");
						var div_3 = document.createElement("SPAN");
						div_1.innerHTML = div_2.innerHTML = div_3.innerHTML = html;
						div.appendChild(div_1);
						div.appendChild(div_2);
						div.appendChild(div_3);

						div.onmouseover = function(div)
						{
							if (div.timer) window.clearTimeout(div.timer);
						} .handle(this, div);

						div.onmouseout = function(div)
						{
							div.timer = window.setTimeout(div.func, 100);
						} .handle(this, div);
						
						div.func = this._marquee.handle(this, div, style, div_1, div_2, div_3, div_1.offsetWidth)
						div.timer = window.setTimeout(div.func, 100);
						break;
					case 11004: //doc
						div.innerHTML = "";
						div.style.height = "150px";
						div.style.overflow = "hidden";

						var div_1 = document.createElement("DIV");
						var div_2 = document.createElement("DIV");
						var div_3 = document.createElement("DIV");
						div_1.innerHTML = div_2.innerHTML = div_3.innerHTML = html;
						div.appendChild(div_1);
						div.appendChild(div_2);
						div.appendChild(div_3);

						div.onmouseover = function(div)
						{
							if (div.timer) window.clearTimeout(div.timer);
						} .handle(this, div);

						div.onmouseout = function(div)
						{
							div.timer = window.setTimeout(div.func, 100);
						} .handle(this, div);

						div.func = this._marquee.handle(this, div, style, div_1, div_2, div_3, div_1.offsetHeight)
						div.timer = window.setTimeout(div.func, 100);
						break;
						break;
				}
			}
		}
		else
			loadQueue.push(this.marquee.handle(this, div, style));
	},

	_marquee: function(div, style, div_1, div_2, div_3, w)
	{
		switch (style)
		{
			case 11002:
				var newScroll = div.scrollLeft + 5;
				div.scrollLeft = newScroll;
				if (div.scrollLeft < newScroll)
				{
					div.removeChild(div_1);
					div.scrollLeft = div.scrollLeft - w;
					div.appendChild(div_1);
					var tmp = div_1;
					div_1 = div_2;
					div_2 = div_3;
					div_3 = tmp;
				}

				div.timer = window.setTimeout(div.func, 100);
				break;
			case 11004:
				var newScroll = div.scrollTop + 5;
				div.scrollTop = newScroll;
				if (div.scrollTop < newScroll)
				{
					div.removeChild(div_1);
					div.scrollTop = div.scrollTop - w;
					div.appendChild(div_1);
					var tmp = div_1;
					div_1 = div_2;
					div_2 = div_3;
					div_3 = tmp;
				}

				div.timer = window.setTimeout(div.func, 100);
				break;
		}
	}
}

/* ============================= Module_NewsModule_Box_HotPicture ============================= */
var Module_NewsModule_Box_HotPicture =
{
	slide: function(div)
	{
		if (window.__isLoaded)
		{
			div = get(div);
			if (div)
			{
				var arr = [];
				for (var i = 0; i < div.childNodes.length; i++)
				{
					var tag = div.childNodes[i];
					if (tag.tagName == "IMG")
						arr.push(tag);
				}
				if (arr.length > 1)
				{
					Element.show(arr[0]);
					window.setTimeout(this._slide.handle(this, arr, 0), 3000);
				}
			}
		}
		else
			loadQueue.push(this.slide.handle(this, div));
	},

	_slide: function(arr, index)
	{
		Element.hide(arr[index++]);
		if (index > arr.length - 1) index = 0;
		Element.show(arr[index]);
		window.setTimeout(this._slide.handle(this, arr, index), 3000);
	}
}
/* ============================= Module_NewsModule_News_UC_NewsDetail ============================= */
var Module_NewsModule_News_UC_NewsDetail =
{
	del: function(btn)
	{
		Window.confirm
		(
			Language.Detail_News_Edit_DeleteConfirm,
			function() { exec(decodeURI(get(btn).href)); }
		);
	},
	
	onchange: function(ctrl)
	{
		var bookID = ctrl.value;
		if (bookID != "")
			get("WEB_Value_TreeBookID").parentNode.innerHTML = WEB_Ajax.GetTreeBookID(bookID, 0).value;
		else
			get("WEB_Value_TreeBookID").innerHTML = "";
	}
}

/* ============================= Module_NewsModule_Other_CommentList ============================= */
var Module_NewsModule_Other_CommentList =
{
	edit: function(id, btn, hdf)
	{
		get(hdf).value = id;
		exec(decodeURI(get(btn).href));
	},

	del: function(id, btn, hdf)
	{
		Window.confirm
		(
			Language.Comment_Form_DeleteConfirm,
			function()
			{
				get(hdf).value = id;
				exec(decodeURI(get(btn).href));
			}
		);
	}
}

/* ============================= Module_NewsModule_Other_CommentForm ============================= */
var Module_NewsModule_Other_CommentForm = 
{
	del: function(btn)
	{
		Window.confirm
		(
			Language.Comment_Form_DeleteConfirm,
			function() { exec(decodeURI(get(btn).href)); }
		);
	}
}

/* ============================= Module_NewsModule_UC_Rating ============================= */
var Module_NewsModule_UC_Rating = new Class();
Module_NewsModule_UC_Rating.prototype =
{
	container: null,
	control: null,

	init: function()
	{
		var con =
		[
			["DIV", { ID: "STAR", className: "Rating_Star_Container", onmouseout: this.restoreRating.handle(this) }, [
				["DIV", { className: "Rating_Star_1", onmouseover: this.changeRating.handle(this, 1, false), onmousedown: this.changeRating.handle(this, 1, true)}],
				["DIV", { className: "Rating_Star_2", onmouseover: this.changeRating.handle(this, 2, false), onmousedown: this.changeRating.handle(this, 2, true)}],
				["DIV", { className: "Rating_Star_3", onmouseover: this.changeRating.handle(this, 3, false), onmousedown: this.changeRating.handle(this, 3, true)}],
				["DIV", { className: "Rating_Star_4", onmouseover: this.changeRating.handle(this, 4, false), onmousedown: this.changeRating.handle(this, 4, true)}],
				["DIV", { className: "Rating_Star_5", onmouseover: this.changeRating.handle(this, 5, false), onmousedown: this.changeRating.handle(this, 5, true)}],
				["DIV", { className: "Rating_Star_6", onmouseover: this.changeRating.handle(this, 6, false), onmousedown: this.changeRating.handle(this, 6, true)}],
				["DIV", { className: "Rating_Star_7", onmouseover: this.changeRating.handle(this, 7, false), onmousedown: this.changeRating.handle(this, 7, true)}],
				["DIV", { className: "Rating_Star_8", onmouseover: this.changeRating.handle(this, 8, false), onmousedown: this.changeRating.handle(this, 8, true)}],
				["DIV", { className: "Rating_Star_9", onmouseover: this.changeRating.handle(this, 9, false), onmousedown: this.changeRating.handle(this, 9, true)}],
				["DIV", { className: "Rating_Star_10", onmouseover: this.changeRating.handle(this, 10, false), onmousedown: this.changeRating.handle(this, 10, true)}]
			]]
		]
		this.starRate = create(con, this.container).STAR;
		this.restoreRating();
	},

	restoreRating: function()
	{
		var rate = 0;
		try
		{
			rate = parseInt(get(this.control).value);
		} catch (err) { }
		this.changeRating(rate, true);
	},

	changeRating: function(rate, change)
	{
		this.starRate.className = "Rating_Star_Container Rating_Star_Rate_" + rate;
		if (change) get(this.control).value = rate;
	}
}


