﻿Gui_Class = function() { };
////////////////////////////////////
Gui_Class.prototype = {

    //LOAD HOME LAYOUT/////////////////////////////////////////////////////////////
    LoadHomeLayout: function() {
        $("#C-Container").load("./Template/Gui/HomeLayout.htm", function() {
            document.title = "caigichothue.com";
            $("#MainKeyword").focus();
            $("#MainKeyword").select();

            //Load Visitor
            System.Select($("#C-Bottom-Visitor"));

            //Load Top Menu
            if (GlobalVariable.YourUserEmail != "") {
                Gui.LoadUserTopMenu();
            }

            // Load MainItem in Home Menu
            MainItem.HomeMenu();

            // Load MainItem in Home Layout
            MainItem.NavigatorHome();

            // Load AdvLogo in Home Layout
            Gui.LoadAdvLogo();

        });
    },

    //LOAD USER LAYOUT/////////////////////////////////////////////////////////////
    LoadUserLayout: function(type, callBack) {
        $("#C-Container").load("./Template/Gui/UserLayout.htm", function() {

            //Load Visitor
            System.Select($("#C-Bottom-Visitor"));

            //Load Top Menu
            if (GlobalVariable.YourUserEmail != "") {
                Gui.LoadUserTopMenu();
            }

            //Load MainItem in User Layout
            MainItem.NavigatorUser();

            // Load AdvLogo in Home Layout
            Gui.LoadAdvLogo();

            ////////////////////////
            //Call callBack function (Gui.LoadMenuLayout)
            if (typeof callBack == "function") {
                callBack.call(this, type);
            }

        });
    },

    //LOAD ADVLOGO LAYOUT/////////////////////////////////////////////////////////////
    LoadAdvLogo: function() {
        $("#AdvLogo").load("./Template/Gui/AdvLogo.htm", function() {
            // Add Scroller Object
            $jScroller.add("#AdvLogo-Container", "#AdvLogo", "right", 3, true);
            // Start Autoscroller
            $jScroller.start();
        });
    },

    //LOAD MENU LAYOUT/////////////////////////////////////////////////////////////
    LoadMenuLayout: function(type) {
        Gui.LoadRightMenu(); //Load Right Menu

        ////////////////////////
        // type=0 ==> Load LeftMenu and call Gui.SearchTemplate
        // type=1 ==> Load LeftMenu and call Gui.InsertTemplate
        // type=2 ==> Load LeftMenu and empty GlobalVariable.MainKeyword and call Gui.SearchTemplate
        // type=3 ==> Load LeftMenu and empty GlobalVariable.MainKeyword and call Gui.ManageTemplate
        // type=default (type is Function) ==> Load LeftMenu and call Function (use in Ajax.RouteQueryString)

        switch (type) {
            case 0: Gui.LoadLeftMenu("Gui.SearchTemplate(1);"); return false; break;
            case 1: Gui.LoadLeftMenu("Gui.InsertTemplate();"); return false; break;
            case 2: GlobalVariable.MainKeyword = ""; Gui.LoadLeftMenu("Gui.SearchTemplate(1);"); return false; break;
            case 3: GlobalVariable.MainKeyword = ""; Gui.LoadLeftMenu("Gui.ManageTemplate(1);"); return false; break;
            default: Gui.LoadLeftMenu(type); return false; break;
        }

    },

    //LOAD RIGHT MENU
    LoadRightMenu: function() {
        GlobalVariable.Tmp = "0";
        MainItem.RightMenu();        
    },

    //LOAD LEFT MENU
    LoadLeftMenu: function(callBack) {
        $("#C-Main-Left").load("./Template/" + GlobalVariable.MainItem + "/LeftMenuTemplate.htm", function() {

            //Set element base on GlobalVariable
            if (GlobalVariable.SearchType == "0") {
                $("#ChoThue").attr("checked", true);
                $("#CanThue").attr("checked", true);
            }
            else {
                if (GlobalVariable.SearchType == "chothue") {
                    $("#ChoThue").attr("checked", true);
                    $("#CanThue").attr("checked", false);
                }
                if (GlobalVariable.SearchType == "canthue") {
                    $("#ChoThue").attr("checked", false);
                    $("#CanThue").attr("checked", true);
                }
            }

            $("#MainKeyword").val(GlobalVariable.MainKeyword);
            $("#MainKeyword").focus();

            //Load SubItem after that callBack (Gui.InsertTemplate or Gui.SearchTemplate or Gui.ManageTemplate)
            SubItem.ListAllByMainItemId($("#SubItem"), GlobalVariable.MainItem, GlobalVariable.SubItem, callBack);

            //Load TopTemplate below
            eval(GlobalVariable.MainItem + ".TopTemplate();");

            //Load TodayTemplate below
            eval(GlobalVariable.MainItem + ".TodayTemplate();");

            //Load LeftMenuAdv below
            $("#LeftMenuAdv").load("./Template/" + GlobalVariable.MainItem + "/LeftMenuAdvTemplate.htm");

        });
    },

    //RELOAD Filter in LeftMenu
    ReloadLeftMenuFilter: function(subItemId) {
        GlobalVariable.Tmp = "0";
        eval(GlobalVariable.MainItem + ".ReloadLeftMenuFilter('" + subItemId + "');");
    },

    //LOAD TOP MENU
    LoadUserTopMenu: function() {
        $("#C-Top1-Username").html("<b>" + GlobalVariable.YourUserEmail + "</b>&nbsp;&nbsp;|&nbsp;<a href='javascipt:void(0);' onclick='User.SelectByUserIdDialog();return false'>Thông tin</a>&nbsp;&nbsp;|&nbsp;<a href='javascipt:void(0);' onclick='Gui.LoadUserLayout(3,Gui.LoadMenuLayout);return false'>Danh sách tin</a>&nbsp;&nbsp;|&nbsp;<a href='#' onclick='User.Logout();'>Thoát</a>");

        if ($("#UserMenu").length > 0) {
            $("#UserMenu").css("display", "inline");
            MainItem.RightMenuCountByUserId();
        }

    },

    //LOAD INSERT base on GlobalVariable.MainItem
    InsertTemplate: function() {
        if (GlobalVariable.YourUserEmail == "") {
            alert("Bạn phải đăng nhập trước khi Đăng Tin");
        }
        else {
            eval(GlobalVariable.MainItem + ".InsertTemplate();");
        }
    },

    //SEARCH ITEMS
    SearchTemplate: function(pageIndex) {
        $("#Caption").html("&nbsp;&nbsp;&nbsp;Thể loại: <b>" + GlobalVariable.MainItemName + "</b>&nbsp;&nbsp;&nbsp;Chuyên mục: <b>" + $("#SubItem option:selected").text() + "</b>");
        title = "Thể loại: " + GlobalVariable.MainItemName + " - Chuyên mục: " + $("#SubItem option:selected").text();
        document.title = title;
        document.getElementsByName('description')[0].setAttribute('content', title);
        document.getElementsByName('keywords')[0].setAttribute('content', title);
        //////////////////
        eval(GlobalVariable.MainItem + ".SearchTemplate(" + GlobalVariable.PageSize + "," + pageIndex + ")");
    },

    //MANAGE ITEMS
    ManageTemplate: function(pageIndex) {
        $("#Caption").html("&nbsp;&nbsp;&nbsp;Thể loại: <b>" + GlobalVariable.MainItemName + "</b>&nbsp;&nbsp;&nbsp;Chuyên mục: <b>" + $("#SubItem option:selected").text() + "</b>");
        title = "Thể loại: " + GlobalVariable.MainItemName + " - Chuyên mục: " + $("#SubItem option:selected").text();
        document.title = title;
        document.getElementsByName('description')[0].setAttribute('content', title);
        document.getElementsByName('keywords')[0].setAttribute('content', title);
        //////////////////
        eval(GlobalVariable.MainItem + ".ManageTemplate(" + GlobalVariable.PageSize + "," + pageIndex + ")");
    },

    //VIEW ITEM
    ViewByMaSo: function(maSo, tieuDe) {
        $("#Caption").html("&nbsp;&nbsp;&nbsp;Thể loại: <b>" + GlobalVariable.MainItemName + "</b>&nbsp;&nbsp;&nbsp;Chuyên mục: <b>" + $("#SubItem option:selected").text() + "</b>");
        //////////////////  
        eval(GlobalVariable.MainItem + ".ViewByMaSo('" + maSo + "','" + tieuDe + "')");
    },

    //UPLOAD Picture
    UploadPictureAll: function() {
        $("a.UploadPicture").each(function() {
            Gui.UploadPicture(this.id);
        });
    },

    UploadPicture: function(elementId) {
        // Prepare for Upload
        //var button = $("#" + elementId), interval;
        var fileName = $("#" + elementId).attr("id");

        new AjaxUpload($("#" + elementId), {
            action: '../../UploadHandler.ashx',
            onSubmit: function(file, ext) {
                this.setData({ 'FileName': fileName, 'FolderName': GlobalVariable.MainItem });
                // change button text, when user selects file
                $("#" + elementId).text('Đang tải...');
                // If you want to allow uploading only 1 file at time,
                // you can disable upload button
                this.disable();
            },
            onComplete: function(file, response) {
                if (response == "True") {
                    eval(GlobalVariable.MainItem + ".UpdatePicByMaSo('" + fileName.substring(0, fileName.length - 2) + "','" + fileName.substring(fileName.length - 1) + "');");
                    alert("Tải ảnh thành công!");
                }
                else
                    alert("Tải ảnh không thành công (chú ý: file < 1MB)");
                $("#" + elementId).text($("#" + elementId).attr("name"));
                // Although plugins emulates hover effect automatically,
                // it doens't work when button is disabled
                //button.removeClass('hover');
                //window.clearInterval(interval);
                // enable upload button
                this.enable();
            }
        });

    },

    //LOAD USER INFO TO CONTACT FIELD
    LoadUserInfo: function() {
        if ($("#LoadUserInfo:checked").val() == "on") {
            $("#LienHeDiaChi").val(GlobalVariable.YourUserAddress);
            $("#LienHeDienThoai").val(GlobalVariable.YourUserPhone);
            $("#LienHeEmail").val(GlobalVariable.YourUserEmail);
            $("#LienHeHoTen").val(GlobalVariable.YourUserFullName)
        }
        else {
            $("#LienHeDiaChi").val("");
            $("#LienHeDienThoai").val("");
            $("#LienHeEmail").val("");
            $("#LienHeHoTen").val("");
        }
    },

    ///////////////////////////////

    KeywordHomePressEnter: function(event) {
        if (event.keyCode == '13') {
            GlobalVariable.MainKeyword = jQuery.trim($('#MainKeyword').val());
            Gui.LoadUserLayout(0, Gui.LoadMenuLayout);
        }
    },
    KeywordHomeClick: function() {
        GlobalVariable.MainKeyword = jQuery.trim($('#MainKeyword').val());
        Gui.LoadUserLayout(0, Gui.LoadMenuLayout);
    },
    KeywordUserPressEnter: function(event) {
        if (event.keyCode == '13') {
            Gui.SearchTemplate(1);
        }
    },



    ////////////STATIC PAGE/////////////////////////////////
    LoadWebIntroduce: function() {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("9", "0", "0", "0", "0", "0", "0");


        if ($("#C-Content").length) {
            $("#C-Content").load("./Template/Gui/WebIntroduce.htm");
        }
        else {
            $("#C-Container").load("./Template/Gui/HomeLayout.htm", function() {
                $("#C-Content").load("./Template/Gui/WebIntroduce.htm");

                //Load Visitor
                System.Select($("#C-Bottom-Visitor"));

                //Load Top Menu
                if (GlobalVariable.YourUserEmail != "") {
                    Gui.LoadUserTopMenu();
                }

                // Load MainItem in Home Layout
                MainItem.NavigatorHome();
            });
        }
    },

    LoadWebContact: function() {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("8", "0", "0", "0", "0", "0", "0");


        if ($("#C-Content").length) {
            $("#C-Content").load("./Template/Gui/WebContact.htm", function() {
                //Auto load User Infomation when login
                if (GlobalVariable.YourUserEmail != "") {
                    $("#LienHeHoTen").val(GlobalVariable.YourUserFullName);
                    $("#LienHeDiaChi").val(GlobalVariable.YourUserAddress);
                    $("#LienHeDienThoai").val(GlobalVariable.YourUserPhone + "   " + GlobalVariable.YourUserMap);
                    $("#LienHeEmail").val(GlobalVariable.YourUserEmail);
                }
            });
        }
        else {
            $("#C-Container").load("./Template/Gui/HomeLayout.htm", function() {
                $("#C-Content").load("./Template/Gui/WebContact.htm", function() {
                    //Auto load User Infomation when login
                    if (GlobalVariable.YourUserEmail != "") {
                        $("#LienHeHoTen").val(GlobalVariable.YourUserFullName);
                        $("#LienHeDiaChi").val(GlobalVariable.YourUserAddress);
                        $("#LienHeDienThoai").val(GlobalVariable.YourUserPhone + "   " + GlobalVariable.YourUserMap);
                        $("#LienHeEmail").val(GlobalVariable.YourUserEmail);
                    }
                });

                //Load Visitor
                System.Select($("#C-Bottom-Visitor"));

                //Load Top Menu
                if (GlobalVariable.YourUserEmail != "") {
                    Gui.LoadUserTopMenu();
                }

                // Load MainItem in Home Layout
                MainItem.NavigatorHome();
            });
        }
    },

    LoadWebFee: function() {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("7", "0", "0", "0", "0", "0", "0");


        if ($("#C-Content").length) {
            $("#C-Content").load("./Template/Gui/WebFee.htm");
        }
        else {
            $("#C-Container").load("./Template/Gui/HomeLayout.htm", function() {
                $("#C-Content").load("./Template/Gui/WebFee.htm");

                //Load Visitor
                System.Select($("#C-Bottom-Visitor"));

                //Load Top Menu
                if (GlobalVariable.YourUserEmail != "") {
                    Gui.LoadUserTopMenu();
                }

                // Load MainItem in Home Layout
                MainItem.NavigatorHome();
            });
        }
    },

    LoadWebInstruction: function() {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("6", "0", "0", "0", "0", "0", "0");


        if ($("#C-Content").length) {
            $("#C-Content").load("./Template/Gui/WebInstruction.htm");
        }
        else {
            $("#C-Container").load("./Template/Gui/HomeLayout.htm", function() {
                $("#C-Content").load("./Template/Gui/WebInstruction.htm");

                //Load Visitor
                System.Select($("#C-Bottom-Visitor"));

                //Load Top Menu
                if (GlobalVariable.YourUserEmail != "") {
                    Gui.LoadUserTopMenu();
                }

                // Load MainItem in Home Layout
                MainItem.NavigatorHome();
            });
        }
    }



};

var Gui = new Gui_Class();