// JavaScript Document

//$(document).ready(function(){



function myFunctions() {
	
	// Drop down nav timer and opener.
	var timeout  = 50; // this is the delay for the DIV to close
	var closetimer = 0;
	var ddmenuitem = 0;
	
	function jsddm_open() {
		jsddm_canceltimer();
		jsddm_close();
		//ddmenuitem = $(document).find('#liveOver').css('visibility', 'visible');}
		ddmenuitem = $(document).find('#liveOver').css('display', 'block');
		}
	
	
	function openMyMenu(myMenu) {	
		jsddm_canceltimer();
		jsddm_close();
		//alert($(this).text());
		//alert(myMenu.data.div + " yo!");
		//ddmenuitem = $(document).find('#liveOver').css('visibility', 'visible');}
		ddmenuitem = $(document).find(myMenu.data.div).css('display', 'block');
		}
	
	
	function openSearch() {	
		jsddm_canceltimer();
		jsddm_close();
		//ddmenuitem = $(document).find('#liveOver').css('visibility', 'visible');}
		ddmenuitem = $(document).find('#searchContainer').css('display', 'block');
		}
	
	function jsddm_close(){
		if(ddmenuitem) ddmenuitem.css('display', 'none');
		$('li#live a').removeClass('hover'); // remove the "mouseover" type
		$('li#work a').removeClass('hover'); // remove the "mouseover" type
		$('li#play a').removeClass('hover'); // remove the "mouseover" type
		$('li#heal a').removeClass('hover'); // remove the "mouseover" type
		$('li#learn a').removeClass('hover'); // remove the "mouseover" type
		$('li#discover a').removeClass('hover'); // remove the "mouseover" type
		$('li#plan a').removeClass('hover'); // remove the "mouseover" type
		$('li#myhed a').removeClass('hover'); // remove the "mouseover" type
		
	}
	
	function jsddm_timer()
	{  closetimer = window.setTimeout(jsddm_close, timeout);}
	
	function jsddm_canceltimer() {
		$('#searchContainer').hide();
		$('li#search a').removeClass("hover");
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
						   
	$('.closeOverMenuLink').click(jsddm_close); // this probably really isn't needed, but ya never know.
						   
	$('#liveOver').bind('mouseover', jsddm_canceltimer);
	$('#liveOver').bind('mouseout', jsddm_timer);
	
	$('#workOver').bind('mouseover', jsddm_canceltimer);
	$('#workOver').bind('mouseout', jsddm_timer);
	
	$('#playOver').bind('mouseover', jsddm_canceltimer);
	$('#playOver').bind('mouseout', jsddm_timer);
	
	$('#learnOver').bind('mouseover', jsddm_canceltimer);
	$('#learnOver').bind('mouseout', jsddm_timer);
	
	$('#healOver').bind('mouseover', jsddm_canceltimer);
	$('#healOver').bind('mouseout', jsddm_timer);
	
	$('#discoverOver').bind('mouseover', jsddm_canceltimer);
	$('#discoverOver').bind('mouseout', jsddm_timer);
	
	$('#planOver').bind('mouseover', jsddm_canceltimer);
	$('#planOver').bind('mouseout', jsddm_timer);
	
	$('#myhedOver').bind('mouseover', jsddm_canceltimer);
	$('#myhedOver').bind('mouseout', jsddm_timer);
	
	
	//$('#mainnav > li').bind('mouseover', jsddm_open);
	//$('#mainnav > li').bind('mouseout',  jsddm_timer);

	//$('#mainnav li#live').bind('mouseover', openLiveMenu);
	$('#mainnav li#live').bind('mouseover', {div: '#liveOver'}, openMyMenu);
	$('#mainnav li#live').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#work').bind('mouseover', {div: '#workOver'}, openMyMenu);
	$('#mainnav li#work').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#play').bind('mouseover', {div: '#playOver'}, openMyMenu);
	$('#mainnav li#play').bind('mouseout', jsddm_timer);

	$('#mainnav li#learn').bind('mouseover', {div: '#learnOver'}, openMyMenu);
	$('#mainnav li#learn').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#heal').bind('mouseover', {div: '#healOver'}, openMyMenu);
	$('#mainnav li#heal').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#discover').bind('mouseover', {div: '#discoverOver'}, openMyMenu);
	$('#mainnav li#discover').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#plan').bind('mouseover', {div: '#planOver'}, openMyMenu);
	$('#mainnav li#plan').bind('mouseout', jsddm_timer);
	
	$('#mainnav li#myhed').bind('mouseover', {div: '#myhedOver'}, openMyMenu);
	$('#mainnav li#myhed').bind('mouseout', jsddm_timer);
	
	$('#liveOver').mouseover(function() { $('li#live a').addClass('hover'); });
	$('#workOver').mouseover(function() { $('li#work a').addClass('hover'); });
	$('#playOver').mouseover(function() { $('li#play a').addClass('hover'); });
	$('#healOver').mouseover(function() { $('li#heal a').addClass('hover'); });
	$('#learnOver').mouseover(function() { $('li#learn a').addClass('hover'); });
	$('#discoverOver').mouseover(function() { $('li#discover a').addClass('hover'); });
	$('#planOver').mouseover(function() { $('li#plan a').addClass('hover'); });
	$('#myhedOver').mouseover(function() { $('li#myhed a').addClass('hover'); });
	
	$('#searchContainer').mouseover(function() {
		$('a.searchNav').addClass('hover');
	});

	// make search "reset" when rolling over "case studies:"
	function resetOvers() {
	//$('#casestudies').mouseover(function() {
		$('#searchContainer').hide();
		$('li#search a').removeClass('hover');
		$('li#live a').removeClass('hover');
		$('li#work a').removeClass('hover');
		$('li#play a').removeClass('hover');
		$('li#heal a').removeClass('hover');
		$('li#learn a').removeClass('hover');
		$('li#discover a').removeClass('hover');
		$('li#plan a').removeClass('hover');
		$('li#myhed a').removeClass('hover');
	//});
	}
	//$('#casestudies').bind('mouseover', resetOvers);
	$('#interiorWrapper').bind('mouseover', resetOvers);
	
	
	/**/
	$('li#search').mouseover(function(){
		$('#searchContainer').show();
	});

	$(".btn-slide").click(function(){
		$("#featuredPanel").slideToggle("slow");
		$(this).toggleClass("active");
		return false;
	});
	
	/***************************************************************
	/*	BEGIN FUNCTION GROUP
	/*	Handles the switching of project view from list to thumbnail
	/*	Used within projects level 2 pages
	****************************************************************/
			
		//listView clicks
		$("#listView").click(function(e){
			e.preventDefault();	//Cancel the link behavior
	
			//$('#paging').hide();
			//$('#catJump').hide();
			//$('#pageDisplay').hide();
			$('#pageTool').hide();
			$('#thumbnails').hide();
			$('.listView').addClass('selected');
			$('.thumbsView').removeClass('selected');
			$('#list').show();
		
		});
		//thumbsView clicks
		$("#thumbsView").click(function(e){
			e.preventDefault();			//Cancel the link behavior
			//mySlider.requestMove(0);	//move slider back to position
			
			//$('#paging').show();
			//$('#catJump').show();
			//$('#pageDisplay').show();
			$('#pageTool').show();
			$('#thumbnails').show();
			$('.listView').removeClass('selected');
			$('.thumbsView').addClass('selected');
			$('#list').hide();
			
		});
	/***************************************************************
	/*	END FUNCTION GROUP
	****************************************************************/
	
	
	/***************************************************************
	/*	BEGIN FUNCTION GROUP
	/*	Handles the subnav for categories
	/*	Used within projects level 1 pages
	****************************************************************/
	/*
	$('.sectionNav ul li a.subnavLink').mouseover(function() {
	
		var i	= 0,
			categories = $(this).parent().siblings(),
			totalcats = categories.length;
		//hide all other nav
		for (var i = 0; i < totalcats; i++) {
			categories.eq(i).find("ul").css('display', 'none');	
			categories.eq(i).find("a.subnavLink").css('opacity', '0.4');	
		}
		//show this nav
		$(this).siblings("ul").css('display', 'block');
		$(this).css('opacity', '1');
	});
	*/
	/***************************************************************
	/*	END FUNCTION GROUP
	****************************************************************/
	



	document.onclick = jsddm_close;
	

}// end myFunctions


/*************************************
/*	name:	newsSlider()
/*	edited:	7/31/2011
/*
/*	Handles the sliding of news entries
****/
function newsSlider() {
	
	var i = 1,
		newsItems = $("div#newsContainer ul.newsItemPreview li"),	//grab all news items
		totalItems = newsItems.length,			//total number of items
		pages = 0,
		itemHeight = 75 + 15,					//height of each item, height + margin
		totalHeight = totalItems * itemHeight,	//total height of all items
		startingTop = 15;						//what the element's starting top css is
	
	//grab global or local settings
	nSpeed = (window.nSpeed === undefined ? 2000 : window.nSpeed);
	nEase = (window.nEase === undefined ? 'easeInOutExpo' : window.nEase);
	nClicks = (window.nClicks === undefined ? 0 : window.nClicks);
	
	//set height of ul.newsItemPreview
	$("ul.newsItemPreview").css("height", totalHeight + "px");	

	
	//Right arrow click event
	$("#newsSlider_next a").click(function(e) {
		e.preventDefault();	//Cancel the link behavior
		moveItems(1);	//increments clicks
	});
	
	//Left arrow click event
	$("#newsSlider_prev a").click(function(e) {
		e.preventDefault();	//Cancel the link behavior
		moveItems(-1);	//decrements clicks
	});
	
	//handle the featured news popup
	$("#featuredNews #close").click(function(){
		$('#featuredNews').hide();
	});
	
	
	//incremenets nClicks based on direction
	function moveItems(direction) {
		if (direction > 0) {
			nClicks++;
		} else if (direction < 0) {
			nClicks--;
		}
		
		moveSlider();

	}
	
	
	//animates the slider
	function moveSlider() {

		var pos_top = (-(itemHeight * 1) * nClicks) + startingTop;	//current position of top
			min_top = -(totalHeight-itemHeight - startingTop);	//minimum value top can be
			customSpeed = nSpeed;
		
		
		if (pos_top > startingTop) {	//if we try to go above the first
			//go to the last one
			pos_top = min_top;
			nClicks = totalItems - 1;
			customSpeed = 0;
		} else if (pos_top < min_top) {	//if we try to go below the last
			//go to the first one
			pos_top = startingTop;
			nClicks = 0;
			customSpeed = 0;
		}
		
		//animate the items
		$("ul.newsItemPreview").animate({
			top: pos_top
			}, {
			duration: customSpeed * .3,
			specialEasing: { left: nEase },
			complete: function() {}
		});
		
	
	}


}	//end newsSlider();

/*************************************
/*	name:	ptSlider()
/*	edited:	6/30/2011
/*
/*	Handles the sliding of project
/*	thumbnails
****/
function ptSlider() {
	
	var i = 0,				//counter variable
		totalWidth = 0,		//width of all groups
		numProjects = 0,	//total number of projects in all groups
		pages = 0,			//calculated pages of slider
		thumbLength = 242;	//width of each thumb element
		groups = $("div#projectThumbs2 div.group");	//groups of thumbnails
		groupsLen = groups.length;	//total number of groups	
		
	//grab global or local settings
	var ptSpeed = (window.ptSpeed === undefined ? 2000 : window.ptSpeed),
		ptEase = (window.ptEase === undefined ? 'easeInOutExpo' : window.ptEase),
		ptClicks = (window.ptClicks === undefined ? 0 : window.ptClicks),
		ptThumbs = (window.ptThumbs === undefined ? 4 : window.ptThumbs);	//number of thumbs per slide page
		ptSlideMove = (window.ptSlideLength === undefined ? 1 : window.ptSlideLength);	//number of thumbs to slide per move
	
	//loop through each group
	for (i; i < groupsLen; i++) {
		//store within groups for later useage
		groups[i]._totalThumbs = groups.eq(i).children("ul").children().length;	//total thumbs for this group
		groups[i]._totalWidth = thumbLength * groups[i]._totalThumbs;			//total width of this group
		groups[i]._startPos	= totalWidth;					//start position of this group
		totalWidth += thumbLength * groups[i]._totalThumbs;	//total width of all groups
		numProjects += groups[i]._totalThumbs;				//total number of thumbs
		groups[i]._endPos = totalWidth;						//end position of this group
	}

	//calculate pages (sets of thumbsPerPage) for slider
	pages = Number(Math.ceil((numProjects / ptThumbs))); //-.01: needed for spacing of projects when multiple of 4
	
	//set width of ptContainer
	$("div#ptContainer").css("width", totalWidth + "px");


	//handle arrow key presses
	$(document).keydown(function(e){
		if (e.keyCode == 37) {	//left arrow
		   moveThumbnails(-1);
		   return false;
		}
		if (e.keyCode == 39) {	//right arrow
		   moveThumbnails(1);
		   return false;
		}
	});
	
	//Right arrow click event
	$("#ptSlider_right a").click(function(e) {
		e.preventDefault();	//Cancel the link behavior
		moveThumbnails(1);	//increments clicks
	});
	
	//Left arrow click event
	$("#ptSlider_left a").click(function(e) {
		e.preventDefault();	//Cancel the link behavior
		moveThumbnails(-1);	//decrements clicks
	});
	
	//public function to move the slider
	this.requestMove = function(customSpeed) {
		moveSlider(customSpeed);
	};
	
	//incremenets ptClicks based on direction
	function moveThumbnails(direction) {
		
		if (direction > 0 && ptClicks >= pages) {	//try to go past max
			return;
		} else if (direction < 0 && ptClicks === 0) {	//trying to go below min
			return;
		} else if (direction > 0 && ptClicks < pages) {	//moving to next
			ptClicks++;
		} else if (direction < 0 && ptClicks > 0) {	//move to prev
			ptClicks--;
		} else {	//something went wrong
			ptClicks = 0;
		}
		moveSlider();	//move the slider
		adjustHeader();	//adjust the group header
		
		//adjust the display number
		$("div#pageDisplay span").text(ptClicks+1 + "-" + (ptClicks + ptThumbs));
		
		
	}
	
	//adjusts the header in case of lingering thumbs
	function adjustHeader() {

		var currentPos = (ptClicks * ptSlideMove) * thumbLength;
		
		//loop through all groups
		for (i = 0; i < groupsLen; i++) {
			//if still show thumbs of previous group
			if (currentPos > groups[i]._startPos && currentPos < groups[i]._endPos) {
				//trying to get some animation to look smoother
				groups.eq(i).children("h3").css("text-indent", currentPos - groups[i]._startPos);
				//var newpos = currentPos - groups[i]._startPos;
				/*groups.eq(i).children("h3").animate({
					'text-indent': newpos
					} , {
				duration: ptSpeed * .6,
				specialEasing: {  },
				complete: function() {}
				});*/
				
			} else {	//reset the indent for all others
				groups.eq(i).children("h3").css("text-indent", 0);	
			}
		}
		
	}

	//animates the slider
	function moveSlider(customSpeed, customClicks) {
		//set custom click/speed to default if not given
		if (customClicks === undefined)	{
			customClicks = ptClicks;
		}
		if(customSpeed === undefined) {
			customSpeed = ptSpeed;
		}

		//show or hide the arrows based on position
		if (ptClicks == pages) {
			$('#ptSlider_right a').hide();
		} else {
			$('#ptSlider_right a').show();
		}
		if (ptClicks == 0) {
			$('#ptSlider_left a').hide();
		} else {
			$('#ptSlider_left a').show();
		}
		
		//animate the thumbs
		$("#ptContainer").animate({
			left: -(thumbLength * ptSlideMove) * customClicks
			}, {
			duration: customSpeed * .3,
			specialEasing: { left: ptEase },
			complete: function() {}
		});
		
	}
	
} //end ptSlider();


/*************************************
/*	name:	pfSlider()
/*	edited:	7/01/2011
/*
/*	Handles the sliding of profile 
/*	(case study) images and captions
****/
function pfSlider() {

	var i = 0,				//counter variable
		numPhotos = 0,		//total number of projects in all groups
		pages = 0,			//calculated pages of slider
		photoLength = 706,	//width of each thumb element
		photosPerPage = 1,	//number of thumbs be slide page
		photos = $("ul#pf_photos li");	//all the photos
		numPhotos = photos.length;		//total number of photos	
		totalWidth = photoLength * numPhotos;	//total width of all photos
	
	
	//grab global or local settings
	pfSpeed = (window.pfSpeed === undefined ? 2000 : window.pfSpeed);
	pfEase = (window.pfEase === undefined ? 'easeInOutExpo' : window.pfEase);
	pfClicks = (window.pfClicks === undefined ? 0 : window.pfClicks);
	
	//calculate pages (sets of photosPerPage) for slider
	pages = numPhotos - 1;	//-1 due to first click going to 1 not 0
	
	//set width of pf_photos
	$("ul#pf_photos").css("width", totalWidth + "px");

	//handle arrow key presses
	$(document).keydown(function(e){
		if (e.keyCode == 37) {	//left arrow
		   movePhotos(-1);
		   return false;
		}
		if (e.keyCode == 39) {	//right arrow
		   movePhotos(1);
		   return false;
		}
	});
	//handle swip with iOS safari
	$("#pf_photos").swipe({
		threshold: { x: 10, y: 150 },
		swipeLeft: function() { movePhotos(1); },
		swipeRight: function() { movePhotos(-1); },
		swipeTopBottom: function(changeY) { $('html,body').animate({
						scrollTop: changeY 
						}, {
						duration: 300,
						specialEasing: { top: pfEase, bottom: pfEase}});
						}
	});
	//handle arrow link clicks
	$("#pfSlider_left a").click(function (ev) {
		ev.preventDefault();
		movePhotos(-1);
    });
	$("#pfSlider_right a").click(function (ev) {
		ev.preventDefault();		
		movePhotos(1);

    });
	//handle bullet clicks
	$("div#pf_nav ul.bullets li a").click(function (ev) {
		ev.preventDefault();
		var next_pos = $(this).parent().index();
		var current_pos = $("div#pf_nav ul.bullets li.current").index();
		
		//hide current elements
		$("div#pf_nav ul.bullets li").eq(current_pos).removeClass('current');
		$("div#pf_nav ul.captions li").eq(current_pos).removeClass('current');
		
		//show next elements
		$("div#pf_nav ul.bullets li").eq(next_pos).addClass('current');
		$("div#pf_nav ul.captions li").eq(next_pos).addClass('current');
		
		//move slider
		pfClicks = next_pos;	//store new position globally
		moveSlider();
    });

	
	//incremenets pfClicks based on direction
	function movePhotos(direction) {
		
		if (direction > 0 && pfClicks >= pages) {	//try to go past max
			return;
		} else if (direction < 0 && pfClicks === 0) {	//trying to go below min
			return;
		} else if (direction > 0 && pfClicks < pages) {	//moving to next
			pfClicks++;
		} else if (direction < 0 && pfClicks > 0) {	//move to prev
			pfClicks--;
		} else {	//something went wrong
			pfClicks = 0;
		}
		
		//move bullet and caption
		$("div#pf_nav ul.bullets li").eq(pfClicks - direction).removeClass('current');
		$("div#pf_nav ul.captions li").eq(pfClicks - direction).removeClass('current');
		$("div#pf_nav ul.bullets li").eq(pfClicks).addClass('current');
		$("div#pf_nav ul.captions li").eq(pfClicks).addClass('current');
		
		moveSlider();	//move the slider			
	}
	
	//animates the slider
	function moveSlider(customSpeed, customClicks) {

		//set custom click/speed to default if not given
		if (customClicks === undefined)	{
			customClicks = pfClicks;
		}
		if(customSpeed === undefined) {
			customSpeed = pfSpeed;
		}

		//show or hide the arrows based on position
		if (pfClicks === pages) {	
			$('#pfSlider_right a').hide();
		} else {
			$('#pfSlider_right a').show();
		}
		if (pfClicks === 0) {
			$('#pfSlider_left a').hide();
		} else {
			$('#pfSlider_left a').css('display', 'block');	//.show() not working, not sure why 7/1/11
		}
		
		//animate the thumbs
		$("#pf_photos").animate({
			left: -(photoLength*photosPerPage) * customClicks
			}, {
			duration: customSpeed * .3,
			specialEasing: { left: pfEase },
			complete: function() {}
		});
		
	}
	
}	//end pfSlider();


/***************************************
/*	name: projectHash
/*
/*	Allows for changing of body class
/*	for projects based on url hash
***/
function projectHash() {

	//lets handle the hash tag in the url
	var url_hash = window.location.hash;
		htmlString = '';
	
	url_hash = url_hash.replace('#', '');
	url_hash = url_hash.split("/");
	//prevent injection by verifying hash
	switch (url_hash[0]) {
		case 'live':
		case 'work':
		case 'play':
		case 'heal':
		case 'learn':
		case 'discover':
		case 'plan':
			//add body class for coloring
			$("body").removeClass().addClass(url_hash[0]);
			
			//add breadcrumb
			var htmlString = '<p>&lt; <a href="/' + url_hash[0] + '"><em>' + url_hash[0] + '</em></a>';
			//add second level breadcrumb
			if (url_hash[1] != '') {
				// 2012-02-16 --- What does this do?  We commented it out, in order for the  < > keys in the projects to work.
				// We also had to change the <a href> to = javascript:void(0) 
				//htmlString += ' &lt; <a href="/' + url_hash[0] + '/' + url_hash[1] + '"><em>' + url_hash[1].replace('_', ' ') + '</em></a></p>';
			}
			//add it to html
			$("div.breadcrumb").html(htmlString);
			
		break;
	}

}	//end projectHash();


//});
