/*
class WPopup {
    
    public width;
    public height;
    public left;
    public right;
    
    public WPopup(title , content , name);
    
    public void open ();
    public void maximize();
    public void restore();
    public void close;
}

class WPopupAjax extends WPopup {

    public loaded;
    
    public WPopupAjax(title , url , name);
    public void load();
    public void setSize();
    
    //*************************** from WPopup *************************
    public void open ();
    public void maximize();
    public void restore();
    public void close();     
}
*/  
window.WPopup = function (title , content , name){
    //settings
    this.background_opacity = 50; // %
    this.width = null;
    this.height= null;
    this.left = null;
    this.top = null;
    this.buttons = [
        'close' /*, 'maximize'*/
    ];

    // ----------------------------------------
    this.title  = title;
    this.content = content;
    this.name = name;
    this.opened = false;     
    this.maximized = false;     
}

window.WPopup['SetActiveInstance'] = function(instance){
	WPopup['ActiveInstance'] = instance;
}

window.WPopup['GetActiveInstance'] = function(){
	return WPopup['ActiveInstance'];
}


window.WPopup.prototype.log = function(message){
	if (console && console.log){	
			try{
				console.log(message);
			}catch(e){}
		}
}


// actions here ------------------------------------------------------------------------------------------------
window.WPopup.prototype.onopen = function(){
		// *************************	nothing to do  *************************
		this.log("window opened!");
}

window.WPopup.prototype.onclose = function(){
	// *************************	nothing to do  *************************
	this.log("window closed!");		
}

window.WPopup.prototype.oncreate = function(){
	// *************************	nothing to do  *************************
	this.log("window created!");		
}

window.WPopup.prototype.ondestroyed = function(){
	// *************************	nothing to do  *************************
	this.log("window destroyed!");		
}

// actions here ------------------------------------------------------------------------------------------------

window.WPopup.prototype.setContent = function(content){
    this.content = content;
    this.element = null;
}   


window.WPopup.prototype.createView = function(){
	
	var scrollTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	var scrollLeft = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
	
	document.body.appendChild(this.getElement());
    
    /// open view
    if(!this.maximized){
        new Draggable(this.frame , {
            onEnd  : this.saveCoord.bind(this)
        });
    }

    var height = this.frame.getHeight();
    var width = this.frame.getWidth();
    var center = this.getCenter(width , height);

    if(this.top && !this.maximized)
        this.frame.style.top = this.top + 'px'; 
    else
        this.frame.style.top = (center.top+scrollTop) + 'px';
        
    if(this.left && !this.maximized){
        this.frame.style.left = this.left + 'px';
    }
    else {
        this.frame.style.left = (center.left+scrollLeft) + 'px';
    }
    
    //this.frame.style.left+=scrollLeft;
    //this.frame.style.top+=500;//scrollTop;
    

    this.frame.style.display = '';	
    
    this.oncreate();
}

window.WPopup.prototype.destroyView = function(){
	try{
        document.body.removeChild(this.getElement());    
    }catch(e){}
    this.ondestroyed();	
}

window.WPopup.prototype.open = function (){
    this.createView();
    // custom user action
    if (!this.opened){
    	this.onopen();
    	this.opened = true;
    }
    // save current instance 
    WPopup.SetActiveInstance(this);
}

window.WPopup.prototype.close = function (){
    this.destroyView();
    // custom user action
    if (this.opened){
    	this.onclose();
    	this.opened = false;   
    }
}

window.WPopup.prototype.saveCoord = function(){
    var pos = this.frame.positionedOffset();   
    this.left = pos[0];
    this.top = pos[1];
}



window.WPopup.prototype.maximize = function (){
    this.maximized = true;

    this.width_old = this.frame.getWidth();
    this.height_old = this.frame.getHeight();
    
    this.destroyView();
    this.element = null;
    
    var dimensions = document.viewport.getDimensions();
    this.width = dimensions.width;
    this.height = dimensions.height;
    
    this.createView();
}

window.WPopup.prototype.restore = function (){
    this.maximized = false;
    this.width = this.width_old;
    this.height = this.height_old;
    this.destroyView();
    this.element = null;
    this.createView();
}

// draw function      
window.WPopup.prototype.getElement = function (){
    if (! this.element) {
        this.element = this.draw();
    } 
    return this.element;
}

window.WPopup.prototype.draw = function (){
    this.frame = this.drawFrame();

    this.backgr= this.drawBackground();
    
    this.container = new Element('div');
    this.container.appendChild(this.backgr);
    this.container.appendChild(this.frame);
    
    return this.container;
}

window.WPopup.prototype.getCenter = function (width , height){
    var dimensions = document.viewport.getDimensions();
    
    return {
        left : (dimensions.width - width) / 2 , 
        top  : (dimensions.height - height) / 2 
    };
}

window.WPopup.prototype.drawFrame = function (){
    var extraStyles = '';
    if (this.width)extraStyles += 'width:' + parseInt(this.width) + 'px;';
    if (this.height)extraStyles += 'height:' + parseInt(this.height) + 'px;';
    
    var table = new Element('table' , {border : 0 ,
                                     style : 'position:absolute;display:none;' + extraStyles });
    table.className = 'popup_frame';
    table.cellPadding="0";
    table.cellSpacing="0";  
    var tbody = new Element('tbody');
    
    var trHeader = new Element('tr');
    var trBody = new Element('tr');
    
    var tdHeader = new Element('td');

    var tdBody = new Element('td');
    tdHeader.width = tdBody.width = '100%';
    // header
    tdHeader.vAlign = 'top';
    tdHeader.className = 'popup_header';
    tdHeader.appendChild (this.drawHeader());

    // body
    tdBody.vAlign = 'top';
    tdBody.appendChild (this.drawBody());
    tdBody.className = 'popup_body';

    trHeader.appendChild(tdHeader);
    trBody.appendChild(tdBody);

    tbody.appendChild(trHeader);
    tbody.appendChild(trBody);
    
    table.appendChild(tbody);

    return table;
}

window.WPopup.prototype.drawBody = function (){
    var div = new Element('div');
    div.innerHTML = this.content;   
    return div;
}

window.WPopup.prototype.drawHeader = function (){
    var table = new Element('table' , {border:0 , width : '100%'});
    table.cellPadding="0";
    table.cellSpacing="0";
    var tbody = new Element('tbody');
    var tr = new Element('tr');
    
    var tdTitle = new Element('td');
    var tdActions = new Element('td');
    
    tdTitle.onselectstart=function(){return false} // IE
    tdTitle.style.MozUserSelect="none" // FireFox
    
    tdTitle.innerHTML = this.title;
    tdTitle.className = 'popup_title';
    tdTitle.ondblclick= this.changeSize.bind(this);
    
    tdActions.align = 'right';
    tdActions.appendChild(this.drawActions());
    tdActions.className = 'popup_title';
    
    tr.appendChild (tdTitle);
    tr.appendChild (tdActions);
    
    tbody.appendChild (tr);
    table.appendChild (tbody);
    
    return table;
}

window.WPopup.prototype.changeSize = function (){
    
    if ( this.maximized ) {
        this.restore();
        
    } else {
        this.maximize();
    }
}

window.WPopup.prototype.drawActions = function (){
    var table = new Element('table');
    table.cellPadding="0";
    table.cellSpacing="0";
    
    var tbody = new Element('tbody');
    
    var tr = new Element('tr');
    
    // close
    var tdClose = new Element('td', {'class': 'popup_actions'});
    //var buttonClose = new Element('input' , {type : 'button' , value : 'X'});
    var buttonClose = new Element('img' , {src : 'module/Theme12/theme/img/gadget/max.gif'});
    buttonClose.onclick = this.close.bind(this);
    tdClose.appendChild (buttonClose);
    
    // miximize
             
    var tdMax = new Element('td');
    var buttonMax = new Element('input' , {type : 'button' , value : '[]'});
    buttonMax.onclick = this.maximize.bind(this);
    tdMax.appendChild (buttonMax);
    
    // restore
    var tdRest = new Element('td');
    var buttonRest = new Element('input' , {type : 'button' , value : 'r'});
    buttonRest.onclick = this.restore.bind(this);
    tdRest.appendChild (buttonRest);

    if(this.buttons.indexOf('maximize') >= 0){
        if (this.maximized){
            tr.appendChild (tdRest);
        }else{
            tr.appendChild (tdMax);
        }
    }
    if(this.buttons.indexOf('close') >= 0){
        tr.appendChild (tdClose);
    }

    tbody.appendChild (tr);
    table.appendChild (tbody);
    return table;
}

window.WPopup.prototype.changeOpacity = function (node , percent){
    var opacity = (Prototype.Browser.IE) ? "filter" : "opacity";
    percent = (Prototype.Browser.IE) ? "alpha(opacity=" + percent + ")" : percent/100;
    node.style[opacity] = percent;
}

window.WPopup.prototype.drawBackground = function (){
    var div = $(document.createElement('DIV'));
    div.className = 'popup_shader';
    this.changeOpacity(div , this.background_opacity);
    return div;
}

window.WPopupAjax = function (title , url , name){
    this.title = title;
    this.url = url;
    this.name = name; 
    
    this.loaded= false;
    this.popup = new WPopup(title , '' , name);            
}

window.WPopupAjax.prototype.load = function (){
    if (!this.loaded){
        new Ajax.Request(this.url, 
            {
                method    : 'get',
                onSuccess : this.bindData.bind(this)
            }
        );
    }else{
        this.popup.open(); 
    }
}
window.WPopupAjax.prototype.bindData = function(r){
    this.popup.setContent(r.responseText);  
    this.loaded = true;
    this.popup.open(); 
}
window.WPopupAjax.prototype.clearCache = function(){
    this.loaded = false;
}

/// delegation
/// restore , maximize , open , close
window.WPopupAjax.prototype.restore = function(){
    this.popup.restore();
}

window.WPopupAjax.prototype.maximize = function(){
    this.popup.maximize();
}

window.WPopupAjax.prototype.open = function(){
    this.load();
}

window.WPopupAjax.prototype.close = function(){
    this.popup.close();
}
window.WPopupAjax.prototype.setSize = function(width,height){
    this.popup.width = width;
    this.popup.height= height;
}