//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.
MooTools.More={version:"1.2.4.4",build:"6f6057dc645fdb7547689183b2311063bd653ddf"};String.implement({parseQueryString:function(){var b=this.split(/[&;]/),a={};
if(b.length){b.each(function(g){var c=g.indexOf("="),d=c<0?[""]:g.substr(0,c).match(/[^\]\[]+/g),e=decodeURIComponent(g.substr(c+1)),f=a;d.each(function(j,h){var k=f[j];
if(h<d.length-1){f=f[j]=k||{};}else{if($type(k)=="array"){k.push(e);}else{f[j]=$defined(k)?[k,e]:e;}}});});}return a;},cleanQueryString:function(a){return this.split("&").filter(function(e){var b=e.indexOf("="),c=b<0?"":e.substr(0,b),d=e.substr(b+1);
return a?a.run([c,d]):$chk(d);}).join("&");}});

Gallery = new Class({
	
	initialize:function(container, nav, viewable, transition, interval){	
		//console.log("Gallery.initialize");
		
		this.transition = transition;
		this.container = $(container);
		this.nav = $(nav);
		this.slider = this.container.getElement('ul');
		this.items = this.container.getElements('li');		
		this.buttonBack = this.nav.getElement('a[href$=back]');
		this.buttonNext = this.nav.getElement('a[href$=next]');
		
		this.currentPage = 1;
		this.interval = interval;
		this.viewable = viewable;
		this.viewable_height = this.container.getStyle('height').toInt() + this.container.getStyle('margin').toInt();
		this.viewable_width = this.container.getStyle('width').toInt() + this.container.getStyle('margin').toInt();
		this.pageCount = Math.ceil(this.items.length/this.viewable);
		
		this.slider.set('tween', {duration: 'short'});
		
		this.addEvents();
		this.show(this.currentPage);
		
		//this.isPlaying = true;
		//this.timer = this.showNext.periodical(this.interval, this);
		if(this.interval != null)
		{
			this.play();
		}
		else
		{
			this.stop();	
		}

	},
	
	addEvents:function(){
		//console.log("Gallery.addEvents");
		this.buttonBack.addEvent('click', function(e) {
			e.stop();
			this.stop();
			this.showPrev();
		}.bind(this));	
		
		this.buttonNext.addEvent('click', function(e) {
			e.stop();
			this.stop();
			this.showNext();
		}.bind(this));	
		
		this.container.addEvent('mouseenter', function() {
			this.pause();
		}.bind(this));
		
		this.container.addEvent('mouseleave', function() {
			this.play();
		}.bind(this));	

		
	},
	
	stop: function(){
		this.isPlaying = false;
		this.isStopped = true;
		$clear(this.timer);
	},
	
	pause:function(){
		this.isPlaying = false;
		$clear(this.timer);
	},
	
	play:function(){
		
		if(!this.isStopped)
		{
			this.isPlaying = true;
			this.timer = this.showNext.periodical(this.interval, this);
		}
	},
	
	toggle:function(){
		if(this.isPlaying){
			this.pause();
		}
		else{
			this.play();
		}
	},

	
	show:function(page){

		//console.log("Gallery.show(",page,")");
		this.currentPage = page;
		
		//this.slider.tween('margin-top', - ((this.currentPage-1)*this.viewable_height));
		if(this.transition == 'scroll')
		{
			this.slider.tween('margin-top', - ((this.currentPage-1)*this.viewable_height));
		}
		if(this.transition == 'slide')
		{
			this.slider.tween('margin-left', - ((this.currentPage-1)*this.viewable_width));
		}
		if(this.transition == 'fade')
		{
			//this.slider.tween('opacity', - ((this.currentPage-1)*this.viewable_width));
		}
	
		if(this.currentPage >= this.pageCount)
		{
			this.buttonNext.setOpacity(0);
			this.buttonNext.setStyle('cursor','default');
		}
		else
		{
			this.buttonNext.setOpacity(1);
			this.buttonNext.setStyle('cursor','pointer');
		}
		if(this.currentPage <=1 )
		{
			this.buttonBack.setOpacity(0);
			this.buttonBack.setStyle('cursor','default');
		}
		else
		{
			this.buttonBack.setOpacity(1);
			this.buttonBack.setStyle('cursor','pointer');
		}
	},
	
	showNext:function(){
		//console.log("Gallery.showNext");
		if(this.currentPage < this.pageCount )
		{
			this.show(this.currentPage+1);
		}
	},
	
	showPrev:function(){
		//console.log("Gallery.showPrev");
		if(this.currentPage > 1 )
		{
			this.show(this.currentPage-1);
		}
	}
});	// EOF Gallery

window.addEvent('domready',function(){

	$$('input.search').each(function(el){
		if(el.value == '') el.value = 'Search';
		el.addEvents({
			'focus':function(){if(el.value == 'Search') el.value = '';},
			'blur':function(){if(el.value == '') el.value = 'Search';}
		});
	});
	
	$$('#mce-EMAIL').each(function(el){
		var prefill = 'Email Address';
		if(el.value == '') el.value = prefill;
		el.addEvents({
			'focus':function(){if(el.value == prefill) el.value = '';},
			'blur':function(){if(el.value == '') el.value = prefill;}
		});
	});
	
	$$('input.locator').each(function(el){
		var prefill = 'Zip Code';
		if(el.value == '') el.value = prefill;
		el.addEvents({
			'focus':function(){if(el.value == prefill) el.value = '';},
			'blur':function(){if(el.value == '') el.value = prefill;}
		});
	});
	
});

