var Challenge = new Class({

	Implements: Options,
	
	options : {
	
	},
	
	initialize : function(){
		this.hover();
		this.puzzle();
		this.formcheck();
		this.conditions();
	},
	
	hover : function(){
		//make down class
		$$('.btns, .participate').each(function(btn){
			btn.store('class', btn.get('class').split(' ')[1]);
			btn.addEvents({
				mousedown : function(){
					this.addClass(this.retrieve('class')+'_hover');
					this.store('down', true);
				},
				mouseup : function(){
					this.removeClass(this.retrieve('class')+'_hover');
					this.store('down', false);
				},
				mouseout : function(){
					this.removeClass(this.retrieve('class')+'_hover');
					this.store('down', false);
				},
				mouseenter : function(){
					if(this.retrieve('down')) this.addClass(this.retrieve('class')+'_hover')
				}
			});
		});
	},
	
	puzzle : function(){
		if(!$('puzzle')) return false;
		
		var counter = 0;
		var puzzele = [];
		if(!Cookie.read('pzzl')) Cookie.write('pzzl', 'false', { path : '/' });
		
		//create structure
		for(var i = 0; i < 6; i++){
			var l = i > 2 ? 3 : 0;
			var t = i > 2 ? 168 : 0;
			var z = i+1;
			puzzele[i] = new Element('div', {
				styles : {
					width : 242,
					height : 166,
					background : '#000 url(/img/mask.png)',
					border : '1px solid #276a8a',
					position : 'absolute',
					top : 2 + t,
					left : 2 + (i-l) * 244
				},
				id : 'pzzl_'+z
			}).inject('puzzle');
		}
		
		$('puzzle').getElement('img').setStyle('display', 'block');
		
		//fx on message
		$$('.help')[0].addEvent('click', function(){
			this.fade(0);
			
			puzzele.each(function(zone, z){
				zone.addEvent('click', function(){
					if (counter < 3) {
						counter++;
						this.fade(0);
						var chain = Cookie.read('pzzl').split('-');
						chain.push(zone.id);
						Cookie.write('pzzl', chain.unique().join('-'), { path : '/', duration : '10' });
					} else {
						this.fade(0.9).pauseFx(0).fade(1);
					}
				});
			});
			
			Cookie.read('pzzl').split('-').each(function(el){
				if(el != 'false' && el) {
					$(el).fireEvent('click');
				}
			});
		});
		
		//center message
		var w = $$('.help')[0].getWidth();
		$$('.help')[0].setStyle('marginLeft', (732 - w) / 2);
		
	},
	
	formcheck : function(){
		if($('coord')) {
			new FormCheck('coord');
		}
	},
	
	conditions : function(){
		if (!$('conditions')) return;
		var slider = new Slider('slider', 'knob', {
			mode : 'vertical',
			wheel : true,
			offset : -16,
			onChange : function(step){
				$('inner').setStyle('top', ($('inner').getHeight() - $('content').getHeight() + 8) * -(step/100));
			}
		});
		
		$('inner').addEvent('mousewheel', function(e){
			new Event(e).stop();
			slider.set(slider.step - e.wheel * 10);
		});
	}
});

var Konami = new Class({

	code: ["up","up","down","down","left","right","left","right","b","a","enter"],
    
	step: 0,

	initialize: function(func1,func2,func3){
		if($defined(func2))
			this.func2 = func2;
		if($defined(func3))
			this.func3 = func3;
		this.func = func1;
		window.addEvent('keyup',this.keystroke.bind(this));
	},
    
	keystroke: function(e){
		ev = new Event(e);
    	
		if(ev.key == this.code[this.step]){
			if($defined(this.func2))
				this.func2.attempt(this.step);
			this.step++;
			if(this.step == this.code.length){
				this.step = 0;
				this.func.attempt();
			}
		} else {
			this.step = 0;
			if($defined(this.func3))
				this.func3.attempt();
		}
	}
    
});

window.addEvent('domready', function(){
	new Challenge();
	
	new Konami(function(){
	      document.body.setStyles({
			'-webkit-transform': 'rotate(-180deg)',
			'-moz-transform': 'rotate(-180deg)'
		});
    });
});
