﻿/*----------------------------------------------------------------
Copyright (C) 2009 Subcon China Corporation
File function:SubconChina popup js division
Create tag:Lyan 2009-4-13

Change tag:
Change description:
----------------------------------------------------------------*/
//设置弹出层默认配置
var scPopConfig = {};
scPopConfig = {
    opac: 0.50, //透明度
    bgcolor: '#999999', //背景色
    bzi: null, //z index
    it: null,
    il: null
};

(function($) {
    $.fn.OpenWin = function(options) {
        var twin = window.parent, cover;
        while (twin.parent && twin.parent != twin) {

            twin = twin.parent; //取得顶级窗口
        }
        var tdoc = twin.document; //取得顶级窗口文档

        var getzi = function() {//取得z-index
            if (!scPopConfig.bzi) {
                scPopConfig.bzi = 999;
            }
            return ++scPopConfig.bzi;
        };

        var resizehdl = function() {//resize 处理函数
            if (!cover) return;
            //根据DTD的不同模式使doc指向不同节点
            var rel = $.isDTD(tdoc) ? tdoc.documentElement : tdoc.body;
            $.setStyle(cover, {
                'width': Math.max(rel.scrollWidth, rel.clientWidth, tdoc.scrollWidth || 0) - 1 + 'px',
                'height': Math.max(rel.scrollHeight, rel.clientHeight, tdoc.scrollHeight || 0) - 1 + 'px'
            });
        };
        var oldScrollTop = 0;
        var oldMargin = 0;
        return {
            //t ：窗口标题，文本型数据
            //p ：内容页文件，文本型数据
            //w ：窗口的宽度，数值型数据
            //h ：窗口的高度，数值型数据
            //c ：是否显示遮罩层，布尔型数据，默认不遮罩
            //i ：设置加载内容页的形式，混合型数据。有3种形式，（true|false|'hcode'|'scroll'）。为true时内容页为外部链接，为false时为自己的内容页（此为默认值）。为'hcode'时为html代码。noscroll隐藏滚动条
            //n ：定义可编程窗口名称，不用传递参数的弹出层可以不填
            //s ：是否随窗口滚动条滚动，默认不滚动
            //l ：窗口左上角坐标参数，默认居中{ top: 100, left: 100 }
            Popup: function(t, p, w, h, c, i, n, s, l) {
                if (i !== "scroll") {
                    this.scrollHide();
                }
                if (c) {
                    this.DisplayFilter();
                } else {
                    if (cover) {
                        cover = null; //如果不显示遮罩层，有也关闭
                    }
                }
                var dinfo = { tit: t, page: p, win: window, topw: twin, link: i, sc: s };
                var clsize = $.getClientSize(twin), spos = $.getWindowPosiotion(twin);

                // 弹出层随滚动条滚动
                var schdl = function() {
                    var rel = $.getWindowPosiotion(twin);
                    $.setStyle(dfrm, { 'top': rel.Y + scPopConfig.it + 'px', 'left': rel.X + scPopConfig.il + 'px' });
                };

                var it = (l && l.top) ? spos.Y + l.top : Math.max(spos.Y + (clsize.h - h - 20) / 2, 0);
                var il = (l && l.left) ? spos.X + l.left : Math.max(spos.X + (clsize.w - w - 20) / 2, 0);

                var dfrm = tdoc.createElement('iframe');
                $.popStyle(dfrm);
                if (n) {
                    dfrm.id = n;
                }
                dfrm.frameBorder = 0;
                dfrm.src = '/PageLetters/PopupWin.htm';
                $.setStyle(dfrm, {
                    'position': 'absolute', 'top': it + 'px', 'left': il + 'px',
                    'width': w + 'px', 'height': h + 'px', 'zIndex': getzi()
                });

                // 判断是否随滚动条滚动
                if (s) { scPopConfig.it = it - spos.Y; scPopConfig.il = il - spos.X; };
                if (s) {
                    $.addEvent(twin, 'scroll', schdl);
                }

                tdoc.body.appendChild(dfrm);
                dfrm._dlgargs = dinfo;
            },
            ShowTip: function(p) {
                w = 300;
                h = 40;
                this.DisplayFilter("#fff", 0.01);
                var clsize = $.getClientSize(twin), spos = $.getWindowPosiotion(twin);
                var it = Math.max(spos.Y + (clsize.h - h - 20) / 2, 0);
                var il = Math.max(spos.X + (clsize.w - w - 20) / 2, 0);
                it = it / 2;

                var tipsId = "fix"; //Math.round(Math.random() * 100);
                var borderW = 6;
                var tips = document.createElement('div');
                $.setStyle(tips, {
                    'position': 'absolute', 'top': it + 'px', 'left': il + 'px',
                    'width': w + 'px', 'height': h + 'px', 'zIndex': getzi()
                });
                tips.id = "tip_" + tipsId;
                var iHtml = '<div style="z-index: ' + getzi() + ';width: 100%; height: 100%; overflow: hidden; word-wrap : break-word; padding:5px;" ><iframe id="t_frm_' + tipsId + '" style="position: absolute; width:100%; height: 100%; top: 0;left: 0; scrolling:no;border:' + borderW + 'px solid Green;background-color:#fff" frameborder="no" src="about:blank"></iframe>';
                iHtml += '<div style="z-index: ' + getzi() + ';position:absolute;height:100%;width:100%;left:50%;top:50%;margin-left: -120px;margin-right: -12px;float:left;vertical-align:middle;over-flow:hidden; word-wrap:break-word; marggin:5px;"><img src="/images/loadingtip.gif" style=" vertical-align:middle;" /><span style="line-height:24px;padding-left:10px;top:0px;">' + p + '</span></div>';
                iHtml += '</div>';
                tdoc.body.appendChild(tips);
                // auto  proper the heigth and width of the frame to cover the select
                var DivRef = document.getElementById("tip_" + tipsId);
                DivRef.innerHTML = iHtml;
                var IfrRef = document.getElementById("t_frm_" + tipsId);
                DivRef.style.display = "block";
                IfrRef.style.width = DivRef.offsetWidth + 2 * borderW + "px";
                IfrRef.style.height = DivRef.offsetHeight + 2 * borderW + "px";
                IfrRef.style.zIndex = DivRef.style.zIndex - 5;
                IfrRef.style.display = "block";
            },
            HideTip: function() {
                var filter = $("#tip_fix").OpenWin.GetCover();
                $('#tip_fix').remove();
                this.HideFilter(filter);
            },
            hideAnyFilter: function() {
                var filter = $().OpenWin.GetCover();
                this.HideFilter(filter);
            },
            Gallery: function(type, id) {
                scPopConfig.opac = 0.70;
                scPopConfig.bgcolor = "#000";
                var w = 610;
                var h = 550;
                //遮罩
                this.DisplayFilter();

                var dinfo = { win: window, topw: twin };
                var clsize = $.getClientSize(twin), spos = $.getWindowPosiotion(twin);

                var it = Math.max(spos.Y + (clsize.h - h - 20) / 2, 0);
                var il = Math.max(spos.X + (clsize.w - w - 20) / 2, 0);
                var dfrm = tdoc.createElement('iframe');
                $.popStyle(dfrm);
                // define popup frame name                                                                                          
                dfrm.id = "SUBCON_GALLARY";
                dfrm.frameBorder = 0;
                dfrm.allowTransparency = true;
                dfrm.src = '/Template/Gallery.aspx?type=' + type + '&id=' + id;
                $.setStyle(dfrm, {
                    'position': 'absolute', 'top': it + 'px', 'left': il + 'px',
                    'width': w + 'px', 'height': h + 'px', 'zIndex': getzi()
                });
                tdoc.body.appendChild(dfrm);
                dfrm._dlgargs = dinfo;
            },
            // 关闭弹出层
            CloseDiv: function(d, c) {
                var dlg = ('object' == typeof (d)) ? d.frameElement : document.getElementById(d);
                if (dlg) {
                    $.removeNode(dlg);
                }
                if (c) {
                    this.HideFilter(c);
                }
            },
            // 显示遮罩
            DisplayFilter: function(mycolor, myocp) {
                cover = tdoc.createElement('div');
                $.popStyle(cover);
                if (mycolor) {
                    $.setStyle(cover, {
                        'position': 'absolute', 'zIndex': getzi(), 'top': '0px',
                        'left': '0px', 'backgroundColor': mycolor
                    });
                }
                else {
                    $.setStyle(cover, {
                        'position': 'absolute', 'zIndex': getzi(), 'top': '0px',
                        'left': '0px', 'backgroundColor': scPopConfig.bgcolor
                    });
                }
                if (myocp) {
                    $.setOpacity(cover, myocp);
                }
                else {
                    $.setOpacity(cover, scPopConfig.opac);
                }

                if ($.browser.msie && $.browser.version == "6.0") {//IE6防select遮挡问题
                    var ifrm = tdoc.createElement('iframe');
                    $.popStyle(ifrm);
                    ifrm.hideFocus = true;
                    ifrm.frameBorder = 0;
                    ifrm.src = "";
                    $.setStyle(ifrm, {
                        'width': '100%', 'height': '100%', 'position': 'absolute', 'left': '0px',
                        'top': '0px', 'filter': 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
                    });
                    cover.appendChild(ifrm);
                }

                $.addEvent(twin, 'resize', resizehdl);
                resizehdl();
                tdoc.body.appendChild(cover);
            },
            // 取得遮罩
            GetCover: function() { return cover; },
            // 关闭遮罩
            HideFilter: function(o) { $.removeNode(o); cover = null; o = null; },
            scrollHide: function() {
                // save old positon
                //oldScrollTop = (document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;
                //var pwbody = tdoc.getElementsByTagName((document.compatMode == "BackCompat") ? "BODY" : "HTML")[0];
                //pwbody.style.overflow = "hidden"; //hide scroll
                //pwbody.style.paddingRight = '17px';
            },
            scrollShow: function() {
                //var pwbody = tdoc.getElementsByTagName((document.compatMode == "BackCompat") ? "BODY" : "HTML")[0];
                //pwbody.style.overflow = pwbody.style.paddingRight=""; //show  scroll
                //(document.documentElement) ? (document.documentElement.scrollTop = oldScrollTop) : (document.body.scrollTop = oldScrollTop);
            }
        };
    } ();
})(jQuery)

// 系统标签选择接口，引入subconpopup后可以直接调用
var SysTag = {};
SysTag.PopupWidth = 990;
SysTag.PopupHeight = 520;
// 选择行业
// 参数：赋值的input的ID，文本的input的ID，类型
// 1供应商 2产品 3买家 4订单 5需求
SysTag.Industry = function(valueId, textId, type) {
    $().OpenWin.Popup( g_lang_19, "/PageLetters/SysTag/IndustryTag.aspx?type=" + type + "&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}
//选择产品类 上同
SysTag.SystemColumn = function (valueId, textId, type) {
    $().OpenWin.Popup(g_lang_200, "/PageLetters/SysTag/ColumnTag.aspx?type=" + type + "&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}
//选择产品类型，与上同
SysTag.Production = function (valueId, textId) {
    $().OpenWin.Popup(g_lang_200, "/PageLetters/SysTag/Admin/IndustryTag.aspx?ctl=1&cp=1&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}
// 选择工艺，type 1供应商 2订单
SysTag.TechnologySearch = function (valueId, textId, type) {
    $().OpenWin.Popup(g_lang_128, "/PageLetters/SysTag/Technologyinfo.aspx?type=" + type + "&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 选择地区
// 1供应商 2产品 3买家 4订单 5需求 6供应商目标市场 7产品目标市场
SysTag.Area = function(valueId, textId, type) {
    $().OpenWin.Popup(g_lang_18, "/PageLetters/SysTag/AreaTag.aspx?type=" + type + "&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 选择认证，与上同
SysTag.Identify = function(valueId, textId, type) {
    $().OpenWin.Popup(g_lang_129, "/PageLetters/SysTag/Identify.aspx?v=" + valueId + "&t=" + textId + "&type=" + type + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 以下为管理标签调用
// 行业
SysTag.IndustryAdmin = function(valueId, textId, type) {
    $().OpenWin.Popup(g_lang_19, "/PageLetters/SysTag/admin/IndustryTag.aspx?type=" + type + "&v=" + valueId + "&t=" + textId, SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 地区
SysTag.AreaAdmin = function(valueId, textId) {
    $().OpenWin.Popup(g_lang_18, "/PageLetters/SysTag/Admin/AreaTag.aspx?v=" + valueId + "&t=" + textId, SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 认证
SysTag.IdentifyAdmin = function(valueId, textId) {
    $().OpenWin.Popup(g_lang_129, "/PageLetters/SysTag/Admin/Identify.aspx?ctl=1&v=" + valueId + "&t=" + textId, SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 选择工艺，与上同
SysTag.Technology = function (valueId, textId, type) {
    $().OpenWin.Popup(g_lang_128, "/PageLetters/SysTag/Admin/Technologyinfo.aspx?type=" + type + "&v=" + valueId + "&t=" + textId + "&R=" + Math.random(), SysTag.PopupWidth, SysTag.PopupHeight, true);
}

// 加工能力
// type 参数 1:加工材料 2:加工精度 3:加工重量 4:加工尺寸
SysTag.MakeAbility = function(valueId, textId, type) {
    $().OpenWin.Popup( g_lang_130, "/PageLetters/SysTag/MakeAbility.aspx?v=" + valueId + "&t=" + textId + "&type=" + type, SysTag.PopupWidth, 420, true);
}

// 产品服务类
SysTag.ProductService = function(valueId, textId) {
    $().OpenWin.Popup(g_lang_132, "/PageLetters/SysTag/Admin/IndustryTag.aspx?ctl=1&cp=1&v=" + valueId + "&t=" + textId, SysTag.PopupWidth, 420, true);
}

var FavTag = {};
// 收藏类型 0 - 产品 1 - 订单 2 - 供应商 3 - 采购商 4 - 需求
FavTag.Add = function (id, type, tagIds, urlreferrer) {
    urlreferrer = urlreferrer || location.href;
    //判断用户是否是未审核用户
    var userType;
    $.get("/asmx/user/usertype.ashx?rond=" + Math.random, function (data) {
        userType = data;
        if (userType == "0") {
            ShowMsgTip({ tip: g_lang_User_Notapproved_notice1,
                icon: "diWarning",
                title: g_lang_OP_1,
                okayBtn: { show: true, text: g_lang_28, disabled: false }
            });
            return false;
        }
        else {

            $().OpenWin.Popup(g_lang_133, "/PageLetters/SysTag/FavTag.aspx?type=" + type + "&id=" + id + "&tid=" + tagIds + "&url=" + encodeURIComponent(urlreferrer), 480, 380, true);
        }
    });

}

FavTag.Mark = function (markId, type, tagIds) {
    $().OpenWin.Popup(g_lang_133, "/PageLetters/SysTag/FavTag.aspx?type=" + type + "&mid=" + markId + "&tid=" + tagIds, 480, 380, true);
}

FavTag.SetFavText = function(trueOrFalse) {
    if (trueOrFalse) {
        $("#fav_tag_text").text( g_lang_134);
    }
    else {
        $("#fav_tag_text").text( g_lang_135);
    }
}

var FileTag = {};
//邮件文件类型 0 发件 1收件
FileTag.Mark = function (markId, type, tagIds) {
    $().OpenWin.Popup(g_lang_137, "/PageLetters/SysTag/FileTag.aspx?type=" + type + "&mid=" + markId + "&tid=" + tagIds, 480, 380, true);
}
