$(document).ready(function() {
  
  $(window).load(function() {
    
    if($("body").hasClass("home")) {
      var i = Math.floor(Math.random() * quotes.length);
                      
      $("#quote").html(quotes[i]["quote"] + "&rdquo;");
      $("#cite").html(quotes[i]["cite"]);
      var qpos = $("#quote").offset().top - 20;
      
      $("body").append("<img src='/images/quote-left.png' alt='' id='quotemark' style='top: " + qpos + "px;' />")
          
      var pos = $("#cite").offset().top + parseInt($("#cite").height()) - 6;

      $("#container").wrap("<div style='background: url(/images/" + quotes[i]["colour"] + ".png) 0 " + pos + "px repeat-x;'/>")
    }

  })

  $(".menuitem").each(function() {

    
    if($(this).parent().hasClass("on")) {
      if($(this).find("ul:first li").length == 3) {
        $(this).css({marginTop: '41px'});
      } else if($(this).find("ul:first li").length == 4) {
        $(this).css({marginTop: '19px'});
      } else if($(this).find("ul:first li").length == 2) {
        $(this).css({marginTop: '57px'});
      } else if($(this).find("ul:first li").length == 1) {
        $(this).css({marginTop: '76px'});
      } else if($(this).find("ul:first li").length == 5) {
        $(this).css({marginTop: '3px'});
      }
      
    } else {
    
      $(this).mouseleave( function() { $(this).animate({marginTop: '99px'}) } );
      if($(this).find("ul:first li").length == 3) {
        $(this).mouseenter( function() { $(this).animate({marginTop: '41px'}) } );
      } else if($(this).find("ul:first li").length == 4) {
        $(this).mouseenter( function() { $(this).animate({marginTop: '19px'}) } );
      } else if($(this).find("ul:first li").length == 2) {
        $(this).mouseenter( function() { $(this).animate({marginTop: '57px'}) } );
      } else if($(this).find("ul:first li").length == 1) {
        $(this).mouseenter( function() { $(this).animate({marginTop: '76px'}) } );
      } else if($(this).find("ul:first li").length == 5) {
        $(this).mouseenter( function() { $(this).animate({marginTop: '3px'}) } );
      }
    }
  })
  
  if($("h1").length == 1) {
    
    if($("h1").next().get(0).nodeName.toLowerCase() == "h2") {
      var colours = ["orange","dark-yellow","aqua","purple","green"];
      var i = 0
      
      $("h2").each(function() {

        var pos = $(this).offset().top + $(this).height();
        $("#container").wrap("<div style='background: url(/images/" + colours[i] + ".png) 0 " + pos + "px repeat-x;'/>")
        i++;

      })
    } else {
    
      if($("body").hasClass("who-we-are")) {
        var colour = "yellow"
      } else if($("body").hasClass("what-we-do")) {
        var colour = "green"
      } else if($("body").hasClass("contact-us")) {
        var colour = "blue"
      }
    
      var pos = $("h1").offset().top + $("h1").height();
      $("#container").wrap("<div style='background: url(/images/" + colour + ".png) 0 " + pos + "px repeat-x;'/>")    
    }
    
  } else if($("h1").length > 1) {
    
    var colours = ["orange","dark-yellow","aqua","purple","green"];
    var i = 0
    $("h1").each(function() {
    
      if($(this).next().get(0).nodeName.toLowerCase() == "h1") {
        $(this).hide()
      } else {
        var pos = $(this).offset().top + $(this).height();
        $("#container").wrap("<div style='background: url(/images/" + colours[i] + ".png) 0 " + pos + "px repeat-x;'/>")
        i++;
      }
    
    })
  }
    
})
