$(document).ready(function(){
	
	// Cufon text replacement
	Cufon.replace('#nav, .contentHeader h2, .contentHeader h3, .expertiseDescription h3, .expertiseSubPage .subContentNav, .vml');
	
	
	// preload images
	$.preloadCssImages();
	
	// nav pull down 
	$("#nav li").hover(
		function(){
			var subnav = $(this).children().eq(2);			
			if (subnav.length>0) {		
				
				$(this).children().eq(0).addClass('active');				
				subnav.after('<div id="shadow"><div class="inner"></div></div>');
				var h = subnav.height();

				subnav
					.show()
					.css('opacity',0);
					
				$("#shadow")
					.css({
						height: 0,
						opacity: 0
					})
					.animate({
						height: h,
						opacity: 0.53
					}, 'fast');
					
				
				subnav.animate({
						opacity: 1
					}, 300);
				$("#shadow .inner").height(h-3);
			}
		},
		function() {
			var subnav = $(this).children().eq(2);
			if (subnav.length>0) {
				$(this).children().eq(0).removeClass('active');
				subnav.hide();	
				$("#shadow").remove();
				
			}
		}
	);
	
	
	
	// Home page auto adjust width of ul for slider
	if ( $("#home .mainArea").length>0 ) {
		var w = $("#home .mainArea ul li").width() * 935;
		$("#home .mainArea ul").width(w)
	}
		
	// home slider
	$("#home .projectCaption a, .btnHomeNext").click(function(){
		var url = $(this).attr('href');
		if (url!='#') {
			$("#home .mainArea").scrollTo(url, 200, {offset:-10});	
		}
		return false;
	});
	
	
	
	// Client hover
	$("#workClient .mainArea li").hover(
		function(){
			var $this = $(this);
			var url = $this.children().eq(0).attr('href');
			if (url!=undefined && url!='') {			
				$this.append('<div id="clientHover"></div>');
				$("#clientHover")
					.css('opacity',0)
					.animate({
						opacity: .6		 
					}, 'slow');
				$("#clientHover").prev().fadeIn('slow');
			} else {
				$this.css('cursor','default');
			}
		},
		function(){
			$("#clientHover").prev().fadeOut('slow');
			$("#clientHover").remove();
		}
	);
	// click event
	$("#clientHover").live('click', function(){
		// get url
		var url = $(this).prev().prev().attr('href');
		if (url!=undefined) {
			window.location = url;
		}
	});
	
	
	
	// Featured work landing page
	if ($("#workFeatured .mainArea li").length>0) {
		$("#workFeatured .mainArea li").css('opacity',.4);
	}
	$("#workFeatured .mainArea li").hover(
		function(){
			$(this).animate({
				opacity: 1				
			}, 'slow');	
			
			$(this).children().eq(1)
				.css({
					bottom: '-36px',
					opacity: 0
				})
				.show()
				.animate({
					bottom: '5px',
					opacity: .95
				}, 'fast');
		},
		function(){
			$(this).animate({
				opacity: .4				
			}, 'slow');	

			$(this).children().eq(1)
				.animate({
					bottom: '-36px',
					opacity: 0
				}, 'fast');
		}
	);
	
	
	
	// Featured work page details 
	if ( $("#workFeaturedProject .productThumbs li a").length>0 ) {
		var total_thumbs = $("#workFeaturedProject .slideshow li").length;
		
		$("#workFeaturedProject .productThumbs li a").css('opacity', .5);
		$("#workFeaturedProject .productThumbs li a.active").css('opacity', 1);
		
		// adjust ul width
		$($("#workFeaturedProject .slideshow ul")).width(total_thumbs*631);
		
	}
	$("#workFeaturedProject .productThumbs li a")
		.hover(
			function() {
				var $this = $(this);
				
				if ($this.hasClass('active')==false) {
					 $this.animate({
						opacity: 1
					}, 300);
				}
				
			}, 
			function() {
				var $this = $(this);
				if ($this.hasClass('active')==false) {
					 $this.animate({
						opacity: .5
					}, 300);
				}
			}
		)
		.click(function() {
			var $this = $(this);
			var li = $this.attr('href');
			$("#workFeaturedProject .productThumbs li a")
				.removeClass('active')
				.css('opacity', .5);
				
			$this.addClass('active').css('opacity', 1);		

			// change caption
			var title = $this.attr('title');
			if (title!='') {
				$("#workFeaturedProject .productFull p").html(title);	
			}			

			$("#workFeaturedProject .slideshow").scrollTo(li, 200);	
			return false;
		}
	);
	
	// move previous
	$("#workFeaturedProject .productFull .caption .prev").click(function(){
		var current_active = $("#workFeaturedProject .productThumbs li a.active");
		var current_thumb = current_active.attr('href').split('-')[1];
		var new_thumb = 0;
		
		if (current_thumb==1) {
			new_thumb = total_thumbs;			
		} else {
			new_thumb = current_thumb - 1;			
		}

		$("#workFeaturedProject .productThumbs li:eq("+ parseInt(current_thumb-1) +")").children()
			.removeClass('active')
			.css('opacity', .5);
			
		$("#workFeaturedProject .productThumbs li:eq("+ parseInt(new_thumb-1) +")").children()
			.addClass('active')
			.animate({
				opacity: 1
			}, 300);
		
		// change caption
		var title = $("#workFeaturedProject .productThumbs li:eq("+ parseInt(new_thumb-1) +")").children().attr('title');
		if (title!='') {
			$("#workFeaturedProject .productFull p").html(title);	
		}			
		
		$("#workFeaturedProject .slideshow").scrollTo('#product-'+new_thumb, 200);			
				
		return false;																	 
	});
	
	// move next 
	$("#workFeaturedProject .productFull .caption .next").click(function(){
		var current_active = $("#workFeaturedProject .productThumbs li a.active");
		var current_thumb = current_active.attr('href').split('-')[1];
		var new_thumb = 0;
		
		if (current_thumb==total_thumbs) {
			new_thumb = 1;			
		} else {
			new_thumb = parseInt(current_thumb) + 1;			
		}
		
		$("#workFeaturedProject .productThumbs li:eq("+ parseInt(current_thumb-1) +")").children()
			.removeClass('active')
			.css('opacity', .5);
			
		$("#workFeaturedProject .productThumbs li:eq("+ parseInt(new_thumb-1) +")").children()
			.addClass('active')
			.animate({
				opacity: 1
			}, 300);

		// change caption
		var title = $("#workFeaturedProject .productThumbs li:eq("+ parseInt(new_thumb-1) +")").children().attr('title');
		if (title!='') {
			$("#workFeaturedProject .productFull p").html(title);	
		}			

		$("#workFeaturedProject .slideshow").scrollTo('#product-'+new_thumb, 200);	
		
				
		return false;																	 
	});
	
	$("#beforePhotoShow").click(function() {
		$("#beforePhoto").slideDown('fast');
		$(".accomplishment .vml").css('opacity', .1);
		return false;
	});
	$("#beforePhotoClose").click(function() {
		$("#beforePhoto").slideUp('fast');
		$(".accomplishment .vml").css('opacity', 1);
		return false;
	});
	
	
	
	// Gallery 
	if ( $("#workGallery .thumbs").length>0 ) {
		var total_thumbs = $("#workGallery .thumbs li").length;
		$("#workGallery .thumbs li a").css('opacity', .4);
		$("#workGallery .thumbs li a.active").css('opacity', 1);

		// adjust the full image ul width
		$("#workGallery .mainArea .full ul").width($("#workGallery .mainArea .full li").length*852);
		
		// hide nav
		$("#workGallery .mainArea .full .movePrev, #workGallery .mainArea .full .moveNext").css('opacity', 0);
		$("#workGallery .mainArea .full .movePrev").hide();
	}
	
	$("#workGallery .thumbs li a")
		.hover(
			function() {
				var $this = $(this);
				if ($this.hasClass('active')==false) {
					 $this.animate({
						opacity: 1
					}, 300);
					 console.log($this);
				}
			},
			function() {
				var $this = $(this);			
				if ($this.hasClass('active')==false) {
					 $this.animate({
						opacity: 0.4
					}, 300);
					 console.log($this);
				}
			}
		)
		.click(function(){
			var $this = $(this);
			var id = $this.attr('href');
			var caption = $this.attr('title');		
			$("#workGallery .full .wrap").scrollTo(id, 200);	
			$("#workGallery .full p").html(caption);
			
			$("#workGallery .thumbs li a").css('opacity', .4).removeClass('active');
			$this.addClass('active').animate({ opacity: 1 }, 300);
			
			var index = $("#workGallery .thumbs li a").index($("#workGallery .thumbs li a.active"));
			if (index<total_thumbs-1) {
				$("#workGallery .mainArea .full .moveNext").show();
			}
			if (index>0) {
				$("#workGallery .mainArea .full .movePrev").show();
			}
						
			return false;
		});
	
	$("#workGallery .thumbs .prev").click(function(){
		
		var i = $("#workGallery .thumbs ul li").index( $('#workGallery .thumbs li a.current').parent() );
		
		if (i==0) {
			i = total_thumbs-13;
		} else {
			i = i - 1;
		}
		
		$("#workGallery .thumbs li a").removeClass('current');
		$("#workGallery .thumbs li:eq("+i+") a").addClass('current');
		$("#workGallery .thumbs .wrap").scrollTo($("#workGallery .thumbs li").eq(i), 200);
		return false;											   
	});
	$("#workGallery .thumbs .next").click(function(){
		
		var i = $("#workGallery .thumbs ul li").index( $('#workGallery .thumbs li a.current').parent() );
		
		if (i==total_thumbs-13) {
			i = 0;
		} else {
			i = i + 1;
		}
		
		$("#workGallery .thumbs li a").removeClass('current');
		$("#workGallery .thumbs li:eq("+i+") a").addClass('current');
		$("#workGallery .thumbs .wrap").scrollTo($("#workGallery .thumbs li").eq(i), 200);
		return false;											   
	});
	
	// move next
	$("#workGallery .mainArea .full .moveNext")
		.hover(
			function() {
				var index = $("#workGallery .thumbs li a").index($("#workGallery .thumbs li a.active"));				
				var new_index = index+1;
				if (new_index<total_thumbs) {
					$(this).animate({opacity: 0.75},300);
				}
			},
			function() {
				$(this).animate({opacity: 0},300);
			}
		)	
		.click(function(){
			var index = $("#workGallery .thumbs li a").index($("#workGallery .thumbs li a.active"));
			var new_index = index+1;
			if (new_index<total_thumbs) {
				var id = $("#workGallery .thumbs li a").eq(new_index).attr('href');
				var caption = $("#workGallery .thumbs li a").eq(new_index).attr('title');
				$("#workGallery .full .wrap").scrollTo(id, 200);
				$("#workGallery .full p").html(caption);
				
				$("#workGallery .thumbs li a").removeClass('active').css('opacity', .4);
				$("#workGallery .thumbs li a").eq(new_index).addClass('active').css('opacity', 1);
			} 
			if (index==total_thumbs-2) {
				$(this).hide();
			}
			
			if ($("#workGallery .mainArea .full .movePrev").css('display')!='block') {
				$("#workGallery .mainArea .full .movePrev").show();
			}
			
			return false;
		}
	);

	// move previous
	$("#workGallery .mainArea .full .movePrev")
		.hover(
			function() {
				var index = $("#workGallery .thumbs li a").index($("#workGallery .thumbs li a.active"));				
				var new_index = index-1;
				if (index>0) {
					$(this).animate({opacity: 0.75},300);
				}
			},
			function() {
				$(this).animate({opacity: 0},300);
			}
		)	
		.click(function(){
			var index = $("#workGallery .thumbs li a").index($("#workGallery .thumbs li a.active"));
			var new_index = index-1;
			if (index>0) {
				var id = $("#workGallery .thumbs li a").eq(new_index).attr('href');
				var caption = $("#workGallery .thumbs li a").eq(new_index).attr('title');
				$("#workGallery .full .wrap").scrollTo(id, 200);
				$("#workGallery .full p").html(caption);
				
				$("#workGallery .thumbs li a").removeClass('active').css('opacity', .4);
				$("#workGallery .thumbs li a").eq(new_index).addClass('active').css('opacity', 1);
			} 
			if (index==1) {
				$(this).hide();
			}
			
			if ($("#workGallery .mainArea .full .moveNext").css('display')!='block') {
				$("#workGallery .mainArea .full .moveNext").show();
			}
			
			return false;
		}
	);

	
	
	// Leadership
	$("#leadership .contentHeader ul li").hover(
		function() {
			$(this).prepend('<div id="leadershipHover"></div>');				
			$("#leadershipHover").css('opacity',0).animate({opacity: 0.38},300);
		},
		function() {
			$("#leadershipHover").remove();			
		}
	);
		
	$("#leadership #leadershipHover").live('click',function(){
		var url = $(this).next().attr('href');
		window.location = url;
	});
	
	if ($("#leadership .contentHeader ul li.active").length>0) {
			$("#leadership .contentHeader ul li.active").prepend('<div id="leadershipActive"></div>');				
			$("#leadershipActive").css('opacity',0).animate({opacity: 0.38},300);		
	}
	
	
	
	
	// Culture
	if ($("#culture .mainArea .thumbs li.active").length>0) {
		$("##culture .mainArea .thumbs li.active").prepend('<div id="leadershipActive"></div>');				
		$("#leadershipActive").css('opacity',0).animate({opacity: 0.38},300);		
	}
	$("#culture .mainArea .thumbs li").hover(
		function() {
			$(this).prepend('<div id="leadershipHover"></div>');				
			$("#leadershipHover").css('opacity',0).animate({opacity: 0.38},300);
		},
		function() {
			$("#leadershipHover").remove();			
		}
	);
	$("#culture #leadershipHover").live('click',function(){
		var url = $(this).next().attr('href');
		var caption = $(this).next().attr('title');
		
		$("#culture .mainArea .full img").attr('src', url);
		$("#culture .mainArea .full p").html(caption);
		
		$("#leadershipActive").remove();
		$(this).parent().prepend('<div id="leadershipActive"></div>');
		$("#leadershipActive").css('opacity',0).animate({opacity: 0.38},300);	
		
		return false;
	});	
	$("#culture .mainArea .full .prev").click(function(){
		var i = $("#culture .mainArea .thumbs li").index( $('#leadershipActive').parent() );
		
		if (i==0) {
			i = $("#culture .mainArea .thumbs li").length - 1;
		} else {
			i = i - 1;	
		}
		
		var a = $("#culture .mainArea .thumbs li:eq("+ i +") a");
		var url = a.attr('href');
		var caption = a.attr('title');
		
		$("#culture .mainArea .full img").attr('src', url);
		$("#culture .mainArea .full p").html(caption);
		
		$("#leadershipActive").remove();
		a.parent().prepend('<div id="leadershipActive"></div>');
		$("#leadershipActive").css('opacity',0).animate({opacity: 0.38},300);	
		
		return false;
	});
	$("#culture .mainArea .full .next").click(function(){
		var i = $("#culture .mainArea .thumbs li").index( $('#leadershipActive').parent() );

		if (i==$("#culture .mainArea .thumbs li").length - 1) {
			i = 0;
		} else {
			i = parseInt(i) + 1;	
		}
		
		var a = $("#culture .mainArea .thumbs li:eq("+ i +") a");
		var url = a.attr('href');
		var caption = a.attr('title');
		
		$("#culture .mainArea .full img").attr('src', url);
		$("#culture .mainArea .full p").html(caption);
		
		$("#leadershipActive").remove();
		a.parent().prepend('<div id="leadershipActive"></div>');
		$("#leadershipActive").css('opacity',0).animate({opacity: 0.38},300);	

		return false;
	});
	
	
	
	// scrollbars
	if ($('.scroll-pane').length>0) {
		$('.scroll-pane').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
		$('.jScrollPaneTrack').height($('.scroll-pane').parent().height());
	}
	
	
	
	// strategy hover state on sub nav
	$(".expertiseSubPage .subContentNav li a").hover(
		function() {
			$(this).addClass('activeHover');	
			Cufon.refresh();
		}, 
		function() {
			$(this).removeClass('activeHover');
			Cufon.refresh();
		}
	);
	
	
	// Map
	$(".flashArea li a").hover(
		function() {
			$(this).next().fadeIn();	
		}, 
		function() {
			if ($(this).next().hasClass('active')==false) {
				$(this).next().fadeOut();	
			}
		}
	);
	if ( $(".flashArea li a").length>0 ) {
		$(".flashArea li .active").fadeIn();
	}
	
	
	/**
	 * Modal Functionalities 
	 **/
	positionModal = function(){
		 var wWidth = window.innerWidth;
		 var wHeight = window.innerHeight;
	
		 if (wWidth==undefined) {
			 wWidth = document.documentElement.clientWidth;
			 wHeight = document.documentElement.clientHeight;
		 }
	
		 var boxLeft = parseInt((wWidth / 2) - ( $("#modal").width() / 2 ));
		 var boxTop = parseInt((wHeight / 2) - ( $("#modal").height() / 2 ));
	
		 // position modal
		 $("#modal").css('margin', boxTop + 'px 0 0 ' + boxLeft + 'px');		
	}

	loadModal = function(url, className){
		 $("body").append('<div id="modalBackground"></div>');
		 $("#modalBackground").css("opacity", 0).fadeTo("slow", "0.55");
		 $("body").append('<div id="modalWrapper"></div>');
		 $("#modalWrapper").append('<div id="modal"></div>');	
		 if (className!='') {
			 $('#modal').addClass(className);
		 }

		$.ajax({
			 url: url,
			 cache: false, 
			 success: function(html) {		
				 $("#modal").html(html);					
				 $("#modal").append('<a href="#" class="closeModal"><span class="hide">Close</span></a>');
				 positionModal();
				 Cufon.replace('.modalvml');
				 
				 if ( $(".progress").length>0) {
					 $(".progress").animate({width: '100%'}, 6000, function(){ closeModal(); });
				 }
			 }, 
			 error: function() {
				alert("Error loading page");
				closeModal();
			 }
		 });
		
		return false;
	}
	
	
	closeModal = function(){
		 $("#modalWrapper").remove();
		 $("#modalBackground").remove();
	}	

	// close modal trigger
	$(".closeModal, .cancelModal").live('click', function(){
		 closeModal();
		 return false;
	});	
		
				
	// open modal
	$('.modal').live('click', function(){
		var url = $(this).attr('href');
		loadModal(url);
		return false;
	});	
	
	$('#launchLogin').click(function(){
		var url = $(this).attr('href');
		loadModal(url, 'boxLogin');
		return false;								 
	});
	
	
	// delete file
	$('.delete').click(function(){
		var url = $(this).attr('href');
		loadModal(url, 'boxLogin');
		$('body').data('row', $(this).parent().parent());
		return false;								 
	});
	$("#btnContinueDelete").live('click', function(){
		closeModal();
		$('body').data('row').fadeOut();
		return false;											   
	});
	
	
	// file upload
	$('#file').MultiFile({
		STRING: {
			remove:'Remove', 
			icon_url: './images/'
		},
		list: '#fileContainer'
	});
	
	$("#uploadFile").click(function(){
		var url = './uploading.html';
		loadModal(url, 'boxLogin');
		return false;
	});

});