var clearMenu; // declare timeout globally

function clearSubmenu(whichid){
		$("#"+whichid).fadeOut();
	}
	
$(document).ready(function(){
	// hide accessible scrollers 
	$("#leftSideP").css("overflow","visible");
	$(".script").show();
		
	placeMiddle();
	
	try{
	 $('#content').jScrollPane({showArrows: true});	
	}catch(err)
	  {
	  //Handle errors here
	  }
	  
   try{
	 $('#contentRight').jScrollPane({showArrows: true});	
	}catch(err)
	  {
	  //Handle errors here
	  }
	
  $("#topmenu li").mouseenter(function(){
		  clearTimeout(clearMenu);
		  var thisid = this.id;
		  $(".submenu").hide();
		  $("#sub_"+thisid).fadeIn();
		  
	  }).mouseleave(function(){
		  clearTimeout(clearMenu);
		  var thisid = this.id;
		  $(".submenu").hide();
		  $("#sub_"+thisid).show();
		  clearMenu = setTimeout("clearSubmenu('sub_"+thisid+"')",3000);
	  });
  
  $(".submenu").mouseenter(function(){
		  clearTimeout(clearMenu);
		  var thisid = this.id;
		  $("#"+thisid).show();
		  
	  }).mouseleave(function(){
		  clearTimeout(clearMenu);
		  var thisid = this.id;
		  clearMenu = setTimeout("clearSubmenu('"+thisid+"')",3000);
	  });
	  
	   $("#submenu_page li.off").children().children().mouseenter(function(){	
	   		if($(this).parent().parent().hasClass('off')){
				//alert("This' grandparent is on");
				var newsrc = $(this).attr("src").replace("_off.gif","_on.gif");
		  		$(this).attr("src",newsrc);
			}
	    	
	  });
	  $("#submenu_page li.off").children().children().mouseleave(function(){
	  		//alert($(this).parent().parent().attr("class"));
	  		if($(this).parent().parent().hasClass('off')){
		  		var newsrc = $(this).attr("src").replace("_on.gif","_off.gif")
		  		$(this).attr("src",newsrc);	
 			}
	  });
	
	  		
		try{
			var newsrc = $("#submenu_page li.on").children().children().attr("src").replace("_off.gif","_on.gif");
						$("#submenu_page li.on").children().children().attr("src",newsrc);
		}
		catch(e){
			 //catch and just suppress error
		}

	// full screen image
 	var bgImage = $('#bgImage');		
    
	function resizeImg() {
		
		var imgwidth = bgImage.width();
		var imgheight = bgImage.height();
		var winwidth = $(window).width();
		var winheight = $(window).height();
		var widthratio = winwidth / imgwidth;
		var heightratio = winheight / imgheight;
		var widthdiff = Math.round(heightratio * imgwidth);
		var heightdiff = Math.round(widthratio * imgheight);

		if(heightdiff>winheight) {
			bgImage.css({
          		'width': winwidth+'px',
          		'height': heightdiff+'px'
        	});
      	} else {
        	bgImage.css({
          		'width': widthdiff+'px',
          		'height': winheight+'px'
        	});
      	}

	 	$("#bgImage").show();
    }
	
    resizeImg();
	placeFooter();
	
    $(window).resize(function() {
      	resizeImg();
		//set footer
		placeFooter();
		
		placeMiddle();
    }); 
});


function placeFooter(){
	var height = $("html").height();
	var footertop = Math.round(height-238);
	//$("#logo").html(height);
	if (height<620){
		$("html").css({'height':'620px'});
		$("#container").css({'height':'620px'});
	}
	$("#footer").css({'top':footertop+'px'});
	// resize img
	//resizeImg();
}

function placeMiddle(){
	var themid = parseInt($(window).height()/2);
		var thetoppos = themid-390;
		if(thetoppos<-40){thetoppos=-40;}
		$("#contentCont").css({'top':thetoppos});
}
