function validate(formName){
    $(".formErrors").hide();
    errorFlag = false;
    errors = "";
    
    if ($("#contactName").attr("value") == "" || $("#contactName").attr("value") == null){ errorFlag = true; errors += "Please fill out your name. <br>";}
    email = $("#contactEmail").attr("value");
    if (email != "" && email != null){
        //if (email.indexOf("@") > 0 && email.lastIndexOf(".") > 0 && email.lastIndexOf("@") < email.indexOf(".")){ /*all good. the conditional statement is easier to test positive than negative*/ }
        //else {errorFlag = true; errors += "Please provide a valid email address.<br>";}
    }
    else {errorFlag = true; errors += "Please provide an email address. <br>";}
    if ($("#contactMessage").attr("value") == "" || $("#contactMessage").attr("value") == null){ errorFlag = true; errors += "Please provide a message to send!<br>"; }   
    if ($("#contactSecurity").attr("value") !== "elevator"){ errorFlag = true; errors += "Please fill out the security text. <br>"; }
    
    if (errorFlag == true){ $(".formErrors").html(errors).slideDown(); }
    else {$("#" + formName).submit();}
}


      function moveMenu() {
        var topOfMenu = $(window).height() - 20;
    if(topOfMenu <= 578){$(".menu ul").css({"top": topOfMenu + "px"});
}
else {$(".menu ul").css({"top": "578px"});}
    
};
      
var resizeTimer = null;
      $(window).bind('resize', function() {
      if (resizeTimer) clearTimeout(resizeTimer);
      resizeTimer = setTimeout(moveMenu, 50);
      });

$(document).ready(function() {
    $("#lightbox").hide();
    $(".slider").hide();
    $("#lightboxBlack").hide();
    $(".formErrors").hide();
     $(window).load(function(){
        $("#loading").fadeOut(600, function(){
            $(".wrapper").animate({marginTop: "5px"}, 800, "easeOutCubic", function(){
            var locale = window.location.toString();
            var pos = locale.substr(locale.indexOf("#") + 1);
            moveMenu();
            if (parseInt(pos) >= 0){
            openPanel(parseInt(pos), "override");
            }
            else{openPanel(4, "override");}
        });
        });
     });
 
    $("ul.eb_accordian li").not(":first-child").css({height: "20px"});
      $("ul.eb_accordian li").bind("mouseenter", function(){
            $(this).siblings().stop().animate({height: "20px"});
            $(this).stop().animate({height: "240px"});
            });
      
 
    $("a.openSlider").each(function(){
        $(this).click(function(){
            openPanel($(this).attr("href").substr(1));
            });   
    });
    $(".menu li").bind("mouseenter", function(){
        $(this).stop().animate({height: "30px", top: "-10px"}, 200, "easeInOutCubic");
        }).bind("mouseleave", function(){
        $(this).stop().animate({height: "20px", top: "0px"}, 200, "easeInOutCubic");
        });
    
  function lbOn(img, desc){
    //reset size
    $("#lightboxBlack").css({opacity: ".6"}).fadeIn("fast");
    $("#lightbox p").empty();
    $("#lightbox").css({width: "100px", height: "100px", marginLeft: "-50px"})
    $("#loading").show(); //show loading
    $("#lightbox img").hide();
    $("#lightbox").fadeIn("fast"); //introduce box
    //create a new image object
    var theImage = new Image();
    theImage.src = img.replace("thumbnail", "fullsize"); //load it from the src
    theImage.onload = function(img) {
        //animate window size, swap src's, fade in image add paragraph, remove loading
                        
                        $("#lightbox").animate({
                            width: theImage.width + 30 + "px",
                            height: theImage.height + 60 + "px",
                            marginLeft: (((theImage.width + 30)/2) * -1) + "px"
                        }, function(){
                            $("#loading").fadeOut();
                            $("#lightbox img").attr({src: theImage.src}).fadeIn("normal");
                            $("#lightbox p").text(desc);
                            });
                        
        }
    }
  
  $("#lightbox").click(function(){
    $(this).fadeOut("slow");
    $("#lightboxBlack").fadeOut("fast");
    });
  $(".lb").click(function(){
    lbOn($(this).attr("src"), $(this).attr("title"));
  });



  function openPanel(obj, mode){
    //if we get the object, animate it, if we get it's index, reference it, then animate it!
    if (!$.isArray(obj)){obj = $(".slider").eq(obj);}//jQuery objects are Arrays, and numbers never are!
    
    if ($(".slider:visible")[0] != obj[0]){
        if (mode == "override"){
            $(".slider").hide(); 
            obj.show().animate({height: "450px", top: "6px"}, 900, "easeInCubic");
        }
        else {
        $(".slider:visible").animate({
            height: "1px", 
            top: "455px"
            }, 1000, "easeOutCubic", function(){
          $(".slider").hide();      
          obj.show().animate({height: "450px", top: "6px"}, 900, "easeInCubic");
          });
        }
    }
  }
});
