
//arrays for holding levels of div's shown
var levelsAR = new Array();
var curListItemAR = new Array();
//array for holding one below current list item
//so know's where to put it
var nextListItemAR = new Array();
var maxLevel = 0;
var curMain = "";
var catID = "";
var hasSetFontSize = false;
//array of tags
var tagsAR = new Array();
//scroll booleans
var isUpDown = false;
var isDownDown = false;
//color of background
var butBackgroundColor = '#f2f2f2';
var butColor = '#666666';
//array for menu hovers
var hoversAR = new Array();
hoversAR['projects'] = '#886fcb';
hoversAR['about'] = '#6f87a1';
hoversAR['blog'] = '#ee4894';
hoversAR['store'] = '#e88e20';
hoversAR['contact'] = '#cbf1da';
//array for menu hover backgrounds
var hoverBackgroundsAR = new Array();
hoverBackgroundsAR['projects'] = '#c9c9c8';
hoverBackgroundsAR['about'] = '#e39899';
hoverBackgroundsAR['blog'] = '#ede681';
hoverBackgroundsAR['store'] = '#b1bf9d';
hoverBackgroundsAR['contact'] = '#96a9c8';
//array for colors of sections
var colorsAR = new Array();
colorsAR['projects'] = '#939391';
colorsAR['about'] = '#c63133';
colorsAR['blog'] = '#dacd02';
colorsAR['store'] = '#637e3b';
colorsAR['contact'] = '#2c5291';
//for hiding levels before showing correct one
function hideLevels(lvl,div)
{
	if(levelsAR[lvl])
	{
		for(var i=lvl;i<=maxLevel;i++)
		{
			if(levelsAR[i] != div)
			{
				levelsAR[i].hide();
				$('#nav-scroll-up-'+i).hide();
				$('#nav-scroll-down-'+i).hide();
				//remove color class from current list item
				if(curListItemAR[i])
				{
					$('.level-'+i).find('.current').removeClass('current');
					$('#nav-arrow-'+i).hide();
				}
			}
		}
		maxLevel = lvl;
	}	
	var url = location.hash;
	oldSubPageAR = url.substring(1,url.length-1).split('/');
}

//for showing correct div
function showDiv(div,tit,lvl)
{
	//determine whether should use title of project or type of page
	//depending on whether the list item pressed has that title
	div.find('li a').each(function() {
		if($(this).text().indexOf(tit) != -1 && $(this).text().indexOf(' | ') != -1)
		{
			var tAR = $(this).text().split(' | ')
			$(this).text(tAR[tAR.length-1]);
		} else 
		{
			$(this).text($(this).text().split(' | ')[0]);
		}
	});
	//show corresponding arrow
	$('#nav-arrow-'+lvl).fadeIn(200,function() {
		//show div
		div.fadeIn(500);
		setProjectsScroll(div);
	});
}	

function animateNavUp(div)
{
	var li = div.find('ul li:first');
	var speed = 100;
	var ease = 'linear';
	var ease2 = 'linear';
	//animate to top position
	li.parent().animate({marginTop:-1*li.outerHeight()/2},speed,ease,function() {
		//move top li to bottom
		li.parent().css('marginTop',li.outerHeight()/2);
		var fLi = li.parent().children(":first").remove();
		div.find('ul').append(fLi);
		//and fade it in
		fLi.fadeTo(speed,1,ease2);
		li.parent().animate({marginTop:0},speed,ease2,function() {
			li.parent().css('marginTop',0);
			if(isDownDown)
			{
				animateNavUp(div);
			}
		})
	})
	//fade out first
	li.parent().children(":first").fadeTo(speed,0,ease);
}

//for if something is selected
function animateNavUp2(div)
{
	var li = div.find('ul li:first').next();
	var speed = 100;
	var ease = 'linear';
	var ease2 = 'linear';
	//animate to top position
	li.animate({marginTop:-1*li.outerHeight()/2},speed,ease,function() {
		//move top li to bottom
		var nF = $(this).next();
		nF.css('marginTop',li.outerHeight()/2);
		var fLi = $(this).remove();
		fLi.css('marginTop',0);
		div.find('ul').append(fLi);
		//and fade it in
		fLi.fadeTo(speed,1,ease2);
		nF.animate({marginTop:0},speed,ease2,function() {
			$(this).css('marginTop',0);
			if(isDownDown)
			{
				animateNavUp2(div);
			}
		})
	})
	//fade out first
	li.fadeTo(speed,0,ease);
}

function animateNavDown(div)
{
	var li = div.find('ul li:last');
	var speed = 100;
	var ease = 'linear';
	var ease2 = 'linear';
	//animate to top position
	li.parent().animate({marginTop:li.outerHeight()/2},speed,ease,function() {
		//move top li to bottom
		var fLi = li.parent().children(":last").remove();
		div.find('ul').prepend(fLi);
		li.parent().css('marginTop',-1*li.outerHeight()/2);
		//and fade it in
		fLi.fadeTo(speed,1,ease2);
		li.parent().animate({marginTop:0},speed,ease,function() {
			li.parent().css('marginTop',0);
			if(isUpDown)
			{
				animateNavDown(div);
			}
		})
	})
	//fade out first
	li.parent().children(":last").fadeTo(speed,0,ease);
}

function animateNavDown2(div)
{
	var li = div.find('ul li:first').next();
	var nF = div.find('ul li:last');
	var speed = 100;
	var ease = 'linear';
	var ease2 = 'linear';
	//animate to top position
	li.animate({marginTop:li.outerHeight()/2},speed,ease,function() {
		//move top li to bottom
		nF.css('marginTop',-1*li.outerHeight()/2);
		nF.remove();
		li.css('marginTop',0);
		li.before(nF);
		//and fade it in
		nF.fadeTo(speed,1,ease2);
		nF.animate({marginTop:0},speed,ease2,function() {
			$(this).css('marginTop',0);
			if(isUpDown)
			{
				animateNavDown2(div);
			}
		})
	})
	//fade out last
	nF.fadeTo(speed,0,ease);
}

function resetPositions()
{
	//hide blog scroll
	$('.blog-arrow').hide();
	//reset blog tags
	tagsAR = new Array();
	//for projects rearranging I think
	for(var i=0;i<=maxLevel;i++)
	{
		if(curListItemAR[i])
		{
			var cLi = curListItemAR[i];
			var nLi = nextListItemAR[i];
			var bLi = cLi.next();
			if(nLi.html() != bLi.html())
			{
				cLi.remove();
				nLi.before(cLi);
			}
		}
		
	}
}

//see if need to reposition current item before moving to next item
function repositionCheck(li,div,txt,isC)
{
	var lvl = Number(div.attr('class').split(' ')[1].split('-')[1]);
	//bump up level if setCurrent
	if(isC)
	{
		lvl++;
	}
	//animate list item directly after where item should go, as well as item
	var cLi = curListItemAR[lvl];
	var nLi = nextListItemAR[lvl];
	if(cLi == undefined || cLi.next().html() == nLi.html())
	{
		if(isC)
		{
			//list without subtree
			setCurrent(li,div);
		} else
		{
			setSection(li,div,txt);
		}
	} else if(nLi.html() == null) {
		if(isC)
		{
			//list without subtree
			setCurrent(li,div);
		} else
		{
			setSection(li,div,txt);
		}
	} else
	{
		var bLi = cLi.next();
		var speed = 100;
		var ease = 'linear';
		var ease2 = 'linear';
		//cLi.fadeTo(speed,0,ease);
		nLi.animate({marginTop:li.outerHeight()/2},speed,ease);
		cLi.animate({marginTop:-1*li.outerHeight()/2},speed,ease,function() {
			//remove cli
			$(this).remove();
			bLi.css('marginTop',li.outerHeight()/2);
			//insert before next
			nLi.before($(this));
			//and fade it in
			$(this).fadeTo(speed,1,ease2);
			//repos margins
			nLi.css('marginTop',-1*li.outerHeight()/2);
			$(this).css('marginTop',0);
			nLi.animate({marginTop:0},speed,ease);
			bLi.animate({marginTop:0},speed,ease,function() {
				if(isC)
				{
					//list without subtree
					setCurrent(li,div);
				} else
				{
					setSection(li,div,txt);
				}
			});
		});
	}
}
//if no subsections then just move up and change to white
function setCurrent(li,div)
{
	var lvl = Number(div.attr('class').split(' ')[1].split('-')[1]);
	maxLevel = Math.max(lvl,maxLevel);
	hideLevels(lvl+1,div);
	levelsAR[lvl] = div;
	//set color of text
	li.parent().find('.current').removeClass('current');
	li.find('a').addClass('current');
	//and push into current array
	curListItemAR[lvl+1] = li;
	//push previous item in as well so know where to insert if scrolls
	nextListItemAR[lvl+1] = li.next();
	//animate list item up if not in top spot
	if(li.prev().length == 0)
	{
		var url = location.hash;
		oldSubPageAR = url.substring(1,url.length-1).split('/');
		return;
	}
	var speed = 100;
	var ease = 'linear';
	var ease2 = 'linear';
	li.parent().animate({marginTop:-1*li.outerHeight()/2},speed,ease,function() {
		//move top li to bottom
		li.parent().css('marginTop',li.outerHeight()/2);
		var fLi = li.parent().children(":first");
		fLi.remove();
		li.parent().append(fLi);
		//and fade it in
		fLi.fadeTo(speed,1,ease2);
		li.parent().animate({marginTop:0},speed,ease2,function() {
			li.parent().css('marginTop',0);
			if(li.html() != li.parent().children(":first").html())
			{
				setCurrent(li,div);
			} 
		})
	})
}

function setSection(li,div,txt)
{
	//hide any divs at same level
	var lvl = div.attr('class').split(' ')[1].split('-')[1];
	maxLevel = Math.max(lvl,maxLevel);
	hideLevels(lvl,div);
	levelsAR[lvl] = div;
	//set color of text
	li.parent().find('.current').removeClass('current');
	li.find('a').addClass('current');
	//and push into current array
	curListItemAR[lvl] = li;
	//push previous item in as well so know where to insert if scrolls
	nextListItemAR[lvl] = li.next();
	if(li.prev().length == 0)
	{
		showDiv(div,txt,lvl);
		checkSubSection(div);
		var url = location.hash;
		oldSubPageAR = url.substring(1,url.length-1).split('/');
	} else
	{
		var speed = 100;
		var ease = 'linear';
		var ease2 = 'linear';
		//animate to top position
		li.parent().animate({marginTop:-1*li.outerHeight()/2},speed,ease,function() {
			//move top li to bottom
			li.parent().css('marginTop',li.outerHeight()/2);
			var fLi = li.parent().children(":first").remove();
			var lLi = li.parent().append(fLi);
			//and fade it in
			fLi.fadeTo(speed,1,ease2);
			li.parent().animate({marginTop:0},speed,ease2,function() {
				li.parent().css('marginTop',0);
			})
			setSection(li,div,txt);
		})
	}
	
}

function setSubNavClicks()
{
	//click for categories
	$('#sub-nav .cat-nav li a').live("click",function(e) {
		e.preventDefault();
		isNavClick = true;
		currentSubPageAR = new Array();
		location.hash = location.hash.split("/")[0]+"/";
		clickCat($(this));
		loadAjaxContent(location.hash);
	});

	//click for pages
	$('#sub-nav .page-nav li a').live("click",function(e) {
	currentSubPageAR = new Array();
		var url = location.hash;
		isNavClick = true;
		clickPage($(this));
	});
	
	//click for tags
	$('#sub-nav .tag-nav li a').live("click",function(e) {
	currentSubPageAR = new Array();
		var url = location.hash;
		isNavClick = true;
		clickTag($(this));
	});
}

function clickTag(but)
{
	var t = but.attr("href").substr(0,but.attr("href").length-1).split('#tag/')[1];
	if(but.hasClass("current"))
	{
		but.removeClass("current");
		but.css('color','#9C265C');
		tagsAR.splice(tagsAR.indexOf(t), 1);
	} else
	{
		but.addClass("current");
		if(tagsAR.indexOf(t) == -1)
		{
			tagsAR.push(t);
		}
	}
	//load ajax if hash doesn't change
	if(location.hash == but.attr('href'))
	{
		loadAjaxContent(location.hash);
	}
}

function clickCat(but)
{
	var pId = but.parent().attr('id').split('-')[2];
	catID = pId;
	var cD = $('#cat-'+pId);
	var li = but.parent();
	//if already current check to see if from footer
	/*if(but.hasClass('current'))
	{
		checkSubSection($('#cat-'+pId));
		return false;
	}*/
	//see if div with id exists
	//if not then go to page, if so then show div
	if(cD.length == 0)
	{
		var div = li.parent().parent();
		repositionCheck(li,div,'',true);
	} else
	{
		repositionCheck(li,cD,but.text(),false);
	}
}

function clickPage(but)
{
	//don't do shit if already current
	/*if(but.hasClass('current'))
	{
		return false;
	}*/
	var pId = but.parent().attr('id').split('-')[2];
	var cD = $('#page-'+pId);
	var li = but.parent();
	//see if div with id exists
	//if not then go to page, if so then show div
	if(cD.length == 0)
	{
		var div = li.parent().parent();
		repositionCheck(li,div,'',true);
	} else
	{
		repositionCheck(li,cD,but.text(),false);
	}
}


function checkSubSection(div)
{
	var ind;
	var sStr;
	var but;
	if(currentCat != "" && currentCat != undefined)
	{
		var but = $('.cat-nav.level-0 #cat-link-'+currentCat+' a');
		currentCat = "";
		clickCat(but);
	} else if(currentSubPageAR.length != 0 && div)
	{
		sStr = 'a[href="#';
		for(var i=0;i<=currentSubPageAR.length;i++)
		{
			ind = i;
			sStr+=currentSubPageAR[i]+'/';
			but = div.find(sStr+'"]');
			if(but.length != 0 && !but.hasClass('current'))
			{
				break;
			}
		}
		if(but.length == 0)
		{
			return;
		}
		clickPage(but);
	} else
	{
		if(currentSubPageAR[0] == "blog")
		{
			currentSubPageAR[0] = "tag";
		}
		sStr = '.level-0 ul li a[href="#';
		for(var i=0;i<=currentSubPageAR.length;i++)
		{
			ind = i;
			sStr+=currentSubPageAR[i]+'/';
			but = $('#sub-nav').find(sStr+'"]');
			if(but.length != 0)
			{
				break;
			}
		};
		if(but.length != 0)
		{
			clickPage(but);
			return;
		}
		//exception for project categories
		/*if($('#sub-nav').find('.cat-nav').css('display') == 'block' && but.length == 0)
		{
			but = $('#sub-nav').find('.cat-nav').find('.current');
			if(but.length != 0)
			{
				clickCat(but);
			}
			return;
		}*/
		//exception for top nav menu
		if(currentSubPageAR.length == 1  && !isNavClick)
		{
			but = $('.menu li a[href="#'+currentSubPageAR[0]+'/"]');
			if(but.length != 0)
			{
				setTopNav(but);
				return;
			}
		}
		if(but.length == 0)
		{
			return;
		}
	}
}

function setProjectsScroll(div)
{
	if(div.find('ul').children('li').length > 6)
	{
		var lvl = div.attr('class').split(' ')[1].split('-')[1];
		//show scrollers for this level
		var scrUp = $('#nav-scroll-up-'+lvl);
		var scrDown = $('#nav-scroll-down-'+lvl);
		scrUp.fadeIn(500);
		scrDown.fadeIn(500);
		//unset any previous clicks
		scrUp.unbind('mouseup');
		scrDown.unbind('mouseup');
		scrUp.unbind('mousedown');
		scrDown.unbind('mousedown');
		scrUp.unbind('mouseout');
		scrDown.unbind('mouseout');
		//set listeners for scrollers
		scrUp.mousedown(function() {
			isUpDown = true;
			//see if has a currently selected item
			if(div.find(".current").length == 0)
			{
				animateNavDown(div);
			} else
			{
				animateNavDown2(div);
			}
		});
		scrUp.mouseup(function() {
			isUpDown = false;
		})
		scrUp.mouseout(function() {
			isUpDown = false;
		})
		
		scrDown.mousedown(function() {
			isDownDown = true;
			//see if has a currently selected item
			if(div.find(".current").length == 0)
			{
				animateNavUp(div);
			} else
			{
				animateNavUp2(div);
			}
		});
		scrDown.mouseup(function() {
			isDownDown = false;
		})
		scrDown.mouseout(function() {
			isDownDown = false;
		})
	}
}

function setBlogScroll()
{
	
	//show scrollers for this level
	var scrLeft = $('#blog-arrow-left');
	var scrRight = $('#blog-arrow-right');
	scrLeft.fadeIn(500);
	scrRight.fadeIn(500);
	//unset any previous clicks
	scrLeft.unbind('click');
	scrRight.unbind('click');
	
	scrRight.click(function() {
		//remove last element from tag nav
		var lD = $('.tag-nav:last');
		lD.remove();
		lD.css({marginRight:-170});
		$('#blog-tags').prepend(lD);
		//animate margin of last element
		lD.animate({marginRight:0},500,'easeOutCubic');
	});
	scrLeft.click(function() {
		var fD = $('.tag-nav:first');
		fD.animate({marginRight:-170},500,'easeOutCubic',function() {
			fD.remove();
			fD.css({marginRight:0});
			$('#blog-tags').append(fD);
		})
	})
}

//clear all current classes because changing nav
function clearCurrents()
{
	$('#sub-nav').find('.current').each(function() {
		$(this).removeClass('current');
	})
}

function delayTopNav(but)
{
	$('#home-slideshow').fadeOut('slow',function() {
		setTopNav(but);
	})
}

//for running top nav
function setTopNav(but) 
{
	if($('#home-slideshow').is(':visible'))
	{
		delayTopNav(but);
		return;
	}
	var tit = but.attr('title');
	//set as current menu item
	//fade out old item
	var oldCur = but.parent().parent().find('.current-menu-item');
	if(oldCur.length != 0 && tit != oldCur.attr('title'))
	{
		oldCur.removeClass('current-menu-item');
		oldCur.animate({
			backgroundColor:butBackgroundColor,
			color:butColor
		 });
	}
	//fade in current item
	if(tit != "search" && tit != oldCur.attr('title'))
	{
		but.addClass('current-menu-item');
		but.animate({
			backgroundColor:hoverBackgroundsAR[tit],
			color:hoversAR[tit]
		 });
	}
	//see if div with title of button as class exists
	//if not then go to page, if so then show div
	var div = $('#sub-nav .'+tit);
	if(div.length == 0)
	{
		$('#sub-nav').fadeOut(function() {
			$('#sub-nav-container').slideUp('slow',function() {
				$('#sub-nav-container').css({backgroundColor:'transparent'});
				//hide callouts
				$('#blog-callout').hide();
				$('#projects-callout').hide();
				if(!tit)
				{
					//hide slideshow
					$('#sub-nav-container').css('height','537px');
					$('.coda-slider-wrapper').css('height','537px');
					$('.coda-slider').css('height','545px');
					$('#sub-nav-container').slideDown('slow',function() {
						$('#home-slideshow').fadeIn('slow',function() {
							$('#home-slideshow-info').show();
							$('#home-slidehow-info-circle').show();
						})
					});
				} else
				{
					//hide slideshow
					$('#home-slideshow').hide();
					$('#home-slidehow-info-circle').hide();
					$('#home-slideshow-info').hide();
				}
				//hide all arrows
				$('.down-arrow').hide();
				$('.up-arrow').hide();
				//to correct position
				resetPositions();
				hideLevels(0,false);
				clearCurrents();
				oldCur = $('.menu li a.current-menu-item');
				if(oldCur.length != 0 && tit != "store" && tit != "contact" && oldCur && tit != "about")
				{
					oldCur.removeClass('current-menu-item');
					oldCur.animate({
						backgroundColor:butBackgroundColor,
						color:butColor
					 });
				}
				//reset all variables
				//arrays for holding levels of div's shown
				levelsAR = new Array();
				curListItemAR = new Array();
				//array for holding one below current list item
				//so know's where to put it
				nextListItemAR = new Array();
				maxLevel = 0;
				curMain = "";
			})
		});
	} else
	{
		//hide levels if not current section
		if(curMain != tit)
		{
			curMain = tit;
			//hide all arrows
			$('.down-arrow').hide();
			$('.up-arrow').hide();
			
			$('#sub-nav').fadeOut(function() {
				$('#sub-nav-container').slideUp('slow',function() {
					//hide callouts
					$('#blog-callout').hide();
					$('#projects-callout').hide();
					//hide slideshow
					$('#home-slideshow').hide();
					$('#home-slidehow-info-circle').hide();
					$('#home-slideshow-info').hide();
					//make subnav visible again
					$('#sub-nav').show();
					//change color
					$('#sub-nav-container').css('background-color',colorsAR[tit]);
					//show correct arrow
					$('#'+tit+'-arrow').show();
					$('#'+tit+'-arrow-up').fadeIn();
					//to correct position
					resetPositions();
					hideLevels(0,false);
					clearCurrents();
					$(this).css('height','200px');
					$(this).slideDown('slow',function() {
						div.fadeIn(500);
						
						//if blog then set font size if any are too small
						if(tit == "blog")
						{
							//fade in callout
							$('#blog-callout').fadeIn();
							$('#blog-tags').css('display','block');
							setBlogFontSize();
							if($('.tag-nav').length > 5)
							{
								setBlogScroll();
							}
						} else if(tit == "projects")
						{
							if((currentCat == "" || currentCat == undefined) && currentSubPageAR.length <= 1)
							{
								$('#projects-callout').fadeIn();
							}
							setProjectsScroll(div);
						}
						levelsAR[0] = div;
						//see if need to show subcat or subnav
						if(curMain != "blog")
						{
							$('#blog-tags').css('display','none');
							checkSubSection();
						} else
						{
							checkTags();
						}
					});
				})
			})
		} else
		{
			resetPositions();
			hideLevels(0,false);
			clearCurrents();
			div.fadeIn(500);
			if(tit == "projects")
			{
				setProjectsScroll(div);
			}
			levelsAR[0] = div;
		}
	}
}

function setBlogFontSize()
{
	if(!hasSetFontSize)
	{
		hasSetFontSize = true;
		$('#sub-nav .blog span').each(function() {
			$(this).textfill({maxFontPixels:15,maxHeight:18,maxWidth:175});
		})	
	}
}

//for blog tag section
function checkTags() {
	//find tag in array and highlight as current
	$('.tag-nav a[href="#tag/'+currentSubPageAR[1]+'/"]').addClass('current');
	tagsAR.push(currentSubPageAR[1]);
}


$(document).ready(function() {
	//main nav to open and show categories
	$(".menu li a").click(function(e) {
		//hide slidehow info div if there
		$('#home-slideshow-caption').hide();
		catID = "";
		tags = "";
		curMain = "";
		isNavClick = true;
		currentSubPageAR = new Array();
		setTopNav($(this));
	});
	
	//set hovers for menu items
	$(".menu li a").hover(function(e) {
		$(this).stop(true, true).animate({
			backgroundColor:hoverBackgroundsAR[$(this).attr('title')],
			color:hoversAR[$(this).attr('title')]
		 });
	},
	function(e) {
		if(!$(this).hasClass('current-menu-item'))
		{
			$(this).stop(true, true).animate({
				backgroundColor:butBackgroundColor,
				color:butColor
			 });
		}
	})
	//set clicks for subnav items
	setSubNavClicks();
})
