/*	---------------------------------------------------------------------------
	Override firebug when it doesn't exist
*/

if (!window.console || !console.firebug) {
	var firebug = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (i=0;i<firebug.length;++i) window.console[firebug[i]] = function() {}
}

/*	---------------------------------------------------------------------------
	CLASS:		Element
	METHOD:		setSelected(value)
	AUTHOR:		Ryan J. Salva, ryan@capitolmedia.com
	REVISED:	May 2009
	ABOUT:		Utility function designed to set the value of a select box
*/

Element.implement ({
	setSelected: function(value) {
		if (this.get('tag') == 'select') {
			var options = this.getElements('option');
			if ($defined(options) && options.length > 0) {
				options.each(function(option,index) {
					if (option.value == value) {
						this.selectedIndex = index;
					}
				}.bind(this));
			}
		} else {
			this.value = value;
		}
	}
});


/*	---------------------------------------------------------------------------
	CLASS:		Layout(el[,columns])
	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
	REVISED:	January 2008
	EXAMPLE:	<div id="Wrapper"><div id="Left">Foo</div><div id="Right">Bar</div></div>
				var x = new Layout('Wrapper',['Left','Right']);
	ABOUT:		Utility function designed to fix any layout using aboslute positioning for each column
				Adjusts the page to make wrapper as tall as the highest column (left, right, etc.)
				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.el.set('tween', {duration: 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.el.tween('height',y);
	}
});


/*	---------------------------------------------------------------------------
	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:		Modal([options]);
	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")
	EVENTS:		onHide				fires when closing the Modal box
				onShow				fires when showing the Modal box
				onStart				fires when first initializing the Modal box
				onStep				fires when stepping next or previous in a series
				onUpdate			fires when the window resizes
	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
	REVISED:	March 5, 2010
*/

Modal = new Class({
	Implements: [Events, Options],
	options: {
		'speed': 500,
		'maskColor': '#000',
		'maskOpacity': .3,
		'width': 400,
		'height': 200,
		'classPrefix': 'modal',
		'onHide': $empty,
		'onShow': $empty,
		'onStart': $empty,
		'onStep': $empty,
		'onUpdate': $empty
	},
	initialize: function(options) {
		this.setOptions(options);
		this.el = null;
		this.series = null;
		
		this.iframe = new Element('iframe',{
			'src': 'javascript:void(0);',
			'frameborder': 0,
			'scrolling': 'no',
			'styles':{
				'position': 'absolute',
				'top': 0,
				'left': 0,
				'width': '100%',
				'height': '100%',
				'opacity': 0
			}
		});

		this.mask = new Element('div', {
			'class':this.options.classPrefix+'Mask',
			'styles':{
				'position':'absolute',
				'top': 0,
				'left': 0,
				'opacity': 0,
				'width': '100%',
				'background':this.options.maskColor,
				'z-index': 9999
			},
			'events':{
				'click':function(e) {
					this.hide();
				}.bind(this)
			},
			'tween':{
				'duration':this.options.speed
			}
		});

		this.container = new Element('div',{
			'class':this.options.classPrefix+'Container',
			'styles':{
				'position': 'absolute',
				'visibility': 'hidden',
				'width': '100%',
				'margin': 0,
				'z-index': 10000
			},
			'events':{
				'click':function(e) {
					this.hide();
				}.bind(this)
			},
			'tween':{
				'duration': this.options.speed
			}
		});
		
		this.box = new Element('div',{
			'class':this.options.classPrefix+'Box',
			'styles':{
				'margin':'0 auto',
				'width': this.options.width,
				'height':this.options.height
			},
			'events': {
				'click': function(e) {
					e.stopPropagation();
				}
			},
			'morph': {
				'duration':this.options.speed
			}
		});

		this.message = new Element('div',{
			'class':this.options.classPrefix+'Message',
			'morph':{
				'duration':this.options.speed
			}
		});

		this.close = new Element('a', {
			'href':'#', 
			'text':'Close',
			'class':this.options.classPrefix+'Close',
			'events': {
				'click': function(e) {
					e.stop();
					this.hide();
				}.bind(this)
			}
		});
		
		this.next = new Element('a', {
			'href':'#',
			'text':'Next',
			'class':this.options.classPrefix+'Next',
			'events': {
				'click': function(e) {
					e.stop();
					this.step(1);
				}.bind(this)
			}
		});
		
		this.previous = new Element('a', {
			'href':'#',
			'text':'Previous',
			'class':this.options.classPrefix+'Previous',
			'events': {
				'click': function(e) {
					e.stop();
					this.step(-1);
				}.bind(this)
			}
		});

		// build our DOM
		this.mask.adopt(this.iframe);
		this.container.adopt(this.box)
		this.box.adopt(this.message, this.close);
		
		window.top.addEvent('keydown', function(e) {
			if(!this.el) return true;

			switch (e.key) {

				// close the modal on esc, backspace, delete, space or enter
				case 'esc':
					this.hide();
					break;

				// continue about our business
				default:
					break;
			}
		},this);
		
		window.top.addEvent('resize',function(e) {
			if (this.el) this.update();
		}.bind(this));
		
		this.fireEvent('onStart');
	},
	
	show: function(obj,series) {
		
		// paranoia: make sure there's nothing in this.box when we first open it
		if (this.el) this.el.dispose();
		
		// setup our styles before going visible
		this.mask.setStyles({
			'opacity':0,
			'height': $(window).getScrollSize().y
		});

		this.container.setStyles({
			'opacity':0,
			'top': $(window).getScroll().y + 100,
			'left':0,
			'visibility':'visible'
		});
		
		this.box.setStyles({
			'width':this.options.width,
			'height':this.options.height
		});
		
		this.message.setStyles({
			'opacity':0
		});
		
		// hide all select elements
		$$('select').setStyle('display','none');
		
		// add our mask and container to the DOM
		document.body.appendChild(this.mask);
		document.body.appendChild(this.container);
		
		// if we're showing a series of items, we have a few extra steps
		if (series && $type(series) == 'array') {
			
			// make sure we have one long list
			this.series = series.flatten();

			// display next/previous buttons
			this.box.adopt(this.previous, this.next);

			// find the current element in the series
			this.index = this.series.indexOf(obj);

			// if the element can't be found, put it at the end
			if (this.index < 0) this.series.include(obj);
		} else {
			this.series = null;
		}
		
		// animate into view
		this.state = 'mask';
		this.animate(obj);
		
		// fire the show event
		this.fireEvent('onShow');

		// return the element to be displayed
		return obj;
	},
	
	animate: function(obj) {
		this.timer = $clear(this.timer);
		
		// mask > load > resize > show > hide > load > resize > show > (repeat)
		switch (this.state) {
			case 'mask':
				// animate the dialog box into view
				this.mask.tween('opacity',this.options.maskOpacity);
				this.container.tween('opacity',1);
				this.state = 'load';
				this.timer = this.animate.delay(this.options.speed,this,obj);
				break;
			case 'load':
				switch($type(obj)) {
					case 'array':
						this.series = obj;
						this.animate(this.series[0]); // pluck the first element in the array and attempt to load it
						return null;
					case 'element':
						this.state = 'resize';
						this.animate(obj);
						return null;
					case 'string':
						obj = new Element('div',{'html':obj,'styles':{'width':this.options.width,'height':this.options.height}});
						this.state = 'resize';
						this.animate(obj);
						return null;
					case 'object':
						try {
							obj.send();
						} catch (error) {
							this.animate('Invalid request.');
						}
						return null;
					default:
						return false;
						break;
				}
				break;

			case 'resize':
				if (this.el) this.el.dispose();

				// temporarily add obj to the DOM so we can determine size
				obj.setStyles({
					'opacity':0,
					'visibility':'hidden',
					'display':'block',
					'position':'absolute',
					'top':0,
					'left':0
				});
				document.body.appendChild(obj);
				var size = obj.getSize();
				
				// resize the box to the dimensions of the new element
				this.box.morph({'width':size.x,'height':size.y});
				this.state = 'show';
				this.timer = this.animate.delay(this.options.speed,this,obj);
				break;

			case 'show':
				this.el = obj;
				this.el.setStyles({
					'opacity':1,
					'position':'static',
					'visibility':'visible'
				});
				this.message.adopt(this.el);
				this.message.morph({'opacity':1});
				this.state = null;
				break;

			case 'hide':
				this.message.morph({'opacity':0});
				this.state = 'load';
				this.timer = this.animate.delay(this.options.speed,this,obj);
				break;

			default:
				break;
		}
	},
	
	step: function(steps) {

		// only continue if we're in a series of elements
		if (!this.series || this.series.length <= 0) return false;
		
		this.index = this.index + steps;
		if (this.index < 0) this.index = this.series.length -1;
		if (this.index >= this.series.length) this.index = 0

		// which element do we display next?
		var obj = this.series[this.index];
		
		// if the element doesn't exist, abort mission
		if (!obj) return false;
		
		this.state = 'hide';
		this.animate(obj);
		this.fireEvent('onStep');
	},

	update: function() {
		this.mask.setStyle('height',$(window).getScrollSize().y);
		this.fireEvent('onUpdate');
	},

	hide: function() {
		
		 // take elements out of the DOM so we can reuse them next time
		this.container.dispose();
		this.next.dispose();
		this.previous.dispose();
		var el = this.el.dispose();
		
		// hide all select elements
		$$('select').setStyle('display','inline');
		


		// return everything to normal
		this.state = null;
		this.series = null;
		this.timer = $clear(this.timer);
		this.mask.fade('out');
		this.fireEvent('onHide');
		return el;
	}
});




