var PageLoader = new Class({
	stack: 0,
	locked: 0,
	
	loading: function() {
		this.adjust();
		this.stack++;
		this.act();
	},
	
	loaded: function() {
		this.stack--;
		this.act();
	},
	
	toggle: function(bool) {
		if(bool === true)
			this.loading();
		else if(bool === false)
			this.loaded();
	},
	
	lock: function() {
		this.adjust();
		this.locked++;
		this.act();
	},
	
	unlock: function() {
		this.locked--;
		this.act();
	},
	
	toggleLocked: function(bool) {
		if(bool === true)
			this.lock();
		else if(bool === false)
			this.unlock();
	},
	
	act: function() {
		if(this.locked > 0)
			$('loader')
				.addClass('locked')
				.removeClass('noshow');
		else if(this.stack > 0)
			$('loader')
				.removeClass('locked')
				.removeClass('noshow');
		else
			$('loader')
				.removeClass('locked')
				.addClass('noshow');
	},
	
	adjust: function() {
		if(!(Browser.Engine.trident  && Browser.Engine.version <= 4))
			return;
		var 
			scroll = window.getScroll(),
			coords = window.getCoordinates();
		$('loader')
			.getElement('span')
			.setStyles({
				height: coords.height,
				width: coords.width
			});
		[
			$('loader').getElement('span'),
			$('loader').getElement('div')
		].each(function(o) {
			o.setStyles({
				top: scroll.y,
				left: scroll.x
			});
		});
	}
});
var Loader = new PageLoader();

var message = new Class({
	klass: 'message',
	initialize: function(msg) {
		if(Browser.Engine.trident) {
			alert(msg);
			return;
		}
		Loader.lock();
		$('popup')
			.getElement('span')
			.set('text', msg);
		$('popup')
			.removeClass('message')
			.removeClass('success')
			.removeClass('error');
		$('popup')
			.addClass(this.klass);
		$('popup')
			.removeClass('noshow')
			.center();
		try {
			DD_belatedPNG.fix('.png');
		} catch(err) { }
	}
});
var success = new Class({
	Extends: message,
	klass: 'success'
});
var error = new Class({
	Extends: message,
	klass: 'error'
});

/*
	Extending MooTools natives
*/
Element.implement({
	center: function() {
		var coords = {
			self: this.getCoordinates(),
			win: window.getCoordinates()
		};
		this.setStyles({
			top: ((coords.win.height / 2) - (coords.self.height / 2) + window.getScroll().y) + 'px',
			left: ((coords.win.width / 2) - (coords.self.width / 2) + window.getScroll().x) + 'px'
		});
		return this;
	},
	
	hovering: function() {
		var attributes = {
			coords: this.getCoordinates(),
			pos: this.getPosition()
		}, scroll = window.getScroll();
		attributes.pos = {
			x: attributes.pos.x - scroll.x,
			y: attributes.pos.y - scroll.y
		};
		if(
			(mouse.x >= attributes.pos.x && mouse.x <= attributes.pos.x + attributes.coords.width) &&
			(mouse.y >= attributes.pos.y && mouse.y <= attributes.pos.y + attributes.coords.height)
		)
			return true;
		return false;
	}
});

var mouse = {
	x: false,
	y: false
};
document.addEvent('mousemove', function(e) {
	mouse = Event(e).client;
});

window.addEvents({
	'domready': function() {
		try {
			DD_belatedPNG.fix('.png');
		} catch(err) { }
	},
		
	'resize': function() {
		Loader.adjust();
	},
	
	'scroll': function() {
		Loader.adjust();
	}
});