var mootools = new Class({
	options: {
		// options here
	},
	initialize: function(options){
		this.setOptions(options);
		this.skyscraper();
		this.clickLogo();
		this.myGames();
		this.saveGame();
		this.catGames();
		this.loadMoreGames();
		this.tabBar();
		this.gameSlider();
		if ($('highscoreSearchID')) {
			this.highscores('',$('highscoreSearchID').value);
		}
		this.highscoreSearch();
		this.highscoresScroll();
	},
	skyscraper: function() {
		if($('bannerRight')) {
			$('bannerRight').setStyle('visibility','visible');
		}
	},
	clickLogo: function() {
		if($('logo')) {
			$('logo').addEvent('mouseenter',function(e){
				$('logo').setStyle('cursor','pointer');
			});
			$('logo').addEvent('mouseleave',function(e){
				$('logo').setStyle('cursor','auto');
			});
			$('logo').addEvent('click',function(e){
				window.location.href='http://www.1001games.fr';
			});
		}
	},
	myGames: function(){
		if($('myGamesButton')) {
			var element = $('myGamesButton');
			$('myGamesContent').setStyle('height','0px');
			var slider = new Fx.Styles('myGamesContent', {duration:300, wait:false});
			
			new Ajax('_mygames.php', {
				method: 'get',
				update: $('myGamesContent'),
				evalScripts:true,
				onComplete:function(){
					if(Cookie.get("myGamesButton")=='open') {
						$('myGamesContent').setStyle('height','90px');
						$('myGamesIMG').src='http://www.1001games.fr/images/myGamesOpened.gif';
					}
				}
			}).request();
			
			element.addEvent('mouseenter',function(e){
				element.setStyle('background-color','#FFFFFF');
				element.setStyle('cursor','pointer');
			});
			element.addEvent('mouseleave',function(e){
				element.setStyle('background-color','#93CFE5');
				element.setStyle('cursor','auto');
			});
			element.addEvent('click',function(e){
				if($('myGamesContent').getStyle('height').toInt()=='0') {
					Cookie.set("myGamesButton","open");
					$('myGamesIMG').src='http://www.1001games.fr/images/myGamesOpened.gif';
					slider.start({
						'height':'90px'
					});
				}
				if($('myGamesContent').getStyle('height').toInt()=='90') {
					Cookie.set("myGamesButton","closed");
					$('myGamesIMG').src='http://www.1001games.fr/images/myGamesClosed.gif';
					slider.start({
						'height':'0px'
					});
				}
			});
		}
	},
	saveGame: function() {
		if($('myGamesSaveGame')) {
			var element = $('myGamesSaveGame');
			element.addEvent('mouseenter',function(e){
				element.setStyle('background-color','#FFFFFF');
				element.setStyle('cursor','pointer');
			});
			element.addEvent('mouseleave',function(e){
				element.setStyle('background-color','#93CFE5');
				element.setStyle('cursor','auto');
			});
			element.addEvent('click',function(e){
				new Ajax('_saveGame.php', {
					method: 'get',
					update: $('saveGameAction'),
					evalScripts:true,
					data:'id='+element.title,
					onComplete:function(){
						new Ajax('_mygames.php', {
							method: 'get',
							update: $('myGamesContent'),
							evalScripts:true,
							onComplete:function(){
								//alert('Game saved');
								element.remove();
							}
						}).request();
					}
				}).request();
			});
		}
	},
	catGames: function(){
		if($('catGames')) {
			$$('#catGames li').each(function(element){
				var fx = new Fx.Styles(element, {duration:200, wait:false});
				element.addEvent('mouseenter',function(e){
					fx.start({
						'background-color':'#CCEAF5'
					});
				});
				element.addEvent('mouseleave',function(e){
					fx.start({
						'background-color':'#FFFFFF'
					});
				});
			});
		}
	},
	loadMoreGames:function() {
		if($('tabBar') && $('bottomBarContent')) {
			new Ajax('_moreGames.php', {
				method: 'get',
				update: $('bottomBarContent'),
				evalScripts:true,
				onComplete:function(){
					$('bottomBarContent').removeClass('loading');
				}
			}).request();
		}
	},
	tabBar:function(){
		if($('tabBar') && $('bottomBarContent')) {
			$$('#tabBar ul li a').each(function(element){
				var data=element.title;
				element.addEvent('click',function(e) {
					var e = new Event(e).stop();
					new Ajax(element.href, {
						method: 'get',
						update: $('bottomBarContent'),
						evalScripts:true,
						data:data,
						onRequest:function() {
							$('bottomBarContent').addClass('loading');
						},
						onComplete:function(){
							$('bottomBarContent').removeClass('loading');
						}
					}).request();
				});
			});
		}
	},
	gameSlider:function(){
		if($('gameSlider')) {
			var sWrapper = $('wrapper').getStyle('width').toInt();
			var sHeader = $('header').getStyle('left').toInt();
			var sLogo = $('logo').getStyle('left').toInt();
			var sHeaderText = $('headerText').getStyle('right').toInt();
			var sHeaderBox = $('headerBox').getStyle('right').toInt();
			var sContainer = $('container').getStyle('width').toInt();
			var sGameContainerW = $('gameContainer').getStyle('width').toInt();
			var sGameContainerH = $('gameContainer').getStyle('height').toInt();
	
			var wrapper = new Fx.Style('wrapper','width');
			var header = new Fx.Style('header','left');
			var logo = new Fx.Style('logo','left');
			var headerText = new Fx.Style('headerText','right');
			var headerBox = new Fx.Style('headerBox','right');
			var container = new Fx.Style('container','width');
			var gameContainerW = new Fx.Style('gameContainer','width');
			var gameContainerH = new Fx.Style('gameContainer','height');
			
			var slider = new Slider($('gameSlider'), $('gameSliderButton'), {
				steps:10,	
				offset:0,
				onChange: function(pos){
					oriPos = pos;
					pos = pos-5;
					var add = 0;
					add=pos*50;					
					var math = (100/sGameContainerW)*(sGameContainerW+add);
				
					if(oriPos>=5) {
						wrapper.set(sWrapper+add);
						header.set(sHeader+(add/2));
						logo.set(sLogo+(add/2));
						headerText.set(sHeaderText+(add/2));
						headerBox.set(sHeaderBox+(add/2));
						container.set(sContainer+add);
					}
					gameContainerW.set(sGameContainerW+add);					
					gameContainerH.set(sGameContainerH*(math/100));
				}		 
			}).set(5);
		}
	},
	highscores:function(query,gameValue){
		if($('highscoresContentDay')) {
			var element = $('highscoresContentDay').getChildren();
			element = element[0];
			this.loadHighscore(element,'day',query,gameValue);
		}
		if($('highscoresContentWeek')) {
			var element = $('highscoresContentWeek').getChildren();
			element = element[0];
			this.loadHighscore(element,'week',query,gameValue);
		}
		if($('highscoresContentAll')) {
			var element = $('highscoresContentAll').getChildren();
			element = element[0];
			this.loadHighscore(element,'all',query,gameValue);
		}
		
	},
	highscoreSearch:function(){
		if($('highscoreSearchQuery')) {
			$('highscoreSearchQuery').addEvent('keyup',function(){
				$('highscoresContentDay').setStyle('top','0px');
				$('highscoresContentWeek').setStyle('top','0px');
				$('highscoresContentAll').setStyle('top','0px');
				this.highscores($('highscoreSearchQuery').value,$('highscoreSearchID').value);
			}.bind(this));
		}
	},
	loadHighscore:function(element,type,query,gameValue) {
		new Ajax('_highscores.php', {
			method: 'get',
			update: element,
			evalScripts:false,
			data:'type='+type+'&query='+query+'&gameValue='+gameValue
		}).request();
	},
	highscoresScroll:function(){
		$$('.highscoresPreviousIMG').each(function(element){
			var fx = new Fx.Styles(element.getParent(),{duration:500,transition:Fx.Transitions.Quart.easeOut,wait:false});
			element.addEvent('mouseenter',function(e){
				fx.start({'opacity':0.01});
			});
			element.addEvent('mouseleave',function(e){
				fx.start({'opacity':1});
			});
		});
		$$('.highscoresNextIMG').each(function(element){
			var fx = new Fx.Styles(element.getParent(),{duration:500,transition:Fx.Transitions.Quart.easeOut,wait:false});
			element.addEvent('mouseenter',function(e){
				fx.start({'opacity':0.01});
			});
			element.addEvent('mouseleave',function(e){
				fx.start({'opacity':1});
			});
		});
		this.highscoreActive = true;
		if($('highscoresWeek')) {			
			$('highscoresPreviousWeek').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollUp('Week');
				}
			}.bind(this));
			$('highscoresNextWeek').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollDown('Week');
				}
			}.bind(this));
			
		}
		if($('highscoresDay')) {
			$('highscoresPreviousDay').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollUp('Day');
				}
			}.bind(this));
			$('highscoresNextDay').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollDown('Day');
				}
			}.bind(this));
		}
		if($('highscoresAll')) {
			$('highscoresPreviousAll').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollUp('All');
				}
			}.bind(this));
			$('highscoresNextAll').addEvent('click',function(e){
				var e = new Event(e).stop();
				if(this.highscoreActive==true) {
					this.hsScrollDown('All');
				}
			}.bind(this));
		}
	},
	hsScrollUp: function(subEl) {
		var scroller = $('highscoresContent'+subEl);
		var wrapHeight = $('highscoresContainer'+subEl).getStyle('height').toInt();
		var now = scroller.getStyle('top').toInt();
		var fxSlide = new Fx.Styles(scroller,{
			duration:500,
			transition:Fx.Transitions.Quart.easeOut,
			wait:false,
			onComplete:function(){
				this.highscoreActive = true;
			}.bind(this)
		});
		if(now<0) {
			this.highscoreActive = false;
			fxSlide.start({
				'top':(now+wrapHeight)+'px'
			});
		}
	},
	hsScrollDown: function(subEl) {
		var scroller = $('highscoresContent'+subEl);
		var wrapHeight = $('highscoresContainer'+subEl).getStyle('height').toInt();
		var now = scroller.getStyle('top').toInt();
		var height = scroller.getSize().size.y;
		var fxSlide = new Fx.Styles(scroller,{
			duration:500,
			transition:Fx.Transitions.Quart.easeOut,
			wait:false,
			onComplete:function(){
				this.highscoreActive = true;
			}.bind(this)
		});
		var last = 0-(((height/wrapHeight)-1)*wrapHeight);
		if(now>last) {
			this.highscoreActive = false;
			fxSlide.start({
				'top':(scroller.getStyle('top').toInt()-wrapHeight)+'px'
			});
		}
	}
});
mootools.implement(new Options, new Events);
window.addEvent('domready', function() {
	var moo = new mootools();
});