﻿var XU = {
    namespace: function() {
        var a = arguments, o = null, i, j, d;
        for (i = 0; i < a.length; i++) {
            d = a[i].split('.');
            o = XU;
            for (j = (d[0] == 'XU') ? 1 : 0; j < d.length; j++) {
                o[d[j]] = o[d[j]] || {};
                o = o[d[j]];
            }
        }
        return o;
    }
};
XU.namespace('ui');
XU.namespace('util');
XU.namespace('app');
XU.namespace('page');
XU.namespace('config');

XU.env = {
    domain: 'xuxinhua.net',
    staticRoot: '',
    swfRoot: '',
    wwwRoot: 'http://xuxinhua.net/'
};

function createDelegate(object, method) {
    var shim = function() {
        method.apply(object, arguments);
    }
    return shim;
}

(function() {
    XU.browser = {
        IE: !!(window.attachEvent && !window.opera),
        IE6: navigator.userAgent.indexOf('MSIE 6.0') > -1,
        IE7: navigator.userAgent.indexOf('MSIE 7.0') > -1,
        Opera: !!window.opera,
        WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
        Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
    };
})();


(function() {
    XU.ui.dialog = function(params) {
        this.overlay = this.buildOverlay().appendTo("body");

        //_iframe : null,
        this._buttons = [];

        this.container = $('<div class="pop_dialog_div"/>').css({
            zIndex: 1000 + 2, position: 'absolute',
            width: 500,
            left: '50%',
            top: '20%',
            marginLeft: -(Math.ceil((500) / 2))
        }).append(this.buildHTML()).appendTo("body");

        this.header = $("#ui_dialog_header");
        this.body = $("#ui_dialog_body");
        this.footer = $("#ui_dialog_footer");
    };
    XU.ui.dialog.prototype = $.extend(XU.ui.dialog.prototype, {
        buildHTML: function() {
            return [
                '<table style="width: 100%; height: 100%;" class="pop_dialog_table">',
                    '<tbody>',
                        '<tr>',
                            '<td class="pop_topleft"></td>',
                            '<td class="pop_border"></td>',
                            '<td class="pop_topright"></td>',
                        '</tr>',
                        '<tr>',
                            '<td class="pop_border"></td>',
                            '<td class="pop_content">',
                                '<h2><span id="ui_dialog_header"></span></h2>',
                                '<div class="dialog_content">',
                                    '<div id="ui_dialog_body" class="dialog_body"></div>',
                                    '<div id="ui_dialog_footer" class="dialog_buttons"></div>',
                                '</div>',
                            '</td>',
                            '<td class="pop_border"></td>',
                        '</tr>',
                        '<tr>',
                            '<td class="pop_bottomleft"></td>',
                            '<td class="pop_border"></td>',
                            '<td class="pop_bottomright"></td>',
                        '</tr>',
                        '</tbody>',
                    '</table>'
            ].join('');
        },
        buildOverlay: function() {
            return $('<div style="background-color:#000;"/>').css({
                opacity: 30 / 100,
                height: '100%',
                width: '100%',
                position: 'fixed',
                left: 0,
                top: 0,
                zIndex: 1000 + 1
            });
        },
        addButton: function(html, callback) {
            var btn = new XU.ui.button(html);
            btn.registerEvent('click', this, callback);

            this._buttons.push(btn);
            this.footer.append(btn.instance);
        },
        setHeader: function(headerContent) {
            this.header.empty();
            this.header.append(headerContent);
        },
        setBody: function(bodyContent) {
            this.body.empty();
            this.body.append(bodyContent);
        },
        close: function() {
            this.container.hide().remove();
            this.overlay.hide().remove();
        }
    });
})();

(function() {
    XU.ui.button = function(html) {
        this.instance = $(html);
    };

    XU.ui.button.prototype = $.extend(XU.ui.button.prototype, {
        registerEvent: function(eventName, callbackObj, callbackFuc) {
            if (typeof callbackFuc != 'undefined') {
                this.instance.bind(eventName, function() {
                    callbackFuc.call(callbackObj);
                });
            }
        }
    });
})();

(function() {
    XU.ui.tabView = function(parms) {
        parms = $.extend({
            selectedClass: 'select',
            event: 'mouseover',
            mouseOverDelay: 0.2
        }, parms);

        this._currentTab = null;
        this._tabs = [];
        this._selectedClass = parms.selectedClass;
        this._event = parms.event;
        this._mouseOverDelay = parms.mouseOverDelay;
    };
    XU.ui.tabView.prototype = $.extend(XU.ui.dialog.prototype, {
        addTab: function(t) {
            t = $.extend({
                labelId: '',
                contentId: ''
            }, t);
            this._tabs.push(t);
            var This = this;
            if (this._event == 'mouseover') {
                var isMouseOn = true;
                var timer = null;
                
                $("#" + t.labelId).bind("mouseover", function(e) {
                    //if ( !isMouseOn ) return;
                    var f = function(){This.setCurrentTab(t);};
                    timer = setTimeout(f,This._mouseOverDelay*1000);
                    
                }).bind("mouseleave", function(e){
                    isMouseOn = false;
					if ( timer ) clearTimeout( timer );
                });
            }
            if(this._currentTab==null && this._tabs.length>0){
                this.setCurrentTab(this._tabs[0]);
            }
        },
        setCurrentTab: function(t) {
            var label = $("#" + t.labelId);
            var content = $("#" + t.contentId);
            if (t != this._currentTab) {
                if (this._currentTab != null) {
                    $("#" + this._currentTab.labelId).removeClass(this._selectedClass);
                    $("#" + this._currentTab.contentId).css("display", "none");
                }
            }
            this._currentTab = t;
            label.addClass(this._selectedClass);
            content.css("display", "block");
            return t;
        }
    });
})();