/*
---
DESCRIPTION: For GrossmütterRevolution Global Functions.

LICENSE: Closed license

AUTHORS:
- Rajeev J. Sebastian
- Nathan Querido

COPYRIGHT:
- QUERIDODESIGN, Switzerland - http://www.queridodesign.net/

DEPENDENCIES:
- MooTools-Core 1.2.4

- MooTools-More 1.2.4
...
*/

if(!$defined(window.console)) {
	window.console = {
		log: function(s) {}
	}
}

// DEFAULT FORM ELEMENT VALUE CLASS :: JanK
var InputText = new Class({

	initialize: function(element){
		this.element = element = $(element);
		this.text = element.value;
		element.store('inputText', this);
		element.addEvents({
			focus: (function(){
				if (element.value == this.text){
					var obs = element.retrieve('valueObserver', false);
					if(obs) obs.set('');
					else element.value = '';
					element.removeClass('default_text');
				}
			}).bind(this),
			blur: (function(){
				if (element.value == ''){
					var obs = element.retrieve('valueObserver', false);
					if(obs) obs.set(this.text);
					else element.value = this.text;
					element.addClass('default_text');
				}
			}).bind(this)
		});
	},
	
	isValid: function(){
		return (this.element.value && this.element.value != this.text)
	},
	value: function(){
		console.log('inputtext value', this.element.value);
		return this.isValid() ? this.element.value : '';
	}
});

Element.implement({

	fadeOut: function(){
		this.fade(Browser.Engine.trident ? 'hide' : 'out');
	},

	fadeIn: function(){
		this.fade(Browser.Engine.trident ? 'show' : 'in');
	}

});

var LoginDropDown = new Class({
	Implements: [Options],
	
	options: {
		fadeOptions: {
			duration: 'short'
		},
		inputStyles: {
			'normal': {'color':'#000'},
			'error': {'color':'#f00'}
		}
	},
	
	initialize: function(element, options) {
		this.setOptions(options);
		this.element = $(element);
		this.menuShown = false;
		this.menu = this.element.getElement('.login_menu');
		this.menu.set('tween', this.options.fadeOptions);
		this.menu.setStyle('display', 'block').fade('hide');
		this.show = this.element.getElement('.login_show');
		this.toggler = this.documentClicked.bind(this);
		this.show.addEvent('click', this.showClicked.bind(this));
		
		/* setup form stuff */
		this.email = this.menu.getElement('input.email').retrieve('inputText');
		this.password = this.menu.getElement('input#password-password').retrieve('inputText');
		this.validator = new Form(this.menu);
		// console.log(this.validator);
		this.menu.getElement('button').addEvent('click', this.doLogin.bind(this));
		this.menu.getElement('form').addEvent('submit', this.doLogin.bind(this));
		this.request = new Request({
			url: this.menu.getElement('form').getAttribute('action'),
			onSuccess: this.loginSuccess.bind(this),
			onFailure: this.loginFailure.bind(this)
		});
	},
	
	showClicked: function(ev){
		ev.stop();
		console.log('toggling');
		if(this.menuShown) {
			this.menu.fadeOut();
			this.menuShown = false;
			document.removeEvent('click', this.toggler);
		} else {
			this.menu.fadeIn();
			this.menuShown = true;
			document.addEvent('click', this.toggler);
		}
	},
	documentClicked: function(ev){
		if($(ev.target).getParent('.login_menu')) return true;
		this.showClicked(ev); return false;
	},
	doLogin: function(ev){
		ev.stop();
		console.log(ev, ev.target);
		var email = false;
		var password = false;

		
		if(this.validator.validate()) {
			console.log('form valid ... submitting');
			this.request.post({'email':this.email.value(), 'password':this.password.value()})
		} else {
			console.log('form invalid!');
		}
	},
	loginSuccess: function(){
		console.log('login success');
		this.menu.fadeOut();
		this.menuShown = false;
		// window.location.reload(true);
		window.location = window.location.href; //force a GET always
	},
	loginFailure: function(){
		console.log('login failure');
		this.validator.setInvalid(this.email.element);
		this.validator.setInvalid(this.password.element);
	}
});

window.addEvent('domready', function(){
	if($defined($$('.login_menu')[0])) { 
		//once you are logged in, there is no password field
		pass = new PassShark('password',{
				interval: 100,
				duration: 500,
				replacement: '%u25CF',
				prefix: 'password-',
				debug: false,
				inputText: true
		});
		// GRAB ALL INPUTS AND USE DEFAULT TEXT WITH CSS CLASS '.default_text' :: JanK
		$$('input.email').each(function(input){
		    new InputText(input);
		});
		login = new LoginDropDown($$('.login')[0]);
	}
	
	// GRAB ALL INPUTS AND USE DEFAULT TEXT WITH CSS CLASS '.default_text' :: JanK
	$$('input.search_input').each(function(input){
	    new InputText(input);
	});


});
