// <![CDATA[

var NewsTicker = Class.create({
	initialize: function(){
		if(!($("momento") && $("momentoText"))) return;
		this.container = $("momentoText").cleanWhitespace().absolutize();
		var spans = this.container.immediateDescendants();
		this.l = parseInt(spans[0].getWidth()+spans[1].getWidth());

		this.timer = setInterval(this.scrollNews.bind(this), 40);
		
		Event.observe(window, "unload", function(){ clearInterval(this.timer); }.bind(this));
	},
	
	scrollNews: function(){
		var left = parseInt(this.container.style.left);

		if(left==-this.l)
		{
			this.container.style.left = "0px";
		}
		else
		{
			this.container.style.left = left-1 + "px";
		}
	}
});

var UpdatesChecker = Class.create({
	initialize: function(){
		this.f     = $('updatesForm');
		this.email = $('email');
		this.def   = "Enter your email";

		if(!this.f){ return(false); }

		this.f.observe('submit', this.checkForm.bind(this));
		this.email.observe('focus', this.updateValue.bind(this));
		this.email.observe('blur', this.updateValue.bind(this));
	},
	
	checkForm: function(event){
		event.stop();

		var email = $F('email');

		if(email=="" || email==this.def){
			$('infos').update('<p class="error">Please, enter your email to get our updates.</p>');
		}
		else if(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(email)===false){
			$('infos').update('<p class="error">Please, enter a valid email.</p>');
		}
		else{
			new Ajax.Request('./updates.php',
			{
				method:     'post',
				parameters: 'email=' + $F('email'),
				onCreate:   Prototype.emptyFunction,
				onSuccess:  function(){
					$('infos').update('<p class="valide">Thank you for your interest!</p>');
					$('email').value = "";
					var timer = setTimeout(function(){
						$('infos').update('');
						clearTimeout(timer);
					}.bind(this), 3000);
				}
			});
			return true;
		}
	},
	
	updateValue: function(){
		var email = this.email.value;

		if(email==this.def){
			this.email.value = "";
		}
		else if(email==""){
			this.email.value = this.def;
		}
		
		$('infos').update('');
	}
});

document.observe("dom:loaded", function(){	
	new NewsTicker();
	new UpdatesChecker();
});

// ]]>