$(function(){
	
	$('[href]')
		.css({cursor: 'pointer'})
		.click(function(e){
			var href = $(this).attr('href');
			if(e.metaKey || $(this).attr('target')=='_blank'){
			 	window.open(href);
			}else{
			 	window.location = href;
			}
			return false;
		});
	
	$("[href^='http://']").attr("target","_blank");
	
	$('.index_service')
		.css({cursor: 'pointer'})
		.mouseenter(function(){
			$(this).animate({opacity: 0.6});
		})
		.mouseleave(function(){
			$(this).animate({opacity: 1});
		});
		
	setTimeout(function(){
		$('.video_td').html('<iframe src="http://player.vimeo.com/video/34755082?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1&amp;loop=1" width="640" height="360" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
	},4000);
});

/*
 * TimeoutManager
 * 
 */
TimeoutManager = function(){
	var timeouts = [];
	var self = this;
	this.setTimeout = function(obj,functionName,time){
		var exists = false;
		for(var i=0; i<timeouts.length; i++){
			if(timeouts[i].obj==obj && timeouts[i].functionName==functionName){
				exists = true;
			}
		}
		if(!exists){
			var doFunction = function(){
				self.clearTimeout(obj,functionName);
				obj[functionName]();
			}
			timeouts.push({
				obj:obj,
				functionName:functionName,
				id: setTimeout(doFunction,time)
			});
		}
	}
	this.clearTimeout = function(obj,functionName){
		for(var i=0; i<timeouts.length; i++){
			if(timeouts[i].obj==obj && timeouts[i].functionName==functionName){
				clearTimeout(timeouts[i].id);
				timeouts.splice(i,1);
				i--;
			}
		}
	}
	this.getTimeout = function(obj,functionName){
		var timeout = false;
		for(var i=0; i<timeouts.length; i++){
			if(timeouts[i].obj==obj && timeouts[i].functionName==functionName){
				timeout = timeouts[i];
			}
		}
		return timeout;
	}
}
TimeoutManager = new TimeoutManager();

/*
 * AnimationBroadcaster
 * 
 */
AnimationBroadcaster = function(){
	var listeners = [];
	var animationInterval = false;
	this.FRAME_RATE = 20;
	this.addListener = function(obj,functionName){
		listeners.push({obj:obj,functionName:functionName});
		if(!animationInterval) this.activateBroadcaster();
	}
	this.removeListener = function(obj,functionName){
		for(var i=0; i<listeners.length; i++){
			if(listeners[i].obj == obj && listeners[i].functionName == functionName){
				listeners.splice(i,1);
				i--;
			}
		}
		if(listeners.length==0) this.deactivateBroadcaster();
	}
	this.activateBroadcaster = function(){
		animationInterval = setInterval(this.broadcast,Math.floor(1000/this.FRAME_RATE));
	}
	this.deactivateBroadcaster = function(){
		clearInterval(animationInterval);
		animationInterval = false;
	}
	this.broadcast = function(){
		for(var i=0; i<listeners.length; i++){
			listeners[i].obj[listeners[i].functionName]();	
		}
	}
}
AnimationBroadcaster = new AnimationBroadcaster();

/*
 * EaseFunctions
 * 
 */
EaseFunctions = function(){

//By Robert Penner http://www.robertpenner.com/easing/
this.easeInSine = function(t, b, c, d) {
	if(t==d) return b+c;
	return -c*Math.cos(t/d*(Math.PI/2))+c+b;
}

this.easeOutSine = function(t, b, c, d) {
	if(t==d) return b+c;
	return c*Math.sin(t/d*(Math.PI/2))+b;
}

this.easeInLinear = function(t, b, c, d) {
	if(t==d) return b+c;
	return c*t/d+b;
}

this.easeOutLinear = function(t, b, c, d) {
	if(t==d) return b+c;
	return c*t/d+b;
}
}
EaseFunctions = new EaseFunctions();

/*
 * Ease
 * 
 */
Ease = function(easeParams){
	
var interval;

var frameCounter = 0;

var easeParams;

this.easeParams = easeParams;

//Set up the beginning value.
if(easeParams.beginning==undefined){
	if(easeParams.getter){
		easeParams.beginning = parseFloat(easeParams.subject[easeParams.getter]());
	}else{
		easeParams.beginning = parseFloat(easeParams.subject[easeParams.property]);	
	}
	if(!easeParams.beginning) easeParams.beginning = 0;
}

//Set up the end object.
if(!easeParams.endObject){
	if(easeParams.endProperty){
		easeParams.endObject = easeParams.subject;
	}else{
		easeParams.endObject = {endProperty:easeParams.end};
		easeParams.endProperty = "endProperty";
	}
}

//Default callback object is the subject.
if(!easeParams.callbackObject) easeParams.callbackObject = easeParams.subject;

//Default ease function is easeOutSine.
if(!easeParams.easeFunction) easeParams.easeFunction = EaseFunctions.easeOutSine;

//Figure frames from seconds or set to default value.
if(!easeParams.frames){
	if(easeParams.seconds) easeParams.frames = Math.floor(easeParams.seconds*AnimationBroadcaster.FRAME_RATE);
	else easeParams.frames = 16;
}

this.enterFrame = function() {
	try{
		//Figure out the end value (this may change from frame to frame).
		if(easeParams.endObjectGetter){
			var endValue = parseInt(easeParams.endObject[easeParams.endObjectGetter]());
		}else{
			var endValue = parseInt(easeParams.endObject[easeParams.endProperty]);
		}
		
		//Figure out the current value.
		var currentValue = easeParams.easeFunction(
			frameCounter, 
			easeParams.beginning, 
			endValue-easeParams.beginning, 
			easeParams.frames
		);
		if(!easeParams.allowFractions) currentValue = Math.floor(currentValue);
		if(easeParams.unit) currentValue += easeParams.unit;
		
		//Set the current value.
		if(easeParams.setter){
			easeParams.subject[easeParams.setter](currentValue);
		}else{
			easeParams.subject[easeParams.property] = currentValue;
		}
		
		//Stop the ease if it is that time.
		if (frameCounter == easeParams.frames) {
			EaseManager.removeEase(this);
			if (easeParams.callback) easeParams.callbackObject[easeParams.callback]();
		}
		
		frameCounter++;
	}catch(e){
		//throw(e);
	}
}

//Set the subject to the beginning, in case it is not already there.
this.enterFrame();

}

/*
 * EaseManager
 * 
 */
EaseManager = function(){

var eases = [];

this.createEase =  function(easeParams){
	var ease = new Ease(easeParams);
	this.removeEase(ease);
	eases.push(ease);
	AnimationBroadcaster.addListener(ease,'enterFrame');
}

this.removeEase = function(ease){
	for(var i=0; i<eases.length;i++){
		//If the subject matches and either the property or setter matches,
		//then remove the ease.
		if(eases[i].easeParams.subject==ease.easeParams.subject
		   && (
		   ease.easeParams.property && eases[i].easeParams.property==ease.easeParams.property ||
		   ease.easeParams.setter && eases[i].easeParams.setter==ease.easeParams.setter
		   )
		){
			AnimationBroadcaster.removeListener(eases[i],'enterFrame');
			eases.splice(i,1);
		}
	}	
}
}
EaseManager = new EaseManager();

/*
Provided as documentation only

EaseParams = function(){

//object and property to ease
this.subject;
this.property;

//the getter and setter method to use if
//a property is not available
this.getter;
this.setter;

//the units - %,px or whatever
//this is in case you want to do css
//properties where the value is actually 
//a string in a silly format: 300px or 75%
this.unit;

//in general, the value is rounded to an integer,
//unless we specify otherwise here
this.allowFractions;

//duration of ease
this.frames = 16;
this.seconds;

//start point of ease (default is current value of ease property)
this.beginning;

//end point of ease (either a number OR an object and property name)
this.endObject;
this.endProperty;
this.end;//Number

//endObject can have a getter instead of a property
this.endObjectGetter;

//callback when ease is complete
this.callbackObject;
this.callback;

//function to use for the ease
this.easeFunction;

}
*/

/*
 * MenubarManager
 * 
 */
MenubarManager = function(){
	var menubarIsActivated = false;
	var menus = [];
	var buttons = [];
	var activatedButton;
	var self = this;
	this.activateMenubar = function(button){
		if(!menubarIsActivated){
			menubarIsActivated = true;
			activatedButton = null;
			this.setActivatedButton(button);
		}
	}
	this.deactivateMenubar = function(){
		try{
			menubarIsActivated = false;
			self.setActivatedButton(null);
		}catch(e){
			//throw(e);
		}
	}
	this.isActivated = function(){
		return activated;
	}
	this.setupMenubar = function(){
						
		//The navbar starts the deactivate timer onmouseout.
		document.getElementById('navbar').onmouseout = function(e){
			TimeoutManager.setTimeout(MenubarManager,"deactivateMenubar",1200);
		}
		document.getElementById('navbar').onmouseover = function(){
			TimeoutManager.clearTimeout(MenubarManager,"deactivateMenubar");
		}
	
		//We distinguish nav elements from other elements by their class names,
		//and assign functions and properties appropriately.
		
		var navElements = document.getElementsByTagName('*');
		for(var i=0; i<navElements.length; i++){
			var e = navElements[i];
			
			
			switch(e.className){
				case 'navbar_button':
					//Set the highlight color.
					e.highlightColor = '#ede1c1';
				break;
				case 'menu_button':
					//Set the highlight color.
					e.highlightColor = '#cbcbcb';
					//Add to the parent menu button collection.
					if(!e.parentNode.buttons) e.parentNode.buttons = [];
					e.parentNode.buttons.push(e);
				break;
			}
			
			switch(e.className){
				case 'menu_button':
				case 'cascading_menu':
					//Assign the parentMenu property.
					if(e.parentNode.className=='cascading_menu'){
					   e.parentMenu = e.parentNode;
					}
				break;
			}
			
			//Add the actions and stuff.
			switch(e.className){
				case 'navbar_button':
				case 'menu_button':
					//Mix in functions from the MenuButton prototype.
					ObjectTools.mixIn(e,MenuButton);
					//Assign the parentMenu property.
					if(e.parentNode && e.parentNode.className=='cascading_menu'){
						e.parentMenu = e.parentNode;
					}
					//Remember the background color.
					e.backgroundColor = e.style.backgroundColor;
					//Add to the buttons array.
					buttons.push(e);				
				break;
				case 'cascading_menu':
					//Mix in functions from the Menu prototype.
					ObjectTools.mixIn(e,Menu);
					//Assign this to a button as it's submenu.
					e.previousSibling.submenu = e;
					//Set the visibility.
					e.style.display = 'none';
					//Add to the menus array.
					menus.push(e);
				break;
			}
		}
	}
	this.setActivatedButton = function(button){		
		
		if(activatedButton!=button){
			activatedButton = button;
			
			//Figure out which menus to keep.
			var keepers = [];
			
			if(activatedButton){
				
				TimeoutManager.clearTimeout(activatedButton.parentMenu,"hide");
				
				//If the button has a submenu, show the menu, 
				//and mark the menu as a keeper.
				if(activatedButton.submenu && menubarIsActivated){
					activatedButton.submenu.show();
					keepers.push(activatedButton.submenu);	
				}
				
				//If the button has a parent menu, mark the menu,
				//and it's ancestors, as keepers.
				var menu = activatedButton.parentMenu;
				if(menu) do{
					keepers.push(menu);
				}while(menu = menu.parentMenu);
			}
			//Reset button backgrounds.
			for(var i=0; i<buttons.length; i++){
				if(buttons[i]!=activatedButton){
					if(!ObjectTools.contains(buttons[i].submenu,keepers)){
						buttons[i].resetBackgroundColor();
					}
				}
			}
			//Hide the other menus.
			for(var i=0; i<menus.length; i++){
				if(!ObjectTools.contains(menus[i],keepers)){
					//Do we need to get out of the way of another menu?
					if(!activatedButton || activatedButton.submenu){
						//Hide the menu NOW.
						menus[i].hide();
					}else{
						//Hide the menu in a little bit.
						menus[i].hideLater();
					}
				}
			}	
		}
	}
}
MenubarManager = new MenubarManager();

MenuButton = function(){
	this.isOver;
	this.onmouseover = function(e){
		try{
			if(DomTools.getEventTarget(e)==this){
				this.style.backgroundColor = this.highlightColor;
				MenubarManager.setActivatedButton(this);
			}
		}catch(e){
			//throw(e);
		}
	}
	this.resetBackgroundColor = function(){
		this.style.backgroundColor = this.backgroundColor;
	}
	this.onclick = function(){
		if(!this.href) MenubarManager.activateMenubar(this);
	}
}
MenuButton = new MenuButton();

Menu = function(){
	this.show = function(){
		TimeoutManager.clearTimeout(this,"hide");
		this.style.zIndex = 1;
		EaseManager.createEase({
			subject:this,
			setter:'setOpacity',
			getter:'getOpacity',
			end:1,
			allowFractions:true
		});
		this.style.display = 'block';
	}
	this.hide = function(){
		//is it already hidden?
		if(this.style.display == 'none') return;
		TimeoutManager.clearTimeout(this,"hide");
		this.style.zIndex = 0;
		EaseManager.createEase({
			subject:this,
			setter:'setOpacity',
			getter:'getOpacity',
			end:0,
			allowFractions:true,
			callbackObject:this,
			callback:'finishedHiding'
		});
	}
	this.hideLater = function(){
		//is it already hidden or hiding?
		if(this.style.display == 'none' || TimeoutManager.getTimeout(this,"hide")) return;
		TimeoutManager.setTimeout(this,"hide",1000);
	}
	this.finishedHiding = function(){
		this.style.display = 'none';
	}
	this.setOpacity = function(opacity){
		for(var i=0; i<this.buttons.length; i++){
			if(this.buttons[i].filters){
				this.buttons[i].filters.item(0).opacity = Math.floor(opacity*100);	
			}else{
				this.buttons[i].style.opacity = opacity;
			}
		}
	}
	this.getOpacity = function(){
		var opacity;
		if(this.buttons[0].filters){
			opacity = this.buttons[0].filters.item(0).opacity/100;
		}else{
			opacity = this.buttons[0].style.opacity;
		}
		if(!opacity) opacity = 0;
		return opacity;
	}
}
Menu = new Menu();

ObjectTools = function(){
	this.mixIn = function(iceCream,cherries){
		for(name in cherries){
			iceCream[name] = cherries[name];	
		}
	}
	this.contains = function(needle,haystack){
		for(var i=0; i<haystack.length; i++){
			if(needle==haystack[i]) return true;
		}
		return false;
	}
}
ObjectTools = new ObjectTools();

DomTools = function(){
	this.getEventTarget = function(e){
		//Credit Peter-Paul Koch
		//quirksmode.org
		var target;
		if (!e) var e = window.event;
		if (e.target) target = e.target;
		else if (e.srcElement) target = e.srcElement;
		if (target.nodeType == 3) // defeat Safari bug
			target = target.parentNode;
		return target;
	}
}
DomTools = new DomTools();

log = function(message){
	//if(console) console.log(message);
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(document.createTextNode(message));
	body.appendChild(document.createElement('br'));
}

MenubarManager.setupMenubar();

//replace headings with images

function replace_with_image(p){
	
	var div = p.div;
	delete p.div;
	
	//grab the text
	var textEl = div.firstChild;
	p.text = textEl.nodeValue;
	div.removeChild(textEl);
	
	//create the query string
	var query = [];
	for(var name in p){
		query.push(escape(name)+"="+escape(p[name]));
	}
	query = "?"+query.join("&");
	
	//create the image
	var e = document.createElement("img");
	e.src = "heading.php"+query;
	e.style.display = "block";
	div.appendChild(e);
}

var elements = document.getElementsByTagName('*');
for(var i=0; i<elements.length; i++){
	var e = elements[i];
	switch(e.className){
		case 'page_heading':
			replace_with_image({div: e, size: 24, pad_bottom: 8, font: "frutiger_light"});
		break;
		case 'page_subheading':
			replace_with_image({div: e, color: "a6a66b", size: 12, font: "frutiger_italic"});
		break;
		case 'thumb_heading':
			replace_with_image({div: e, size: 11, pad_bottom: 4});
		break;
		case 'thumb_subheading':
			replace_with_image({div: e, color: "a6a66b", size: 11, font: "frutiger_bold_italic"});
		break;
	}
}
