// GENERIC HOVER DELAY TRIGGER
var HoverDelay = Class.create({
	initialize : function(trigger, options){
		this.options = Object.extend({enterCb : function(){},	leaveCb : function(){},	delay : 0.5}, options || {});
		this.trigger = $(trigger);
		this.timeout = null; this.active = false;
		this.setup();
	},
	setup : function(){
		var eEvt = this.open.bindAsEventListener(this);
		var lEvt = this.close.bindAsEventListener(this);
		this.trigger.observe('mouseenter', eEvt);
		this.trigger.observe('mouseleave', lEvt);
		this.trigger.observe('hoverdelay:stop', function(){
			this.trigger.stopObserving('mouseenter', eEvt);
			this.trigger.stopObserving('mouseleave', lEvt);
		}.bind(this));
		document.observe('pop:active', function(){this.inactive=true;}.bind(this));
		document.observe('pop:inactive', function(){this.inactive=false;}.bind(this));
	}, 
	open : function(event){
		if (this.inactive) return;
		this.timeout = (function(){
			this.trigger.addClassName("active");
			this.options.enterCb.bind(this)();
			this.active = true;
		}).bind(this).delay(this.options.delay);
	},
	close : function(){
		if (this.inactive) return;
		if (this.timeout) {
			window.clearTimeout(this.timeout);
			this.timeout = null;
		}
		if (this.active){
			this.options.leaveCb.bind(this)();
			this.active = false;
			this.trigger.removeClassName("active");
		}
	}
});

var PopUp = Class.create({
	initialize : function(link, pop, opts){
		this.options = Object.extend({closeSelector : '.close', modal: false}, opts || {});
		this.pop = pop.setStyle({display:'none'});
		this.link = link;
		this.setup();
		this.pop.observe('pop:close', this.close.bind(this));
	},
	setup : function(){
		this.link.observe('click', this.open.bind(this));
		this.pop.select(this.options.closeSelector).each(function(el){
			el.observe('click', this.close.bind(this));
		}.bind(this));
	},
	open : function(ev){
		if (ev) ev.stop();
		this.pop.appear({duration:.2});
		if (this.options.modal)document.fire('pop:active');
	},
	close : function(ev){
		if (ev) ev.stop();
		this.pop.fade({duration:.2});
		if (this.options.modal) document.fire('pop:inactive');
	}
});

var KickOut = Class.create(PopUp, {
	setup : function(){
		new HoverDelay(this.link, {
			enterCb : this.open.bind(this),
			leaveCb : this.close.bind(this)
		});
	}
});

var ToolTip = Class.create(PopUp, {
	setup : function(){
		new HoverDelay(this.link, {
			enterCb : this.open.bind(this),
			leaveCb : this.close.bind(this),
			delay : 1
		});
	}
});
/************ tabs *************/
var Tabs = Class.create();
Tabs.prototype = {
	initialize: function(container, active) {
		this.container = $(container);

		this.togglers  = this.container.select('ul.togglers li');
		this.tabs      = this.container.select('.tabContent');
	
		this.active    = active || 0;
		this.setup();

	},
	setup: function() {
		this.tabs[this.active].addClassName('active');
		this.togglers[this.active].addClassName('active');
		this.togglers.each(function(el, i) {
			el.onclick = function() {
				if (i != this.active) {
					el.addClassName('active');
					this.togglers[this.active].removeClassName('active');
					
					if(this.tabs.length == this.togglers.length){
						this.tabs[this.active].removeClassName('active');
						this.tabs[i].addClassName('active');
					}
				}
				this.active = i;
				return false;
			}.bind(this);
		}.bind(this));
	}
}
var SimpleReveal = Class.create({
	initialize : function(container, options){
		this.container = $(container);
		this.options = Object.extend({toggleClass:'active', oLinkSel:'.open', cLinkSel:'.close', contentSel:'.content', duration:.5, cb:function(){}}, options || {});
		this.setup();
	},
	setup : function(){
		if (this.options.tLinkSel)
			this.toggles = this.container.select(this.options.tLinkSel).invoke('observe', 'click', this.open.bind(this));
		else{
			this.container.select(this.options.oLinkSel).invoke('observe', 'click', this.open.bind(this));
			this.container.select(this.options.cLinkSel).invoke('observe','click', this.close.bind(this));
		}
		this.content = this.container.select(this.options.contentSel).first().hide();
	},
	open : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.toggle(this.content, 'blind', {duration:this.options.duration, afterFinish:function(){this.effect=null;}.bind(this)});
		this._cleanUp();
	},
	close : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.BlindUp(this.content, {afterFinish:this._cleanUp.bind(this, true), duration:this.options.duration});
	},
	_cleanUp : function(reset){
		this.container.toggleClassName(this.options.toggleClass);
		this.options.cb.apply(this);
		if (reset) this.effect = null;
	}
});


document.observe('dom:loaded', function(){
	var divs = $('subnav_sites');
	if (divs)
		new KickOut(divs, divs.down("ul"));
	
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
});