//////////////////////////////////////////////////////////////
//Set Variables
/////////////////////////////////////////////////////////////

var transitionSpeed = 500;
var scrollSpeed = 500;
var fadeDelay = 100;
var currentProject = "";
var nextProject = "";

//highslide
hs.graphicsDir = 'images/highslide/';
hs.showCredits = false;
hs.outlineType = 'rounded-white';
hs.align = 'center';


var slideshowImages = [
	['nps-timeline','National Park Service - Timeline',3,565],
	['nps-subject','National Park Service - Subject Pages',4,660],
	['nps-park','National Park Service - Park Pages',5,660],
	['world-bank-ypp','World Bank - Young Professionals Program',2,580],
	['nps-education','National Park Service - Education Pages',5,570],
	['state-of-working-america','Economic Policy Institute - State of Working America',4,565],
    ['nps-state','National Park Service - State Pages',4,602],
    ['dos-j1','Department of State - J-1 Visa',3,636],
	['astho-gallery','Association of State and Territorial Health Officials - Vaccination Gallery',4,577],
	['nps-cw-150','National Park Service - Civil War Sesquicentennial',2,660],
	['usip','U.S. Institute of Peace',3,660],
	['mli','Aspen Institute - Ministerial Leadership Initiative',2,660],
	['world-bank-arabic','World Bank - Arabic Language Site',1,660],
	['cis','Communities in School',4,660],
	['ucf-today','University of Central Florida - Today',3,660],
	['brains-4-rent','Brains 4 Rent',4,622],
	['astho-pres-chal','Association of State and Territorial Health Officials - Presidential Challenge',2,660],
	['duke-fl','Duke University - Faith and Leadership',2,660],
	['neh','National Endowment for the Humanities - EDSITEment',3,660],
	['benjamin-moore','Benjamin Moore Online',2,466]
];

var images = [
  	['flickr-highslide','Flickr + Highslide',3],
  	['hotwire-revealed','Hotwire Revealed',1],
  	['best-sell-price','Best Sell Price',1]
];

//////////////////////////////////////////////////////////////
//Functions
/////////////////////////////////////////////////////////////

function getSlideShow(project, text, imagesNum, height) {
	var html = "<ul>";
	var directory = project.replace(/-/g,"_");
	for (i=1; i<=imagesNum; i++) {
		html += '<li style="height:' + height + 'px !important;"><a class="highslide" href="images/work/' + directory + '/large/' + i + '.png" title="' + text + ' screenshot"><img src="images/work/' + directory + '/' + i + '.png" alt="' + text + ' screenshot ' + i + '"/></a></li>';
	}
	html += "</ul>";
	return html;
}

function getImages(project, text, imagesNum, below) {
	below = typeof(below) != 'undefined' ? below : false;
	var cnt = 1;
	var html = "";
	var directory = project.replace(/-/g,"_");
	//if there are more than 2 images and below is false, only loop through the first images
	if(!below && imagesNum > 2) {
		imagesNum = 2;
	}
	//loop through the remaining images
	if(imagesNum > 2 && below) {
		cnt = 3;
	}
	for (i=cnt; i<=imagesNum; i++) {
		html += '<p><a class="highslide" href="images/work/' + directory + '/large/' + i + '.png" title="' + text + ' screenshot"><img src="images/work/' + directory + '/' + i + '.png" alt="' + text + ' screenshot ' + i + '"/></a></p>';
	}
	return html;
}


function getProject(project) {
	return $("#project-" + project);
}

function getProjectLink(project) {
	return $("#thumb-" + project + " a");
}

function getSlideShowLink(project) {
	return $("#project-" + project + " .slideshow");
}

function hideProject(){
	$(".close").click(function() {
		currentProject = getProject($(".project-thumbs a.selected").attr("href").replace("\#",""));		
		//fade out the current project
		currentProject.fadeOut(transitionSpeed, function(){
			//remove the selected class from thumbnail
			$(".project-thumbs a.selected").removeClass("selected");
		});
		//close the project container
		$("#projects").delay(fadeDelay).animate( {height: 0 }, transitionSpeed );
	});
}

function showProject(project, animate) {
	if ($(".project-thumbs a.selected").attr("href")) {
		currentProject = getProject($(".project-thumbs a.selected").attr("href").replace("\#",""));		
	}	
	if (!getProjectLink(project).hasClass("selected")) {
		//project has a slideshow
		if(getSlideShowLink(project).length)
		{
			//first time
			if(!$("#project-" + project + " .slideshow ul").length)
			{
				//loop through each slideshow
				$.each(slideshowImages, function(index) { 
					//match slide show with project
					if(slideshowImages[index][0] == project) {
						getSlideShowLink(project).append(getSlideShow(slideshowImages[index][0], slideshowImages[index][1], slideshowImages[index][2], slideshowImages[index][3]));
					}
				});
				
				//hide project
				hideProject();
				
				//create slider
				$(".slideshow", getProject(project)).easySlider({
					speed: transitionSpeed,
					project: project,
					continuous: true,
					numeric: true,
					controlsBefore: '<div class="slideshow-controls clearfix">',
					controlsAfter: '</div>'		
				});
				
				//enable highslide on slideshow
				$(".highslide").click(function(){
					return hs.expand(this);
				});
			}
		}
		//project doesnt have a slideshow
		else{
			//first time
			if(!$("#project-" + project + " .two-third p").length)
			{
				//loop through each image
				$.each(images, function(index) { 
					//match slide show with project
					if(images[index][0] == project) {
						//column can only fit two images, place first 2 images in side column, the remaining images are placed below
						if(images[index][2] > 2)
						{
							//first two images go in the side column
							$("#project-" + project + " .two-third").append(getImages(images[index][0], images[index][1], 2));
							//remaining images go below
							$("#project-" + project + " .one-third").after(getImages(images[index][0], images[index][1], images[index][2], true));
						}
						//place images in the side column
						else
						{
							$("#project-" + project + " .two-third").append(getImages(images[index][0], images[index][1], images[index][2]));
						}
					}
				});
				
				//hide project
				hideProject();
				
				//enable highslide on images
				$(".highslide").click(function(){
					return hs.expand(this);
				});
			}
		}
		
		//fade out the current project
		if (currentProject) {
			currentProject.fadeOut(transitionSpeed);
		}
		
		//fade in the next project
		getProject(project).delay(fadeDelay).fadeIn(transitionSpeed);
		//add the selected class to the thumbnail
		$(".project-thumbs a.selected").removeClass("selected");
		getProjectLink(project).addClass("selected");	
		//adjust the height of project container
		$("#projects").animate({ height: getProject(project).outerHeight() }, scrollSpeed );				
	}
	
	//scroll to the top of the projects
	$.scrollTo("#projects", scrollSpeed, { offset:-25 });
}

function getMap(){
	
    var myLatlng = new google.maps.LatLng(38.895112,-77.036366);
    
    var myOptions = {
    	zoom: 10,
    	center: myLatlng,
    	disableDefaultUI: true,
    	center: myLatlng,
    	mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Washington D.C."
    }); 
}

function toggleAllThumbs()
{
	//fade in current text
	$('#all').fadeToggle(transitionSpeed, function()
	{
		//change show all text
		if($('#all').is(":visible")) {
			$('#show-all').text("View Less");
		}
		else {
			$('#show-all').text("View All");
			$.scrollTo("#work", scrollSpeed);
		}
	});
}

//////////////////////////////////////////////////////////////
//Initialize
/////////////////////////////////////////////////////////////
$(document).ready(function(){	
	
	//colorbox
	$(".lightbox").colorbox({width:"80%", height:"80%", iframe:true});

	//add click events to project thumbnails
	$(".project-thumbs a").click(function() {
		showProject($(this).attr("href").replace("\#",""));
	});
	
	//add click events to experience links
	$(".experience li a").click(function() {
		showProject($(this).attr("href").replace("\#",""));
	});
	
	//show project is there is a hash in the URL
	var project = location.hash.replace("\#","");
	if (getProject(project).length) {
		showProject(project, true);
		toggleAllThumbs();
	}
	
	//show all projects
	$('#show-all').click(function() {
		toggleAllThumbs();
	});
	
	//on logo click scroll to top
	$('#logo a').click(function() {
		$.scrollTo("#main", scrollSpeed);
	});
	
	//validate verify
	jQuery.validator.addMethod("equals", function(value, element, param) { 
		return this.optional(element) || value === param; 
	}, jQuery.format("You must enter {0}"));

	//validate form
	$("#contact-form").validate({
		//hide error messages
		errorPlacement: function() {},
		rules: {
			verify: {
				required: true,
				minlength: 1,
				equals: "5"
			}
		},
		submitHandler: function() {
			//submit form
			$.post("contact.php", $("#contact-form").serialize(), function(data){
				//hide form
				$("#contact-form").fadeOut(function()
				{
					//email was sent
					if(data[0] == 1) {
						$("#success .name").text($("#name").val());
						$("#success").fadeIn();
					}
					//email failed
					else {
						$("#error").show();
					}
				});
			});
		}
	});
	
	getMap();
	
});

//load visual nav
$(window).load(function() {
	// Setup the site navigation
	$('#nav').visualNav({
		link              : 'a',
		targetAttr        : 'href',
		inViewClass       : 'inView',
		selectedClass     : 'selected',
		selectedAppliedTo : 'a',
		contentClass      : 'soloSection',
		contentLinks      : 'visualNav',
		bottomMargin      : 200,
		animationTime     : 700
    });
});
