﻿/// <reference path="mootools-1.2.4-core-yc.js" />
// BizSite 的基本JS方法
// 注：本类下方法与 BizSite 项目绑定，无法分拆。 通用方法写到 mootools-Extension （mootools的扩展类）中

function Flash(url, width, height) {
    if (width == undefined) width = "100%";
    if (height == undefined) height = "100%";
    var str = "";
    str = '<object id="MyFlash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + width + '" height="' + height + '">';
    str += '<param name="movie" value="' + url + '">';
    //str += '<param name="wmode" value="opaque">';  
    str += '<param name="wmode" value ="transparent">';
    str += '<param name="menu" value="false">';
    str += '<param name="quality" value="high">';
    str += '<embed src="' + url + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed>';
    str += '</object>';
    document.write(str);
}

// 格式 ad2|RegionID  或  pic|colID(為0表示素有)|count
function FlashAD2(params, width, height) {
    params += "|" + Math.random();
    var str = "";
    if(width == undefined) width = "100%";
    if(height == undefined) height ="100%";
    str = "<object class=\"flash\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\">" +
            "<param name=\"movie\" value=\"/public/ad2.swf\" />" +
            "<param name=\"quality\" value=\"high\" />" +
            "<param name=\"menu\" value=\"false\"/>" +
            "<param name=\"wmode\" value=\"opaque\">" +
            "<param name=\"FlashVars\" value=\"bcastr_xml_url=/Config/Flash.ashx?Type=" + params + "&IsShowBtn=1\"/>" +
            "</object>";
    document.write(str);
}
//百易Flash图片展播 未完成
function FlashAD3(param, width, height) {
    param += "|" + Math.random();
    var str = "";
    if (width == undefined) width = "625px";
    if (height == undefined) height = "210px";
    str = "<object class=\"flash\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\">" +
            "<param name=\"movie\" value=\"/Web/ts.BuyE/BuyShow.swf\" />" +
            "<param name=\"quality\" value=\"high\" />" +
            "<param name=\"menu\" value=\"false\"/>" +
            "<param name=\"wmode\" value=\"opaque\">" +
            "<param name=\"FlashVars\" value=\"topstory_xml=/Config/Flash.ashx?Type=" + param + "\"/>" +
            "</object>";
    document.write(str);
}


// 二级菜单
var Menu = {};

// isAuto :  自动定位
Menu.Bind = function (obj, isAuto) {
    obj.getElements("a[class^=son]").each(function (item) {
        item.addEvents({
            "mouseover": function () {
                var obj = this.getNext();
                if (obj.get("tag") == "ul") {
                    obj.set("styles", {
                        //"visibility": "visible",
                        "left": this.getLeft(),
                        "top": this.getTop() + this.getSize().y
                    });
                    this.store("show", true);
                    obj.fade("in");
                }
            },
            "mouseleave": function () {
                var obj = this.getNext();
                if (obj.get("tag") != "ul") return;
                this.store("on", false);
                (function () {
                    if (!item.retrieve("on")) {
                        // obj.setStyle("visibility", "hidden");
                        obj.fade("out");
                    }
                }).delay(100);
            }
        });
    });

    obj.getElements("ul").each(function (item) {
        var obj = item.getPrevious();
        if (obj.get("tag") == "a") {
            item.addEvent("mouseenter", function () {
                obj.store("on", true);
            });
            item.addEvent('mouseleave', function () {
                obj.store("on", false);
                // item.setStyle("visibility", "hidden");
                item.fade("out");
            });
        }
    });
};

// 根据栏目名更改链接地址
Menu.SetLink = function (container) {
    new Request({
        "url": "/BizSite/Ajax/Handler.ashx?ac=column",
        "onSuccess": function (response) {
            var list = eval(response);
            list.each(function (item) {
                var lnk = container.getElement("a[text=" + unescape(item.Name) + "]");
                if (lnk != null) lnk.set("href", unescape(item.Url));
            });
        }
    }).send();
}



/*  产品搜索的相关方法   */
var Search = {};

Search.Bind = function (cate, key, btn) {
    var url = location.href.toLowerCase();
    if (url.indexOf("/productsearch") != -1) {
        // 通过服务器端去拿cookie，解决中文乱码的问题
        new Request({
            url: "/Config/Cookie.ashx",
            method: "get",
            data: { ac: "read", name: "CateID,Key", r: Math.random() },
            onSuccess: function (response) {
                var obj = eval(response);
                key.set("value", unescape(obj.Key));
                cate.set("value", unescape(obj.CateID));
            }
        }).send();
    }
    btn.addEvent("click", function () {
        var cateID = cate.get("value");
        var query = escape("{CateID:" + cateID + ",Key:" + key.get("value") + "}");
        location.href = "/Config/Cookie.ashx?query=" + query + "&Url=/ProductSearch.aspx";
    });
    key.addEvent("keyup", function (e) {
        if (e.key == "enter") {
            btn.fireEvent("click");
        }
    });
};

// 投票对象
var Vote = {};

// 绑定所有的投票的按钮 注意：投票按钮需要包含btnVote的样式
Vote.Bind = function () {
    $$("input[class^=btnVote]").each(function (item) {
        item.addEvent("click", function () {
            var id = item.get("id").substring(1);
            item.set("disabled", "disabled");
            new Request({
                url: "/config/vote.ashx",
                method: "post",
                data: { "ArticleID": id },
                onSuccess: function (response) {
                    alert(response);
                    item.set("disabled", "");
                }
            }).send();
        });
    });
}

// 綁定投票按鈕
Vote.Submit = function (btn) {
    var btn = $(btn);
    var obj = btn.getParent("div.VoteBlock");
    var isNull = true;
    obj.getElements("input.item").each(function (item) {
        if (item.get("checked")) isNull = false;
    });
    if (isNull) {
        alert("请选择要投票的选项");
        return;
    }
    btn.set("disabled", "disabled");
    var form = btn.getParent("form");
    form.set("send", {
        "onSuccess": function (response) {
            alert("投票成功！");
            Vote.View();
            btn.set("disabled", "");
        }
    });
    form.send();
};

// 查看
Vote.View = function () {
    
};


// 图片相关方法
var Pic = {};

// 设置产品图片自动缩放到上一级容器内并且居中
Pic.Middle = function (obj) {
    obj = $(obj);
    var pic = obj.getParent(".pic");
    if (obj.getSize().x > pic.getSize().x) { obj.setStyle("width", pic.getSize().x - 5); }
    if (obj.getSize().y > pic.getSize().y) {
        obj.setStyles({
            "height": pic.getSize().y - 5,
            "width": "auto"
        });
    }
    obj.setStyle("margin-top", (pic.getSize().y - obj.getSize().y) / 2);
};


// 文章相關方法
var Article = {};

Article.IsSupport = false;

// 支持或者踩
Article.Support = function (obj, ac, id) {
    if (Article.IsSupport) {
        alert("您已经表过态了!");
        return;
    }
    obj = $(obj);
    var num = obj.getElement("span.num");
    new Request({
        "url": "/BizSite/Ajax/Handler.ashx?ac=support",
        "method": "post",
        "data": { ID: id, ac: ac },
        "onSuccess": function (response) {
            num.set("text", response);
            Article.IsSupport = true;
        }
    }).send();
}
