//	---------------------------------------------------------------------------
//	Script: string.cnet.js
//	These are mootools authored extensions designed to allow prototype.lite libraries run in this environment.
//	
//	Dependancies:
//	mootools - <Moo.js>, <String.js>, <Array.js>
//	
//	Author:
//	Aaron Newton, <aaron [dot] newton [at] cnet [dot] com>
//	
//	Class: String
//	This extends the <String> prototype.

String.implement({
	stripTags: function() {
		return this.replace(/<\/?[^>]+>/gi, '');
	},
	stripScripts: function() {
		return this.replace(/<script[^>]*?>.*?<\/script>/img, '');
	},
	evalScripts: function() {
		var scripts = this.match(/<script[^>]*?>.*?<\/script>/g);
		if(scripts) scripts.each(function(script){
			eval(script.replace(/^<script[^>]*?>/, '').replace(/<\/script>$/, ''));
		});
	},
	replaceAll: function(searchValue, replaceValue, regExOptions) {
		return this.replace(new RegExp(searchValue, $pick(regExOptions,'gi')), replaceValue);
	},
	urlEncode: function() {
		if (this.indexOf('%') > -1) return this;
		else return escape(this);
	},
	parseQuery: function() {
		var vars = this.split(/[&;]/);
		var rs = {};
		if (vars.length) vars.each(function(val) {
			var keys = val.split('=');
			if (keys.length && keys.length == 2) rs[encodeURIComponent(keys[0])] = encodeURIComponent(keys[1]);
		});
		return rs;
	},
	tidy: function() {
		var txt = this.toString();
		$each({
			"[\xa0\u2002\u2003\u2009]": " ",
			"\xb7": "*",
			"[\u2018\u2019]": "'",
			"[\u201c\u201d]": '"',
			"\u2026": "...",
			"\u2013": "-",
			"\u2014": "--"
		}, function(value, key){
			txt = txt.replace(new RegExp(key, 'g'), value);
		});
		return txt;
	}
});


//	---------------------------------------------------------------------------
//	FUNCTION:	fixHeight()
//	LAST MODIFIED: 09/28/07
//	AUTHOR:	Ryan J. Salva
//
//	Utility function designed to fix any layout using aboslute positioning for column
//
//	IMPLEMENTATION:
//	Add fixHeight(window,'load',fixHeight('Column1','Column2'... 'Column99') to any page
//	Most Capitol Media websites use the column ids: Left, Right, Middle and Canvas

function fixHeight() {
	var x = 0;
	for (i=0;i<arguments.length;i++) {
		if ($(arguments[i]) && $(arguments[i]).scrollHeight > x) x = $(arguments[i]).scrollHeight;
	}
	var wrapper;
	if (wrapper = $('Wrapper')) wrapper.setStyle('height',x);
}
window.addEvent('load',function(){fixHeight('Canvas','Left','Right');});



//	---------------------------------------------------------------------------
//	CLASS:	Layout()
//	
//	LAST MODIFIED: 09/28/07
//	AUTHOR:	Ryan J. Salva
//
//	Utility function designed to fix any layout using aboslute positioning for column
//
//	IMPLEMENTATION:
//	The following code adjusts the page to make wrapper as tall as the highest column (left, right, etc.)
//	var x = new Layout('Wrapper',['Left','Right','Middle','Canvas']);
//	Most Capitol Media websites use the column ids: Left, Right, Middle and Canvas

var Layout = new Class({
	initialize: function(el,columns){
		this.columns = columns;
		this.el = $(el);
		if (!$defined(this.el)) return false;
		
		this.el.setStyle('overflow','hidden');
		this.columns.each(function(col,index) {
			this.columns[index] = $(col);
		}.bind(this));
		this.columns = this.columns.clean();
		this.fx = new Fx.Tween(this.el,'height',100);
		this.periodical = this.update.bind(this).periodical(200);
	},
	update: function() {
		var y = 0;
		this.columns.each(function(col,index) {
			var h = col.getCoordinates().height;
			if(h > y) y = h;
		});
		this.fx.start(y);
	}
});



//	---------------------------------------------------------------------------
//	FUNCTION:	fixPNG()
//	AUTHOR:		Ryan J. Salva
//
//	Enables transparecy for PNG files in IE
//	NOTE: Makes most colors a little darker than the GIF counterpart
//	
//	REQUIRED ASSETS:
//	files/site/x.gif (1x1 transparent GIF)

function fixPNG(){
	$$('img[src$=png]').each(function(el){
		var coord = el.getCoordinates();
		el.setStyles({
			width: coord.width,
			height: coord.height,
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + el.src + '", sizingMethod="scale")',
			visibility: 'hidden'
		});
		el.src = 'site/x.gif';
		el.addEvent('load',function() {
			el.setStyle('visibility','visible');
		});
	});
}
if(window.ie6) window.addEvent('domready', fixPNG);



//	---------------------------------------------------------------------------
//	CLASS:		Menu()
//	AUTHOR:		Ryan J. Salva
//	REVISED:	December 2007
//
//	Creates a drop-down menu for navigation. Also Corrects Windows IE support 
//	for LI:hover and adds an <IFRAME> behind drop-down menus to keep the 
//	menu above <SELECT> elements.
//
//	REQUIREMENTS:
//	Styles found in default.css
//	
//	TESTED IN:
//	Windows: IE 6, Firefox 1, Opera 8
//	Mac: IE 5.2, Firefox 1, Safari 1

var Menu = new Class({
	Implements: Options,
	options: {
		iframe: false,
		onComplete: Class.empty,
		onStart: Class.empty
	},
	initialize: function(el,options){
		this.el = $(el);
		if (!$defined(this.el)) return false;
		this.setOptions(options);
		
		this.el.getElements('li').each(function(li,index) {
			li.addEvent('mouseenter',function() {
				this.addClass('hover');
			});
			li.addEvent('mouseleave',function() {
				this.removeClass('hover');
			});
		});
		if (this.options.iframe) this.addIframe();
	},
	addIframe: function() {
		this.el.getElements('ul').each(function(ul,index) {
			var coord = ul.getCoordinates();
			var iframe = new Element('iframe',{'src':'about:blank','styles':{
				overflow:'hidden',
				border:0,
				width: coord.width,
				height: coord.height,
				left: 0,
				top: 0,
				zIndex: -10,
				opacity: 0
			}});
			iframe.injectTop(ul);
			ul.setStyle('z-index',99);
		});
	}
});





// ------------------------------------------------------------------
//	AUTHOR: Ryan J. Salva
//	MODIFIED: December 22, 2007
// 
//	DESCRIPTION:
//	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,
		onComplete: Class.empty,
		onStart: Class.empty
	},
	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[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;
	},
	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) $clear(this.periodical);
		this.next = (this.next==this.imgs.length-1)?0:this.next+1;
		
		var appear = this.imgs[this.next].effect('opacity', {'duration': this.options.duration});
		appear.start(0,1);

		var prev = (this.next==0)?this.imgs.length-1:this.next-1;
		var disappear = this.imgs[prev].effect('opacity',{'duration':this.options.duration});
		disappear.start(1,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);
		this.el.setStyles({
			overflow:'hidden',
			position:'relative'
		});
		this.thumbs = $$(this.options.thumbSelector);
		this.fx = new Fx.Morph(this.el, { 'duration': 150 });
		var crsl = this;
		
		this.thumbs.each(function(anchor,index) {
			anchor.addEvent('mouseover',function(e) {
				var e = new Event(e);
				e.stop();
				this.loadImage(anchor.href,'Carousel'+index);
				this.thumbs.removeClass('Active');
				anchor.addClass('Active');
			}.bind(this));
			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
			});
		});
	}
});



// -------------------------------------------------
//	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 CarouselTWO = new Class({
	Implements: Options,
	options: {
		thumbSelector: 'a.ThumbnailTWO',
		imageClass:'CarouselImageTWO',
		onComplete: Class.empty,
		onStart: Class.empty
	},
	initialize: function(el,options) {
		this.setOptions(options);
		this.el = $(el);
		this.el.setStyles({
			overflow:'hidden',
			position:'relative'
		});
		this.thumbs = $$(this.options.thumbSelector);
		this.fx = new Fx.Morph(this.el, { 'duration': 150 });
		var crsl = this;
		
		this.thumbs.each(function(anchor,index) {
			anchor.addEvent('mouseover',function(e) {
				var e = new Event(e);
				e.stop();
				this.loadImage(anchor.href,'CarouselTWO'+index);
				this.thumbs.removeClass('Active');
				anchor.addClass('Active');
			}.bind(this));
			anchor.addEvent('click',function(e) {
				var e = new Event(e);
				e.stop();
				this.loadImage(anchor.href,'CarouselTWO'+index);
				this.thumbs.removeClass('Active');
				anchor.addClass('Active');
			}.bind(this));
		}.bind(this));
		this.loadImage(this.thumbs[0].href,'CarouselTWO0');
	},
	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
			});
		});
	}
});



function checkBlank(el, name){
	var value = document.getElementById(el).value;
	if(value == ""){
		document.getElementById(el).value=name;	
	}
}


/*	---------------------------------------------------------------------------
	CLASS:		FormTip(selector[,options]);
	OPTIONS:	className: CSS class given to each tip (default: 'FormTip')
	EVENTS:		onComplete
				onStart
	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
	LICENSE:	MIT License, <http://en.wikipedia.org/wiki/MIT_License>	
	REVISED:	January 2008
	EXAMPLE:	<input type="text" name="FooBar" title="Please provide your full name" />
				var tip = new FormTip('input[title]');
*/
var FormTip = new Class({
	Implements: Options,
	options: {
		className: 'FormTip',
		onComplete: Class.empty,
		onStart: Class.empty
	},
	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
			},
			'text':title
		});
		this.tip.inject(el.getParent(),'inside');
		this.tip.tween('opacity',1); 
	},
	hide: function() {
		this.tip.remove();
	}
});




