$(function() {

	/* Home Page slideshow */

	if($(".home .slide").size() > 0) {

		/* This function fills the Home Page slide nav with equal height links */

		$.slideThumbs = [];
		$.slideThumbs.number = $("#slideshow-overlay ul li").size();
		$.slideThumbs.parentHeight = $("#slideshow-overlay").height();
		$.slideThumbs.height = ($.slideThumbs.parentHeight / $.slideThumbs.number) + 'px';
		
		$("#slideshow-overlay ul li a").css({
			lineHeight: $.slideThumbs.height
		});

		/* Cycle Home Page Slides */
		
		$(".home #slides").cycle({
			speed: 550,
			timeout: 5000,
			pause: true,
			pager:  '#slideshow-overlay ul', 
			pagerAnchorBuilder: function(idx, slide) { 
				return '#slideshow-overlay ul li:eq(' + idx + ') a'; 
			} 
		});
		
		$('#slideshow-overlay ul li a').click(function() { 
			$('.home #slides').cycle('pause'); 
		});
		
	}

	/* Kinetic Scroller for History Page */
	
	if($("#scroller-container").length) {$("#scroller-container").kinetic({x:true,y:false,maxvelocity:7,slowdown:0.8});
		$('#scroll-left').click(function(){
			$('#scroller-container').kinetic('start', { velocity: -1 });
			return false;
		});
		$('#scroll-right').click(function(){
			$('#scroller-container').kinetic('start', { velocity: 1 });
			return false;
		});	
	}

	
	if(!($("body").hasClass("ie7")) && !($("body").hasClass("ie8"))) {
	
		/* Clocks on Contact Page */
		
		if($("#office-map").length) {CoolClock.findAndCreateClocks()}
	
	}
	
	/* Colorbox first call */

	$("a.modal").colorbox(boxOptions);
	$("a.open-form").colorbox(formOptions);
	
	/* Contact Page AJAX */
	
	/* Get hash and reflect shown offices on map. */
	if($(".contact").length) {
	
		/* AJAX Call */
		if(window.location.hash) {
			var hash = window.location.hash;
			var sendData = hash.replace('#','');
			doContactAjax(sendData,'load');
		} else {
			$("#contact-services li a:first").addClass("active");
		}
	
		$("#contact-services a").click(function(e) {
		
			e.preventDefault();
			var fullHash = $(this).attr("href");
			var hash = fullHash.replace('title-','');
			window.location.hash = hash;
			
			var sendData = $(this).attr("rel");
	
		});
		
		$(window).bind('hashchange', function() {
			
			var hash = window.location.hash;
			var sendData = hash.replace('#','');
			doContactAjax(sendData,'click');
			
		});

	}
	
	if($(".companies").length) {
	
		var hash = window.location.hash;
		
		if(!hash) {
			$(".companies article").not(":first").hide();
			$(".companies #side-navigation li:first").addClass("active");
		} else {
			$(".companies article").not(hash).hide();
			$('a[href="'+hash+'"]').parent().addClass("active");
			$("html").scrollTop(0);
		}
		
		$('.companies #side-navigation a[href^="#"]').click(function(e) {
			e.preventDefault();
			
			var parent = $(this).parent();
			var panel = $(this).attr("href");
			var hash = panel.replace('#','');
			
			var scrollto = $("html").scrollTop();

			window.location.hash = hash;
			$("html").scrollTop(scrollto);
			
		});
		
		$(window).bind('hashchange', function() {
		
			if(window.location.hash != '') {
				var panel = window.location.hash;
			} else {
				var panel = "#home";
			}
			
			var panelLink = $('a[href="'+panel+'"]').parent();
			
			$(".companies article:visible").not(panel).fadeOut(100, function() {
				$(panel).fadeIn(200);
				$("#side-navigation li").not(panelLink).removeClass("active");
				$(panelLink).addClass("active");
			});
			
		});
	
	}
	
	/* Get rid of Companies link (for now) */
	
	$('a[href="/companies/"]').each(function() {
		var value = $(this).text();
		$(this).replaceWith('<span>'+value+'</span>');
	});

});

/* Contact AJAX */

function doContactAjax(f,g) {
	if(typeof f != 'undefined') {
		var link = $('a[rel="'+f+'"]');
		$.ajax({
			type: 'GET',
			url: '/_ajax/contact',
			data: {'url_title': f},
			success: function(data) {
			
				var response = $(data);
			
				if(typeof g != 'undefined' && g == 'click') {
					$("#offices").stop().fadeTo(150, 0, function() {
						$("#offices").html(data).fadeTo(200, 1);
						$("a.modal").colorbox(boxOptions);
						$("a.open-form").colorbox(formOptions);
					});
				} else {
					$("#offices").html(data);
					$("a.modal").colorbox(boxOptions);
					$("a.open-form").colorbox(formOptions);
				}

				$(".marker").removeClass("active");
				
				$("#contact-services li a").not(link).removeClass("active");
				$(link).addClass("active");
				
				var offices = response.find(".office");
				
				$(offices).each(function() {
					var markerLocation = '#'+$(this).attr("rel");
					if($.browser.msie) {$(markerLocation).hide().addClass("active").show();} else {$(markerLocation).hide().addClass("active").fadeIn(150);}
				});
				
				var modal = response.find(".modal");
				var form = response.find(".open-form");
				
			}
		});
	} else {
		console.log("Please specify AJAX $_GET data.");
	}
}

/* Colorbox Settings */

var boxOptions = {iframe: true, innerWidth: 900, innerHeight: 480, fixed: true, opacity: 0.8, fastIframe: false}
var formOptions = {iframe: true, innerWidth: 500, innerHeight: 440, fixed: true, opacity: 0.8, scrolling: false, transition: 'none', fastIframe: false}
