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

var Zoom = Class.create(PopUp, {
	setup : function(){
		new HoverDelay(this.link, {
			enterCb : this.open.bind(this)
		});
		this.pop.observe('mouseleave', this.close.bind(this));
	}
});

/* Prototype-based javascript window dimensions
http://textsnippets.com/posts/show/835 
----------------------------------------------------------------------------------------*/
Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
}

/* Center a DOM element, prototype based
http://textsnippets.com/posts/show/836
----------------------------------------------------------------------------------------*/
Position.Center = function(element, parent) {
	var w, h, pw, ph;
	var d = Element.getDimensions(element);
	w = d.width;
	h = d.height;
	Position.prepare();
	if (!parent) {
			var ws = Position.GetWindowSize();
			pw = ws[0];
			ph = ws[1];
	} else {
			pw = parent.offsetWidth;
			ph = parent.offsetHeight;
	}
	element.style.top = (ph/2) - (h/2) +  Position.deltaY + "px";
	element.style.left = (pw/2) - (w/2) +  Position.deltaX + "px";
}

var ToutSlide = Class.create({
	initialize : function(trigger){
	this.trigger = trigger;
	this.tout = trigger.down('.tout_info');
	this.fade = this.tout.down('p.fade');
	this.inittop = parseInt(this.tout.getStyle('top').replace('px',''));
	if (this.fade)
		this.to = this.inittop- this.fade.getHeight();
	else
		this.to = 0;
		new HoverDelay(this.trigger, {
			enterCb : this.show.bind(this),
			leaveCb : this.hide.bind(this),
			delay: .3
		});
	},
	show : function(){
		this.tout.morph({top:this.to+'px'},{duration:.4});
		if (this.fade) this.fade.appear();
	},
	hide : function(){
		this.tout.morph({top:this.inittop+'px'},{duration:.4});
		if (this.fade) this.fade.fade({duration:.3});
	}
});

var FormPop = Class.create({
	initialize : function(link, target){
		this.link = link;
		this.target = target;
		if(target.down('iframe'))
			this.iframe = target.down('iframe');
		else {
			this.iframe = new Element("iframe", {allowTransparency:"true",frameborder:"0",scrolling:"no",border:"0"});
			target.insert(this.iframe);
		}

		this.href = this.link.readAttribute('href').slice(0,-1)+'pop/';
		this.setup();
		if (this.link.id.indexOf('apply_') == 0){
			this.job_id = this.link.id.split('apply_')[1];
		}
			
	},
	setup : function(){
		this.link.observe('click', this.show.bind(this));
		this.boundScroll = this.scroll.bind(this);
		this.boundResize = this.resize.bind(this);
		document.observe('form:closed', function(){
			Event.stopObserving(window, 'scroll', this.boundScroll);
			Event.stopObserving(window, 'resize', this.boundResize);
		}.bind(this));
		document.observe('pop:resized', this.boundResize);
	},
	show : function(ev){
		ev.stop();
		this.iframe.writeAttribute('src',this.href+(this.job_id ? ('?gk_id='+this.job_id) : ''));
		Position.Center(this.iframe);
		this.target.appear({duration:.1});
		Event.observe(window, 'scroll', this.boundScroll);
		//Event.observe(window, 'resize', this.boundResize);
		this.scroll();
	},
	cleanup : function(){
		Event.stopObserving(window, 'scroll', this.boundScroll);
		//Event.stopObserving(window, 'resize', this.boundResize);
	},
	scroll : function(ev){
		Position.prepare();
		this.target.style.top = Position.deltaY+'px';
	},
	resize : function(o){
		Event.stopObserving(window, 'scroll', this.boundScroll);
		if (Position.GetWindowSize()[1] > o.memo)
			Event.observe(window, 'scroll', this.boundScroll);
	}
	});

var Tabs = Class.create();
Tabs.prototype = {
	initialize: function(container, active) {
		this.container = $(container);
		this.togglers  = this.container.select('.toggle li');
		this.tabs      = this.container.select('.tab_content');
		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));
	}
}
/************ slideshows *************/
var SlideShow = Class.create();
SlideShow.prototype = {
	initialize : function(initial, contents, delay){
		this.initial = initial;
		this.contents = contents;
		this.delay = delay;
		this.current = initial;
		this.start();
		this.show = null, this.hide = null;
	},
	start : function(){
		if (this.timer) return;
		this.timer = setTimeout(this.next.bind(this), this.delay * 1000); //new PeriodicalExecuter(this.next.bind(this), this.delay);
	},
	pause : function(){
		if (this.timer){
			clearTimeout(this.timer);
			this.timer = false;
		}
		this.hide ? this.hide.cancel() : 0; 
		this.show ? this.show.cancel() : 0;
	},
	next : function(){
		this.timer = false;
		if (this.current == this.contents.length-1)
			this.goTo(0);
		else
			this.goTo(this.current+1);
		this.start();
	},
	goTo : function(idx, dur){
		if (idx == this.current) return;
		this.hide = new Effect.Fade(this.contents[this.current], {duration : dur || 2});
		this.current = idx;
		this.show = new Effect.Appear(this.contents[this.current],{duration: dur || 2});
	}
};
document.observe('dom:loaded', function(){
	var divs = $('nav_divisions');
	if (divs)
		new KickOut(divs, divs.down("#subnav_divisions"));
	
	var respop = $('overlay_resume');
	$$('.submitresume').each(function(e){
		new FormPop(e, respop);
	});
	var pospop = $('overlay_position');
	$$('.submitposition').each(function(e){
		new FormPop(e, pospop);
	});
	$$('.tout_slide').each(function(t){
		new ToutSlide(t);
	});
	
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
});

/**************** contact map ********************/

$$('#contactFooterLinks li').each(function(ele){
	ele.observe('mouseover', function(){
		var ele_class = $('usMapRoll').className;
		$('usMapRoll').removeClassName(ele_class);
		$('usMapRoll').addClassName(this.id);
		ele.observe('mouseout', function(){
			$('usMapRoll').removeClassName(ele_class);
		});
	});
});



Effect.Transitions.EaseTo = function(pos) {
    return Math.pow(pos,0.25);
}; 

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
