﻿/// <reference path="Lib/Lib.js" />

window.onerror = function()
{
	return true;
};

function logout(hpl)
{
	Window.confirm
	(
		Language.Logout_Confirm,
		function() { exec(decodeURI(get(hpl).href)); }
	);
}

function __openNewWin(obj)
{
	var href = $(obj).attr("olink");
	if (href != null && href != "") window.open(href, "_blank");
}

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();
	},
	
	openAMResource: 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_Admin/Server.aspx?Action=resource&s=1", 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)
	{
		var c = txt.value.length;
		if (c <= max)
			get("Module_NewsModule_Other_ContactForm_Count").value = max - txt.value.length;
		else
		{
			txt.value = txt.value.substr(0, 400);
			return false;
		}
		return true;	
	}
}

/* ============================= 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 : "");
	},
	
	printpdf: function(id, catID)
	{
		window.open("VNVN_Web/PdfPrint.aspx?ArticleID=" + id + "&CategoryID=" + catID, "_blank");
	},
	
	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)
	{
		var c = txt.value.length;
		if (c <= max)
			get("Module_NewsModule_News_UC_SendToFriend_Count").value = max - txt.value.length;
		else
		{
			txt.value = txt.value.substr(0, 400);
			return false;
		}
		return true;
	}
}

/* ============================= 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)
	{
		$(function()
		{
			div = $("#" + div);
			var length = div.children().length,
				n = 0;
			window.setInterval(function()
			{
				div.children(":eq(" + n++ + ")").hide();
				if (n >= length) n = 0;
				div.height(div.children(":eq(" + n + ")").show().height());
			}, delay);			
		});
	},

	marquee: function(div, style)
	{
		$(function()
		{
			div = $("#" + div).scrollLeft(0);
			switch (style)
			{
				case 11002: //ngang
					
					var maxHeight = 0,
						maxWidth = 0;
					div.find(">div").each(function()
					{
						var h = $(this).height();
						if (h > maxHeight) maxHeight = h;
						maxWidth += $(this).width();
					}).wrap("<td></td>");

					var html = div.html(),
						n = div.width() / maxWidth + 2;

					while (n-- > 0)
					{
						html += html;
					}
					var tr = div.height(maxHeight).html("").width(div.width()).css({"white-space": "nowrap", overflow: "hidden"})
												  .html("<table cellpading='0' cellspacing='0'><tr>" + html + "</tr></table>")
												  .mouseover(function()
												  {
													  timer = false;
												  })
												  .mouseout(function()
												  {
													  timer = true;
												  }).find(">table>tbody>tr");
					var timer = true,
						firstWidth = tr.children(":first").width();
					window.setInterval(function()
					{
						if (timer)
						{
							var n1 = div.scrollLeft();
							if (n1 > firstWidth)
							{
								tr.children(":first").remove().appendTo(tr);
								firstWidth = tr.children(":first").width();
								div.scrollLeft(n1 - firstWidth - 1);
							}
							else
							{
								div.scrollLeft(n1 + 1);
							}
						}
					}, 100);
					break;
				case 11004: //doc
					if (div.children().length > 1)
					{
						var height = div.children("div:first").height() + div.children("div:nth-child(2)").height();
						div.height(height).css("overflow", "hidden").html(div.html() + div.html());
						window.setInterval(function()
						{
							var firstChild = div.children("div:first"),
								height = firstChild.height(),
								newHeight = div.children("div:nth-child(2)").height() + div.children("div:nth-child(3)").height();

							div.animate({scrollTop: height, height: newHeight}, 500, "linear", function()
							{
								firstChild.remove();
								div.attr("scrollTop", 0).append(firstChild);
							});
						}, 5000);
					}
					break;
			}
		});
	}
}

/* ============================= 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)); }
		);
	},
	
	fontDecrease: function()
	{
		var size = (Module_NewsModule_News_UC_NewsDetail.getCookie("curFont") * 1) - 1;
		if (size <= -10) size = -10;
		Module_NewsModule_News_UC_NewsDetail.setCookie("curFont", size);
		Module_NewsModule_News_UC_NewsDetail.setFont();
	},
	
	fontIncrease: function()
	{
		var size = (Module_NewsModule_News_UC_NewsDetail.getCookie("curFont") * 1) + 1;
		Module_NewsModule_News_UC_NewsDetail.setCookie("curFont", size);
		Module_NewsModule_News_UC_NewsDetail.setFont();
	},
	
	setFont: function()
	{
		$(function()
		{
			var size = Module_NewsModule_News_UC_NewsDetail.getCookie("curFont");
			if ((size * 1) != 0)
			{
				var style = $("#FontStyleTag");
				if (style.length == 0)
					style = $("<div id='FontStyleTag'></div>");
				style.remove().html("<style>#Article_Detail_Content_2, #Article_Detail_Content_2 *{font-size:" + (100 + 5 * (size * 1)) + "% !important;}</style>").appendTo(document.body);
			}
			else
			{
				$("#FontStyleTag").remove();
			}
		});
	},
	
	setCookie: function(c_name, value)
	{
		document.cookie = c_name+ "=" + escape(value);
	},

	getCookie: function(c_name)
	{
		if (document.cookie.length > 0)
		{
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start != -1)
			{
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";", c_start);
				if (c_end==-1)
					c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	},
	
	showAjaxDetail: function(btn, id, div)
	{
		div = $("#" + div);
		if (div.data("load") != "1")
		{
			WEB_Ajax.GetArticleDetail(id, function(res)
			{
				if (res.value != null)
				{
					var divDetail = div.data("load", "1").children(":last");
					divDetail.show().width(divDetail.width()).hide().html(res.value);
					Module_NewsModule_News_UC_NewsDetail._showAjaxDetail(btn, div);
				}
			});
		}
		else
			Module_NewsModule_News_UC_NewsDetail._showAjaxDetail(btn, div)
	},
	
	_showAjaxDetail: function(btn, div)
	{
		if (this.__prevPanel != null && this.__prevPanel != div)
		{
			this.__prevPanel.children(":first").slideDown(600);
			this.__prevPanel.children(":last").slideUp(600);
			this.__prevBtn.removeClass().addClass("NewsList_News_ExpandDetail");
			this.__prevPanel = null;
			this.__prevBtn = null;
		}
		this.__prevPanel = div;
		this.__prevBtn = $(btn);
		var divDetail = div.children(":last");
		if (divDetail.css("display") == "none")
		{
			div.children(":first").slideUp(600);
			divDetail.slideDown(600, function()
			{
				$("html,body").animate({
					scrollTop: $(btn).offset().top - 10
				}, 600);
			});
			$(btn).removeClass().addClass("NewsList_News_CollapseDetail");
		}
		else
		{
			div.children(":first").slideDown(600);
			divDetail.slideUp(600);
			$(btn).removeClass().addClass("NewsList_News_ExpandDetail");
			this.__prevPanel = null;
			this.__prevBtn = null;
		}
	}
}

/* ============================= 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;
	}
}

var Module_NewsModule_Other_UC_Poll =
{
	checkPoll: function(name)
	{
		return $("input[name='" + name + "']:checked").length > 0;
	}
}

Module_NewsModule_News_UC_NewsDetail.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 = "";
						};

