var RendererAbstract = new Class({

    initialize: function(options) {
        this.setOptions(options);
    },

    /**
		@brief Sugeneruoja lango turinį ir jį grąžina.
		
		@return Object elementas, kurį galima įdėti į langą.
	*/
    render: function() {
        throw new Exception("RendererAbstract: this is an abstract class, you should implement it!");
		
        return null;
    },
	
    onshow: function() {
		
    },
	
    onhide: function() {
		
    },
	
    close: function() {
        this.options.parent.close.bind(this.options.parent);
        this.options.parent.close();
    }

});
RendererAbstract.implement(new Options);

var Dialog = new Class({

    Implements: Options,
	
    options: {
        renderer: 	null,				// objektas, kuris paišys lango turinį.
        title:		"",					// lango antraštė.
        onclose:	null,				// ką daryti išjungus.
        id:			"dialog1",			// dialogo konteinerio id.
        width:		"350px",
        top:		"20%"
    },
	
    container: null,
    overlay: null,
	
    workspace: null,
	
    initialize: function(options) {
        this.setOptions(options);
		
        this.workspace = new Array();
		

        this.workspace['width'] = document.body.clientWidth ? document.body.clientWidth : window.innerWidth;
        this.workspace['height'] = document.body.clientHeight ? document.body.clientHeight : window.innerHeight;
		
        this.render();
        this.close();
    },
	
    open: function() {
        this.overlay.className = "mask show";
        this.overlay.style.display = "block";
        this.overlay.style.position = "fixed";
		
        this.container.style.display = "block";
		
        this.options.renderer.onshow();
    },
	
    close: function() {
        this.overlay.className = "hide";
        this.overlay.style.display = "none";
        if(this.options.onclose != null) {
            this.options.onclose();
        }
		
        this.container.style.display = "none";
		
        this.options.renderer.onhide();
    },
	
    render: function() {
        this.container = new Element('div');
				jQuery(this.container).addClass("yui-panel-container yui-dialog shadow").css({
                'width': this.options.width,
                'top':  this.options.top,
                'left': ((this.workspace['width'] / 2) - (320 / 2)),
                'position': 'absolute',
                'visibility': 'visible',
                'z-index': 101
            });
				
        this.container.style.display = "none";
		
        this.renderOverlay();
        this.renderDialog();
		
        this.container.inject(document.body, 'top');
    },
	
    renderOverlay: function() {
        this.overlay = new Element('div');
				this.overlay.id = "overlay";
				jQuery(this.overlay).addClass("mask").css({
                'width': "100%",
                'height': "100%",
                'position': "fixed",
                'top': "0px",
                'left': "0px",
                'z-index': "100"
            });
        this.overlay.addEvent('click', this.close.bind(this));
        this.overlay.inject(document.body, 'top');
    },
	

    getPageSize: function() {
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY) {  
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;
        
        if (self.innerHeight) { // all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth; 
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }   
        
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else { 
            pageHeight = yScroll;
        }
    
        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){  
            pageWidth = xScroll;        
        } else {
            pageWidth = windowWidth;
        }

        return [pageWidth,pageHeight];
    },  
  
  
    renderDialog: function() {
        var dialog = new Element('div');
				dialog.id = this.options.id;
				jQuery(dialog).addClass("yui-module yui-overlay yui-panel").css({
                'width': this.options.width,
                'top': "0px",
                'visibility': 'visible',
                'position': "relative"
            });
		
        var divHd = new Element('div');
				jQuery(divHd).addClass("hd");
			
        var divTl = new Element('div');
				jQuery(divTl).addClass("tl");
				
        var spanTitle = new Element('span');
        spanTitle.appendText(this.options.title);
				
        var divTr = new Element('div');
				jQuery(divTr).addClass("tr");
				
        divTl.inject(divHd);
        spanTitle.inject(divHd);
        divTr.inject(divHd);
			
        divHd.inject(dialog);
		
        var divBd = new Element('div');
				jQuery(divBd).addClass("bd");
						
        var divBorder = new Element('div');
				jQuery(divBorder).addClass("bord");
				
        this.options.renderer.setOptions({
            parent: this
        });
        var contents = this.options.renderer.render();
        contents.inject(divBorder);
				
        divBorder.inject(divBd);
			
        divBd.inject(dialog);
		
		
        var divFt = new Element('div');
				jQuery(divFt).addClass("ft");
        divFt.innerHTML = "&nbsp;";
			
        divFt.inject(dialog);
		
        var divClose = new Element('div');
				jQuery(divClose).addClass("container-close");
        divClose.addEvent('click', this.close.bind(this));
			
        divClose.inject(dialog);
		
        dialog.inject(this.container);
    }

});

