var _popupmodal;
var popupmodal = Class.create();
popupmodal.prototype = {
	initialize: function(popupName) {
		try{
			this.selectes = $$("select");
		}catch(err){
			this.selectes = new Array();
		}
		this.popupName 	= (popupName) ? popupName : 'Popup';
		this.timeout	= null;
		this.mask 		= $('Mask') 			? $('Mask') 			: null;
		this.popup 		= $(this.popupName) 	? $(this.popupName) 	: null;
		Event.observe(window,'resize',this.updateResize.bind(this));
		Event.observe(window,'scroll',this.updateResize.bind(this));
		Event.observe(this.mask,'click',this.hide.bind(this));
		Event.observe(window, 'keypress', this.keyPressCapture.bind(this), false);
	},
	
	keyPressCapture:function(e){
		var cKeyCode = e.keyCode || e.which;
		if (cKeyCode == 27){ // ESCAPE
			this.hide();
		}
	},
	updateResize:function(){
		var dim  =  this.popup.down(0).getDimensions();
		var size = this.getDimensions(); 
		//var scr  = $(document.body).getDimensions();
		var scr  = this.getDimensions();
		if(size.width!=0 && size.height){
			$(this.popup).setStyle({
				 top : $(document.body).cumulativeScrollOffset()[1]+(size.height/2-dim.height/2)+"px"
				,left: (size.width/2-dim.width/2)+"px"
			});
			$('Mask').setStyle({
				 top : $(document.body).cumulativeScrollOffset()[1]+"px"
				,width:Math.round(scr.width)+"px"
				,height:Math.round(scr.height)+"px"
			});
		}
	},
	getDimensions:function() {
		var dimensions = { }, B = Prototype.Browser;
		$w('width height').each(function(d) {
			var D = d.capitalize();
			if (B.WebKit && !document.evaluate) {
				// Safari <3.0 needs self.innerWidth/Height
				dimensions[d] = self['inner' + D];
			} else if (B.Opera && parseFloat(window.opera.version()) < 9.5) {
				// Opera <9.5 needs document.body.clientWidth/Height
				dimensions[d] = document.body['client' + D]
			} else {
				dimensions[d] = document.documentElement['client' + D];
			}
		});
		return dimensions;
	},
	
	selectDisplay:function(view){
		for(x=0;x<this.selectes.length;x++){
			if(view==true){
				this.selectes[x].show();
			}else{
				this.selectes[x].hide();
			}
		}
		var s = $$('#'+this.popupName+' select');
		for(x=0;x<s.length;x++)
			s[x].show();
	},
	
	show:function(){
		this.selectDisplay(false);
		if(this.mask)
			this.mask.show();
		if(this.popup)
			this.popup.show();
		this.updateResize(this);
	},
	hide:function(){
		this.selectDisplay(true);
		if(this.mask)
			this.mask.hide();
		if(this.popup)
			this.popup.hide();
	}
};

