$(document).ready(function() {
	var $body = $('body');
	$body.addClass('js');
	var $content = $('#content');
	
	$('ul li').filter(':first-child').addClass('f').end().filter(':last-child').addClass('l');
	
	/**
	* Contact Page
	* Google Maps replaces static image
	* Using static long/lat values as geocoder is not very accurate
	*/
	function createMarker($coord,$adr) {
		var lat = $coord.find('.latitude').text(); var lon = $coord.find('.longitude').text();
		point = new GLatLng(lat, lon);
		var marker = new GMarker(point);
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml($adr.parent().html());
		});
		return marker;
	}
	
	var $geo = $('#content div.geo'); $map = $('#map');
	if(typeof GBrowserIsCompatible!='undefined' && (window.GBrowserIsCompatible || GBrowserIsCompatible()) && $map.length==1 && $geo.length>0) {
		if(GMap2==false) return;
		
		$map.replaceWith('<div id="google-map"></div>');
		$body.addClass('has-googlemaps');
		
		var map = new GMap2($('#google-map').get(0));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		var zoom = 9; var point = new GLatLng(52.704194, -1.56641);
		var marker = new GMarker(point);
		map.setCenter(point, zoom);
		
		/**
		* Loop through all the GEO coordinates
		*/
		$geo.each(function(i){
			var marker = createMarker($(this),$(this).prev());
			map.addOverlay(marker);
		});

		
		$(document).unload(GUnload);
	}
	
	/**
	* The Team
	*/
	var $the_team = $('#the-team');
	var $person_info = $('#person-information');
	var delimiter = '';
	
	/**
	* Build the Information Panel
	*/
	var $person_info_h2 = $('h2', $person_info);
	var $person_info_h3 = $('h3', $person_info);
	var $person_info_h2_default = $('h2', $person_info).html();

	function add_person_html() {
		if($('h2', $person_info).length==0) {
			$person_info_h2 = $('<h2></h2>');
			$person_info_h2.appendTo($person_info);
		}
	}
		
	/**
	* When Person is hovered, populate the information panel
	* Only do this if more information on a person isn't present.
	*/
	if($person_info_h3.length=='0') {
		$('li a', $the_team).hover(
			function(){
				var $a = $(this); var $img = $('img', $a);
				var name = $img.attr('alt'); var role = $img.attr('title');
				var $name = $('<strong></strong>').text(name).addClass('fn');
				var $role = $('<em></em>').text(role).addClass('role');
				
				add_person_html();
				$person_info_h2.removeClass('empty').empty().append($name).append(' ' + delimiter + ' ').append($role);
			},
			function(){
				$person_info_h2.remove();
			}
		);
	}
	
	if($.fn.cycle) {
		/**
		* Loads images
		* Then cycles through them with the plugin
		* @uses cycle()
		*/
		var $images = $('img.cycle', $content).wrap('<div id="image-rotate"></div>').end().find('#image-rotate');
		if($images.length===1) {
			var section = $body.attr('class').match(/section-([a-z-]+)/)[1];
			$.get('/'+section+'/_images', function(data,textStatus){
				var settings = {'timeout' : 3000, 'speed' : 1500, 'fx' : 'fade'};
				var o = $.metadata ? $.extend({}, settings, $('img:first', $images).metadata()) : settings;			
				$images.append(data);
				if($('img', $images).length>1) {
					$images.cycle(o);
				}
			});
		}
	}
	
	if($.fn.flash) {
		/**
		* Flash banner above content
		* @uses flash()
		*/
		$('<div></div>').attr('id','flash').insertBefore('#branding').flash({
			src: '/img/header.swf',
			width: 600,
			height: 75,
			wmode: 'transparent'
		});
		
		/**
		* sIFR style replacement for headings
		* @uses flash()
		*/
		$('h1', $content).flash({  
			src: '/fonts/helvetica_condensed.swf', 
			flashvars: { 
				css: [
					'* { color: #990000; }',
					'a { color: #990000; text-decoration: none; }'
				].join(' ')
			}
        },
		{version: 7 },
		function(htmlOptions) {
			htmlOptions.flashvars.txt = this.innerHTML;
			this.innerHTML = '<div>'+this.innerHTML+'</div>';
			var $alt = $(this.firstChild);
			htmlOptions.height = $alt.height();
			htmlOptions.width = $alt.width();
			$alt.addClass('alt');
			$(this).addClass('flash-replaced').prepend($.fn.flash.transform(htmlOptions));						
        });

	}
	
	/**
	* Hide/Show the disciplines navigation
	*/
	var $disciplines = $('#disciplines');
	var $disciplines_p = $disciplines.children('p');
	var $disciplines_ul = $disciplines.children('ul');
	
	var disciplines_ul_width = $disciplines_ul.width();
	var disciplines_ul_height = $disciplines_ul.height(); disciplines_ul_height = 100;
	var disciplines_p_width = $disciplines_p.width()+11;
	var disciplines_cookie = 'disciplines';
	
	/**
	* Check for cookie
	* If cookie exists and set to open, then do not collapse the menu.
	*/
	/*
	if(typeof $.cookie != 'undefined' && $.cookie(disciplines_cookie) && $.cookie(disciplines_cookie)=='open') {
		$disciplines_p.addClass('open');
		$disciplines.css('width',disciplines_ul_width+disciplines_p_width);
		$('#flash').hide();
	}
	else {
		*/
		$disciplines_ul.css('width',0).css('height',0).hide();
		$disciplines.css('width',disciplines_p_width);
	//}	
	
	$('<a></a>').attr('href','#toggle').appendTo($disciplines_p).click(function(){
		var $a = $(this); var $p = $a.parent();
		
		if(!$disciplines_ul.is(':animated')) {
			$p.addClass('movement').removeClass('closed').removeClass('open');
			if($disciplines_ul.is(':hidden')) {
				$p.addClass('opening');
				$disciplines_ul.animate({'width' : disciplines_ul_width}, {'queue' : true, 'duration' : 250}, 'linear')
							   .animate({'height' : disciplines_ul_height}, 500, 'linear', function(){
					$p.removeClass('movement').removeClass('opening').addClass('open');
					if(typeof $.cookie != 'undefined') {
						$.cookie(disciplines_cookie, 'open', { path: '/', expires: 10 });
					}
				});
				$disciplines.animate({'width' : disciplines_p_width+disciplines_ul_width}, 250);
				$('#flash').fadeOut();
			}
			else {
				$p.addClass('closing');
				$disciplines_ul.animate({'height' : 0}, {'queue' : true, 'duration' : 500}, 'linear', function(){})
							   .animate({'width' : 0}, 250, 'linear', function(){
					$(this).hide();
					$p.removeClass('movement').removeClass('closing').addClass('closed');
					if(typeof $.cookie != 'undefined') {
						$.cookie(disciplines_cookie, 'closed', { path: '/', expires: 10 });
					}
					$disciplines.animate({'width' : disciplines_p_width}, 250);
				});
				$('#flash').fadeIn();
			}
		}
		
		$a.blur();
		return false;
	});
	
	$('li a', $disciplines_ul).each(function(){
		var $a = $(this); var $li = $a.parent();
		$('<em></em>').text($a.text()).appendTo($li);
	}).hover(
		function(){
			// show the text info
			var $a = $(this); var $li = $a.parent();
			$('em', $li).show();
		},
		function(){
			// hide the text info
			var $a = $(this); var $li = $a.parent();
			$('em', $li).hide();
		}
	);
	
	/**
	* Client Name Text
	*/
	var $clients = $('#our-clients');
	var $clients_ul = $('ul', $clients);
	var $client_text_p = $('<p></p>').attr('id','client-name');
	$client_text_p.insertAfter($clients_ul);
	
	$('li a', $clients_ul).hover(
		function(){
			// show the text info
			var $a = $(this); var $img = $a.children('img');
			var client = $img.attr('alt');
			$client_text_p.text(client);
		},
		function(){
			// hide the text info
			$client_text_p.empty();
		}
	);
});