﻿var Tipsa = new Class({
	overlay: '',
	button: '',
	container: '',
	
	initialize: function(button) {
		this.button = $(button);
		this.overlay = new Element('div', {
			styles: {
				'height': Window.getScrollHeight()+'px',
				'width': Window.getWidth()+'px',
				'background': '#000',
				'opacity': 0,
				'z-index': 499,
				'position': 'absolute',
				'top': 0,
				'left': 0,
				'display': 'none'
			}
		});
	
		$(document.body).adopt(this.overlay);
		this.button.addEvent('click', function(event) {
		    this.show();
		}.bind(this));
	},
	
	doKeys : function(event) {
		if (event.key == 'enter') {
    		event.preventDefault();
    		if ($('sida1').getStyle('z-index') == 502) {
    			this.changePage();
    		} else {
	    		this.validate();
	    	}
    	} else if (event.key == 'esc') {
    		this.hide();
    	}
	},
	
	show: function() {
	    this.overlay.addEvent('click', function(event) {
		    this.hide();
		}.bind(this));
		document.addEvent('keydown', function(event) {
		    this.doKeys(event);
		}.bind(this));
		this.overlay.setStyle('display', 'block');
		this.overlay.tween('opacity', 0, 0.3);
		this.container = new Element('div', {id: 'tipsa_cont'});
		// hämta via ajax
		// visa när klar
		var busy = new Element('img', {src: 'assets/site/loading.gif'});
		
		$(document.body).adopt(this.container);
		
		var req = new Request.HTML({
			url:'index-ajax.php',
			data: {
				'q': 'assets/snippets/tipsa/tipsa.php',
				'sida': 'Mariannelunds folkhögskola',
				'ts': new Date().getTime()
				},
			method: 'post',
			onSuccess: function(tree) {
				this.container.empty();
				this.container.adopt(tree);
				this.prepare();
			}.bind(this)
		}).send();
		
		this.container.addEvent('click', function(ev) {
			ev.stopPropagation();
		});
	},
	
	prepare: function() {
		if ($('nasta') === null) {
			// svar
			if ($('stang2') === null) {
			// fel
			} else {
			// ok
				$('stang2').addEvent('click', function(event) {
		            this.hide();
		        }.bind(this));
			}
		} else {
			$('stang').addEvent('click', function(event) {
		        this.hide();
		    }.bind(this));
			$('nasta').addEvent('click', function(event) {
		        this.changePage();
		    }.bind(this));
			$('andra').addEvent('click', function(event) {
		        this.changePage();
		    }.bind(this));
			$('skicka').addEvent('click', function(event) {
		        this.validate();
		    }.bind(this));
			$('bytfraga').addEvent('click', function(event) {
		        this.changeQuestion();
		    }.bind(this));
		}
	},
	
	changeQuestion: function() {
		var req = new Request.HTML({
		url:'index-ajax.php?q=assets/snippets/tipsa/tipsa.php&nyq='+(new Date().getTime()),
			method: 'get',
			onSuccess: function(tree) {
				$('kontrollfraga').empty();
				$('kontrollfraga').adopt(tree);
				$('kontrollfraga').highlight('#a0522d', '#faf0e6');
				$('bytfraga').addEvent('click', function(event) {
		            this.changeQuestion();
		        }.bind(this));
			}.bind(this)
		}).send();
	},
	
	changePage: function() {
		if ($('sida1').getStyle('z-index') == 502) {
			$('sida1').fade('out');
			$('sida1').setStyle('z-index', 501);
			$('sida2').setStyle('z-index', 502);
			$('sida1').fade('in');
		} else {
			$('sida2').fade('out');
			$('sida2').setStyle('z-index', 501);
			$('sida1').setStyle('z-index', 502);
			$('sida2').fade('in');
		}
		if (Browser.ie || Browser.opera) {
			$('fg_kommentar').getFirst().set('text', $('kommentar').get('text'));
		} else {
			$('fg_kommentar').getFirst().set('text', $('kommentar').get('value'));
		}
		$('fg_namn').set('text', $('fran_namn').get('value') + ' ');
	},
	
	validate: function() {
		$('tipsa_en_van').fade('out');
		var busy = new Element('img', {src: 'assets/site/loading.gif'});
		this.container.adopt(busy);
		
		var q = new Element('input', {
			'name': 'q',
			'value': 'assets/snippets/tipsa/tipsa.php',
			'type': 'hidden'
		});
		
		$('tipsa_en_van').adopt(q);
		
		var req = new Request.HTML({
			url:'index-ajax.php?ts='+(new Date().getTime()),
			method: 'post',
			onSuccess: function(tree, els, html) {
				if (html.contains('fel')) {
					busy.destroy();
					$('tipsa_en_van').fade('in');
					this.changePage();
					$('iqtest').set('value', '');
					this.changeQuestion();
					q.destroy();
					alert('Fel i formulär!');
				} else {
					this.container.empty();
					this.container.adopt(tree);
					this.prepare();
				}
			}.bind(this)
		}).send($('tipsa_en_van'));
		
	},
	
	hide: function() {
		// byt till transition...
		this.container.fade('out');
		this.container.destroy();
		this.overlay.tween('opacity', 0.3, 0);
		this.overlay.setStyle('display', 'none');
		document.removeEvents('keydown');
	}
});

window.addEvent('domready', function () {
	new Tipsa('tipsa');
});
