jQuery(document).ready(function($){
	
  // is this safari?
  isSafari3 = false;
  if(window.devicePixelRatio) isSafari3 = true;
  
  if (isSafari3){
    //code to show Safari Styles
    $("#topnav .searchbar").css({'margin-left' : '467px', 'top' : '-16px'});
  }
  

	/*	Instantiate navigation menus */
	initNavigation();
	
});

function initNavigation(){
	$("#topnav .nav .container").hover( function(){ topnavHover(this); }, function(){ topnavRollout(this); });
	$("#left_navigation .container").hover( function(){ leftnavHover(this); }, function(){ leftnavRollout(this); });
}

function topnavHover(element){
  var container = $("#"+element.id+".container");
  var title     = $("#"+element.id+" .title");
  var topspacer = $("#"+element.id+" .topspacer");
  var dropdown  = $("#"+element.id+" .dropdown");
  
  dropdown.css("display","block");
  container.addClass("rollover");
  
  if(topspacer.width() != title.width()){
	  var top         = $("#"+element.id+" .top");
	  var top_right   = $("#"+element.id+" .top_right");
	  var children   	= $("#"+element.id+" .children");
    var offsetTop   = container.height();
    var offsetLeft  = container[0].offsetLeft;
    var bottom_l    = $("#"+element.id+" .bottom_l");
    var bottom_m    = $("#"+element.id+" .bottom_m"); 
    var bottom_r    = $("#"+element.id+" .bottom_r");
    
    if(children.css('left')=='auto')
      children.css('left',0)
    if(children.css('margin-left')=='auto')
      children.css('margin-left', 0)
    
    topspacer.css('left', parseFloat(children.css('margin-left'))+parseFloat(children.css('left')));
    topspacer.width(container.width()-parseFloat(children.css('margin-left')));
    top.css('left', topspacer.width()+parseFloat(topspacer.css('left')));
    top.width(children.width() - topspacer.width()-10);
    top_right.css('left', top.width()+parseFloat(top.css('left')));
    
    bottom_l.css('top', children.height()+topspacer.height());
    bottom_l.css('left',parseFloat(children.css('margin-left'))+parseFloat(children.css('left')));
    bottom_m.css('top', bottom_l.css('top'));
    bottom_m.css('left', bottom_l.width()+parseFloat(bottom_l.css('left')));
    bottom_m.width(children.width()-bottom_l.width()-bottom_r.width()+1);
    bottom_r.css('top', bottom_l.css('top'));
    
    /*
     *  Update the bottom right element
     */
    // default value if bottom_left = auto, throws nan if true
    if(bottom_l.css('left')=='auto')
      var bottom_l_left = 0;
    else
      var bottom_l_left = parseFloat(bottom_l.css('left'));
    bottom_r.css('left',bottom_l.width()+bottom_m.width()+bottom_l_left);
  }
}

function leftnavHover(element){
  var bottom_l = $("#left_navigation #"+element.id+" .bottom_l");
  var bottom_m = $("#left_navigation #"+element.id+" .bottom_m");
  var bottom_r = $("#left_navigation #"+element.id+" .bottom_r");
  
  $('#left_navigation .container#'+element.id+' .dropdown').css('display', 'block');
  var top_margin = $("#left_navigation .container#"+element.id+" .children").height() + parseFloat($('#left_navigation .container#'+element.id+' .children').css('top'));  
  
  if(parseFloat(bottom_l.css('top')) != top_margin || parseFloat(bottom_m.css('top')) != top_margin || parseFloat(bottom_r.css('top')) != top_margin){
    if(element.id == $('#left_navigation ul li:first').children()[0].id){
      var dropdown_container = $("#left_navigation .container#"+element.id+" .dropdown");
      dropdown_container.css('margin-top', parseFloat(dropdown_container.css('margin-top')));
    }
    bottom_l.css('top', top_margin);
    bottom_m.css('top', top_margin);
    bottom_r.css('top', top_margin);
  }
}

function topnavRollout(element){
  $(element).removeClass("rollover");  
  $("#topnav .nav #" + element.id + " .dropdown").css("display","none");
}

function leftnavRollout(element){ 
  $("#left_navigation #" + element.id + " .dropdown").css("display","none");
}

function getTitleWidth(element){
  var width = getWidthFloat(element);
  if(element.id == $('#topnav .nav .title .first').id){
    width -= parseFloat(getStyle('#topnav .left .container', "margin-left"));
  }
  return width + "px";
}

function getChildWidthFloat(element, title){
  var width = getWidthFloat(element);
  return width + "px";
}

function getWidthFloat(element){
  return parseFloat(getWidth(element));  
}

function getWidth(element){
  return getStyle(element, "width");
}

function setWidth(element, value){
  setStyle(element, 'width', value);
}

function getLeftFloat(element){
  return parseFloat(getLeft(element));
}

function getLeft(element){
  return getStyle(element, 'left');
}

function setLeft(element, value){
  setStyle(element, 'left', value);
}

function getTopFloat(element){
  return parseFloat(getTop(element));
}

function getTop(element){
  return getStyle(element, 'top');
}

function setTop(element, value){
  setStyle(element, 'top', value);
}

function getHeightFloat(element){
  return parseFloat(getHeight(element));
}

function getHeight(element){
  return getStyle(element, 'height');
}

function setHeight(element, value){
  setStyle(element, 'height', value);
}

function setStyle(element, style, value){
  return $(element).css(style, value);
}

function getStyle(element, style){
  return $(element).css(style);
}

function l(msg){
  console.log(msg);
}

