// preloader for "current projects" slideshow
	var preloadImages = ['media/teach_01.jpg',
  											'media/teach_02.jpg',
  											'media/teach_03.jpg',
  											'media/teach_04.jpg',
  											'media/teach_05.jpg',
  											'media/teach_06.jpg'];
	var imgs = [];
  for (var i=0; i<preloadImages.length;i++) {
  			imgs[i] = new Image();
        imgs[i].src = preloadImages[i];}

$(document).ready(function() {

$('a').focus(function() {
  this.blur();
});

var animDuration = 250;

var hiAnimateDots = {   // settings for hoverintent    
    sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
    interval: 50, // number = milliseconds for onMouseOver polling interval    
    over: linksHandler, // function = onMouseOver callback (REQUIRED)    
    timeout: 250, // number = milliseconds delay before onMouseOut    
    out: dummyFunction = function(){ /* do nothing */ } // function = onMouseOut callback (REQUIRED)
};

$('#navbarTop a, #logo a').hoverIntent(hiAnimateDots);

function linksHandler(){
	anchorHref = $(this).attr('href');
	animateLinks(anchorHref);
}

function animateLinks(anchorHref){
  mailHref = anchorHref.slice(0,6);  
  blogHref = anchorHref.slice(7,19);
  if(anchorHref == "index.html"){
			animateDots(143);
  	}
  if(anchorHref == "background.html"){
			animateDots(330);
  	}
  if(anchorHref == "upcoming.html"){
			animateDots(418);
  	}
  if(anchorHref == "media.html"){
			animateDots(510);
  	}
  if(anchorHref == "consulting.html"){
			animateDots(585);
  	}
  if(blogHref == "strongbrands"){
			animateDots(644);
  	}
  if(anchorHref == "publications.html"){
			animateDots(700);
  	}
  if(mailHref == "mailto"){
			animateDots(770);
		}
}
    
function animateDots(distanceLeft){
	$('#dots').animate({
  left: distanceLeft}, animDuration);
}

function changeContent(newAddress){
  var contentTarget = newAddress + " #content";
  	$('#content').fadeOut(animDuration, function(){
			$('#content').load(contentTarget, function(){
			  if(newAddress=="upcoming.html"){
    	     $.getScript("js/calkinsUpcoming.js", function(){
      	  	$('#content').fadeIn(animDuration, function(){
						  animateLinks(newAddress);
          	});
	        });
  	   } else {
    	   	$('#content').fadeIn(animDuration, function(){
				  animateLinks(newAddress);
	      }); // fadeIn()        
   		} // end if/else 
  	}); // content.load()
  }); // content.fadeOut
} // changeContent

$('#logo a, #navbarTop a:not([href^="http"]):not([href^="mail"]), #content a:not([href^="http"])').live('click', function(evt){
	var newAddress = $(this).attr('href');
//	document.location = '/#/' + newAddress; // this one works every time
	$.address.value(newAddress); // this one always uses the existing root, whether correct or not.
  return false;
  });

$.address.change(function(evt){
	newAddress = $.address.value().slice(1);
  fixAddress(newAddress);
	if(document.URL.match(/[^/]*$/) == ""){
    newAddress = "index.html";
  }
  else {
	  newAddress = $.address.value().slice(1);
  }
	changeContent(newAddress);
  animateLinks(newAddress);
  });

function fixAddress(newAddress){
  if(newAddress.toString().length == 0){
  	if(document.URL == ('http://' + document.domain + '/#/')){
    		document.location = 'http://' + document.domain + '/';
    	} 
    return newAddress;
  }
	var varAddress = '/' + newAddress;
	varAddress = varAddress.match(/[^/]*$/);
  var docUrl = document.URL;
  var urlLength = document.URL.length, docUrlLength = document.URL.length;
  var docFileName = document.URL.match(/[^/]*$/);
  var properUrl = 'http://' + document.domain + '/#/' + docFileName;
  if(docUrlLength == properUrl.length){
	  return varAddress;
  } else { 
	  document.location = 'http://' + document.domain + '/#/' + varAddress;
    return varAddress.slice(1);
  }
  return varAddress;
}

$.address.init(function(event){
  newAddress = document.URL.match(/[^/]*$/);
	  if(newAddress=="upcoming.html"){
 	     $.getScript("js/calkinsUpcoming.js", function(){/*callback if needed*/});
       } 
  if(newAddress == ''){
			// $.address.value("index.html"); // optional
  } else {
  }
  animateLinks(newAddress);
	changeContent(newAddress);
}); // address.init()

}); // doc ready()

