
/* avoid flickering on team page */
document.write('<style type="text/css">body{display:none}</style>');
jQuery(function($) {
	$('body').css('display','block');
});

// jquery ready event
$(document).ready(
	function() {	
		prepTeam();
		doPopups();
	}
);


var _teamSelect;
var _teamContent;
var _map = { team:"teams", advisor:"advisors", board:"boards" };

function printPage() {
	window.print();  
}

function search(){
	var str = document.getElementById( "query" ).value;
	window.open( "http://www.google.com/search?q=" + escape(str) + " site:www.knoppneurosciences.com", "searchwindow" );
	return false;
}

//pop ups
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick = function() {
		window.open (this.href,"email","location=0,status=0,scrollbars=0,resizable=1,directories=0,menubar=0,toolbar=0,width=445,height=350");
        return false;
      }
    }
  }
}

/***************************************************************************
// TEAM
***************************************************************************/
function prepTeam(){
	// if team div exists run setup
	
	var team = exists( "#teamTabs" );
	if( team ){		
		var items = $("#teamTabs li a" );
		jQuery.each(items, function() {
			var href = $(this).attr( "href" );
			$(href).hide();
			setTabClick( $(this).parent(), href );
			doBind( $(this).parent() );
		});
	
		//show the first item or item in hash
		var hasHash = checkHash();
		var item;
		if( hasHash != undefined ){
			item = findHash( hasHash );
			
		}else{
			item = $("#teamTabs li a:first");
		}
		var listItem = $(item).parent();
		var href = $(item).attr( "href" );
		$(href).show();
		$(listItem).addClass( "teamHover" );
		_teamContent = $(href);
		_teamSelect = $(listItem);
		setNavItems();
	}
	
}

function doBind( e ){
	$(e).bind("mouseenter", function(){
	  $(this).addClass("teamHover");
	}).bind("mouseleave", function(){
	  $(this).removeClass("teamHover");
	}).removeClass("teamHover"); 
	
}

function unBind( e ){
	$(e).unbind("mouseleave").addClass( "teamHover" );
	
}


function setNavItems(){
	var dl = (document.location).toString();
	var onTeam = dl.indexOf( "team" ) > -1;
	var items = $("li .team a" );
	jQuery.each(items, function() {
			var href = $(this).attr( "href" ).split("#")[1];
			var listItem = findHash( href );
			href = "#" + _map[href];;
			//alert("HREF : " + href );
			$(this).click( 
				function(event){
					event.preventDefault();
					updateContent( $(listItem).parent(), href );	
					return false;
				});
			$(this).attr("href", "#" );
		});
}

function clearHref(){
	var items = $("#teamTabs li a" );
	jQuery.each(items, function() {
		$(this).attr( "href", "#" ); // remove href
	});
}

//find the element that is linked to the hash
function findHash( hash ){
	var items = $("#teamTabs li a" );
	var item;
	//alert(" Hash : " + hash + " : " + "Mapped Item : " + _map[hash] );
	hash = "#" + _map[hash];
	//alert("Hash : " + hash );
	jQuery.each(items, function() {
		var href = $(this).attr( "href" );
		if( href == hash ){
			item = $(this );
		}
	});
	
	return item;
}

function setTabClick( e, href ){	
	$(e).click( 
		function(event){
			event.preventDefault();
			updateContent( e, href );	
		});
}

function updateContent( e, href ){
	if( _teamSelect != undefined && _teamSelect != $(this) ){
		$( _teamContent ).hide();
		doBind( $(_teamSelect ) );
	}	
	//window.location.hash = href;
	$(href).show();
	_teamContent = $(href);
	_teamSelect = $(e);
	unBind( $(e) );
}






/***************************************************************************
// HELPERS
***************************************************************************/

//return the hash in the url
function checkHash(){
	var dl = (document.location).toString();
	var hasHash = dl.split("#")[1];
	return hasHash;
}

/* 	@param		string		#element */
function exists( e ){
	return $(e).length > 0;
}
