﻿/// <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) {
        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 = $(div);
                        var height = div.children("div:first").height() + div.children("div:nth-child(2)").height();

                        div.height(height);
                        div.css("overflow", "hidden");
                        div.html(div.html() + div.html());

                        div.func = this._marquee.handle(this, div, style);
                        div.timer = window.setTimeout(div.func, 5000);
                        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 + 1;
                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, 70);
                break;
            case 11004:
                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);
                    div.append(firstChild);
                    div.timer = window.setTimeout(div.func, 5000);
                });
                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)); }
		);
    },

    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.__prevPanel = null;
        }
        this.__prevPanel = div;
        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;
        }
    }
}

/* ============================= 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;
    }
}


var PageSize =
{
    FixHeight_Right: function(h) {
        if ($("#d_RightContent").height() <= h) {
            get("d_RightScroll").className = "Right_Center_2";
            get("d_Right").style.padding = "0px 15px 0px 0px";
        }
        else {
            get("d_Right").className = "Right_Center";
            get("d_Right").style.padding = "";
            get("d_Right").style.height = "448px";
            get("d_RightScroll").style.height = "448px";
            get("d_RightScroll").className = "ShowScrollbar";
            get("d_RightContent").style.padding = "0px 5px 0px 0px";

            if (Browser.IE6) {
                get("d_RightContent").style.width = "573px";
            }
            else if (Browser.IE7) {
                get("d_RightContent").style.width = "570px";
            }
        }
    },

    FixHeight_Left: function(h) {
        if ($("#d_LeftContent").height() <= h) {
            get("d_LeftScroll").className = "Left_Center_2";
            get("d_Left").style.padding = "0px 15px 0px 0px";
        }
        else {
            get("d_Left").className = "Left_Center";
            get("d_Left").style.padding = "";
            get("d_Left").style.height = "448px";
            get("d_LeftScroll").style.height = "448px";
            get("d_LeftScroll").className = "ShowScrollbar";
            get("d_LeftContent").style.padding = "0px 0px 0px 0px";

            if (Browser.IE6) {
                get("d_LeftContent").style.width = "193px";
            }
            else if (Browser.IE7) {
                get("d_LeftContent").style.width = "100%";
            }
        }
    },

    FixHeight_Right_2: function(h, dh) {
        if ($("#d_RightContent").height() + dh <= h) {
            get("d_RightScroll").className = "Right_Center_2";
            get("d_RightContent").style.padding = "0px 15px 0px 10px";
            get("d_Right").style.padding = "0px 15px 0px 0px";
        }
        else {
            get("d_Right").className = "Right_Center";
            get("d_Right").style.padding = "";
            get("d_Right").style.height = "448px";
            get("d_RightScroll").style.height = "448px";
            get("d_RightScroll").className = "ShowScrollbar";
            get("d_RightContent").style.padding = "0px 5px 0px 0px";

            if (Browser.IE6) {
                get("d_RightContent").style.width = "573px";
            }
            else if (Browser.IE7) {
                get("d_RightContent").style.width = "570px";
            }
        }
    }
}

var Module_NewsModule_Box_PictureSlide =
{
    CurrentLargeImage: 0,

    changeImage: function(largeImage, id) {
        var img = get("Module_NewsModule_Box_PictureSlide_Large_Image");
        if (this.CurrentLargeImage != id) {
            get("Module_NewsModule_Box_PictureSlide_Large_Image_Loading").style.display = "";
            img.src = largeImage;
            img.style.display = "none";
            this.CurrentLargeImage = id;
        }
    },

    thumonload: function(img, id) {
        $("#Thum_Image_" + id).hide();
        $(img).show();
    },

    onloadLargeImage: function(img) {
        $("#Module_NewsModule_Box_PictureSlide_Large_Image_Loading").hide();
        $(img).fadeIn(800);
    },

    first: true,
    move: false,
    x: 0,
    y: 0,

    largeImageStartMove: function(img, e) {
        Module_NewsModule_Box_PictureSlide.x = e.clientX;
        Module_NewsModule_Box_PictureSlide.y = e.clientY;
        if (Module_NewsModule_Box_PictureSlide.first) {
            $(document).mousemove(function(event) {
                if (Module_NewsModule_Box_PictureSlide.move) {
                    var newX = event.clientX,
						newY = event.clientY,
						pNode = img.parentNode;

                    pNode.scrollLeft -= newX - Module_NewsModule_Box_PictureSlide.x;
                    pNode.scrollTop -= newY - Module_NewsModule_Box_PictureSlide.y;

                    Module_NewsModule_Box_PictureSlide.x = newX;
                    Module_NewsModule_Box_PictureSlide.y = newY;
                    Event.cancel(event);
                }
            }).mouseup(function(event) {
                Module_NewsModule_Box_PictureSlide.move = false;
            })
        }
        Module_NewsModule_Box_PictureSlide.move = true;
        if (!e) e = window.event;
        Event.cancel(e);
    }
}
