window.addEvent('domready',function(){Page.setup();});


var Page = {
		currentImage:-1,
		setup: function()
		{
			var i = $$('.input, .input_e');
			i.addEvent('focus',function(e){Page.focus(e)});
			i.addEvent('blur',function(e){Page.blur(e)});
			if ($('ns') != null)
				$('ns').addEvent('submit',function(e){Page.subscribeRequest(e)});
			
			$$('#pastnewsletter a').set('html','(View Recent Newsletter)');
			
			Page.Promo = $$('.mainpromo img');
			Page.Promo.set('tween', { duration: '1000', transition: 'sine:in' });
        
			 
			Page.Promo.setOpacity(0);
			Page.Promo.setStyle('visibility', 'hidden');
			Page.Promo.removeClass('hidden');

        
        var nextImage = function() {
            if (this.currentImage >= 0) {
                this.Promo[this.currentImage].fade(0);
            }
            this.currentImage++;
            if (this.currentImage >= this.Promo.length)
                this.currentImage = 0;

            this.Promo[this.currentImage].fade(1);
            
        };
        if (Page.Promo.length > 0) {

            nextImage.apply(Page);
            if (Page.Promo.length > 1)
                nextImage.periodical(6000, Page);
        }

		}
		,
		focus: function(e)
		{
			e=new Event(e);
			e.target.setStyle('background-position','0 -28px');
		},
		blur: function(e)
		{
			e=new Event(e);
			e.target.setStyle('background-position','0 0');
		},
		subscribeRequest: function(e)
		{
			e=new Event(e);
			e.stop();
			var er = false;
			if ($('name').value.trim().length == 0) { er = true; $('name').setProperty('class','input_e'); } else { $('name').setProperty('class','input'); };
			if (!$('email').value.test("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$","i")) { er = true; $('email').setProperty('class','input_e'); } else { $('email').setProperty('class','input'); };

			if (!er)
			{			
			$('loading').toggleClass('hidden');
			var post=$('ns').toQueryString()+'&ajax=1';
				req = new Request({url:$('ns').getProperty('action'), autoCancel:true, onSuccess: Page.subscribeResponse});
				req.send(post);
			}
			else
			{
				$('ns_intro').set('html','<p class="error">Sorry, there was a problem with some of the information you entered. Please check below and try again:</p>');
			}
			
		},
		subscribeResponse: function(r)
		{
			$('loading').toggleClass('hidden');
			if (r.length > 2 && r.substr(0,2) == "OK")
			{	
				$('ns_form').set('html',r.substr(3,r.length-3));
			}
			else
			{
				$('ns_intro').set('html','<p class="error">Sorry, there was a problem subscribing you to the e-newsletter. Please try again later.</p>');
			}
		}
	}
	
