//========================================================================
//Extend Remote.Ajax to give onStateChange (XHR.transport) functionality
//========================================================================
var Ajax = Ajax.extend({
	options: {
		onStateChange: Class.empty
	},
	onStateChange: function() {
		this.fireEvent('onStateChange', this.transport);
		this.parent();
	}
});

//========================================================================
//AjaxLoader v1.0
//========================================================================
var AjaxLoader = {
	initialize:function(){  	
		if(!document.getElementById || !document.createTextNode){return;} //check for DOM support
		
		//declare object-wide variables
		this.LinkArray = $$('.ajax');
		this.Container = $('ajax_content');
		this.Loading = $('loading');
		
		if(!$$(this.LinkArray) || !$(this.Container) || !$(this.Loading)) {return;} //check for required elements	
			this.LinkArray.each(function(Link, i) {
				Link.addEvent('click', function(e){var ev = new Event(e); ev.preventDefault();});//ADD EVENTS TO EACH
				Link.addEvent('click', AjaxLoader.loadContent.pass([i, Link], AjaxLoader));
			});
			
			//DECLARE EFFECT
			this.Fade = new Fx.Style(this.Container, 'opacity', {
				duration: 400, 
				transition: Fx.Transitions.Quart.easeOut
			});
	},
	
	loadContent:function(i, Link){
		AjaxLoader.Fade.set(0);  //set transparent
		this.AjaxRequest = new Ajax(Link.rel,{
								method: 'get',
								update: AjaxLoader.Container,	
								evalScripts: true,
								onStateChange: function(trans){
										if(trans.readyState == 1){
											AjaxLoader.Loading.setHTML('<img src="images/loading.gif" alt="" />');
										}					
									},
								onComplete: function(){
											AjaxLoader.Fade.start(0,1); //fade back in
											AjaxLoader.Loading.setHTML('');
									}
								}).request();	//request new data
		
	}
};	

//========================================================================
//Ajax Form Sender v1.0
//========================================================================
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('3 A={t:0(a,c){$(a).y(\'v\',0(e){3 6=$(\'w\');4 x(e).d();s.f({5:9,q:0(7){h(7.i==1){$(\'2\').b(\'<k l="n/2.r" u="" />\')}},8:0(){3 z=4 g(c,{j:\'m\',o:6,5:9,8:0(){$(\'2\').b(\'\')}}).p()}})})}};',37,37,'function||loading|var|new|evalScripts|Container|trans|onComplete|true|form|setHTML|url|stop||send|Ajax|if|readyState|method|img|src|get|images|update|request|onStateChange|gif|this|init|alt|submit|ajax_content|Event|addEvent|AjaxRequest|sendForm'.split('|'),0,{}))


//========================================================================
//mooFormy v1.0
//========================================================================
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('7 3={9:o(g,p,q,z){2(g.E){g.E(p,q,z);f K}D 2(g.B){7 r=g.B(\'L\'+p,q);f r}D{f 8}},M:o(){2(!n.N||!n.O){f}7 s=n.G(\'P\');I(7 i=0;i<s.J;i++){7 5=s[i];2(5.p==\'Q\'&&5.4.m(\'u\')){3.9(5,\'w\',3.h,8);3.9(5,\'x\',3.j,8);3.9(5,\'y\',3.h,8);3.9(5,\'A\',3.j,8);2(5.c!=\'\'){5.a=5.c}2(5.4){5.d=5.4}}}7 v=n.G(\'R\');I(7 i=0;i<v.J;i++){7 6=v[i];2(6.4.m(\'t\')){3.9(6,\'w\',3.h,8);3.9(6,\'x\',3.j,8);3.9(6,\'y\',3.h,8);3.9(6,\'A\',3.j,8);2(6.b!=\'\'){6.a=6.b}2(6.4){6.d=6.4}}}},h:o(e){7 1=l.k?l.k.C:e?e.1:F;2(!1){f}2(1.c==1.a){1.c=\'\'}2(1.4.m(\'u\')){2(1.4==1.d){1.4=\' u H\'}}2(1.b==1.a){1.b=\'\'}2(1.4.m(\'t\')){2(1.4==1.d){1.4=\' t H\'}}},j:o(e){7 1=l.k?l.k.C:e?e.1:F;2(!1){f}2(1.c==\'\'&&1.a){1.c=1.a}2(1.b==\'\'&&1.a){1.b=1.a}2(1.4!=\'\'&&1.d){1.4=1.d}}}',54,54,'|target|if|mooFormy|className|theInput|theTA|var|false|addEvnt|defaultText|textContent|value|defaultClassName||return|element|change||restore|event|window|match|document|function|type|listener||formInputs|clrTextArea|clrInput|formTAs|mouseover|mouseout|focus|useCapture|blur|attachEvent|srcElement|else|addEventListener|null|getElementsByTagName|inputborder|for|length|true|on|init|getElementById|createTextNode|input|text|textarea'.split('|'),0,{}))


//========================================================================
//slimbox v1.41
//========================================================================
/*
	Slimbox v1.3 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = {

	init: function(options){
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightbox/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);

		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.image = new Element('div').setProperty('id', 'lbImage').injectInside(this.center);
		

		this.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		this.prevLink = new Element('a').setProperties({id: 'lbPrevLink', href: '#'}).setStyle('display', 'none').injectInside(this.bottom);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.bottom);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};

		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},

	click: function(link){
		if (link.rel.length == 8) return this.show(link.href, link.title);

		var j, imageNum, images = [];
		this.anchors.each(function(el){
			if (el.rel == link.rel){
				for (j = 0; j < images.length; j++) if(images[j][0] == el.href) break;
				if (j == images.length){
					images.push([el.href, el.title]);
					if (el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);
	},

	show: function(url, title){
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum){
		this.images = images;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.9);
		return this.changeImage(imageNum);
	},

	position: function(){
		this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		if (window.ie) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function(){
		return this.changeImage(this.activeImage-1);
	},

	next: function(){
		return this.changeImage(this.activeImage+1);
	},

	changeImage: function(imageNum){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;

		this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.fx.image.hide();
		this.center.className = 'lbLoading';

		this.preload = new Image();
		this.preload.onload = this.nextEffect.bind(this);
		this.preload.src = this.images[imageNum][0];
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			this.image.style.backgroundImage = 'url('+this.images[this.activeImage][0]+')';
			this.image.style.width = this.bottom.style.width = this.preload.width+'px';
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';

			this.caption.setHTML(this.images[this.activeImage][1] || '');
			this.number.setHTML((this.images.length == 1) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length);

			if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1][0];
			if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1][0];
			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			if (this.activeImage) this.prevLink.style.display = '';
			if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};
window.addEvent('domready', Lightbox.init.bind(Lightbox));
