/**************************************************************
	Script		: Rhythmus
	Version		: 0.1
	Authors		: Pieter Soudan
	Desc		: a Rhythmus Class for controlling all JS functionality for the Rhythmus corporate website
	Licence		: property of Rhythmus
**************************************************************/

var Rhythmus = new Class({
    Implements: Options,
    options: {
        service: 'service.php',
	publicdir: 'publicdir',
	assetsdir: 'assetsdir',
	debugcntnr: 'debugmode',
	pagename: 'default',
	pagetype: 'default',
	profile: 'normal',
	debugmode: false,
	delay:6000
    },
    
    initialize: function(options){
	this.setOptions(options);
        Browser.ie6 = (Browser.Engine.trident && Browser.Engine.version == 4);
	window.ie6 = Browser.ie6;
	
	if(this.options.debugmode) {
	    if(!window.console){
		//this.debugscreen = new DebugScreen({position:'bottomLeft'});
	    }
	    this.debug('*** DEBUG MODUS ***');
	}
        this.svg = new SVGDowngrade();
        this.initSubscribe();
        this.wasReset = false;
    },
    
    start:function(){
        
    },
    
    initSubscribe: function(){
        $('box').addEvent('click', this.resetForm.bind(this));
        $('subscribe').addEvent('click', this.sendForm.bind(this));
        this.myRequest = new Request.HTML({method: 'post', url: this.options.publicdir+'/'+this.options.service+'?action=subscribe', 'update':$('feedback')});
	this.myRequest.addEvent('complete',this.formSent.bind(this));
	
    },
    
    resetForm: function(){
        if(!this.wasReset){
            $('box').set('value', '');
            this.wasReset = true;
        }
    },
    
    sendForm: function(){
        //validate email
        if(this.is_valid_email($('box').value)) {
            $('subscribe').setStyle('display', 'none');
            $('loading').setStyle('visibility', 'visible');
            this.myRequest.get({'email':$('box').value});
        }
        else $('feedback').set('html', 'email is niet geldig');
        //this.formSent();
    },
    formSent: function(){
        $('form').set('html', '<b>Bedankt!</b>');
    },
    is_valid_email: function(email){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
    }
});

//guarantee singleton
var site;
function createRhythmus(options){
    site = new Rhythmus(options);
    return site;
}
