var Popup = Class.create();
Popup.prototype = {
	popup: null,
	popupLoading: null,
	popupLoadingImg: null,
	popupContent: null,
	iframe: null,
	hiddenSelects: null, // for correcting IE stupidity of having selects on top of everything
	initialize: function(options) {
	    this.options = {
	      	url: '#',
	      	width: 300,
	      	height: 300,
	      	method: "get",
	     	header: true,
	      	center: true,
	      	onLoad: null, 
	      	draggable: false,
	      	destroyOnClose:true,
	      	loadingImg: null
	    }
	    Object.extend(this.options, options || {});

		this.popup = document.createElement('div');

		if (this.options.header) {
			var header = document.createElement("div");
			Element.addClassName(header,"popupHeader");
			this.popup.appendChild(header);
			
			el = document.createElement("div");
			Element.addClassName(el,"popupClose");
			el.innerHTML = "zamknij X";
			header.appendChild(el);
			Event.observe(el, "click", this.closePopup.bindAsEventListener(this));
			
			
			el = document.createElement("br");
			el.setAttribute("clear","all");
			header.appendChild(el);
			if (this.options.draggable) {
			    new Draggable(this.popup,{
			    	handle:header,
					starteffect: null , 
					endeffect: null 
			          	});
			}
			
			if (this.options.loadingImg) {
				this.popupLoadingImg = new Image();
				this.popupLoadingImg.src = this.options.loadingImg;
				this.popupLoading = document.createElement("div");
				Element.addClassName(this.popupLoading,"popupLoading");
				header.appendChild(this.popupLoading);
			}
		}
		
		this.popupContent = document.createElement("div");
		Element.addClassName(this.popupContent,"popupContent");
		Element.hide(this.popup);
		this.popup.appendChild(this.popupContent);
		
		document.getElementsByTagName('body')[0].appendChild(this.popup);
		this.show();
		this.loadContent();
	},
	startLoading: function() {
		if (this.popupLoading && this.popupLoadingImg) {
			this.popupLoading.appendChild(this.popupLoadingImg);
		}
	},
	stopLoading: function() {
		if (this.popupLoading) {
			this.popupLoading.innerHTML = "";
		}
	},
	show: function() {	
	  	this.popup.style.width = this.options.width +"px";
	  	this.popup.style.height = this.options.height +"px";
	  	if (this.options.center) {
			Element.addClassName(this.popup,"popup");
			if (self.innerWidth) {
				frameWidth = self.innerWidth;
				frameHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientWidth) {
				frameWidth = document.documentElement.clientWidth;
				frameHeight = document.documentElement.clientHeight;
			} else if (document.body) {
				frameWidth = document.body.clientWidth;
				frameHeight = document.body.clientHeight;
			}			
			var realScrolledArray = this.realScrolled();
			this.popup.style.left = (frameWidth - this.options.width)/2+realScrolledArray[0] + 'px';
			this.popup.style.top = (frameHeight - this.options.height)/2+realScrolledArray[1] + 'px';
	  	}
	  	// are you a stupid user using Internet Explorer?
	  	if (this.popup.getClientRects) {
	  		this.hideSelectsUnderPopup();
	  	}
		Element.show(this.popup);
	},
	hideSelectsUnderPopup: function() {
		this.hiddenSelects = new Array();
  		var windowScrollY = 0;
		var windowScrollX = 0;
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			windowScrollY = document.body.scrollTop;
			windowScrollX = document.body.scrollLeft;
		}
		var selectBoxes = document.getElementsByTagName('select');			
		var left = parseInt(this.popup.style.left, 10) - windowScrollX;
		var top = parseInt(this.popup.style.top, 10) - windowScrollY;			
		//alert('Y: '+windowScrollY+'| X: '+windowScrollX+' | top: '+ top+' | parsedTop: '+parseInt(this.popup.style.top, 10));
		var right = left + this.options.width;
		var bottom = top + this.options.height;
		for (var i = 0; i < selectBoxes.length; i++) {
			if (selectBoxes[i].getClientRects) {
				var rects = selectBoxes[i].getClientRects();
				for (var j = 0; j < rects.length; j++) {
					if (rects[j].top < bottom
						&& top < rects[j].bottom 
						&& rects[j].left < right
						&& left < rects[j].right && selectBoxes[i].style.visibility != 'hidden') {
							selectBoxes[i].style.visibility = 'hidden';
							this.hiddenSelects[this.hiddenSelects.length] = selectBoxes[i];
							break;
					}
				}
			}
		}
	},
	unHideSelectsUnderPopup: function() {
		if (this.hiddenSelects) {
			for (var i = 0; i < this.hiddenSelects.length; i++) {
	   			this.hiddenSelects[i].style.visibility = 'visible';		
			}
		}
	},
	closePopup: function() {
		if (this.options.destroyOnClose) {
			Element.remove(this.popup);	
			this.popup = null;
		} else {
			Element.hide(this.popup);
		}
		this.unHideSelectsUnderPopup();
	},
	loadContent: function(url) {
			this.startLoading();
			new Ajax.Updater(this.popupContent,url || this.options.url, {
				method: this.options.method, 
				evalScripts: true,
				onComplete: this.onLoadedContent.bind(this),
				onFailure: this.onError.bind(this)
			});
	},
	onLoadedContent: function() {
			this.popupContent.style.visibility = "";
			this.stopLoading();
			if (this.options.onLoad) {
				this.options.onLoad();
			}
	  		// gemius
	},
	onError: function() {
		this.stopLoading();
		showError();
	},
	realScrolled: function() {
		var x,y;
		if (self.pageYOffset) // all except Explorer
		{
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		return [x, y];
	}
}
