$(document).ready(function(){
	/* MENU */
	$('#menuHolder li').hover(function(){
		$(this).children('.subNav').stop(true, true).slideDown('normal');
		}, 
		function(){
		$(this).children('.subNav').stop(true, true).slideUp('normal');
	});

	/* FORM VALIDATION */
  $("#form").validate();

	/* 	GALLERY   */
	if ($("#gallery_thumbs").length) {
		var img = $("<div id='spinner'><img src='../../images/spinner.gif'/></div>");
		img.hide();
		$("#gallery_main").append(img);
		$("#gallery_main img:first").attr("src", $("#gallery_thumbs a").eq(0).attr("href")).load(resizeMainIMG);
	  $("#gallery_thumbs a").click(function(){
	  	var _src = $(this).attr("href");
	  	$("#gallery_main img:first").fadeOut("fast", function(){
	  		$("#spinner").show();
		  	$("#gallery_main img:first").attr("src", _src).load(function(){
		  		$("#spinner").hide();
					resizeMainIMG();
			  	$("#gallery_main img:first").fadeIn("fast");
				});
	  	});
	  	return false;
	  });
	}
	
//	$("#sectionTitle h2").visfr();
});


function resizeMainIMG() {

	var _obj = $("#gallery_main img:first");
/*
  var maxWidth = _obj.width(); // Max width for the image
  var maxHeight = _obj.height();       // Max height for the image
*/

  var maxWidth = 640;
  var maxHeight = 640;

  _obj.css("width", "auto").css("height", "auto"); // Remove existing CSS
  _obj.removeAttr("width").removeAttr("height"); // Remove HTML attributes
  var width = _obj.width();    // Current image width
  var height = _obj.height();  // Current image height

  if(width > height) {
    // Check if the current width is larger than the max
    if(width > maxWidth){
      var ratio = maxWidth / width;   // get ratio for scaling image
      _obj.css("width", maxWidth); // Set new width
      _obj.css("height", height * ratio);  // Scale height based on ratio
      height = height * ratio;        // Reset height to match scaled image
    }
  } else {
    // Check if current height is larger than max
    if(height > maxHeight){
      var ratio = maxHeight / height; // get ratio for scaling image
      _obj.css("height", maxHeight);   // Set new height
      _obj.css("width", width * ratio);    // Scale width based on ratio
      width = width * ratio;  // Reset width to match scaled image
    }
  }
}

