﻿jQuery.fn.popMenu = function(menuId, autoPosition, top, speed) {
    $(this).click(function() {
        var offset = $(this).offset();
        if (autoPosition) {
            $(menuId).css({
                top: offset.top + top + "px",
                left: offset.left + "px"
            });
        }
        $(menuId).toggle(speed);
    });

    $(document).click(function(e) {
        if (e.target.id != $(menuId).selector.substring(1)) {
            $(menuId).fadeOut(speed);
        }
    });

    $(this).click(function (e) {
        e.stopPropagation();
    });
}

jQuery.fn.dialog = function(title, content, bottom, initWidth, needClose) {
    var docWidth = document.documentElement.clientWidth;
    var docHeight = $(document).height();
    var scrHeight = document.documentElement.clientHeight;
    var popPosition = "fixed";
    if ($.browser.msie && $.browser.version == 6.0) {
        popPosition = "absolute";
    }

    var pDiv = '<div id="pop-overlay" class="pop-overlay"></div>' +
	'<div class="pop-main" id="pop-main" >' +
	    '<table cellpadding="0" cellspacing="0" width="100%">' +
        '<tr>' +
            '<td class="top-left"></td>' +
            '<td class="border-top"></td>' +
            '<td class="top-right"></td>' +
        '</tr>' +
        '<tr>' +
            '<td class="border-left">&nbsp;</td>' +
            '<td >' +
		        '<div class="pop-title">' + title;
    if (needClose) {
        pDiv += '<a href="javascript:;">&nbsp;</a>';
    }
    pDiv += '</div>' +
		'<div class="pop-content">' + content + '</div>';
    if (bottom != "") {
        pDiv += '<div class="pop-bottom">' + bottom + '</div>';
    }
    pDiv += '</td>' +
            '<td class="border-right">&nbsp;</td>' +
         '</tr>' +
         '<tr>' +
            '<td class="bottom-left"></td>' +
            '<td class="border-bottom"></td>' +
            '<td class="bottom-right"></td>' +
        '</tr>' +
        '</table></div>';
    $(pDiv).appendTo($("body"));

    var topHeight = (scrHeight - $("#pop-main").height()) / 2;
    if ($.browser.msie && $.browser.version == 6.0) {
        popPosition = "absolute";
        topHeight = topHeight + document.documentElement.scrollTop;
    }
    
    $('#pop-overlay').css({
        width: docWidth,
        height: docHeight,
        opacity: 0,
        display: "block"
    }).animate({ opacity: 0.5 }).bgiframe();

    $('#pop-main').css({
        position: popPosition,
        width: initWidth,
        left: (docWidth - initWidth) / 2,
        top: topHeight,
        display: "block"
    }).animate({ opacity: 1 });


    function close() {
        $('#pop-overlay').animate({ opacity: 0 }, function() { $('#pop-overlay').remove() });
        $('#pop-main').animate({ opacity: 0 }, function() { $('#pop-main').remove() });
    }

    $('.pop-title a').click(function() {
        close();
    })
}

jQuery.fn.processBar = function (title, needClose) {
    var docWidth = document.documentElement.clientWidth;
    var docHeight = $(document).height();
    var scrHeight = document.documentElement.clientHeight;
    var initWidth = 300;
    var popPosition = "fixed";

    var pDiv = '<div id="pop-overlay" class="pop-overlay"></div>' +
	'<div class="pop-main" id="pop-main" >' +
	    '<table cellpadding="0" cellspacing="0" width="100%">' +
        '<tr>' +
            '<td class="top-left"></td>' +
            '<td class="border-top"></td>' +
            '<td class="top-right"></td>' +
        '</tr>' +
        '<tr>' +
            '<td class="border-left">&nbsp;</td>' +
            '<td >' +
		        '<div class="pop-title">' + title;
    if (needClose) {
        pDiv += '<a href="javascript:;">&nbsp;</a>';
    }
    pDiv += '</div><div class="pop-content">' +
		            '<div class="pop-process"></div>' +
		        '</div>' +
            '</td>' +
            '<td class="border-right">&nbsp;</td>' +
         '</tr>' +
         '<tr>' +
            '<td class="bottom-left"></td>' +
            '<td class="border-bottom"></td>' +
            '<td class="bottom-right"></td>' +
        '</tr>' +
        '</table></div>';
    $(pDiv).appendTo($("body"));

    var topHeight = (scrHeight - $("#pop-main").height()) / 2;
    if ($.browser.msie && $.browser.version == 6.0) {
        popPosition = "absolute";
        topHeight = topHeight + document.documentElement.scrollTop;
    }

    $('#pop-overlay').css({
        width: docWidth,
        height: docHeight,
        opacity: 0,
        display: "block"
    }).animate({ opacity: 0.5 }).bgiframe();

    $('#pop-main').css({
        position: popPosition,
        width: initWidth,
        left: (docWidth - initWidth) / 2,
        top: topHeight,
        display: "block"
    }).animate({ opacity: 1 });

    function close() {
        $('#pop-overlay').animate({ opacity: 0 }, function () { $('#pop-overlay').remove() });
        $('#pop-main').animate({ opacity: 0 }, function () { $('#pop-main').remove() });
    }

    $('.pop-title a').click(function () {
        close();
    })
}

jQuery.fn.emptyDialog = function(dialogId, initWidth) {
    var docWidth = document.documentElement.clientWidth;
    var docHeight = $(document).height();
    var scrHeight = document.documentElement.clientHeight;
    var popPosition = "fixed";
    var topHeight = (scrHeight - $(dialogId).height()) / 2 - 20;
    if ($.browser.msie && $.browser.version == 6.0) {
        popPosition = "absolute";
        topHeight = topHeight + document.documentElement.scrollTop;
    }
    var pDiv = '<div id="pop-overlay" class="pop-overlay"></div>';
    $(pDiv).appendTo($("body"));

    $('#pop-overlay').css({
        width: docWidth,
        height: docHeight,
        opacity: 0,
        display: "block"
    }).animate({ opacity: 0.5 }).bgiframe();

    $(dialogId).css({
        position: popPosition,
        width: initWidth,
        left: (docWidth - initWidth) / 2,
        top: topHeight,
        opcity: 1,
        display: "block"
    });

    function close() {
        $('#pop-overlay').hide(0, function() { $('#pop-overlay').remove() });
        $(dialogId).hide();
    }

    $('.pop-title a').click(function() {
        close();
    })
}

$.fn.numeral = function (needDecimal) {
    var needDecimal = needDecimal;
    $(this).css("ime-mode", "disabled");
    this.bind("keypress", function () {
        if (needDecimal) {
            if (event.keyCode == 46) {
                if (this.value.indexOf(".") != -1) {
                    return false;
                }
            } else {
                return event.keyCode >= 47 && event.keyCode <= 57;
            }
        } else {
            return event.keyCode >= 47 && event.keyCode <= 57;
        }
    });
    this.bind("blur", function () {
        if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
            this.value = this.value.substr(0, this.value.length - 1);
        } else if (isNaN(this.value)) {
            this.value = "";
        }
    });
    this.bind("paste", function () {
        var s = clipboardData.getData('text');
        if (!/\D/.test(s));
        value = s.replace(/^0*/, '');
        return false;
    });
    this.bind("dragenter", function () {
        return false;
    });
    this.bind("keyup", function () {
        if (/(^0+)/.test(this.value)) {
            this.value = this.value.replace(/^0*/, '');
        }
    });
};

$.extend($.fn, {
    tooltip: function(options) {
        var op = $.extend({
            text: "",
            width: 250,
            left: 12,
            zIndex: 9999,
            speed: 150
        }, options);

        var tipDiv = "<div id='" + op.tipID + "' class='tooltip'>" +
                         "<div class='tooltipContent'>" +
                             "<span class='tooltipPointer'><span class='tooltipPointerInner'></span></span>" + op.text +
                         "</div>" +
                     "</div>";
        var tipid = '#' + op.tipID;
        $(this).hover(function() {
            $(tipDiv).appendTo($("body"));
            var offset = $(this).offset();
            $(tipid).css({
                top: offset.top + "px",
                left: offset.left + $(this).width() + op.left + "px",
                width: op.width,
                zIndex: op.zIndex
            }).fadeIn(op.speed);
        }, function() {
            $(tipid).remove();
        });
    },

    watermark: function(options) {
        var op = $.extend({
            text: "",
            markColor: "#bbb",
            textColor: "#000"
        }, options);

        if ($(this).val() == "") {
            $(this).val(op.text).css("color", op.markColor);
        }

        $(this).focus(function() {
            if ($(this).val() == op.text) {
                $(this).val("").css("color", op.textColor);
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).val(op.text).css("color", op.markColor);
            }
        });
    },

    closePop: function(options) {
        var op = $.extend({
            //遮罩层ID
            overlayID: "#pop-overlay",
            //是否删除弹出层节点
            removePop: false
        }, options);

        if (op.removePop) {
            $(this).remove();
        }
        else {
            $(this).hide();
        }
        $(op.overlayID).remove();
    }
});
