var powermf = {

	init: function(){
		
		if ($$('.slideshow')[0]) powermf.initSlideshow();
		
		if ($('monthly-plans') && $('pay-as-you-go')) powermf.initPlans();
		
		if ($('browse-clients')) powermf.initClients();
		
		if ($('txtCoupon')) powermf.couponCode();
	
		if ($$F('.block-content.twitter')) powermf.fetchLatestTwitterPost();
	
	},

	initSlideshow: function(){
	  var scrollFx = new Fx.Scroll($$('.slideshow')[0]);
	  var position = 1;
	  
	  $$('div.slide-1, div.slide-2, div.slide-3, div.slide-4').each(function(el){
	    var next = new Element('a').set('html', 'Next<span></span>').addClass('next').injectInside(el);
	    
	    next.addEvent('click', function(e){
	      e = Event(e).stop();

        this.setOpacity(0);
        
	      scrollFx.start(position * 980, 0);
	      
	      $$('.slideshow-nav')[0].removeClass('current-' + position).addClass('current-' + String(position + 1));
	      
	      position++;
	    });
	  });
	  
	  $$('li.slide-1, li.slide-2, li.slide-3, li.slide-4, li.slide-5').each(function(el){
	    el.addEvent('click', function(e){
	      e = Event(e).stop();
    
	      var current = position;
	      position    = parseInt(el.get('class').replace(/slide-/, ''));
	      
	      scrollFx.start((position - 1) * 980, 0);
	      
	      $$('.slideshow-nav')[0].removeClass('current-' + current).addClass('current-' + position);
	      
	      $$('.next').each(function(el){ el.fade('in'); });
	    });
	  });
	},
	
	initPlans: function(){
		var hash = window.location.hash;
		
		if (hash == '') hash = '#monthly-plans';
		
		$('n-monthly').store('html', $('n-monthly').get('html'));
		$('n-prepaid').store('html', $('n-prepaid').get('html'));
		
		powermf.selectPlan(hash);
		
		$$('.pricing-tabs').each(function(el){
			el.addEvent('click', function(e){
				e = Event(e).stop();
				
				powermf.selectPlan(el.getElement('a').get('href'));
			});
		});
	},
	
	selectPlan: function(hash){
		var selected = hash.split('#')[1];

		switch(selected) {
			case 'monthly-plans':
				$('n-monthly').set('html', $('n-monthly').get('html').replace(/<a[^>]*>/i, '<strong>').replace(/<\/a>/i, '</strong>'));
				$('n-prepaid').set('html', $('n-prepaid').retrieve('html'));
				
				$('monthly-plans').show();
				$('pay-as-you-go').hide();
			break;
			case 'pay-as-you-go':
				$('n-prepaid').set('html', $('n-prepaid').get('html').replace(/[^>]*/i, '<strong>').replace(/<\/a>/i, '</strong>'));
				$('n-monthly').set('html', $('n-monthly').retrieve('html'));
				
				$('monthly-plans').hide();
				$('pay-as-you-go').show();
			break;			
		}
	},
	
	initClients: function(){
		var state 	= 0;
		var quotes = $$('div.quote');

		$('browse-clients').addEvent('click', function(e){
			e = Event(e).stop();
			
			quotes[state].hide();
			
			if (state >= quotes.length - 1) {
				state = 0;
			} else {
				state++;
			}
			
			quotes[state].show();
		});
	},
	
	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: 'powermf', 
	    count: 1, 
	    enableLinks: true, 
	    ignoreReplies: true, 
	    clearContents: true,
	    template: '<blockquote id="tweet">%text%</blockquote><p class="meta">Posted by <a href="http://twitter.com/powermf">@powermf</a> %time%</p>'
	  });
	}
	
}

window.addEvent('domready', powermf.init);



/* Extension to MooTools core */

Element.implement({
	
	show: function(){
		if (this.hasClass('hide')) {
			return this.removeClass('hide');
		}
	},
	
	hide: function(){
		if (!this.hasClass('hide')) {
			return this.addClass('hide');
		}
	}
	
});

Window.implement({
	
	$$F: function(selector){
		return $$(selector)[0];
	}
	
});