// JavaScript Document

$(window).resize(function(){
	setListSpace();
	getWindowSize();
	setSubNav();
});

$(document).ready(function(){
	if($('#mainContent').css('display') == 'none') {
		$('#wrapper').css({'overflow': 'auto'});
	}
	// function to set width and scroll for project/portfolio list
	setListSpace();
	// function to display/hide next and previous arrows on portfolio pages
	setArrows();
	//get the width of the window
	getWindowSize();
	// add active arrow to current project
	 setActiveProject();
	
	//apply width and mouseover effects for homepage images
	$('#homePortfolio').mouseover(function(e) {
		var wrapperWidth = $('#wrapper').width();
		var portfolioWidth = $('.portRow').width();
		var homeLeft = $('#homePortfolio').offset().left;
		var portfolioLeft = $('.portRow').css('left');
		var portfolioLeft = portfolioLeft.replace('px','')
		var offsetLeft = e.pageX;
		var scrollSet = wrapperWidth/2;
		var scrollAmount = wrapperWidth - portfolioWidth;
		var scrollRemaining = portfolioLeft - scrollAmount;
		var scrollSpeed =  Math.abs(scrollRemaining * 5);
		//alert(scrollSpeed);
		if(offsetLeft < scrollSet) {
			$('.portRow').stop().animate({'left': '0'}, 1000);
			$('#mainContent').stop().animate({'margin-left': '-12px', 'padding-right': '0px'}, 500);
		}
		else if(offsetLeft > scrollSet) {
			$('.portRow').stop().animate({left: scrollAmount}, 1000);
			$('#mainContent').stop().animate({'margin-left': '0px', 'padding-right': '12px'}, 500);
		}
	});
	$('#homePortfolio').mouseout(function (e) {
		$('.portRow').stop();
		$('#mainContent').stop();
	});
	$('.portDetail').live('mouseover',function(){
		$(this).children('.portCaption').show();
		$(this).css({'z-index': 100});
	});
	
	$('.portDetail').live('mouseout', function(){
		$(this).children('.portCaption').hide();
		$(this).css({'z-index': 0});
	});
	$('.portCaption').live('click', function(){
		var projectLink = $(this).find('.projName a').attr('href');
		window.location = projectLink;
	});
	
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version.substr(0,3)<"8.0")
		fixIE();
	});	
	
	//display portfolio subnav
	$('li a.portfolio').click(function() {
		$(this).parent().addClass('openNav');
		setSubNav();
		$('.portfolio ul').toggle(400);
		return false;
	});
	
	
	
	//gallery thumbnail navigation
	$('#galleryNav li a').click(function() {
		var clicked = this;
		var imgToShow = $(clicked).attr('href');

		$('#galleryNav li a').each(function() {
			if(this != clicked) {
				$(this).removeClass('active');
			}
			else {
				$(this).addClass('active');
			}
		});
		
		$('#gallery img').each(function() {
			if($(this).attr('src') == imgToShow) {
				$(this).addClass('imgToShow');
			}
			else if($(this).attr('src') != imgToShow && $(this).hasClass('current')) {
				$(this).animate({opacity: 0}, function() {
					$(this).removeClass('current');
					$(this).removeAttr("style");
					$('#gallery img').each(function() {
						if($(this).hasClass('imgToShow')) {
							$(this).removeClass('imgToShow').addClass('current');
						}
					});
					setArrows();			
				});
			}
		});
		return false;
	});
	
	//gallery next/previous navigation
	$('#nextPrev div, #nextPrevLg div').click(function() {
		imgArr = jQuery.makeArray($('#gallery img'));
		if($(this).hasClass('next')) {
			if($('#gallery img.current').next('img').length > 0) {
				$('#gallery img.current').next('img').addClass('imgToShow');
			}
			else{
				$('#gallery img:first').addClass('imgToShow');
			}
		}
		else if($(this).hasClass('previous')) {
			if($('#gallery img.current').prev('img').length > 0) {
				$('#gallery img.current').prev('img').addClass('imgToShow');
			}
			else{
				$('#gallery img:last').addClass('imgToShow');
			}
		}
		$('#gallery img.current').animate({opacity: 0}, function() {
			$(this).removeClass('current');
			$(this).removeAttr("style");
			$('#gallery img.imgToShow').removeClass('imgToShow').addClass('current');
			
			$('#gallery img').each(function(i,val) {
				if($(val).hasClass('current')) {
					var currIndex = i;
					var activeThumb = $('#galleryNav li a').get(currIndex);
					$('#galleryNav li a').each(function() {
						if(this != activeThumb) {
							$(this).removeClass('active');
						}
						else {
							$(this).addClass('active');
						}
					});
				}
			});
			setArrows();
		});
	});
	
	// Expertise script to pull in content from different expertise pages
	var timeout = null;
	var expertiseDefault = $('#expertiseContent').html();
	$('#expertiseNav a').click(function() {
		return false;
	});
	$('#expertiseNav ul li a').mouseover(function() {
		var activeLink = this;
		$(this).parent('li').addClass('active');
		$('#expertiseNav ul li a').each(function() {
			if(this != activeLink && $(this).parent('li').hasClass('active')) {
				$(this).parent('li').removeClass('active');
			}
		});
		var contentLink = $(this).attr('href');
		if (timeout) {
			clearTimeout(timeout);
			timeout = null;
		}
		timeout = setTimeout(loadExpertise, 100);
		function loadExpertise() {
			$('#expertiseContent').load(contentLink + ' #expertiseContent div.html', function(response, status, xhr) {
				if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$("#error").html(msg + xhr.status + " " + xhr.statusText);
				}
			});
		}
	});
});




function reorderThumbs() {
	function randOrd() { 
		return(Math.round(Math.random())-0.5);
	}
 
	return($(".portRow").each(function() {
		var $this = $(this);
		var $thumbs = $('.portDetail, .funFact');
		var thumbsCount = $thumbs.length;
		 
		if (thumbsCount > 1) {
			$thumbs.remove();
			var indices = new Array();
			for (i=0;i<thumbsCount;i++) { 
				indices[indices.length] = i;
			}
			indices = indices.sort(randOrd);
			var timeDelay = 0;
			$.each(indices,function(j,k) { 
				$this.append($thumbs.eq(k));
				$thumbs.eq(j).children('img').delay(timeDelay).animate({opacity: 1}, 500);
				timeDelay += 300;
			});
		}
	}));
	$('.portDetail').clearQueue();
}
function reorderThumbsFlash() {
	function randOrd() { 
		return(Math.round(Math.random())-0.5);
	}
 
	return($(".portRow").each(function() {
		var $this = $(this);
		var $thumbs = $('.portDetail');
		var thumbsCount = $thumbs.length;
		 
		if (thumbsCount > 1) {
			$thumbs.remove();
			var indices = new Array();
			for (i=0;i<thumbsCount;i++) { 
				indices[indices.length] = i;
			}
			indices = indices.sort(randOrd);
			var timeDelay = 0;
			$.each(indices,function(j,k) { 
				$this.append($thumbs.eq(k));
				$thumbs.eq(j).children('img').delay(timeDelay).animate({opacity: 1}, 500);
				timeDelay += 300;
			});
		}
	}));
	$('.portDetail').clearQueue();
}

function setListSpace() {
	$('#portfolioList .scrollLeft, #portfolioList .scrollRight').hide();
	$('#portfolioList ul').css({'left': 0});
	var availArea = $('#portRight').width();
	$('#portfolioList ul').css({'width': availArea});
	// size of thumbnails plus 3px padding between each
	var thumbWidth = 777;
	if(availArea < thumbWidth) {
		var scrollAmount = availArea - thumbWidth;
		var scrollSpeed =  Math.abs(scrollAmount * 5);
		$('#portfolioList ul').css({'width': thumbWidth});
		showHideScrollers();
		$('#portfolioList ul').stop();
		$('#portfolioList .scrollLeft').mouseover(function() {
			$('#portfolioList ul').stop();
			$('#portfolioList ul').animate({left: 0}, scrollSpeed, function() {
				showHideScrollers();
			});
		});
		$('#portfolioList .scrollRight').mouseover(function() {
			$('#portfolioList ul').stop();
			$('#portfolioList ul').animate({left: scrollAmount}, scrollSpeed, function() {
				showHideScrollers();
			});
		});
	}
}

function showHideScrollers() {
	if($('#portfolioList ul').css('left') == '0px') {
		$('#portfolioList .scrollLeft').hide();
	}
	else {
		$('#portfolioList .scrollLeft').show();
	}

	if($('#portfolioList ul').css('left') < '0px') {
		$('#portfolioList .scrollRight').hide();
	}
	else {
		$('#portfolioList .scrollRight').show();
	}
}

function setArrows() {
	//if you're viewing the last project and there are no more images, hide the next arrows
	if($('#portfolioList li.active').next('li').length == 0 && $('#gallery img.current').next('img').length == 0) {
		$('#nextPrev div.next, #nextPrevLg div.next').hide();
	}
	//if there are more images to display, go ahead and show the next arrows
	else if($('#gallery img.current').next('img').length > 0) {
		$('#nextPrev div.next, #nextPrevLg div.next').show();
	}	
	//if you're viewing the first project in the list and there aren't any prior images in the gallery, hide the previous arrows
	if($('#portfolioList li.active').prev('li').length == 0 && $('#gallery img.current').prev('img').length == 0) {
		$('#nextPrev div.previous, #nextPrevLg div.previous').css({'visibility': 'hidden'});
	}
	//if there are more images to display, go ahead and show the next arrows
	else if($('#gallery img.current').prev('img').length > 0) {
		$('#nextPrev div.previous, #nextPrevLg div.previous').css({'visibility': 'visible'});
	}
}

function setSubNav() {
		$('#globalNav li.expertise ul').hide();
		if(windowWidth < 970) {
			$('.dropDivider').hide();
			$('.portfolio ul').css({'position': 'absolute', 'top': '18px', 'width': '100%', 'margin': '0px', 'padding': '0px 0px 0px 5px'});
		}
		else {
			$('.dropDivider').show();
			$('.portfolio ul').css({'position': 'relative', 'top': '0px', 'width': 'auto', 'margin': '0px 0px 0px -5px', 'padding': '0px'});
		}
}

function 	getWindowSize() {
	windowWidth = $(window).width();
}

// Adds grey arrow above thumbnail image
function setActiveProject() {
	var portWidth = $('#gallery').width();
	var portHalf = portWidth/2;
	$('#portfolioList ul li').each(function() {
		if($(this).hasClass('active')) {
			var projectLeft =  $(this).position().left;
			var arrowLeft =  projectLeft + 62;
			var arrowRight =  projectLeft - 23;
			if(projectLeft > portHalf) {
				$('.activeArrowRight').css({'left': arrowRight, 'display': 'block'});
			}
			else {
				$('.activeArrowLeft').css({'left': arrowLeft, 'display': 'block'});
			}
		}
	});
}

function fixIE() {
	var wrapperWidth = $('#wrapper').width();
	$('#mainContent').css({'width': wrapperWidth-12});
}
