
var jsWin = Class.create();
jsWin.instance=function(){
	if(typeof(this.jswin)=='undefined'){
		this.jswin=new jsWin();
	}
	return(this.jswin);
}




jsWin.prototype = {

	element:null,
	_body:null,
	content:null,
	options:null,
	optionsDefault:null,

	initialize: function(options) {
		
		this.options=$H(options);
		
		this.optionsDefault = $H({
			modale:false,
			width:500,
			height:0
		});
		
		if(options=="fr_FR")
			this.element=Builder.node('div',{style:'display:none;',className:'jsWin'},[
			Builder.node('div',{className:'titleBar'},[
				Builder.node('div',{className:'title'},''),
				Builder.node('div',{className:'close', style:'cursor:hand;cursor:pointer;'},'Fermer')
			]),
			Builder.node('div',{className:'content'},''),
			Builder.node('div',{className:'footerBar', align:'right'},'')
			]);
		
		else
		
			this.element=Builder.node('div',{style:'display:none;',className:'jsWin'},[
			Builder.node('div',{className:'titleBar'},[
				Builder.node('div',{className:'title'},''),
				Builder.node('div',{className:'close', style:'cursor:hand;cursor:pointer;'},'close')
			]),
			Builder.node('div',{className:'content'},''),
			Builder.node('div',{className:'footerBar', align:'right'},'')
			]);
		
		
		
		this._body=$$('body')[0];
		this._body.appendChild(this.element);
		
		this.content = Element.down(this.element,('div.content'));
		
		this.setWidth(this._getOption('width'));
		this.setHeight(this._getOption('height'));
		
		
		this.element.down('div.close').onclick=function(e){this.hide()}.bind(this);
		
		
		Event.observe(window,'resize',function(e){
			this.center();
		}.bind(this));
	
	
	},
	
	_getOption: function(name){
		if(typeof(this.options.get(name))!='undefined'){
			return(this.options.get(name));
		}
		return(this.optionsDefault.get(name));
	},
	
	setCloseType: function(type){
		if(type=='remove'){
			this.element.down('div.close').onclick=function(e){this.remove()}.bind(this);
		}else{
			this.element.down('div.close').onclick=function(e){this.hide()}.bind(this);
		}
	},
	
	center: function(){
		var elSize		= this.element.getDimensions();
		var viewportSize	= document.viewport.getDimensions();
		var scrollValue		= document.viewport.getScrollOffsets();
		
		var t = Math.round((viewportSize["height"] - elSize["height"])/2)// + scrollValue[1];
		var l = Math.round((viewportSize["width"] - elSize["width"])/2) + scrollValue[0];
		
		this.element.setStyle({ top:t+"px", left:l+"px" });
		
	},
	
	show: function(){
		this.center();
		Element.show(this.element);
	},
	
	hide: function(){
		Element.hide(this.element);
	},
	
	remove: function(){
		this._body.removeChild(this.element);
		delete(jsWin.jswin);
	},
	
	setWidth: function(w){
		w = Math.min(document.viewport.getWidth()-80, w);
		Element.setStyle(this.element,{width:parseInt(w)+10+"px"});
	},
	
	setHeight: function(h){
		h = Math.min(document.viewport.getHeight()-80, h);
		if(parseInt(h)!= 0){
			Element.setStyle(this.content,{height:parseInt(h)+"px"});
		}else{
			Element.setStyle(this.content,{height:'auto'});
		}
	},
	
	setSize: function(w, h){
		this.setWidth(w);
		this.setHeight(h);
	},
	
	maximize: function(){
		var marginWidth=90;
		var marginHeight=200;
		var viewportSize	= document.viewport.getDimensions();
		this.setWidth(viewportSize['width']-marginWidth);
		this.setHeight(viewportSize['height']-marginHeight);
		this.center();
	},
	
	setTitle: function(txt){
		this.element.down('div.title').update(txt);
	},
	
	setContent: function(data){
		this.content.update(data);
	},

	setFooter: function(txt){
		this.element.down('div.footerBar').update(txt);
	}
	
}
