

/*	---------------------------------------------------------------------------
	CLASS:		Element
	METHOD:		fix();
	ABOUT:		Fixes alpha transparency in IE6
	REVISED:	February 27, 2008
*/

Element.implement({
	fix: function(){
		if(!Browser.Engine.trident) return this;
		var src;
		var size = this.getSize();
		if(this.get('tag')=='img'){
			src = this.get('src');
			if(src.indexOf('.png') < 0) return this;
			this.set('src', '/site/x.gif');
		} else {
			var bg = this.getStyle('background-image');
			if(bg && bg!='none')
				src = bg.match(/\(([^)]+)\)/)[1];
			if(src.indexOf('.png') < 0) return this;
		}
		if (src) {
			if(this.getStyle('display')=='inline' && !['input', 'textarea', 'button'].contains(this.get('tag'))) {
				this.setStyles({
					'display': 'block',
					'width': size.x,
					'height': size.y
				});
			}
			this.setStyles({
				'background': '',
				'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", src="'+src+'", sizingMethod="crop")'
			});
		}
		return this;
	}
});
if(Browser.Engine.trident4) window.addEvent('domready', function() {
	$$('img[src$=png]').fix();
});



/*	---------------------------------------------------------------------------
	CLASS:		FormTip(selector,[className])
	AUTHOR:		Ryan J. Salva
	REVISED:	December 2008
*/

var FormTip = new Class({
	Implements: Options,
	options: {
		className: 'FormTip'
	},
	initialize: function(selector,options){
		this.setOptions(options);
		this.els = $$(selector);
		this.els.each(function(el,index){
			el.addEvent('focus',this.show.bindWithEvent(this));
			el.addEvent('blur',this.hide.bindWithEvent(this));
		}.bind(this));
	},
	show: function(e) {
		var e = new Event(e);
		var el = e.target;
		if ($type(el) != 'element') return false;
		var title = el.getAttribute('title');
		if (!$defined(title)) return false;
		var pos = el.getPosition(el.getOffsetParent());
		var width = el.getWidth();
		this.tip = new Element('div',{'class':this.options.className,'styles':{
			opacity: 0,
			position: 'absolute',
			left: pos.x + width,
			top: pos.y
		}}).setText(title);
		this.tip.inject(el.getParent(),'inside');
		this.tip.tween('opacity',1); 
	},
	hide: function() {
		this.tip.remove();
	}
});



/*	---------------------------------------------------------------------------
	CLASS:		Fader(container,[pause,duration,loop])
	AUTHOR:		Ryan J. Salva
	MODIFIED: December 22, 2007
 
	creates a single, rotating image on a page

	IMPLEMENTATION:
	<div id="Container">
		<img src="1.jpg" /><img src="2.jpg" /><img src="3.gif" />
	</div>
	<script type="text/javascript">
		window.addEvent('domready',function() { 
			var f = new Fader('Container');
			f.start();
		});
	</script>
*/

var Fader = new Class({
	Implements: Options,
	options: {
		pause: 5000,
		duration: 1000,
		loop: true
	},
	initialize: function(container,options) {
		this.setOptions(options);
		this.container = $(container);
		this.imgs = this.container.getElements('img');
		this.imgs.setStyles({
			'position':'absolute',
			'top':0,
			'left':0,
			'opacity':0
		});
		this.imgs.set('tween',{duration:this.options.duration});

		this.imgs[0].setStyle('opacity',1);
		this.el = new Element('div',{'styles': {
			'position':'relative'
	    }});
	    this.el.injectInside(this.container);
	    this.el.adopt(this.imgs);
		this.next = 0;
		this.start();
	},
	start: function() {
		this.show();
		this.periodical = this.show.bind(this).periodical(this.options.pause);
	},
	stop: function() {
		$clear(this.periodical);
	},
	show: function() {
		if (!this.options.loop && this.next==this.imgs.length-1) this.stop();
		this.next = (this.next==this.imgs.length-1)?0:this.next+1;
		var prev = (this.next==0)?this.imgs.length-1:this.next-1;
		
		this.imgs[this.next].tween('opacity',1);
		this.imgs[prev].tween('opacity',0);
	}
});


/*	---------------------------------------------------------------------------
	AUTHOR: Ryan J. Salva
	LAST MODIFIED: 12/28/07

	Creates a kind of image gallery with thumbnail images and a single, full-size image
*/

var Carousel = new Class({
	Implements: Options,
	options: {
		thumbSelector: 'a.Thumbnail',
		imageClass:'CarouselImage',
		onComplete: Class.empty,
		onStart: Class.empty
	},
	initialize: function(el,options) {
		this.setOptions(options);
		this.el = $(el);
		if (!$defined(this.el)) return false;
		this.el.setStyles({
			overflow:'hidden',
			position:'relative'
		});
		this.thumbs = $$(this.options.thumbSelector);
		this.fx = new Fx.Morph(this.el, { 'duration': 250 });
		var crsl = this;
		
		this.thumbs.each(function(anchor,index) {
			anchor.addEvent('click',function(e) {
				var e = new Event(e);
				e.stop();
				this.loadImage(anchor.href,'Carousel'+index);
				this.thumbs.removeClass('Active');
				anchor.addClass('Active');
			}.bind(this));
		}.bind(this));
		this.loadImage(this.thumbs[0].href,'Carousel0');
	},
	loadImage: function(src,id) {
		this.fx.cancel();
		this.fx.start({
			opacity:0
		}).chain(function() {
			this.el.getElements('img').setStyle('opacity',0);
			if($(id)) {
				// show it right away
				this.showImage($(id))
			} else {
				// load and add it to the DOM first
				var img = new Element('img',{'src':src,'id':id,'class':this.options.imageClass,'styles':{
					position: 'absolute',
					top: 0,
					left: 0
				}});
				img.addEvent('load',function() {
					img.injectInside(this.el);
					this.showImage(img);
				}.bind(this));
			}
		}.bind(this));
	},
	showImage: function(img) {
		img.setStyle('opacity',1);
		var coord = img.getCoordinates();
		this.fx.start({
			'height':coord.height,
			'width':coord.width
		}).chain(function(){
			this.start({
				opacity:1
			});
		});
	}
});


/*	---------------------------------------------------------------------------
	CLASS:		Ticker()
	AUTHOR:		Ryan J. Salva
	REVISED:	January 2008
*/

var Ticker = new Class({
	Implements: Options,
	options: {
		speed: 1500,
		delay: 8000,
		direction: 'vertical',
		onComplete: Class.empty,
		onStart: Class.empty
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getCoordinates().height;
			this.items.each(function(li,index) {
				w += li.getCoordinates().width;
			});
		} else {
			w = this.el.getCoordinates().width;
			this.items.each(function(li,index) {
				h += li.getCoordinates().width;
			});
		}
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		this.fx = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
			var i = (this.current==0)?this.items.length:this.current;
			this.items[i-1].injectInside(this.el);
			this.el.setStyles({
				left:0,
				top:0
			});
		}.bind(this)});
		this.current = 0;
		this.next();
	},
	next: function() {
		this.current++;
		if (this.current >= this.items.length) this.current = 0;
		var pos = this.items[this.current];
		this.fx.start({
			top: -pos.offsetTop,
			left: -pos.offsetLeft
		});
		this.next.bind(this).delay(this.options.delay+this.options.speed);
	}
});

/*	---------------------------------------------------------------------------
	CLASS:		Modal();
	OPTIONS:	speed: the transition speed (default:500)
				maskColor: the background color of the mask (default: black)
				width: default width of the dialog box (default:400px)
				height: default height of the dialog box (default: auto)
				classPrefix: used to define unique classes for each dialog box (default: "Modal")
				onHide: event
				onShow: event
				onStart: event
	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
	REVISED:	January 2008
*/

var Modal = new Class({
	Implements: [Events, Options],
	options: {
		speed: 200,
		maskColor: '#000000',
		width: 400,
		height: 'auto',
		classPrefix: 'Modal',
		onHide: Class.empty,
		onShow: Class.empty,
		onStart: Class.empty
	},
	initialize: function(options) {
		this.setOptions(options);
		this.isShowing = false;
		var classPrefix = this.options.classPrefix;
		this.mask = new Element('div', {'styles':{
			'position':'absolute',
			'top': 0,
			'left': 0,
			'opacity': 0,
			'height': (window.getHeight() > window.getScrollHeight()) ? window.getHeight() : window.getScrollHeight(),
			'width': '100%',
			'background':this.options.maskColor,
			'z-index': 9999
		}}).addClass(classPrefix+'Mask');
		this.pop = new Element('div',{'styles':{
			'position': 'absolute',
			'visibility': 'hidden',
			'width': '100%',
			'margin': 0,
			'z-index': 10000
		}}).addClass(classPrefix+'Pop');
		this.container = new Element('div',{'styles':{
			'margin':'0 auto'
		}}).addClass(classPrefix+'Container');
		this.close = new Element('div').adopt(new Element('a', {'href':'#', 'text':'Close'})).addClass(classPrefix+'Close');

		this.fade = new Fx.Tween(this.mask, 'opacity', {duration:this.options.speed});
		this.slide = new Fx.Tween(this.pop, 'top', {duration: this.options.speed});
		
		this.fireEvent('onStart');
	},
	show: function(el, title) {
		var classPrefix = this.options.classPrefix;
		switch($type(el)) {
			case 'element':
				this.el = new Element('div',{'styles':{'height':this.options.height}}).adopt(el.clone().cloneEvents(el));
				break;
			case 'string':
				this.el = new Element('div',{'styles':{'height':this.options.height}}).set('html', el);
				break;
			default:
				return false;
				break;
		}
		var message = new Element('div').addClass(classPrefix+'Message').adopt(this.el);
		if(title) var title = new Element('div').setText(title).addClass(classPrefix+'Title');
		
		if(this.isShowing) {
			this.container.adopt(title, message);
		} else {
			this.close.getElement('a').addEvent('click', function(event) {
				event.stop();
				this.hide();
			}.bind(this));
			window.addEvent('keydown', function(event) {
				if(event.key == 'esc') this.hide();
			}.bind(this));
			
			$$('object', 'select').setStyle('visibility', 'hidden');
			
			$$('body').adopt(this.mask, this.pop);
			this.container.adopt(this.close, title, message);
			this.container.inject(this.pop, 'inside').setStyle('width', this.options.width);
		}
		
		this.pop.setStyles({
			'top': window.getScroll().y - this.pop.getSize().y,
			'visibility':'visible'
		});

		this.fade.start(0.8);
		var slideTo = ((window.getSize().y - this.container.getSize().y) / 2 + window.getScroll().y);
		this.slide.start(slideTo);
		this.periodical = this.update.periodical(100, this);
		this.isShowing = true;
		this.fireEvent('onShow');
	},
	update: function() {
		var slideTo = ((window.getSize().y - this.container.getSize().y) / 2 + window.getScroll().y);
		this.slide.start(slideTo);
		var h = (window.getSize().y > window.getScrollSize().y) ? window.getSize().y : window.getScrollSize().y;
		this.mask.setStyle('height', h);
	},
	hide: function() {
		$$('object', 'select').setStyle('visibility', 'visible');
		$clear(this.periodical);
		this.slide.cancel();
		var slideTo = window.getScroll().y - this.pop.getSize().y;
		this.slide.start(slideTo).chain(function() {
			this.pop.destroy();
			this.fade.start(0).chain(function() {
				this.mask.remove();
				this.isShowing = false;
				this.fireEvent('onHide');
			}.bind(this));
		}.bind(this));
	}
});


/*	---------------------------------------------------------------------------
	CLASS:		ScrollBar(container,content,options)
	OPTIONS:	maxThumbSize
				wheel
				arrows
				hscroll
	MODIFIED:	11/06/2008, Ryan J. Salva
	ORIGIN:		Adapted from http://greghoustondesign.com/examples/mootools/scrollbars/
*/

var ScrollBar = new Class({

	Implements: [Events, Options],
	options: {
		maxThumbSize: 15,
		wheel: 8,
		arrows: true,
		hScroll: false
	},
	initialize: function(main, content, options) {
		this.setOptions(options);
		this.main = $(main);
		this.content = $(content);
		if (this.options.arrows == true){
			this.arrowOffset = 30;
		} else {
			this.arrowOffset = 0;
		}
		
		if (this.options.hScroll == true){
			this.hScrollOffset = 15;
		} else {
			this.hScrollOffset = 0;
		}				

		this.vScrollbar = new Element('div', {
				'class': 'vScrollbar'
			}).inject(this.content,'after');

		if (this.options.arrows == true){				
			this.arrowUp = new Element('div', {
					'class': 'arrowUp'
				}).inject(this.vScrollbar,'inside');
		}	

		this.vTrack = new Element('div', {
				'class': 'vTrack'
			}).inject(this.vScrollbar,'inside');
			
		this.vThumb = new Element('div', {
				'class': 'vThumb'
			}).inject(this.vTrack,'inside');

		if (this.options.arrows == true){				
			this.arrowDown = new Element('div', {
					'class': 'arrowDown'
				}).inject(this.vScrollbar,'inside');
		}		
		
		this.hScrollbar = new Element('div', {
				'class': 'hScrollbar'
			}).inject(this.vScrollbar,'after');

		if (this.options.arrows == true){							
			this.arrowLeft = new Element('div', {
					'class': 'arrowLeft'
				}).inject(this.hScrollbar,'inside');
		}		

		this.hTrack = new Element('div', {
				'class': 'hTrack'
			}).inject(this.hScrollbar,'inside');
			
		this.hThumb = new Element('div', {
				'class': 'hThumb'
			}).inject(this.hTrack,'inside');

		if (this.options.arrows == true){
			this.arrowRight = new Element('div', {
					'class': 'arrowRight'
				}).inject(this.hScrollbar,'inside');
		}											

		this.corner = new Element('div', {
				'class': 'corner'
			}).inject(this.hScrollbar,'after');
		
		this.bound = {
			'vStart': this.vStart.bind(this),
			'hStart': this.hStart.bind(this),				
			'end': this.end.bind(this),
			'vDrag': this.vDrag.bind(this),
			'hDrag': this.hDrag.bind(this),				
			'wheel': this.wheel.bind(this),
			'vPage': this.vPage.bind(this),
			'hPage': this.hPage.bind(this)				
		};

		this.vPosition = {};
		this.hPosition = {};			
		this.vMouse = {};
		this.hMouse = {};			
		this.update();
		this.attach();
		this.fireEvent('init');
	},

	update: function(){
		
		this.main.setStyle('height', this.content.offsetHeight + this.hScrollOffset);
		this.vTrack.setStyle('height', this.content.offsetHeight - this.arrowOffset);
		
		this.main.setStyle('width', this.content.offsetWidth + 15);
		this.hTrack.setStyle('width', this.content.offsetWidth - this.arrowOffset);

		// Remove and replace vertical scrollbar			
		if (this.content.scrollHeight <= this.main.offsetHeight) {
			this.vScrollbar.setStyle('display', 'none');
			if (this.options.hScroll == true){				
				this.hTrack.setStyle('width', this.hTrack.offsetWidth + 15);
			}	
			this.content.setStyle('width', this.content.offsetWidth + 15);	
		} else {
			this.vScrollbar.setStyle('display', 'block');			
		}
		
		if (this.options.hScroll == true){			
		
			// Remove and replace horizontal scrollbar
			if (this.content.scrollWidth <= this.main.offsetWidth) {
				this.hScrollbar.setStyle('display', 'none');
				this.vTrack.setStyle('height', this.vTrack.offsetHeight + this.hScrollOffset);				
				this.content.setStyle('height', this.content.offsetHeight + this.hScrollOffset);	
			} else {
				this.hScrollbar.setStyle('display', 'block');			
			}
		
			// Remove and replace bottom right corner spacer			
			if (this.content.scrollHeight <= this.main.offsetHeight || this.content.scrollWidth <= this.main.offsetWidth) {
				this.corner.setStyle('display', 'none');				
			} else {
				this.corner.setStyle('display', 'block');			
			}		
		
			// Horizontal

			this.hContentSize = this.content.offsetWidth;
			this.hContentScrollSize = this.content.scrollWidth;
			this.hTrackSize = this.hTrack.offsetWidth;

			this.hContentRatio = this.hContentSize / this.hContentScrollSize;

			this.hThumbSize = (this.hTrackSize * this.hContentRatio).limit(this.options.maxThumbSize, this.hTrackSize);

			this.hScrollRatio = this.hContentScrollSize / this.hTrackSize;

			this.hThumb.setStyle('width', this.hThumbSize);

			this.hUpdateThumbFromContentScroll();
			this.hUpdateContentFromThumbPosition();			

		} else {
			this.hScrollbar.setStyle('display', 'none');
			this.corner.setStyle('display', 'none');										
		}

		// Vertical
		
		this.vContentSize = this.content.offsetHeight;
		this.vContentScrollSize = this.content.scrollHeight;
		this.vTrackSize = this.vTrack.offsetHeight;

		this.vContentRatio = this.vContentSize / this.vContentScrollSize;

		this.vThumbSize = (this.vTrackSize * this.vContentRatio).limit(this.options.maxThumbSize, this.vTrackSize);

		this.vScrollRatio = this.vContentScrollSize / this.vTrackSize;

		this.vThumb.setStyle('height', this.vThumbSize);

		this.vUpdateThumbFromContentScroll();
		this.vUpdateContentFromThumbPosition();
		this.fireEvent('update');
	},

	vUpdateContentFromThumbPosition: function(){
		this.content.scrollTop = this.vPosition.now * this.vScrollRatio;
	},
	
	hUpdateContentFromThumbPosition: function(){
		this.content.scrollLeft = this.hPosition.now * this.hScrollRatio;
	},		

	vUpdateThumbFromContentScroll: function(){
		this.vPosition.now = (this.content.scrollTop / this.vScrollRatio).limit(0, (this.vTrackSize - this.vThumbSize));
		this.vThumb.setStyle('top', this.vPosition.now);
	},
	
	hUpdateThumbFromContentScroll: function(){
		this.hPosition.now = (this.content.scrollLeft / this.hScrollRatio).limit(0, (this.hTrackSize - this.hThumbSize));
		this.hThumb.setStyle('left', this.hPosition.now);
	},		

	attach: function(){
		this.vThumb.addEvent('mousedown', this.bound.vStart);
		if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
		this.vTrack.addEvent('mouseup', this.bound.vPage);
		
		this.hThumb.addEvent('mousedown', this.bound.hStart);
		this.hTrack.addEvent('mouseup', this.bound.hPage);			
		
		if (this.options.arrows == true){
			this.arrowUp.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollTop -= this.options.wheel;
					this.vUpdateThumbFromContentScroll();
				}.bind(this).periodical(40))
			}.bind(this));
		
			this.arrowUp.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowUp.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
					
			this.arrowDown.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollTop += this.options.wheel;
					this.vUpdateThumbFromContentScroll();
				}.bind(this).periodical(40))
			}.bind(this));
		
			this.arrowDown.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowDown.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowLeft.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollLeft -= this.options.wheel;
					this.hUpdateThumbFromContentScroll();
				}.bind(this).periodical(40))
			}.bind(this));
		
			this.arrowLeft.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowLeft.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowRight.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollLeft += this.options.wheel;
					this.hUpdateThumbFromContentScroll();
				}.bind(this).periodical(40))
			}.bind(this));
		
			this.arrowRight.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowRight.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
		}
		this.fireEvent('attach');
	},
	
	wheel: function(event){
		this.content.scrollTop -= event.wheel * this.options.wheel;
		this.vUpdateThumbFromContentScroll();
		event.stop();
		this.fireEvent('wheel');
	},

	vPage: function(event){
		if (event.page.y > this.vThumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
		else this.content.scrollTop -= this.content.offsetHeight;
		this.vUpdateThumbFromContentScroll();
		event.stop();
		this.fireEvent('vPage');
	},
	
	hPage: function(event){
		if (event.page.x > this.hThumb.getPosition().x) this.content.scrollLeft += this.content.offsetWidth;
		else this.content.scrollLeft -= this.content.offsetWidth;
		this.hUpdateThumbFromContentScroll();
		event.stop();
		this.fireEvent('hPage');
	},		

	vStart: function(event){
		this.vMouse.start = event.page.y;
		this.vPosition.start = this.vThumb.getStyle('top').toInt();
		document.addEvent('mousemove', this.bound.vDrag);
		document.addEvent('mouseup', this.bound.end);
		this.vThumb.addEvent('mouseup', this.bound.end);
		event.stop();
		this.fireEvent('vStart');
	},
	
	hStart: function(event){
		this.hMouse.start = event.page.x;		
		this.hPosition.start = this.hThumb.getStyle('left').toInt();
		document.addEvent('mousemove', this.bound.hDrag);
		document.addEvent('mouseup', this.bound.end);
		this.hThumb.addEvent('mouseup', this.bound.end);
		event.stop();
		this.fireEvent('hStart');
	},		

	end: function(event){
		document.removeEvent('mousemove', this.bound.vDrag);
		document.removeEvent('mousemove', this.bound.hDrag);			
		document.removeEvent('mouseup', this.bound.end);
		this.vThumb.removeEvent('mouseup', this.bound.end);
		this.hThumb.removeEvent('mouseup', this.bound.end);			
		event.stop();
		this.fireEvent('end');
	},

	vDrag: function(event){
		this.vMouse.now = event.page.y;
		this.vPosition.now = (this.vPosition.start + (this.vMouse.now - this.vMouse.start)).limit(0, (this.vTrackSize - this.vThumbSize));
		this.vUpdateContentFromThumbPosition();
		this.vUpdateThumbFromContentScroll();
		event.stop();
		this.fireEvent('vDrag');
	},
	
	hDrag: function(event){
		this.hMouse.now = event.page.x;
		this.hPosition.now = (this.hPosition.start + (this.hMouse.now - this.hMouse.start)).limit(0, (this.hTrackSize - this.hThumbSize));
		this.hUpdateContentFromThumbPosition();
		this.hUpdateThumbFromContentScroll();
		event.stop();
		this.fireEvent('hDrag');
	}		

});

