﻿$(document).ready(function() {
    $(".txtStyle").focus(function() { jQuery(this).attr("class", "txtCurrent"); });
    $(".txtStyle").blur(function() { jQuery(this).attr("class", "txtStyle"); });

    $(".all_category_more").hover(
      function() {
          $(this).removeClass("all_category_more").addClass("all_category_more_select");
          $("#all_category_Show").show();
      },
      function() {
          $(this).removeClass("all_category_more_select").addClass("all_category_more");
          $("#all_category_Show").hide();
      }
    );

    //加入邮件列表
    TextBox.init("txtSingUpEmail", "SIGN UP TO PANDAWHOLE NEWS");

    $("#btnSingUpEmail").click(function() {
        var _txtNewsLetterMail = $("#txtSingUpEmail");
        var _defalutInfo = "SIGN UP TO PANDAWHOLE NEWS";
        var email = $.trim(_txtNewsLetterMail.val());
        if (email == "" || email == _defalutInfo) {
            _txtNewsLetterMail.focus();
            return;
        } else if (!_isEmail(email)) {
            alert("Please provide a valid e-mail address.");
            _txtNewsLetterMail.focus();
            return;
        } else {
            $.ajax({
                type: "GET",
                url: "/Ajax/JoinMailList.ashx",
                data: { mail: email },
                dataType: "json",
                cache: false,
                timeout: 60000,
                success: function(json) {
                    if (json[0].ResultInfo == "Success") {
                        alert("You have signed up to pandawhole newsletter successfully!");
                        _txtNewsLetterMail.val("");
                        _txtNewsLetterMail.focus();
                        return true;
                    }
                    else {
                        alert("Failed to sign up.");
                        return false;
                    }
                },
                error: function(msg) {
                    alert("Time out!");
                    return false;
                }
            });
        }
    });

})

var TextBox = {
    init: function(id, defalutInfo) {
        var txtBox = $("#" + id);
        var color = txtBox.css("color");
        txtBox.val(defalutInfo);
        txtBox.css("color", "#999");
        txtBox.focus(function() {
            if ($.trim(this.value) == defalutInfo) {
                this.value = "";
                txtBox.css("color", color);
            }
        }).blur(function() {
            if ($.trim(this.value) == "") {
                this.value = defalutInfo;
                txtBox.css("color", "#999");
            }
        });
    }
};

function _isEmail(str) {
    var strReg = /^\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*/;
    return strReg.test(str);
}

function GlobalShowSubCategoryList(categoryId) {
    //$(".subcategoryListUL").css({ "display": "none" });         
    $("#subCategoryList" + categoryId).css({ "display": "block" });
}
function GlobalHiddenSubCategoryList(categoryId) {
    $("#subCategoryList" + categoryId).css({ "display": "none" });
}
function GlobalShowCategoryList() {

    $("#autoMenu").css({ "display": "block" });
    // $(".subcategoryListUL").css({ "display": "none" });         
}

function GlobalHiddenCategoryList() {
    $("#autoMenu").css({ "display": "none" });
}


//检查参数是否为正数
function IsPositiveNum(input) {
    var integer = "^[0-9]+(.[0-9]+)?$"; //正整数
    return (new RegExp(integer)).test(input);
}


/*---------DropDownList Start-------------*/
var DropDownList = {
    bind: function(selectBoxId) {
        var id = "ddl_" + selectBoxId;
        var textId = id + "_text";
        var panelId = id + "_panel";
        var clickBtnId = id + "_btn";
        var sourceSelectBox = document.getElementById(selectBoxId);

        var nHeight = sourceSelectBox.offsetHeight;
        var nWidht = sourceSelectBox.offsetWidth;
        var fontSize = sourceSelectBox.style.fontSize;
        if (fontSize != "") fontSize = "font-size:" + fontSize + ";";
        var html = "<div class='dropDownList' style='width: " + nWidht + "px;" + fontSize + "'>";
        html += "<div id='" + textId + "' class='dropDownList_text' style='width: " + (nWidht - 39) + "px;'></div>";
        html += "<div id='" + clickBtnId + "' class='dropDownList_btn'>&nbsp;</div>";
        html += "<div style='clear: both;'></div>";
        html += "<ul class='dropDownList_option' id='" + panelId + "'>";

        var childrenCount = sourceSelectBox.children.length;
        var optionIndex = 0;
        for (var i = 0; i < childrenCount; i++) {
            var item = sourceSelectBox.children[i];
            if (item.tagName == "OPTGROUP") {
                html += "<div class='dropDownList_optgroup' style='width:" + (nWidht - 4) + "px;'>" + item.label + "</div>";
            }
            else {
                html += "<li oIndex=\"" + optionIndex + "\">" + item.text + "</li>";
                optionIndex++;
            }
        }
        html += "</ul>";
        html += "</div>";
        //alert(html);

        var newSelectBox = document.createElement("SPAN");
        newSelectBox.innerHTML = html;
        newSelectBox.id = id;
        //sourceSelectBox.parentNode.appendChild(newSelectBox);
        sourceSelectBox.parentNode.insertBefore(newSelectBox, sourceSelectBox);
        sourceSelectBox.style.display = "none";

        DropDownList.selectedItem(panelId, sourceSelectBox.selectedIndex, textId);

        var optionPanel = $("#" + panelId);
        var w = optionPanel.width();
        if (w < nWidht) optionPanel.width(nWidht);
        else if (w > nWidht) optionPanel.width(w - 2);
        if (optionPanel.height() > 400) optionPanel.height(400);

        $("#" + clickBtnId).click(function() {
            var panelObj = $("#" + panelId);
            if (panelObj.css("display") != "none") {
                panelObj.hide();
            } else {
                DropDownList.selectedItem(panelId, sourceSelectBox.selectedIndex, textId);
                panelObj.show();
            }
        });

        $("#" + panelId + " li").hover(function() {
            $("#" + panelId + " li").removeClass("dropDownList_selected");
            $(this).addClass("dropDownList_selected");
        }, function() {
            $(this).removeClass("dropDownList_selected");
        });

        $("#" + panelId + " li").click(function() {
            var selectedOptionIndex = parseInt($(this).attr("oIndex"));
            $("#" + textId).html($(this).html());
            var sourceSelectBox = document.getElementById(selectBoxId);
            if (sourceSelectBox.selectedIndex != selectedOptionIndex) {
                sourceSelectBox.selectedIndex = selectedOptionIndex;
                if (sourceSelectBox.onchange) {
                    sourceSelectBox.onchange();
                }
            }
        });

        $(document).click(function(event) {
            if ($(event.target).attr("id") != clickBtnId) {
                $("#" + panelId).hide();
            }
        });
    },

    selectedItem: function(sourceId, selectedIndex, textId) {
        if (selectedIndex < 0) selectedIndex = 0;
        $("#" + sourceId + " li").removeClass("dropDownList_selected");
        var item = $("#" + sourceId + " li:eq(" + selectedIndex + ")");
        item.addClass("dropDownList_selected");
        $("#" + textId).html(item.html());
    },

    remove: function(selectBoxId) {
        var id = "ddl_" + selectBoxId;
        var node = document.getElementById(id);
        if (node) document.body.removeChild(node);
    }

};
/*---------DropDownList end-------------*/
