var Newsberry = {

	init: function(){

		if ($$('.slideshow')[0]) Newsberry.initSlideshow();

		if ($('txtCoupon')) Newsberry.couponCode();

	},

	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'); });
	    });
	  });
	},

	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');
		}
	}

}

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;
	}
	
});
