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: 676,
	      	height: 267,
	      	border_padding: 26,
	      	method: "get",
	     	header: true,
	      	center: false,
	      	onLoad: null,
	      	draggable: false,
	      	destroyOnClose:true,
	      	loadingImg: null
	    }
	    Object.extend(this.options, options || {});

		this.popup = document.createElement('div');
		this.popup.id = 'popup';
		Element.addClassName(this.popup,"popup");

		if (this.options.header) {

			//var popupBg = document.createElement("div");
			//Element.addClassName(popupBg,"bg");
			//this.popup.appendChild(popupBg);

			//var header = document.createElement("div");
			//Element.addClassName(header,"header");

			//this.popup.appendChild(header);

			//var popupClose = document.createElement("a");
			//Element.addClassName(popupClose,"close");
			//popupClose.innerHTML = "zamknij";
			//header.appendChild(popupClose);
			//Event.observe(popupClose, "click", this.closePopup.bindAsEventListener(this));

			//var lu = document.createElement("img");
			//lu.setAttribute("src","/gfx/popup/uwaga.gif");
			//header.appendChild(lu);

			//var popupHText = document.createTextNode("Uwaga");
			//header.appendChild(popupHText);

			this.popupContent = document.createElement("div");
			//Element.addClassName(this.popupContent,"content");
			this.popup.appendChild(this.popupContent);

/*
			var header = document.createElement("div");
			Element.addClassName(header,"popupHeader");
			this.popup.appendChild(header);

			el = document.createElement("div");
			Element.addClassName(el,"popupClose");
			el.innerHTML = "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,"popup");
		Element.hide(this.popup);
//		this.popup.appendChild(this.popup);

		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';
			if(this.options.height > (frameHeight * 0.8)) {
				this.options.height = Math.ceil((frameHeight * 0.8));
				this.popup.style.height = this.options.height + "px";
			}
			var centerHeight = Math.ceil((frameHeight - this.options.height)/2) + realScrolledArray[1] - this.options.border_padding + 'px';
			this.popup.style.top = centerHeight;
	  	}
		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 - 800)/2+realScrolledArray[0] + 'px';
		this.popup.style.top = (frameHeight - 667)/2+realScrolledArray[1] + 'px';
		if (typeof centerHeight != "undefined"){
			this.popup.style.top = centerHeight;
		}
	  	// are you a stupid user using Internet Explorer?
	  	if (this.popup.getClientRects) {
	  		this.hideSelectsUnderPopup();
	  	}
		new Effect.Appear(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,
				onSuccess: 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];
	}
}
