(function($){
    $.fn.zoomOffset = function() {
        var offset = $(this).offset();
        var top = offset.top, left = offset.left;
        var offsetMultiplier = 1;
        if (document.body && document.body.getBoundingClientRect) {
            var bound = document.body.getBoundingClientRect();
            offsetMultiplier = parseFloat(Math.round(((bound.right - bound.left) / document.body.clientWidth) * 100)) / 100;
            if (isNaN(offsetMultiplier)) {
                offsetMultiplier = 1;
            }
            top = Math.round(top / offsetMultiplier),
            left = Math.round(left / offsetMultiplier);
        }
        return { top: top.pxToEm(), left: left.pxToEm(), offsetMultiplier: offsetMultiplier };
    };
})(jQuery);

var isHover = false;
var navTimer = false;
var navDelay = 1000;


function hideNav() {
  clearTimout = navTimer;
  if(!isHover) {
	  $('#meta-nav').css({'z-index': '104'}); /* MST 20110318 */
	  $('#start-nav').removeClass('hover');
	  $('#bg-overlay,#hoverclaim').hide();
	  $('#nav-clone img').hide();
	  $('#nav-clone .active').removeClass('active');
  }
}

function is_touch_device() {  
  try {  
    document.createEvent("TouchEvent");  
    return true;  
  } catch (e) {  
    return false;  
  }  
}


/*
function doNav(elm) {
  $('#nav-holder,#active-nav').remove();
  var cImg = $('img',elm);   
  var cSrc = $(cImg).attr('src').replace(/.png/,'_hover.png'); 
  var left =  $(elm).zoomOffset().left;
  var top =   $('#start-nav').zoomOffset().top;
  var cText = $('span',elm).html();
  var cClaim = $('div',elm).html();
  $('#hovernav').show().html('');
  if($('#hoverclaim').size()==0) {
    $('<div id="hoverclaim"></div>').appendTo('body');
  }
    $('#hoverclaim').css({'top':top});  
    $('#hoverclaim').mouseover(function(){isHover = true}).mouseleave(function(){
      isHover = false;
      navTimer = setTimeout('hideNav()',(navDelay/2));
    });  

  isHover = true;
  $('#hoverclaim').html(cClaim).show();
  var cLoc = $('#hoverclaim a').attr('href');
  $('#hoverclaim').css({'cursor':'pointer'}).click(function(){location.href=cLoc;});
  
  $('<div id="nav-holder">&nbsp;</div><div id="active-nav"><div class="connector">&nbsp;</div><div class="nav-clone"><img src="'+cSrc+'" /><span>'+cText+'</span></div></div>').appendTo('body');
  
  $('#nav-holder').css({'width':$('#start-nav ul').width(),'left':$('#start-nav ul').zoomOffset().left,'top':$(elm).zoomOffset().top}).mouseover(function() {isHover = false; navTimer = setTimeout('hideNav()',(1));});
   var cH = $('#start-nav').height();
   var cW = $('#active-nav div.nav-clone').width();
   
  $('#active-nav').mouseover(function(){isHover = true;}).css({'cursor':'pointer','left':left,'top':top,'height':cH,'width':cW}).click(function(){location.href=cLoc;});
  $('#bg-overlay').show();
  $('#active-nav').mouseleave(function() {
    isHover = false;
    navTimer = setTimeout('hideNav()',(navDelay));
  });
}
*/

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

function initStart() {
  
    //preloading the hover images of the navigation....
	$('#start-nav li img').each(function(i) {
		var cSrc = $(this).attr('src').replace(/.png/,'_hover.png');
		jQuery.preLoadImages(cSrc);	
	});
	
	if($('#bg-overlay').size()==0) {
      $('<div id="bg-overlay" class="homepage"></div>').appendTo('#wrapper'); /* MST 20110803 */
      $('#bg-overlay').hide().mouseover(function(){isHover = false;navTimer = setTimeout('hideNav()',(navDelay));}).click(function() {isHover = false; hideNav(); });  
    }
	
	// cloning navigation... 
	var nav =  $('#start-nav ul').html();
	var pos =  $('#start-nav ul').zoomOffset();
	$('<div id="nav-clone"><div id="hoverclaim"></div><ul>'+nav+'</ul></div>').appendTo('#wrapper');
	$('#nav-clone ul li > a').append('<span class="claim-connector">&nbsp;</span>'); /* AMI 20110224 */
	$('#hoverclaim').mouseover(function(){isHover = true;});
	$('#nav-clone').css({'top':pos.top});
	$('#nav-clone li').each(function() {
		var cImg = $('img',this);
		$(cImg).wrap('<span class="img"></span>');
		var cSrc = $(cImg).attr('src').replace(/.png/,'_hover.png'); 
		$('img',this).attr('src',cSrc);
	   });                        
	$('#nav-clone ul').mouseleave(function() {
		isHover = false;
		navTimer = setTimeout('hideNav()',(navDelay/2));
	});  
	$('#nav-clone li').mouseover(function() {
		$('#meta-nav').css({'z-index': '5'}); /* MST 20110318 */
		$('#bg-overlay').show();
      isHover = true;
      $('#start-nav').addClass('hover');
	  var cClaim = $('div',this).html();
	  $('#hoverclaim').html(cClaim).show();
	  $('#hoverclaim strong').html('<span>'+$('#hoverclaim strong').text()+'</span>');/* AMI 20110224 */
	  var w = $('#hoverclaim span').width();/* AMI 20110224 */
	  $('#hoverclaim').css({'width':w,'margin-left':(w/2)*-1});/* AMI 20110224 */


	  
	  var cLoc = $('#hoverclaim a').attr('href');
	  $('#hoverclaim').css({'cursor':'pointer'}).click(function(){location.href=cLoc;});
	  $('#nav-clone .active').removeClass('active');
	  $('#nav-clone img').hide();
      $(this).addClass('active');
      $(this).click(function(){location.href=cLoc;}); /*AMI 20110225 - ie click fix */
      $('img',this).show();    
    });
    
   
	
	/*
    $('#start-nav li').mouseover(function() {
      $('#start-nav').addClass('hover');
      doNav(this);
    });
    */

}

$().ready(function() {
	
	/* helping iOS Safari: touch on hover-image fires link - without this fix link fires on third touch */
	if(is_touch_device()) {
		$('#nav-clone li span.img img').live('touchend', function(e) {
		    var el = $(this);
		    var link = el.parent().parent().attr('href');
		    window.location = link;
		});
		$('#bg-overlay').css('height', '300%'); /* fix for overlay breaking in mobile Safari */		
	 }
	
});
