var PopUp = new Class({
	
	Implements: Element,
	initialize: function(){
		this.count = 0;
		this.duration = 200;
		this.overlayOpacity = 0.7;
		this.pop = false;
		this.popTitle = false;
		this.popWrap = false;
		this.popSmall = false;
		this.popImage = false;
		this.overlay = false;
		this.prepareHTML();
		this.setEvents();
	},
	
	Ajax: function(title,url,width,height,dur){
		
		if (!dur) dur = this.duration;
		
		if (this.pop) {
			this.resize(width,height);
			this.popWrap.AjaxUpdate(url);
			this.popTitle.set('html', title);
			return false;
		}
		
		this.addCount();
		this.setOverlay();
		
		if (this.windowWidth < width) width = this.windowWidth - 50;
		if (this.windowHeight < height) height = this.windowHeight - 50;
		
		//this.pop = this.popHTML.clone();
		
		this.pop = this.popHTML.clone().setStyles({
			display: '',
			opacity: 0,
			width: width,
			marginLeft: -(width / 2),
			marginTop: -( (height / 2) + 50)
		});
		
		this.popHead = this.popHeadHTML.clone();
		this.popWrap = this.popWrapHTML.clone().setStyle('height', height);
		this.popTitle = this.popTitleHTML.clone().set('html', title);
		var closeLink = this.closeLinkHTML.set('html', 'Lat aftur');	
		closeLink.addEvent('click', function(e) {
				e.preventDefault();
				this.killOverlay();
				this.kill('pop');
			}.bindWithEvent(this)
		);
		
		this.popHead.adopt(closeLink,this.popTitle);
		this.pop.adopt(this.popHead,this.popWrap);
		
		$(document.body).adopt(this.pop);
		new Fx.Tween(this.pop, {duration: dur } ).start('opacity', 0, 1);
		this.popWrap.AjaxUpdate(url);
	},
	
	Notice: function(message){
		
		this.smallpopWrapHTML.set('html', '<p>' + message + '</p>');
		this.smallpopHTML.adopt(this.smallpopWrapHTML);
		
		this.popSmall = this.smallpopHTML.setStyles({
			marginLeft: -150,
			marginTop: -(Math.round(this.smallpopHTML.getStyle('height')) / 2),
			display: '',
			opacity: 0,
			width: 300
		});
		
		$(document.body).adopt(this.smallpopHTML);
		
		new Fx.Tween(this.popSmall, {duration: 300 } ).start('opacity', 0, 1);
		var remove = new Fx.Tween(this.popSmall, {
			duration: 300,
			onComplete: function(){
				this.popSmall.destroy();
				this.popSmall = false;
			}.bindWithEvent(this)
		});
			
		remove.start.delay(1500, remove, ['opacity', 1, 0]);
		
	},
	
	setOverlay: function(){
		
		if (this.overlay) return false;
		
		this.getPageSize();
		
		this.overlayHTML.setStyles({
			height: this.pageHeight,
			width: this.pageWidth,
			display: ''
		});
		
		new Fx.Tween(this.overlayHTML, {duration: Math.round(this.duration / 2)}).start('opacity', 0, this.overlayOpacity);
		this.overlay = true;
	},
	
	killOverlay: function(){
		
		this.delCount();
		
		if (this.count == 0) {
			new Fx.Tween(this.overlayHTML, {
				duration: Math.round(this.duration * 1.3),
				onComplete: function(){
					this.overlay = false;
				}.bindWithEvent(this)
			}).start('opacity', this.overlayOpacity, 0);
		}
	},
	
	kill: function(what,dur){
		if (!what) what = 'pop';
		if (!dur) dur = this.duration;
		
		eval('var pop = this.' + what + ';');
		
		if (pop) {
			this.killOverlay();
			new Fx.Tween(pop, {
					duration: dur,
					onComplete: function(){
						pop.destroy();
						pop = false;
					}.bindWithEvent(this)
			}).start('opacity', 1, 0);
		}
		
		return false;
	},
	
	delCount: function(){
		this.count = this.count - 1;
	},
	
	addCount: function(){
		this.count = this.count + 1;
	},
	
	getPageSize: function(){
	
		var xScroll = $(window).getScrollWidth();
		var yScroll = $(window).getScrollHeight();
		
		this.windowHeight = $(window).getHeight();
		this.windowWidth = $(window).getWidth();
		
		if(yScroll < this.windowHeight){
			this.pageHeight = this.windowHeight;
		} else { 
			this.pageHeight = yScroll;
		}
	
		if(xScroll < this.windowWidth){	
			this.pageWidth = xScroll;		
		} else {
			this.pageWidth = this.windowWidth;
		}
	
	},
	
	Resize: function(width,height){
		if (this.pop) {
			new Fx.Styles(this.popWrapHTML, {duration: this.duration, transition: Fx.Transitions.linear}).start({'height': height});
			new Fx.Styles(this.pop, {duration: this.duration, transition: Fx.Transitions.linear}).start({
				'width': width,
				'margin-left': -(width / 2),
				'margin-top': -( (height / 2) + 50)
			});
		}
	},
	
	prepareHTML: function(){
		this.overlayHTML = ($('Overlay')) ? $('Overlay') : new Element('div', { 'id': 'Overlay', 'styles': { 'opacity': 0 } }).inject($(document.body));
		if (!this.popHTML) this.popHTML = new Element('div', { 'class': 'popup' });
		if (!this.popWrapHTML) this.popWrapHTML = new Element('div', { 'class': 'wrap', 'styles': { 'overflow': 'auto' } });
		if (!this.popHeadHTML) this.popHeadHTML = new Element('div', { 'class': 'head'} ); 
		if (!this.popTitleHTML) this.popTitleHTML = new Element('span', { 'class': 'title' });
		
		this.smallpopHTML = new Element('div', { 'id': 'PopUpSmall' });
		this.smallpopWrapHTML = new Element('div', { 'class': 'wrap' });
		this.closeLinkHTML = new Element('a', {'href': '#', 'class': 'close' });
	},
	
	setEvents: function(){
		$$([this.overlayHTML,this.closeLinkHTML]).addEvent('click', function(e){
			e.preventDefault();
			if (this.overlay) this.killOverlay();
			if (this.pop) this.kill();
		}.bind(this));
	}
	
});

var popHTML;
var popWrapHTML;

function popupAjax(title,url,width,height,dur)
{
	if (!width) width = 300;
	if (!height) height = 200; 
	if (!dur) dur = 200;
	
	if (popHTML) {
		popupResize(width,height);
		popWrapHTML.opacityUpdate(url);
		popTitleHTML.set('html', title);
		return false;
	}
	
	insertOverlay();
	
	$$('.openx').setStyle('visibility', 'hidden');
	
	popHTML = new Element('div', { 'class': 'popup'});
	popHeadHTML = new Element('div', { 'class': 'head'} ).inject(popHTML);
	popCloseHTML = new Element('a', {'href': '#', 'class': 'close', 'events': { 'click': function(e){ e.preventDefault(); popupKill(); }} }).set('html', 'Lat aftur').inject(popHeadHTML);
	popTitleHTML = new Element('span', { 'class': 'title' }).set('text', title).inject(popHeadHTML);
	popWrapHTML = new Element('div', { 
		'class': 'wrap',
		'styles': {
			'overflow': 'auto',
			'height': height
		}
	}).inject(popHTML);
	
	winWidth = $(window).getWidth();
	winHeight = $(window).getHeight();
	
	if (winWidth < width) width = winWidth - 50;
	if (winHeight < height) height = winHeight - 50;
	
	var fadePop = popHTML.setStyles({
		'display': 'block',
		'opacity': 0,
		'width': width,
		'margin-left': -(width / 2),
		'margin-top': -( (height / 2) + 50)
	}).inject($(document.body));
	
	new Fx.Tween(fadePop, {duration: dur } ).start('opacity', 0, 1);
		
	popWrapHTML.opacityUpdate(url);
	
}

function popupResize(width,height)
{
	if (popHTML) {
		
		winWidth = $(window).getWidth();
		winHeight = $(window).getHeight();
		
		if (winWidth < width) width = winWidth - 50;
		if (winHeight < height) height = winHeight - 50;
		
		popWrapHTML.setStyle('overflow', 'hidden');
		new Fx.Morph(popWrapHTML, {duration: 200, transition: Fx.Transitions.linear}).start({'height': height});
		new Fx.Morph(popHTML, {duration: 200, transition: Fx.Transitions.linear, onComplete: function(){popWrapHTML.setStyle('overflow', 'auto');}}).start({
			'width': width,
			'margin-left': -(width / 2),
			'margin-top': -( (height / 2) + 50)
		});
	}
}

function popupKill(dur)
{
	if (!dur) dur = 200;
	
	overlayKill(dur,true);
	smallpopKill();
	
	if (popHTML) {
		new Fx.Tween(popHTML, {
				duration: dur,
				onComplete: function(){
					popHTML.destroy();
					popHTML = false;
				}
		}).start('opacity', 1, 0);
	}
	
	$$('.openx').setStyle('visibility', 'visible');
	
	return false;
}

window.addEvent('domready', function(){
	
	
	
	//popupAjax('Test jį', '/ajax/output.php?section=bloggar&second=register', 400, 200);
	//Pop = new PopUp();
	
	//Pop.setOverlay();
	//Pop.Notice('Yes! Žetta virkar.');
	//Pop.Ajax('Test jį', '/ajax/output.php?section=bloggar&second=register', 400, 200);
});