var Newsberry = {

	init: function(){
		
		if (exists('#customers')) Newsberry.customers();
		
		if ($('txtCoupon')) Newsberry.couponCode();
	
		if ($$F('.block-content.twitter')) Newsberry.fetchLatestTwitterPost();
	
	},
	
	customers: function(){
		var quotes 	= $$('#customers .quote');
		var state 	= 0;
		
		quotes[state].show();
		
		var timeout = window.setInterval(function(){
			quotes[state].fade('out');

			if (state == (quotes.length - 1))
				state = 0;
			else
				state++;
			
			quotes[state].setOpacity(0).show().fade('in');
		}, 5000);
	},
	
	couponCode: function(){
		var timer;
		
		$('txtCoupon').addEvent('keydown', function(){
			window.clearTimeout(timer);
		});
		
		$('txtCoupon').addEvent('keyup', function(){
			this.getParent('div.form-row').getElement('img.spinner').show();
			this.getParent('div.form-row').getElement('img.checkmark').hide();
			
			timer = window.setTimeout(function(){
				Anthem_InvokePageMethod('ValidateCoupon', $('txtCoupon').get('value'));
			}, 1000);
		});
	},
	
	toggleFormRowType: function(row){
		var row = $(row);
		
		if (row.hasClass('error')) {
			row.removeClass('error');
		} else {
			row.addClass('error');
		}
	},
	
	fetchLatestTwitterPost: function(){
	  getTwitters('tweet', { 
		  id: 'newsberry', 
	    count: 1, 
	    enableLinks: true, 
	    ignoreReplies: true, 
	    clearContents: true,
	    template: '<blockquote id="tweet">%text%</blockquote><p class="meta">Posted by <a href="http://twitter.com/newsberry">@newsberry</a> %time%</p>'
	  });
	}
	
}

window.addEvent('domready', Newsberry.init);



/* Extension to MooTools core */

Element.implement({
	
	show: function(){
		return this.removeClass('hide');
	},
	
	hide: function(){
		return this.addClass('hide');
	},
	
	toggle: function(){
		if (this.hasClass('hide')) {
			return this.show();
		} else {
			return this.hide();
		}
	}
	
});

Window.implement({
	
	$$F: function(selector){
		return $$(selector)[0];
	},
	
	exists: function(selector){
		if ($$(selector).length > 0 || $(selector) != undefined)
			return true;
		else
			return false;
	}
	
});