MooTools.More = {
	'version': '1.2.3.1'
};

/*
	License:
		MIT-style license.

	Authors:
		Valerio Proietti
		Christoph Pojer

	Modified for Anyspace by:
		Olivier Keun
*/

var Tips = new Class({

	Implements: [Events, Options],

	options: {
		onShow: function(tip){
			tip.setStyle('visibility', 'visible');
		},
		onHide: function(tip){
			tip.setStyle('visibility', 'hidden');
		},
		text: function(el){
			return el.get('rel') || el.get('href');
		},
		showDelay: 100,
		hideDelay: 100,
		className: 'thumbHover',
		offset: {x: 5, y: -105},
		fixed: false
	},

	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		if (params.options && params.options.offsets) params.options.offset = params.options.offsets;
		this.setOptions(params.options);
		this.container = new Element('div', {'class': 'tip'});
		this.tip = this.getTip();
		
		if (params.elements) this.attach(params.elements);
	},

	getTip: function(){
		return new Element('div', {
			'class': this.options.className,
			styles: {
				visibility: 'hidden',
				display: 'none',
				position: 'absolute',
				top: -1000,
				left: -0
			}
		}).adopt(
			this.container
		).injectTop(document.body);
	},

	attach: function(elements){
		var read = function(option, element){
			if (option == null) return '';
			return $type(option) == 'function' ? option(element) : element.get(option);
		};
		$$(elements).each(function(element){
			var title = read(this.options.title, element);
			element.erase('title').store('tip:native', title).retrieve('tip:title', title);
			
			var content = read(this.options.text, element);
			element.retrieve('tip:text', '<img src="' + content + '">');
			
			var events = ['enter', 'leave'];
			if (!this.options.fixed) events.push('move');
			
			events.each(function(value){
				element.addEvent('mouse' + value, element.retrieve('tip:' + value, this['element' + value.capitalize()].bindWithEvent(this, element)));
			}, this);
		}, this);
		
		return this;
	},

	detach: function(elements){
		$$(elements).each(function(element){
			['enter', 'leave', 'move'].each(function(value){
				element.removeEvent('mouse' + value, element.retrieve('tip:' + value) || $empty);
			});
			
			element.eliminate('tip:enter').eliminate('tip:leave').eliminate('tip:move');
			
			if ($type(this.options.title) == 'string' && this.options.title == 'title'){
				var original = element.retrieve('tip:native');
				if (original) element.set('title', original);
			}
		}, this);
		
		return this;
	},

	elementEnter: function(event, element){
		$A(this.container.childNodes).each(Element.dispose);
		
		['title', 'text'].each(function(value){
			var content = element.retrieve('tip:' + value);
			if (!content) return;
			
			this[value + 'Element'] = new Element('div', {'class': 'tip-' + value}).inject(this.container);
			this.fill(this[value + 'Element'], content);
		}, this);
		
		this.timer = $clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this, element);
		this.tip.setStyle('display', 'block');
		this.position((!this.options.fixed) ? event : {page: element.getPosition()});
	},

	elementLeave: function(event, element){
		$clear(this.timer);
		this.tip.setStyle('display', 'none');
		this.timer = this.hide.delay(this.options.hideDelay, this, element);
	},

	elementMove: function(event){
		this.position(event);
	},

	position: function(event){
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
		for (var z in props){
			obj[props[z]] = event.page[z] + this.options.offset[z];
			if ((obj[props[z]] + tip[z] - scroll[z]) > size[z]) obj[props[z]] = event.page[z] - this.options.offset[z] - tip[z];
		}
		
		this.tip.setStyles(obj);
	},

	fill: function(element, contents){
		if(typeof contents == 'string') element.set('html', contents);
		else element.adopt(contents);
	},

	show: function(el){
		this.fireEvent('show', [this.tip, el]);
	},

	hide: function(el){
		this.fireEvent('hide', [this.tip, el]);
	}

});

/* Init thumbnail tooltips */
if ($('middle'))
	var screenshots = new Tips($$('.screenshot'));


/* Preloader */
function loadSWF(name, width, height){
   	if (swfobject.hasFlashPlayerVersion('9.0.0')) {
		var so = swfobject.createSWF({data:gameName, width:gameWidth, height:gameHeight}, {bgcolor:'#000000'}, 'game');
	}
}

if ($('gamebox_content')) {

	var progressBar,
		progressWidth,
		playLink,
		interval,
		stepSize,
		count = 5,
		loopCount = 0;

	function loadGame() {
		loadSWF(gameName, gameWidth, gameHeight);

		progressBar = new Element('div', {
			id: 'progressBar'
		}).inject('progress');

		$('progress').addClass('back');

		playLink = new Element('div', {
			id: 'loadLink',
			html: loadText
		}).injectAfter('progress');

		progressWidth = parseInt($('progress').getStyle('width'));
		stepSize = Math.round(progressWidth / steps);
		interval = timer / steps;
		
		updateProgress();
	}

	function updateProgress() {
		progressWidth = (progressWidth - stepSize <= 0) ? 1 : progressWidth - stepSize;
		progressBar.setStyle('width', progressWidth);
		
		if (loopCount != (steps)) {
			setTimeout(updateProgress, interval) 
		} else {
			progressBar.dispose();

			playReady = new Element('a', {
				id: 'playLink',
				href: '',
				html:  readyText
			});
			playReady.replaces(playLink);

			playReady.addEvent('click', function(e) {
				e.stop();
				showGame();
			});

			setTimeout('countDown()', 1000);
		}
		
		loopCount++;
	}

	function countDown() {
		if (count == 0) {
			if ($('progress'))
				showGame();
		} else {
			if ($('playLink'))
				$('counter').set('html', count);
			count--;
			setTimeout('countDown()', 1000);
		}
	}

	function showGame() {
		$('gameContainer').addClass('show');
		$('openx').dispose();
		$('progress').dispose();
		$('playLink').destroy();
		$('ad_gp_728x90').setStyle('display', 'block');
		//$('myGame').focus();
	}
}

