var footprint = {}; // create own namespace

footprint.set_text = function(event){
    this.value = '';
}

footprint.signup = {
    init: function(){        
        var email_value = "Email";
        var name_value = 'First Name';
        var form = $('signup_form_id');
        var name = form.getElement('input[name=name]');
        var email = form.getElement('input[name=email]');
        name.value = name_value;
        email.value = email_value;
        name.addEvent('click', footprint.set_text);
        email.addEvent('click', footprint.set_text);
        name.addEvent('blur', function(event){
                if(this.value == ''){
                    this.value = name_value;
                }
            });
        email.addEvent('blur', function(event){
                if(this.value == ''){
                    this.value = email_value;
                }
            });
                
        /*
        // validate not empty fields
        form.addEvent('submit', function(event){
        new Event(event).stop();
                if(email.value != '' && email.value != email_value && name.value != '' && name.value != name_value){
                    $('signup_form_id').submit();
                }
            });
        */
    }
};

footprint.newsTicker = {

	init: function(el) {
	
		if (!$(el)) {return false;}
		
		this.container = $(el);
		this.newsItems = this.container.getElement('ul');
		this.insertUi();
		this.moreBtn.addEvent('click', this.tick.bind(this));
		
		// set interval+ remove on hover of containers parent (to include more btn)
		var parentContainer = this.container.getParent();
		this.startInterval();
		parentContainer.addEvent('mouseover', this.stopInterval.bind(this));
		parentContainer.addEvent('mouseout', this.startInterval.bind(this));
		
	},
	insertUi: function() {
		this.moreBtn = new Element ('a', {'class': 'more-btn', 'text': 'More', 'href': '#'});
		this.moreBtn.injectAfter(this.container);
	},
	tick: function() {
	
		var self = this,
			pos = this.newsItems.getElement('li:nth-child(2)').getPosition(this.container).y,
			scroll = self.container.getScrollTop(),
			duration = (pos - scroll) / 0.3;
			new Fx.Scroll(self.container, {
				duration: duration,
				offset: {
					'x': 0,
					'y': pos
				},
				onComplete: function() {
					self.newsItems.getElement('li:nth-child(first)').inject(self.newsItems);
					new Fx.Scroll(self.container).set({x: 0, y: 0});
				}
			}).start();
			
			return false;
	},
	startInterval: function() {
		this.interval = setInterval(this.tick.bind(this), 8000);
	},
	stopInterval: function() {
		clearInterval(this.interval);
	}
};

window.addEvent('domready', function() {
	$$('body').addClass('jsenabled');   // Enables css when js is enabled
	footprint.signup.init();
	footprint.newsTicker.init('news-container');
});