
//	---------------------------------------------------------------------------
//	FUNCTION:	openWindow()
//	
//	Opens a new JS window


function openWindow (html, name, layout, width, height) {
	var action = "'" + html + "', '" + name + "', 'menubar='" + layout + ", 'width='" + width + ", 'height='" + height + ", 'toolbar='" + layout + ", 'scrollbars='" + layout + ", 'status='" + layout + ", 'resizeable='" + layout + ", 'toolbar='" +layout; 
	window.open(action);
}



//	---------------------------------------------------------------------------
//	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);
		});
	}
});

//	---------------------------------------------------------------------------
//	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

Modal = new Class({
	Implements: [Events, Options],
	options: {
		speed: 500,
		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
			},
			'class':classPrefix+'Mask'
		});
		this.pop = new Element('div',{
			'styles':{
				'position': 'absolute',
				'visibility': 'hidden',
				'width': '100%',
				'margin': 0,
				'z-index': 10000
			},
			'class':classPrefix+'Pop'
		});
		this.container = new Element('div',{
			'styles':{
				'margin':'0 auto'
			},
			'class':classPrefix+'Container'
		});
		this.close = new Element('div',{'class':classPrefix+'Close'}).adopt(new Element('a', {'href':'#', 'text':'Close'}));

		this.fade = new Fx.Tween(this.mask, {duration:this.options.speed});
		this.slide = new Fx.Tween(this.pop, {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},'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',{'text':title,'class':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('opacity',0.8);
		var slideTo = ((window.getSize().y - this.container.getSize().y) / 2 + window.getScroll().y);
		this.slide.start('top',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('top',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('top',slideTo).chain(function() {
			this.pop.destroy();
			this.fade.start('opacity',0).chain(function() {
				this.mask.destroy();
				this.isShowing = false;
				this.fireEvent('onHide');
			}.bind(this));
		}.bind(this));
	}
});



/*	---------------------------------------------------------------------------
	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;
	}
});


/*	---------------------------------------------------------------------------
	CLASS:		Layout(el[,columns])
	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
	LICENSE:	MIT License, <http://en.wikipedia.org/wiki/MIT_License>	
	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.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.get('tween',{property:'height',duration:100}).start(y);
	}
});


/*	---------------------------------------------------------------------------
	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();
	}
});


/*	---------------------------------------------------------------------------
	CLASS:		Fader(container[,options]);
	OPTIONS:	pause: amount of time each image appears onscreen in milliseonds (default: 5000)
				duration: animation speed in milliseconds (default: 1000)
				loop: true=repeat indefinitely, false=show each image once (default: true)
	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:	<div id="Gallery"><img src="A.jpg" /><img src="B.jpg" /><img src="C.jpg" /></div>
				var gallery = new Fader('Gallery');
				gallery.start()
*/

var Fader = new Class({
	Implements: [Options, Events],
	options: {
		pause: 5000,
		duration: 1000,
		loop: true,
		onComplete: $empty,
		onStart: $empty
	},
	initialize: function(container,options) {
		this.setOptions(options);
		this.container = $(container);
		this.imgs = this.container.getElements('a');
		this.imgs.setStyles({
			'position':'absolute',
			'top':0,
			'left':0,
			'opacity':0,
			'display': 'block'
		});
		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) $clear(this.periodical);
		this.next = (this.next==this.imgs.length-1)?0:this.next+1;
		
		this.imgs[this.next].tween('opacity',1);

		var prev = (this.next==0)?this.imgs.length-1:this.next-1;
		this.imgs[prev].tween('opacity',0);
	}
});



/*	---------------------------------------------------------------------------

	CLASS:		Validate
	VERSION:	2.0
	AUTHOR:		Samuel Birch
	ABOUT:		Form validation
	LICENSE:	Open Source MIT Licence
	
	MODIFIED:	Ryan J. Salva, http://www.capitolmedia.com
				Updated to be compatible with Capitol Media code library
	EXAMPLE:	<input type="text" name="FooBar" class="required email" />
				var valid = new Validate('myForm');
*/

var Validate = new Class({
	
	Implements: [Options,Events],
	options: {
		validateOnBlur: true,
		errorClass: 'error',
		errorMsgClass: 'errorMessage',
		dateFormat: 'MM/dd/yy',
		showErrorsInline: true,
		label: 'Please wait...',
		onFail: $empty,
		onSuccess: $empty
	},

	initialize: function(form, options) {
		this.setOptions(options);
		
		this.form = $(form);
		this.elements = this.form.getElements('.required, .email, .number, .date, .postcode');

		this.list = [];
		
		this.elements.each(function(el,i){
			if(this.options.validateOnBlur){
				el.addEvent('blur', this.validate.bind(this, el));
			}
		}.bind(this));
		
		this.form.addEvent('submit', function(e){
			var event = new Event(e);
			var doSubmit = true;
			this.elements.each(function(el,i){
				if(! this.validate(el)){
					event.stop();
					doSubmit = false
					this.list.include(el);
				}else{
					this.list.remove(el);
				}
			}.bind(this));
			
			if(doSubmit){
				if(this.options.onSuccess){
					event.stop();
					this.fireEvent('onSuccess');
				} else {
					this.form.getElement('input[type=submit]').setProperty('value',this.options.label);
				}
			}else{
				// this.getList();
				this.fireEvent('onFail');
			}
			
		}.bindWithEvent(this));
	},
	
	getList: function(){
		var list = new Element('ul');
		this.list.each(function(el,i){
			if(el.title != ''){
				var li = new Element('li',{'text':el.title});
			}
		});
		return list;
	},
	
	validate: function(el){
		var valid = true;
		this.clearMsg(el);
		
		switch(el.type){
			case 'text':
			case 'textarea':
				if(el.hasClass('required') && el.value == ''){
					valid = false;
					this.setMsg(el, 'This field is required.');
				} else {
					valid = true;
				}
				if (el.value != '') {
	
					if(el.hasClass('email')){
						var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
						if(el.value.toUpperCase().match(regEmail)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid email address');
						}
					}
					
					if(el.hasClass('number')){
						var regNum = /[-+]?[0-9]*\.?[0-9]+/;
						if(el.value.match(regNum)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid number');
						}
					}
					
					if(el.hasClass('postcode')){
						var regPC = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$/
						if(el.value.match(regPC)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid postcode');
						}
					}
					
					if(el.hasClass('date')){
						var d = Date.parse(el.value);
						if(d != null){
							el.value = d.toString(this.options.dateFormat);
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid date in the format: '+this.options.dateFormat.toLowerCase());
						}
					}
				}
					
				break;
				
			case 'select-one':
				if(el.hasClass('required') && el.selectedIndex != 0) {
					valid = true;
				} else {
					valid = false;
					this.setMsg(el, 'This field is required.');
				}
				break
					
			case 'checkbox':
				if(!el.checked){
					valid = false;
					this.setMsg(el);
				}else{
					valid = true;
				}
				break;
				
			case 'radio':
				var rad = $A(this.form[el.name]);
				var ok = false;
				rad.each(function(e,i){
					if(e.checked){
						ok = true;
					}
				});
				if(!ok){
					valid = false;
					this.setMsg(rad.getLast(), 'Please select an option');
				}else{
					valid = true;
					this.clearMsg(rad.getLast());
				}
				break;
				
		}
		return valid;
	},
	
	setMsg: function(el, msg){
		if(msg == undefined){
			msg = el.get('title');
		}
		if(this.options.showErrorsInline){
			if(el.error == undefined){
				var pos = el.getPosition(el.getOffsetParent());
				var width = el.getWidth();
				el.error = new Element('div',{
					'class':this.options.errorMsgClass,
					'styles':{
						opacity: 0,
						position: 'absolute',
						left: pos.x + width,
						top: pos.y
					},
					'text':msg
				});
				el.error.inject(el.getParent(),'inside');
				el.error.tween('opacity',1); 
			}else{
				el.error.set('text',msg);
			}
			el.addClass(this.options.errorClass);
		}
	},
	
	clearMsg: function(el){
		el.removeClass(this.options.errorClass);
		if(el.error != undefined){
			el.error.destroy();
			el.error = undefined;
		}
	}
	
});

function showSubscribe() {
	var subscribe = new Modal({width:420});
	var iframe = new IFrame({ 
		src: '/index.php/page/Display/subscribe',
		styles: { 
			'width': '100%', 
			'height': 300, 
			'border': 'none',
			'background':'#ffffff'
		},
		scrolling: 'no'
	}); 
	subscribe.show(iframe);
}


/*	---------------------------------------------------------------------------
	Initialize the page
*/

window.addEvent('domready',function() {
	// fix IE png transparency bug
	if(Browser.Engine.trident) $$('img[src$=png]').fix();
});
