Kingsview.Trigger = new Class({
	triggers:null,
	friends:[],
	over:null,
	out:null,
	click:null,
	blurTimeout:null,
	stopCurrentEvent:(function(){}),
	timeout:0,
	initialize:function(triggers,o) {
		if ($type(triggers) === 'array') {
			this.triggers = triggers;	
		} else {
			this.triggers = [triggers];
		}	
		for (var opt in o) {
			this[opt] = o[opt];
		}
		this.bindEvents();
	},
	bindEvents:function() {
		if ($chk(this.over)) {this.bindOver(this.over);}
		if ($chk(this.out)) {this.bindOut(this.out);}
		if ($chk(this.click)) {this.bindClick(this.click);}
		if ($chk(this.focus)) {this.bindFocus(this.focus);}
		if ($chk(this.blur)) {this.bindBlur(this.blur);}
	},
	bindEvent:function(which,fn) {
		this.triggers.each(function(trig,index) {
			trig.addEvent(which,function(event) {
				event = new Event(event);
				this.currentTimeout = setTimeout(function() {
					this._theEvent(event,which,trig,fn,index);
				}.bind(this),this.timeout);
			}.bind(this));
		}.bind(this));
	},
	_theEvent:function(event,which,trigger,fn,index) {
		this.friends.include(trigger);
		this.currentEvent = event;
		if (this.isFriend(event,trigger,which)) {
			return this.stopCurrentEvent();
		}
		if (which === 'blur' || which === 'focus') {
			if (which !== 'mouseout') {
				timeoutFunc = function() {
			      fn.attempt(event,index,trigger);
				};
				clearTimeout(this.blurTimeout);
				this.blurTimeout = setTimeout(timeoutFunc,10);	
			}	    
		} else {				
			fn.attempt(event,index,trigger);
		}
	},
	bindOver:function(fn) {
		this.bindEvent('mouseover',fn);
		this.bindEvent('focus',fn);
	},
	bindOut:function(fn) {
		this.bindEvent('mouseout',fn);
		this.bindEvent('blur',fn);
	},
	bindFocus:function(fn) {
	  this.bindEvent('focus',fn);
	},
	bindBlur:function(fn) {
	  this.bindEvent('blur',fn);
	},
	bindClick:function(fn) {
	  this.triggers.each(function(trig,index) {
			trig.addEvents({
			  'click':function(event) {
			    fn.attempt(index,this);
				  event.stop();
				},
				'keydown':function(event) {
				  if (event.key=='enter') {
				    window.location=this.href;
				    event.stop();
				  }				  
				}
			});
		});
	},
	isFriend:function(event,trigger,eventType) {
		var bool = false;
		
		this.friends.each(function(f,index) {	
				if ($type(f) === 'element') {
					if (f == event.target && f !== trigger){bool = true;}
					if (f == event.relatedTarget) {bool = true;}
					if (f.hasChild(event.relatedTarget)) {bool = true;}
					if (f.hasChild(event.target) && eventType !== 'mouseout' && f!==trigger) {bool = true;}	
				}				
		});
		
		
		return bool; 
	},
	makeFriends:function(friends) {
		if ($type(friends) === 'array') {
			this.friends = friends;	
		} else {
			this.friends = [friends];	
		}		
	},
	removeFriends:function() {
		this.friends = [];
	},
	/*Pulled this out of Mootools, because there was an error with not checking cancelBubble*/
	stopCurrentEvent:function() {
		try {
			if (typeof this.currentEvent !== 'undefined' && this.currentEvent !== null) {
				if(this.currentEvent.stopPropagation) {this.currentEvent.stopPropagation();}
				else if (typeof this.currentEvent.cancelBubble !== 'unknown') {
					this.currentEvent.cancelBubble=true;
				} 
				if(this.currentEvent.preventDefault) {
					this.currentEvent.preventDefault();
				} else if (this.currentEvent && typeof this.currentEvent.returnValue !== 'unknown') {
					this.currentEvent.returnValue=false;
				}
			}	
		} catch(e) {};
			 
	  return;
	}
});


Kingsview.Subnav = new Class({
	_open:false,
	container:null,
	links:null,
	containerTrigger:null,
	linkTrigger:null,
	currentContent:null,
	containerHeight:0,
	fadeEffect:null,
	initialize:function(links,container) {
		this.container = container;
		this.links = links;
		this.friends = [];
		this.containerHeight = parseInt(this.container.getStyle('height'),10);
		this.bindLinkEvents();
	},
	bindLinkEvents:function() {
		var me = this;
		var ts = [];
		var links = [];
		this.links.getElements('li').each(function(link,index) {
			links[index] = link;
			var a = link.getElement('a');
			this.friends.push(link);
			this.friends.push(a);
			this.friends.push(this.container);
			ts[index] = new Kingsview.Trigger(link,{
				friends:[this.container,link],
				over:function(e) {
					this.linkTrigger = ts[index];
					this.show(link);
				}.bind(this),
				out:function(e) {
					var removeContainer = true;
					if (this.isFriend(e.relatedTarget)) {
						removeContainer = false;
					}
					
					this.hide(removeContainer);
				}.bind(this)
			});
			link.onclick = function() {
				return false;
			}
		}.bind(this));
		
		this.containerTrigger = new Kingsview.Trigger(this.container,{
			out:function(e) {
				var removeContainer = true;
				var el = null;
				if (this.isFriend(e.relatedTarget)) {
					var el = e.relatedTarget;
					if (e.relatedTarget.getProperty('href')) {
						el = e.relatedTarget.getParent();
					}
					links.each(function(l,index) {
						if (el === l) {
							this.linkTrigger = ts[index];									
						}
					}.bind(this));
					removeContainer = false;
				}
				
				this.hide(removeContainer);
				if (el) {this.show(el);}
			}.bind(this)
		});
	},
	show:function(el) {
		var to = this.isOpen()?0:300;
		this.showTimeout = setTimeout(function() {
			var a = el.getElement('a');
			var anchor = "#" + a.getProperty('href').replace('#','');
			this.content = this.container.getElement(anchor);
			this.li = el;
			this.pad = this.content.getElement('.pad');
			var coords = this.links.getPosition();
			
			this.container.setStyles({
				position:'absolute',
				top:coords.y-this.containerHeight+3+'px',
				left:coords.x+'px'
			});
			if (!window.ie) {
				if (this.fadeEffect !== null) {this.fadeEffect.stop();}
				this.fadeEffect = new Fx.Style(this.pad,'opacity',{duration:700,wait:true});
				var fadeTimeout = null;
				this.fadeEffect.set(0);
				if (this.isOpen()) {
					clearTimeout(fadeTimeout);
					fadeTimeout = setTimeout(function() {
						this.fadeEffect.start(0,1);
					}.bind(this),200);
				} else {
					this.fadeEffect.start(0,1);
				}
				
			} else {
				if (this.isOpen()) {
					this.pad.setStyle('opacity',0);	
					setTimeout(function() {
						this.pad.setStyle('opacity',1);		
					}.bind(this),200);
				} else {
					this.pad.setStyle('opacity',1);
				}					
				
				
			}	
			
			this.li.addClass('gs_homeSubnavSelected');
			this.container.removeClass('hidden');
			this.content.removeClass('hidden');	
			this.containerTrigger.makeFriends(this.li);
			this._open = true;
		}.bind(this),to);					
	},
	hide:function(removeContainer) {
		removeContainer = typeof removeContainer !== 'undefined'?removeContainer:true;
		clearTimeout(this.showTimeout);
		if (removeContainer) {
			this.container.setStyles({
				position:'absolute',
				top:'-1000em',
				left:'-1000em'
			});
			this.container.addClass('hidden');
			this._open = false;
		}
		this.pad.setStyle('opacity',0);
		this.li.removeClass('gs_homeSubnavSelected');
		this.content.addClass('hidden');
		this.containerTrigger.removeFriends();
	},
	isOpen:function() {
		return this._open;
	},
	isFriend:function(el) {
		return this.friends.contains(el);
	}
});
