$(document).ready(function(){	

   
/*
 $("body").prepend('<div id="overlay"><img src="images/whatwedo.jpg"></div>');
	 $("#overlay").css({'position':'absolute','left': '163px','margin-left' : 'auto','margin-right' : 'auto', 'z-index' : '10000000'});
	 $("#overlay").fadeTo(1, 0.5);   
	 $("#overlay").toggle(
			function () {
				$(this).fadeTo(2, 1.0);   
    	},
			function () {
				$(this).fadeTo(2, 0.5);   
    	},
			function () {
				$(this).fadeTo(2, 0.01);   
			},
			function () {
				$(this).fadeTo(2, 0.5);   
    	}
			);
*/

// Auto-click nav from cookies
/*
navID=readCookie(window.location);
if(navID!=null){
	$(window).load(function(){

	$('#'+navID).parent().parent().parent().click();
	$('#'+navID).parent().click();
	$('#'+navID).click();
	});
}*/

// employee brain hovers:
$(".employee").hover(function(){
	var parts=$(this).attr("rel");
	for(i=0; i<4; i++){
		//alert(i+": "+parts.substr(i,1));
		if(parts.substr(i,1)==1){
			$("#brain"+i).fadeIn(300);
		} else {
			$("#brain"+i).fadeOut(300);
		}
	}
},function(){
	for(i=0; i<4; i++){
		if(keepParts.substr(i,1)==1){
			$("#brain"+i).fadeIn(300);		
		} else {
			$("#brain"+i).fadeOut(300);
		}
	}
});

var locked=false;
$('.navTab').click(function(event){locked=true; $(this).showSub(); event.stopPropagation();});
$('.navImg').hover(
	// add _over
	function(){$(this).attr('src',$(this).attr('src').replace(/.gif/,'_over.gif'));},
	// remove _over
	function(){$(this).attr('src',$(this).attr('src').replace(/_over.gif/,'.gif'));}
);
$('.navTab').hover(
	function(event){if(!locked){$(this).children('.subnav').stop(true); $(this).showSub();}},
	function(event){if(!locked){$(this).children('.subnav').stop(true); $(this).children('.subnav').fadeOut();}}
);

$('body').click(function(){$('.navTab').each(function(){locked=false;}); $('.subnav').fadeOut();});

});


jQuery.fn.showSub = function (){
	curSub=$(this).children('.subnav');
	$('.subnav').fadeOut();
	curSub.stop(true);
	curSub.fadeIn();
	curSub.fadeTo(100,1);
}


function slideNav(){
	$('#navStage').animate({"top":"0px"},500,"swing");
	return false;
}

// transform the text logo to full logo
function xformLogo(){

}

var elder=null;
// spiffynav a UL
jQuery.fn.spiffyNav = function(){

	elder=$(this);
	// hide all the second-tier and up elements
	$(this).children('li').children(":not(img)").each(function(){	
		$(this).hide();
	});

	// make the first-tier elements show their children on click
	$(this).children('li').click(function(event){
		setBeauCookie($(this));
		// close the children if it's already shown
		if($(this).hasClass('active')){
		$('.accent').hide();
			$(this).children(":not(img)").each(function(){
				$(this).slideUp();
			});

		// remove the beauCookie
		eraseBeauCookie();

		// make it inactive
		$(this).removeClass('active');

		// swap the arrow
		$(this).children('img').attr('src','./images/level1closed.gif');	
	elder.prev("p").slideDown();
		event.stopPropagation();
		$('.accent').hide();
		return true;
		}		

		// hide the accent
		$('.accent').fadeTo(0,1);
		$('.accent').hide();

		// hide the landing text 
		$(elder).prev("p").hide();

		// hide all the second tier and up  
		$(elder).children('li').children(":not(img)").each(function(){	
			$(this).hide();
		});

		// swap out the shown/hidden image
		$(elder).children('li').children('img').attr('src','./images/level1closed.gif');				
		$(elder).children('li').children('ul').children('li').children('img').attr('src','./images/level2closed.gif');
		$(elder).children('li').children('ul').children('li').children('ul').children('li').children('img').attr('src','./images/level3closed.gif');		
		// remove all the active classes
		$(elder).children('li').removeClass('active');
		// remove second/third tier acitves
		$(elder).children('li').children('ul').children('li').removeClass('active');
		$(elder).children('li').children('ul').children('li').children('ul').children('li').removeClass('active');

		// swap out the shown/hidden image
		$(this).children('img').attr('src','./images/level1open.gif');

		// set the active class
		$(this).addClass('active');

		// show the second-tier of the clicked first-tier
		$(this).children(":not(.stayHid)").each(function(){
			$(this).children().children(":not(img)").hide();
			$(this).slideDown(function(){
				$('#bottomLogo').append(document.createElement('div'));

			});

		});
	});

	// make the second-tier elements show their children on click
	$(this).children('li').children('ul').children('li').click(function(event){

		// hide the accent
		
		$('.accent').fadeTo(0,1);
		$('.accent').hide();

		// skip it if it's already shown
		if($(this).hasClass('active')){			
			$(this).children(":not(img)").each(function(){
				$(this).slideUp();
			});
		// make it inactive
		$(this).removeClass('active');

		// cookie the parent
		setBeauCookie($(this).parent().parent());
		// swap the arrow
		$(this).children('img').attr('src','./images/level2closed.gif');	
		event.stopPropagation();
		return true;
		}	
		setBeauCookie($(this));
		// hide all the third tier and up  
		$(elder).children('li').children('ul').children('li').children(":not(img)").each(function(){	
			$(this).hide();
		});

		// swap out the shown/hidden image
		$(elder).children('li').children('ul').children('li').children('img').attr('src','./images/level2closed.gif');				

		// remove all the active classes
		$(elder).children('li').children('ul').children('li').removeClass('active');

	
		// swap out the shown/hidden image
		$(this).children('img').attr('src','./images/level2open.gif');

		// set the active class
		$(this).addClass('active');

		// show the third-tier+ of the clicked second-tier
		$(this).children(":not(div)").each(function(){
			//$(this).children().children(":not(img)").hide();
			$(this).slideDown(function(){$('.accent').css('height',($(this).parent().height()-6));});
		});

		// set the accent div placement and dimensions
		setAccent(this);
		event.stopPropagation();
	});

	// third tier behavior: 
	// make the third-tier elements toggle their hidden elements on click
	$(this).children('li').children('ul').children('li').children('ul').children('li').click(function(event){

		// hide the accent		
		$('.accent').fadeTo(0,1);
		$('.accent').hide();

		// skip it if it's already shown
		if($(this).hasClass('active')){
			$(this).children(":not(img)").each(function(){
				$(this).hide();			
			});
		$(this).children("p").slideDown(function(){$('.accent').css('height',($(this).parent().height()-6));});

		// cookie the parent
		setBeauCookie($(this).parent().parent());

		// make it inactive
		$(this).removeClass('active');

		// swap the arrow
		$(this).children('img').attr('src','./images/level3closed.gif');	
		// set the accent div placement and dimensions
		setAccent(this);
		event.stopPropagation();
		return true;
		}	

		setBeauCookie($(this));

		// swap out the shown/hidden image
		$(elder).children('li').children('ul').children('li').children('ul').children('li').children('img').attr('src','./images/level3closed.gif');				

		// remove all the active classes
		$(elder).children('li').children('ul').children('li').children('ul').children('li').removeClass('active');

		// swap out the shown/hidden image
		$(this).children('img').attr('src','./images/level3open.gif');

		// set the active class
		$(this).addClass('active');

		// hide the current p
		$(this).children("p").each(function(){
			$(this).slideUp();
		});
		// show the first div
		$(this).children("div:first").slideDown(function(){$('.accent').css('height',($(this).parent().parent().height()-6));});

		// set the accent div placement and dimensions
		setAccent(this);
		event.stopPropagation();
	});

	// make the 'more' button cycle through the divs
	$(".moreButton").click(function(event){
	// put the next div to the right of this one
	//$(this).parent().next("div").slideDown();
	$(this).parent().animate({left:"-310px"},500,"swing",function(){$(this).next("div").fadeIn(function(){setAccent($(this).parent().parent());}); $(this).hide(); $(this).css({left:"0px"});});
	event.stopPropagation();
	});

	// make the 'moreBio' button cycle through the divs
	$(".moreBio").click(function(event){

	// put the next div to the right of this one
	$(this).parent().animate({left:"-310px"},500,"swing",function(){$(this).next("div").fadeIn(); $(this).hide(); $(this).css({left:"0px"});});
	event.stopPropagation();
	});

	$(".lessButton").click(function(event){
	// put the next div to the right of this one
	//$(this).parent().previous("div").slideDown();
	$(this).parent().animate({left:"310px"},500,"swing",function(){$(this).prev("div").fadeIn(function(){setAccent($(this).parent().parent());}); $(this).hide(); $(this).css({"left":"0px"}); });
	event.stopPropagation();
	});

	$(".lessBio").click(function(event){
	// put the next div to the right of this one
	//$(this).parent().previous("div").slideDown();
	$(this).parent().animate({left:"310px"},500,"swing",function(){$(this).prev("div").fadeIn(); $(this).hide(); $(this).css({"left":"0px"}); });
	event.stopPropagation();
	});
	$(this).fadeIn(100);
}


function setAccent(element){
		accentPos=$(element).position();
		accentHeight=$(element).height();
		$('.accent').css({'top':(accentPos.top+18),'height':(accentHeight-10)});
		$('.accent').fadeIn(1500);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function eraseBeauCookie(){
	eraseCookie(window.location);
}

function setBeauCookie(element){
	createCookie(window.location,element.attr('id'),30);
}

var runSlides=null;
var currentSlide=0;
var lastSlide=null;
var nextSlide=null;
var Gimages='';
var Gscrips='';
var Gstart='';
var noshow=null;

jQuery.fn.slideshow = function (images,scrips,start){
	currentSlide=start;
	nextSlide=start+1;
	Gimages=images;
	Gscrips=scrips;
	Gstart=start;
	// show the first slide
	$(this).children('#slideImage').html("<div id='slideImg"+start+"'><img src='"+images[start]+"'></div>");
	$(this).children('#slideImage').children('#slideImg'+start).css("background","url('"+images[(start+1)]+"') no-repeat top left");
	// show the first scrip
	$(this).children('#slideTitle').html(scrips[start]);
	// set the current link as active
	$(this).children('#slideNav').children('#slideJump'+start).toggleClass('active',true);
	runSlides=setTimeout(function(){$('#slideshow').nextSlide();},5000);
}

// goes to the next slide and caches the next next slide
jQuery.fn.nextSlide=function(skip){
	//alert(skip);
	// if we're at the last slide in the series set the next slide to 0 so it loops.
	if(currentSlide>=(Gimages.length-1) && skip===undefined ){ // at the end and not manually moving
		nextSlide=0; 
	} else if(skip<=(Gimages.length-1) && skip!==undefined){ // skipping and not outside our range
		nextSlide=skip;
	} else if(skip!==undefined){nextSlide=0; // skipping outside of range

	} else {
		nextSlide=currentSlide+1;
	}
	if(nextSlide<=-1){nextSlide=0} // never go negative
	//alert(lastSlide+", "+currentSlide+", "+nextSlide);
	if(nextSlide==currentSlide){return false;}


	// remove the last slide which is already transparent
	$(this).children('#slideImage').children('#slideImg'+(lastSlide)).remove();
	// show the next slide
	$(this).children('#slideImage').prepend("<div id='slideImg"+(nextSlide)+"'><img src='"+Gimages[(nextSlide)]+"'></div>");
	$(this).children('#slideImage').children('#slideImg'+(nextSlide)).css("background","url('"+Gimages[(nextSlide+1)]+"') no-repeat top left");
	// fade the current slide
	$(this).children('#slideImage').children('#slideImg'+(currentSlide)).fadeOut(1000);

	// update the active link
	$(this).children('#slideNav').children('#slideJump'+currentSlide).toggleClass('active',false);
	$(this).children('#slideNav').children('#slideJump'+nextSlide).toggleClass('active',true);

	// update the current scrip
	$(this).children('#slideTitle').html(Gscrips[nextSlide]);

	// set the slideback and  link behavior
	$('#slideBack').attr('rel',nextSlide-1);
	$('#slideNext').attr('rel',nextSlide+1);


	// set the numbers for next time
	lastSlide=currentSlide;
	currentSlide=nextSlide;

	// run self again
	runSlides=setTimeout(function(){$('#slideshow').nextSlide();},5000);


}

// goes to the previous slide and caches the previous previous slide
jQuery.fn.prevSlide= function(){

}

// jumps striaght to a slide
jQuery.fn.skipTo= function(slide){

nextSlide=parseInt(slide);

clearTimeout(runSlides);
$('#slideshow').nextSlide(nextSlide);
setTimeout("clearTimeout(runSlides);",2000);
}


