var featureTimer = null;
var featureOver = false;
var exploderWarning = false;

$(document).ready(function() {
	//check for ie destroying all of our hardwork
	if (typeof document.body.style.maxHeight === "undefined") {
		exploderWarning = true;
	} else if(document.all && !window.opera && window.XMLHttpRequest) {
		exploderWarning = true;
	}
	
	if (exploderWarning) {
		$('.feature_info').hide().click(function () {
			window.location = $(this).find('a').attr('href');
		});
	} else {
		$('.feature_info').css({opacity: 0}).click(function () {
			window.location = $(this).find('a').attr('href');
		});
	}
	
	if ($('.feature').length > 1) {
		animateFeature();
	}
	
	if ($('#photo_gallery .photo_with_caption').length > 1) {
		animatePhotoGallery();
	}
	
	// Home page feature
	$(".feature").mouseenter(function() {
		featureOver = true;
		if (exploderWarning) {
			$(this).find("div").stop().show();
		} else {
			$(this).find("div").stop().animate({opacity: 1}, {duration: 1000});
		}
	}).mouseleave(function() {
		featureOver = false;
		if (exploderWarning) {
			$(this).find("div").stop().hide();
		} else {
			$(this).find("div").stop().animate({opacity: 0}, {duration: 1000});
		}
	});
	
	$('#feature .feature .feature_info').hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); });
	
	$('.ui-dropdown').dropdown();
		
	$('#employment_info_form .checkbox').checkbox();

	// About Leaders
	$('#about_leaders').scrollbar({vertical: false, overflow: 'visible'});
	
	// Portfolio: project view actions
	$('#portfolio_container').scrollbar({vertical: false, overflow: 'visible'});
	
	$('#portfolio_preview li').hover(function() {
		$(this).find('.portfolio_img_cover').css({display: 'block'});
	}, function() {
		$(this).find('.portfolio_img_cover').css({display: 'none'});
	});
	
	$('#portfolio_preview li').click(function () {
		window.location = $(this).find('h3 a').attr('href');
	});
	
	$('#portfolio_list').scrollbar();
	
	$('#portfolio_list tbody tr').hover(function () {
		$(this).find('td').css({background: '#fd0', color: '#161616'});
		$(this).find('a').css({color: '#161616'});
	}, function () {
		$(this).find('td').css({background: '#161616', color: '#fff'});
		$(this).find('a').css({color: '#fff'});
	});
	
	$('#portfolio_list tbody tr').click(function () {
		window.location = $(this).find('a').attr('href');
	});
	
	// Portfolio: project detail thumbnail actions
	$('#project_thumbnails').scrollbar({vertical: false});
	$("#project_thumbnails li a").click(loadImage);
	$('.lightbox').lightbox();
	
		// Portfolio: project detail thumbnail actions
	$('#project_thumbnails2').scrollbar({vertical: false});
	
	$('.lightbox').lightbox();
	
	// Contact - Office
	$('#detailed_directions').click(function () {
		$('#directions').slideToggle();
		return false;
	});
	
	$(".contact_map li").hover(function () {
		var index = $(".contact_map li").index(this);
		$("#office_nav li").eq(index).addClass("hover");
	}, function () {
		var index = $(".contact_map li").index(this);		
		$("#office_nav li").eq(index).removeClass("hover");
	});
	
	$("#office_nav li").hover(function() {
		var index = $("#office_nav li").index(this);
		$(".contact_map a").eq(index).addClass("hover");
	}, function() {
		var index = $("#office_nav li").index(this);
		$(".contact_map a").eq(index).removeClass("hover");
	});
	
	$('table.press_list tbody tr').click(newsClick).hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); });
});

/**
 * Event Handlers
 */
	function loadImage() {
		if ($(this).hasClass('selected')) {
			return false;
		}
	
		$("#project_thumbnails a.selected").removeClass("selected");
		$(this).addClass("selected");
		
		var selected = $('#project_thumbnails a.selected').parent('li');
		var index = $("#project_thumbnails li").index(selected);
		
		var caption = $(this).attr('title');
		var newSrc = $(this).attr('href');
		var newFull = $(this).attr('name');
		$("#large_image_load").fadeIn(300);
	
		$("#large_image").animate({opacity: 0.1}, 300, function () {
			$("<img />").load(function () {
				var image = $(this);
				var width = image[0].width;
				var height = image[0].height;
			
				if (width > height) {
					width = Math.round((width / height) * 480);
					var left = ((width - 480) / 2) * -1;
					image.css({left: left}).attr('height', 480);
				}
				else {
					height = Math.round((height / width) * 480);
					var top = ((height - 480) / 2) * -1;
					image.css({top: top}).attr('width', 480);
				}
			
				$("#large_image").html(image);
				$('#caption').html(caption);
				
				$('.view_larger').hide();
				$('.view_larger').eq(index).css({display: "inline-block"});
								
				$("#large_image").animate({opacity: 1}, 400);
				$("#large_image_load").fadeOut(400);
			}).attr("src", newSrc);
		});
	
		return false;
	}
	
	function newsClick(event) {
		var newWindow = window.open($(this).find('a').attr('href'), '_blank');
		//newWindow.focus();
		return false;
	}
	

/**
 * Functions
 */
	function animateFeature()
	{
		featureTimer = setInterval(featureTick, 5000);
	}
	
	function featureTick()
	{
		if (!featureOver)
		{
			$('.feature:last').animate({opacity: 0}, {duration: 1000, complete: function () {
				if (exploderWarning)
				{
					$(this).find('.feature_info').hide();
				}
				else
				{
					$(this).find('.feature_info').css({opacity: 0});
				}
				
				$(this).prependTo($('#feature')).css({opacity: 1});
			}});
		}
	}
	
	function animatePhotoGallery()
	{
		featureTimer = setInterval(photoGalleryTick, 10000);
	}
	
	function photoGalleryTick()
	{
		$('#photo_gallery .photo_with_caption:last').animate({opacity: 0}, {duration: 1000, complete: function () {
			$(this).prependTo($('#photo_gallery')).css({opacity: 1});
		}});
	}
