Function.prototype.Bind = function () {
    var d = this, b = arguments[0], a = new Array();
    for (var c = 1; c < arguments.length; c++) {
        a.push(arguments[c]);
    }
    return function () {
        var f = [];
        for (var e = 0; e < arguments.length; e++) {
            f.push(arguments[e]);
        }
        return d.apply(b, f.concat(a));
    };
};
if (typeof $ == "undefined") {
    $ = function (a) {
        return document.getElementById(a)
    }
};
if (typeof $C == "undefined") {
    $C = function (a) {
        return document.createElement(a)
    }
};
if (typeof $T == "undefined") {
    $T = function (a) { return document.createTextNode(a); };
};
if (typeof Util == "undefined") { Util = {}; };
Util.isIE = (navigator.appName.indexOf("Microsoft", 0) != -1) ? true : false;
Util.setCookie = function (name, value, expires, path, domain) {
    var str = name + "=" + escape(value);
    if (expires) {
        if (expires == 'never') {
            expires = 100 * 365 * 24 * 60;
        }
        var exp = new Date();
        exp.setTime(exp.getTime() + expires * 60 * 1000);
        str += "; expires=" + exp.toGMTString();
    }
    if (path) {
        str += "; path=" + path;
    }
    if (domain) {
        str += "; domain=" + domain;
    }
    document.cookie = str;
}
Util.getCookie = function (name) {
    var tmp, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)", "gi");
    tmp = reg.exec(unescape(document.cookie));
    if (tmp) { return (tmp[2]); }
    return null;
};
Util.AddEvent = function (d, c, b, a) {
    if (d.addEventListener) {
        d.addEventListener(c, b, a);
    } else {
        if (d.attachEvent) {
            d.attachEvent("on" + c, b);
        }
    }
};
Util.RemoveEvent = function (d, c, b, a) {
    if (d.removeEventListener) {
        d.removeEventListener(c, b, a);
    } else {
        if (d.detachEvent) {
            d.detachEvent("on" + c, b);
        }
    }
};
Util.stopEvent = function (event) {
    e = event ? event : window.event;
    if (Util.isIE) {
        e.returnValue = false;
        e.cancelBubble = true;
    } else if (e) {
        e.stopPropagation();
        e.preventDefault();
    }
};
MiniBlog = function () {
    this.initialize.apply(this, arguments);
};
MiniBlog.prototype = {
    element: null,
    thread: null,
    needFixPng: false,
    f_lastScrollY: 0,
    pixelTop: 0,
    Size: { Width: 300, Height: 186 },
    titleHeight: 26,
    zIndex: 1000,
    url: 'about:blank',
    isMin: false,
    is_ie: false,
    is_safari: false,
    is_opera: false,
    showcontent: true,
    template: ['<div id="minblog_con">',
    '<div style="background:url(http://www.p5w.net/blog/tbtj/images/header1.jpg) no-repeat;height:26px;" id="minblog_title">',
    '<a style="position:absolute;left:280px;top:6px;background:url(http://www.p5w.net/blog/tbtj/images/blogrecommend_003.gif) no-repeat;width:15px;height:14px;" id="minblog_btnClose" href="javascript:void(0)"></a>',
    '<a style="position:absolute;left:260px;top:6px;background:url(http://www.p5w.net/blog/tbtj/images/blogrecommend_002.gif) no-repeat;width:15px;height:14px;" id="minblog_btnMin" href="javascript:void(0)"></a>',
    "</div>",
    '<div style="margin:0;overflow:hidden;" id="minblog_content"><iframe frameborder="0" scrolling="no" id="minblog_iframe" src="about:blank"></iframe></div>',
    "</div>"].join(""),
    initialize: function (c, e, d, f, a) {
        this.url = c;
        if (e) {
            this.isMin = e;
        }
        if (d) {
            this.Size = d;
        }
        if (f) {
            this.titleHeight = f;
        }
        if (a) {
            this.zIndex = a;
        }
        var f = Util.getCookie("minblogstatus");

        if (f == 0) {
            this.showcontent = false;
        } else {
            this.showcontent = true;
        }
        this.needFixPng = this.checkNeedFixPng();
        this.pixelTop = (document.documentElement.clientHeight - this.Size.Height) * 1;
        this.build();
    },
    build: function () {
        if ($('miniblogwindow')) return;
        this.element = $C('div');
        this.element.setAttribute("id", 'miniblogwindow');
        var _top = (parseInt(document.documentElement.scrollTop, 10) + (document.documentElement.clientHeight - this.Size.Height));
        this.element.style.display = "none";
        this.element.style.left = "100000px";
        this.element.style.border = "solid 1px #f6d700";
        this.element.style.zIndex = this.zIndex;
        this.element.style.width = this.Size.Width + 'px';
        this.element.style.height = this.Size.Height + 'px';
        this.element.style.margin = '0px';
        this.element.style.position = "absolute";
        this.element.style.top = _top + "px";

        this.element.innerHTML = this.template;
        document.body.appendChild(this.element);
        this.title = $("minblog_title");
        this.btnClose = $("minblog_btnClose");
        this.btnMin = $("minblog_btnMin");
        this.iframe = $("minblog_iframe");
        this.content = $("minblog_content");
        this.title.style.height = this.titleHeight + "px";
        this.content.style.height = (this.Size.Height - this.titleHeight) + "px";
        this.iframe.style.height = (this.Size.Height - this.titleHeight) + "px";
        Util.AddEvent(this.btnMin, "click", this.switchcontent.Bind(this));
        Util.AddEvent(this.btnClose, "click", this.destroy.Bind(this));
    },
    show: function () {
        this.switchcontent(1);
        this.element.style.display = "";
        this.iframe.setAttribute("src", this.url);
        var me = this;
        if (!this.thread)
            this.thread = setInterval(function () { me.autowidth() }, 1);
    },
    autowidth: function () {

        var pos = this.getPosition(this.element);
        var f_diffY = parseInt(this.is_safari ? document.documentElement.scrollTop + document.body.scrollTop : document.documentElement.scrollTop, 10) - 4;
        var tmpHeight = 0;
        if (!this.showcontent) {
            tmpHeight = this.Size.Height * 1 - this.titleHeight;
        }
        f_percent = .1 * (f_diffY - this.f_lastScrollY + tmpHeight);
        if (f_percent > 0)
            f_percent = Math.ceil(f_percent);
        else
            f_percent = Math.floor(f_percent);
        this.pixelTop += f_percent;
        this.element.style.top = this.pixelTop + "px";
        this.element.style.left = pos.Left + "px";
        this.f_lastScrollY = this.f_lastScrollY + f_percent;

    },
    switchcontent: function (flag) {
        if (flag == 1) {
            if (this.showcontent == true) {
                this.btnMin.style.backgroundImage = "url(http://www.p5w.net/blog/tbtj/images/blogrecommend_002.gif)";
                this.content.style.display = "";
                this.element.style.height = (this.Size.Height * 1) + "px";
            } else {
                this.content.style.display = "none";
                this.element.style.height = this.titleHeight + "px";
                this.btnMin.style.backgroundImage = "url(http://www.p5w.net/blog/tbtj/images/blogrecommend_001.gif)";

            }
        } else { 
            var val = "0";
            if (this.showcontent == true) {
                val = "0";
                this.content.style.display = "none";
                this.element.style.height = this.titleHeight + "px";
                this.btnMin.style.backgroundImage = "url(http://www.p5w.net/blog/tbtj/images/blogrecommend_001.gif)";

            } else {
                val = "1";
                this.btnMin.style.backgroundImage = "url(http://www.p5w.net/blog/tbtj/images/blogrecommend_002.gif)";
                this.content.style.display = "";
                this.element.style.height = (this.Size.Height * 1) + "px";
            }  
            Util.setCookie("minblogstatus", val, 60 * 24, "/");
            this.showcontent = !this.showcontent;
        }
    },
    destroy: function () {
        Util.setCookie("minblog", "0", 60 * 24, "/");
        clearInterval(this.thread);
        this.thread = null;
        document.body.removeChild(this.element);
    },
    getPosition: function (obj) {

        var pos = { Left: 0, Top: 0 };
        pos.Left = parseInt((document.documentElement.clientWidth - obj.offsetWidth - 4), 10);
        pos.Top = parseInt(this.is_safari ? document.documentElement.scrollTop + document.body.scrollTop : document.documentElement.scrollTop, 10) + (document.documentElement.clientHeight - obj.offsetHeight);
        return pos;
    },
    checkNeedFixPng: function () {
        var sUserAgent = navigator.userAgent;
        var isOpera = sUserAgent.indexOf("Opera") > -1;
        var isChrome = sUserAgent.indexOf("Chrome") > -1;
        this.is_opera = isOpera;
        //排除Chrome信息，因为在Chrome的user-agent字符串中会出现Konqueror/Safari的关键字   
        var isKHTML = (sUserAgent.indexOf("KHTML") > -1
               || sUserAgent.indexOf("Konqueror") > -1
               || sUserAgent.indexOf("AppleWebKit") > -1) && !isChrome;
        if (isKHTML) {//判断是否基于KHTML，如果时的话在继续判断属于何种KHTML浏览器   
            var isSafari1 = sUserAgent.indexOf("AppleWebKit") > -1;
            this.is_safari = isSafari1;
        }
        var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
        this.is_ie = isIE;
        if (isIE) {
            var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
            reIE.test(sUserAgent);
            var fIEVersion = parseFloat(RegExp["$1"]);
            return fIEVersion < 7.0 ? true : false;
        }
        return false;
    }
};
function initMinBlog() {
    var isShow = Util.getCookie("minblog");
    if (isShow == "0") {
    }
    else {
        var minb = new MiniBlog("http://www.p5w.net/blog/tbtj/index_455.htm");
        minb.show();
    }
}
Util.AddEvent(window, "load", initMinBlog);
