/*
	Extending MooTools natives
*/
Element.implement({
	center: function(opts) {
		opts = opts || {};
		var coords = {
			self: this.getCoordinates(),
			win: window.getCoordinates()
		};
		if(!$type(opts.top) || opts.top === true)
			this.setStyle('top', ((coords.win.height / 2) - (coords.self.height / 2) + window.getScroll().y) + 'px');
		if(!$type(opts.left) || opts.left === true)
			this.setStyle('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;
	},
	
	// used within loading and loaded
	loadCheck: function() {
		if(!$chk(this.retrieve('loadCount'))) {
			this.store('loadCount', 0);
			var cont = new Element('div')
				.addClass('element-loader')
				.addClass('noshow')
				.inject(document.body);
			this.store('loadContainer', cont);
		}
		return this;
	},
	
	loading: function() {
		this.loadCheck();
		this.store('loadCount', this.retrieve('loadCount') + 1);
		if(this.retrieve('loadCount').toInt() > 0) {
			this
				.retrieve('loadContainer')
				.setStyles(this.getCoordinates())
				.removeClass('noshow');
		}
		return this;
	},
	
	loaded: function() {
		this.loadCheck();
		this.store('loadCount', this.retrieve('loadCount') - 1);
		if(this.retrieve('loadCount').toInt() <= 0) {
			this
				.retrieve('loadContainer')
				.addClass('noshow');
		}
		return this;
	}
});

var message = new Class({
	klass: 'message',
	initialize: function(msg) {
		try {
			$clear($('message').retrieve('hiatus'));
			$('message').dispose();
		} catch(err) { }
		var el = new Element('span', {
			id: 'message',
			text: msg
		})
			.addClass(this.klass);
		var fx = new Fx.Tween(el, {
			duration: 380,
			transition: Fx.Transitions.Sine.easeOut,
			onComplete: function() {
				this.dispose();
			}.bind(el)
		});
		el
			.store('hiatus', function() {
				this.start('opacity', 0);
			}
				.bind(fx)
				.delay(4200))
			.inject(document.body)
			.center({
				top: false
			});
		if(Browser.Engine.trident && Browser.Engine.version <= 4)
			try {
				$('message').setStyle('top', window.getScroll().y);
			} catch(err) { }
	}
});
var success = new Class({
	Extends: message,
	klass: 'success'
});
var error = new Class({
	Extends: message,
	klass: 'error'
});

var mouse = {
	x: false,
	y: false
};
document.addEvent('mousemove', function(e) {
	mouse = Event(e).client;
});

window.addEvent('domready', function() {
	try {
		DD_belatedPNG.fix('.png');
	} catch(err) { }
	
	// setting up overtexts
	$$('.overText').each(function(o) {
		new OverText(o);
	});
	OverText.update();
	
	// used to correct comments in IE
	if(Browser.Engine.trident && Browser.Engine.version <= 4)
		window.addEvent('scroll', function() {
			try {
				$('message').setStyle('top', window.getScroll().y);
			} catch(err) { }
		});
});
