/**
 * TODO add documentation
 *
 * $Id: voltimum.message.js,v 1.2 2010-05-18 08:28:51 vladimir Exp $
 * @requires voltimum.jquery.js
 * @requires voltimum.jquery.jqdnr.js
 * @requires voltimum.jquery.dimensions.js
 */
voltimum = window.voltimum || {};

(function($){

    voltimum.Dialog = {
    
        renderWindow: function(x, y){
            // body...
            var _this = this;
            this.container = $(voltimum.Dialog.containerTemplate());
            this.container./*jqDrag('#voltimumHeader').*/find('.close-btn').bind('click', function(e){
                if (typeof _this.onclose == 'function') {
                    _this.onclose(_this.oncloseParams);
                }
                _this.close();
                if (typeof _this.callback == 'function') {
                    _this.callback(_this.params);
                }
            });
            
            var wdth = this.params.width || "300";
            _this.container.css({
                padding: "3px",
                width: wdth + "px",
                top: y,
                left: x,
                zIndex: 25000
            });
            
            $(document.body).append(_this.container);
            
            var cw = parseInt(_this.container.width(), 10);
            var ww = parseInt(_this.container.find('.messageContainer').width(), 10);

            if (cw < ww) {
                _this.container.css({
                    width: ww
                });
            }
            if(this.onopen && typeof this.onopen == 'function'){
            	this.onopen();
            }
            return _this;
        },
        
        close: function(){
            var _this = this;
            try {
                this.container.remove();
                voltimum.Dialog.current = null;
                $('#dialog_bg_underpopup').remove();
                $('html').css({"overflow":"auto"});
            } catch (e) {
            }
        },
        
        containerTemplate: function(){
            var topLabel = '';
            if (this.params && this.params.label) {
                topLabel = this.params.label;
            }
            
            return ''+
            '<div class="voltimumWinContainer" style="position:absolute;">' +
				'<div class="message-content">'+
					this.title+
				'</div>'+
				'<div class="close-btn">Close</div>'+
            '</div>';
        },
        
        
        
        loaded: false,
        
        current: null,
        
        open: function(e, title){
            //e=false; //      CCM-2268
            var x, y, min_x, min_y, max_x, max_y, popup_width = this.params.width || 200, popup_height = this.params.height || 200;
            var el = document.documentElement, b = document.body;
            min_x = $(window).scrollLeft();
            min_y = $(window).scrollTop();
            max_x = ($(window).width()) + ($(window).scrollLeft()) - popup_width;
            max_y = ($(window).height()) + ($(window).scrollTop()) - popup_height;
            
            if (!e) {
                x = $(window).scrollLeft() + $("#global-wrapper-outer").width() / 2 - popup_width / 2;
                y = $(window).scrollTop() + $("#global-wrapper-outer").height() / 2 - popup_height / 2;
            } else {
                x = e.clientX + $(window).scrollLeft();
                y = e.clientY + $(window).scrollTop();
            }
            
            if (this.offset) {
                x += this.offset.x;
                y += this.offset.y;
            }
            
            if (e) {
                if (x < min_x) {
                    x = min_x;
                }
                
                if (y < min_y) {
                    y = min_y;
                }
                
                if (x > max_x) {
                    x = max_x;
                }
                
                if (y > max_y) {
                    y = max_y;
                }
            }
            
            var _this = this;
            
            if (_this.current) {
                _this.current.close();
            }
            
            _this.current = _this.renderWindow(x, y);
          $("body").append("<div id='dialog_bg_underpopup'></div>")
          .find('#dialog_bg_underpopup').css({"height": ($(window).height()*2) + $(window).scrollTop() + "px"});
          $('html').css({"overflow":"hidden"});
        },
        
        message: function(e, content, callback, params, offset){
        	params = params || {};
            this.type = params.type || "window";
            this.title = content;
            this.callback = callback;
            this.params = params || {};
            this.loaded = true;
            var _this = this;
            this.offset = offset;
            _this.open(e);
            return false;
        }
        
    };
})(voltimum.jQuery);

