﻿/*
 * jBox 1.0 - a webpage UI dialog widget written in javascript on top of the jQuery library
 * By Daniel Lin(http://www.aspstat.com)
 * Copyright (c) 2007 Daniel Lin
 * Download by http://www.codefans.net
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * Modified by __timothy__ 2010.3.7
*/
var jBox = {
    boxes: [],
    init: function (id) {
        var holder = $('#jBoxHolder');
        if (holder.length == 0) {
            holder = $('<div id="jBoxHolder"></div>');
            $(document.body).append(holder);
        }
        var box = $('<div class="jBox" id="' + id + '"></div>').appendTo($('<div class="jBox-wraper"></div>').appendTo(holder));
        box.append('<div class="jBox-handler"><h3></h3><button class="jBox-close" title="关闭"></button></div>')
            .append('<div class="jBox-content"></div>')
		    .append('<div class="jBox-status"><button class="jBox-confirm" title="确认">确认</button><button class="jBox-cancel" title="取消">取消</button></div>');

        $('.jBox-wraper').css('zIndex', $('.jBox').length + 1099);
        box.handler = box.find('.jBox-handler');
        box.content = box.find('.jBox-content');
        box.status = box.find('.jBox-status');
        box.handler._parent = box;
        box.status._parent = box;
        box.content._parent = box;
        box.isScrolling = function (bol) { jBox.isScrolling(this, bol); };
        box.setSize = function (w, h) { jBox.setSize(this, w, h); };
        box.moveTo = function (x, y) { jBox.moveTo(this, x, y); };
        box.show = function () { jBox.show(this); };
        box.hide = function () { jBox.hide(this); };
        box.load = function (type, source, title) { jBox.load(this, type, source, title); };
        box.dispose = function () { jBox.dispose(this); }
        box.attr('jBoxID', this.boxes.length);
        box.jBoxID = this.boxes.length;
        this.boxes[this.boxes.length] = box;
        return box;
    },
    load: function (box, type, source, title) {
        var type = type.toLowerCase();
        if (typeof title != 'undefined') { box.find("h3").text(title); }
        if (type == "inline") {
            box.content.html(source);
        } else if (type == "div") {
            box.content.html($('#' + source).html());
        } else if (type == "iframe") {
            box.content.css('overflow', 'hidden');
            if (box.content.children().length < 1 || box.content.children().get(0).tagName != "IFRAME") {
                box.content.html('<iframe src="" frameborder="no"  style="margin:0; padding:0; border:0;width:100%;height:100%" name="_iframe-' + box.jBoxID + '"></iframe>');
            }
            window.frames["_iframe-" + box.jBoxID].location.replace(source);
        }
        box.content.datatype = type;
    },
    open: function (id, type, source, title, attr, confFunc) {
        function getValue(Name) {
            var config = new RegExp(Name + "=([^,]+)", "i");
            return (config.test(attr)) ? eval('(' + RegExp.$1 + ')') : 0;
        }
        var box;
        //Find a jbox element has been created else create new element
        if ($('#' + id).length == 0) {
            box = this.init(id);
        } else {
            box = jBox.boxes[parseInt($('#' + id).get(0).getAttribute('jBoxID'))];
        }
        box.setSize(getValue(("width")), (getValue("height")));
        var xpos = getValue("center") ? "middle" : getValue("left");
        var ypos = getValue("center") ? "middle" : getValue("top");
        box.parent().css("visibility", "visible");
        box.isScrolling(getValue("scrolling"));
        box.isModel = getValue("model");
        if (box.isModel) { jBox.showModel(); }
        if (getValue("draggable")) { box.handler.mousedown(function (e) { jBox.etarget = this; jBox.setupDrag(e); }); }
        if (getValue("scrolling")) { box.Resize.mousedown(function (e) { jBox.etarget = this; jBox.setupDrag(e); }); }
        if (typeof confFunc != 'undefined') {
            box.status.find(".jBox-confirm").click(function () { eval(confFunc + "()"); jBox.hide(box);   });
            box.status.find(".jBox-cancel").css("display", "inline").click(function () { jBox.hide(box);  });
        } else {
            box.status.find(".jBox-confirm").click(function () { jBox.hide(box);   });
        }
        box.handler.find('.jBox-close').click(function (e) { jBox.setupControls(e);   });
        box.load(type, source, title);
        box.moveTo(xpos, ypos);
        return box;
    },
    isScrolling: function (box, bol) { box.content.css("overflow", (bol) ? "auto" : "hidden"); },
    setSize: function (box, w, h) { box.parent().css("width", Math.max(parseInt(w), 150) + 'px'); box.content.css("height", Math.max(parseInt(h), 80) + 'px'); }, // correct the height
    moveTo: function (box, x, y) {
        this.getViewPoint();
        box.parent().css("left", (x == "middle") ? this.scrollPos[0] + (this.docSize[0] - box.get(0).offsetWidth) / 2 + "px" : this.scrollPos[0] + parseInt(x) + "px");
        box.parent().css("top", (y == "middle") ? this.scrollPos[1] + (this.docSize[1] - box.get(0).offsetHeight) / 2 + "px" : this.scrollPos[1] + parseInt(y) + "px");
    },
    show: function (box) {
        box.css('display', 'block');
        if (box.isModel) { jBox.showModel(); }
    },
    hide: function (box) {
        if (typeof box == "undefined") {
            return false;
        } else {
            box.parent().css('visibility', 'hidden');
            if (box.isModel) { jBox.hideModel(); }
        }
        return true;
    },
    dispose: function (box) {
        if (box.isModel) jBox.hideModel();
        for (var i = 0; i < jBox.boxes.length; i++) {
            if (jBox.boxes[i] == box) {
                box.handler._parent = null;
                box.controls._parent = null;
                box.content._parent = null;
                jBox.boxes[i] = null;
                break;
            }
        }
        box.remove();
    },
    setupControls: function (e) {
        var j = jBox;
        var sourceobj = window.event ? window.event.srcElement : e.target;
        var box = j._retBox(sourceobj);
        j.hide(box);
        return false;
    },
    showModel: function () {
        var model = $('#jBox_hideIframe');
        if (model.length == 0) {
            model = $('<div id="jBox_hideIframe" class="jBox-model"></div>');
            model.appendTo(document.body);
            $(window).bind('resize', function () { jBox.showModel(); })
        }
        jBox.getViewPoint();
        model.css('width', jBox.pageSize[0] + 'px');
        model.css('height', jBox.pageSize[1] + 'px');
    },
    hideModel: function () {
        var model = $('#jBox_hideIframe');
        if (model.length > 0)
            model.remove();
    },
    setupDrag: function (e) {
        var j = jBox;
        var boxE = j.etarget;
        var box = j._retBox(boxE);
        var e = window.event || e;
        j.initmousex = e.clientX;
        j.initmousey = e.clientY;
        j.initx = parseInt(box.parent().get(0).offsetLeft);
        j.inity = parseInt(box.parent().get(0).offsetTop);
        j.width = parseInt(box.parent().get(0).offsetWidth);
        j.contentheight = parseInt(box.content.get(0).offsetHeight);
        if (box.content.datatype == "iframe") {
            box.css('backgroundColor', '#F8F8F8');
            box.content.css('visibility', 'hidden');
        }
        document.onmousemove = j.getDistance;
        document.onmouseup = function () {
            if (box.content.datatype == "iframe") {
                box.content.css('visibility', 'visible');
            }
            j.stopDrag();
        }
        return false;
    },
    getDistance: function (e) {
        var j = jBox;
        var etarget = j.etarget;
        var e = window.event || e;
        j.distancex = e.clientX - j.initmousex;
        j.distancey = e.clientY - j.initmousey;
        var box = j._retBox(etarget);
        if (etarget.className == 'jBox-handler') {
            box.parent().css('left', j.distancex + j.initx + 'px');
            box.parent().css('top', j.distancey + j.inity + 'px');
        }
        return false;
    },
    stopDrag: function () {
        jBox.etarget = null;
        document.onmousemove = null;
        document.onmouseup = null;
    },
    getViewPoint: function () {
        var ie = document.all && !window.opera;
        var domclientWidth = document.documentElement && parseInt(document.documentElement.clientWidth) || 100000;
        this.standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
        this.scrollPos = [(ie) ? this.standardbody.scrollLeft : window.pageXOffset,
			(ie) ? this.standardbody.scrollTop : window.pageYOffset];
        this.docSize = [(ie) ? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent)) ? window.innerWidth : Math.min(domclientWidth, window.innerWidth - 16),
			(ie) ? this.standardbody.clientHeight : window.innerHeight];
        if (ie) {
            this.scrollSize = [(document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth,
			   (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight];
        }
        else {
            this.scrollSize = [document.body.scrollWidth, window.innerHeight + window.scrollMaxY];
        }
        this.pageSize = [(this.scrollSize[0] < this.docSize[0]) ? this.docSize[0] : this.scrollSize[0],
			(this.scrollSize[1] < this.docSize[1]) ? this.docSize[1] : this.scrollSize[1]];
    },
    saveViewState: function (box) {
        this.getViewPoint();
        box.lastPos = [parseInt((box.css('left') || box.get(0).offsetLeft)) - jBox.scrollPos[0],
			parseInt((box.css('top') || box.get(0).offsetTop)) - jBox.scrollPos[1]];
        box.lastSize = [parseInt(box.css('width')),
			parseInt(box.css('height'))];
    },
    _retBox: function (dom) {
        return jBox.boxes[parseInt($(dom).parents('div.jBox').get(0).getAttribute('jBoxID'))];
    }
}


