var banners;

$(function() {
    // <IE7 Navigation
    if ($.browser.msie && $.browser.version < 7) {
        // Emulate menu hover
        $("#navigation li").mouseover(function() {
            $(this).addClass("hover");
        });
        $("#navigation li").mouseout(function() {
            $(this).removeClass("hover");
        });
        $("a.anatomy_link").hover(function() {
            $("span", this).css("display", "block");
        },function() {
            $("span", this).css("display", "none");
        });
        // Style required labels
        $("span.required + label").addClass("required");
    }

    // Banner rotator
    var bg = $("div#banner a img").attr("src");
    $("div#banner a").css("background-image", "url(" + bg + ")");
    $.ajax({
        type:       "GET",
        url:        "ajax_banner.php",
        dataType:   "json",
        success: function(data) {
            banners = data;
            setTimeout("rotate()", 5000);
        }
    });

    // Alternate row colours
    $("table.enhancedtable tr:even").addClass("even");

    // Datepicker
    $(":input.date").datepicker({
        buttonImage:    'images/date.gif',
        buttonText:      'Select date',
        dateFormat:      'yy-mm-dd',
        showOn:        'both'
    });
    $(".ui-datepicker-trigger img").each(function() {
        $(this).after("<span>"+ $(this).attr("alt") +"<\/span>");
    });

    // Colour switcher
    if ($("#colour-switcher").length) {
      colourSwitchPreload();
      $("#colour-switcher .chips img").hover(colourSwitchEnter, colourSwitchLeave);
    }
});
function resetAndHide(ele) {
    $("dd.mandatory").remove();
    $(ele).parents("form").hide();
}
function rotate() {
    var bg      = $("div#banner a");
    var img     = $("div#banner img[width=245]");

    if (banners.length > 0) {
        var rand = banners.shift();
        banners.push(rand);

        img.fadeOut("slow", function() {
            img.attr("src", "uploads/images/" + rand.image);
            img.fadeIn("slow", function() {
                bg.css("background-image", "url(uploads/images/" + rand.image + ")");
            });
            $(img).parents("a").attr("href", "gallery.php?view=" + rand.id);
        });
        setTimeout("rotate()", 5000);
    }
}
function validate(form) {
    var valid = true;
    $("dd.mandatory").remove();
    $("#" + form + " :input.required").each(function() {
        if ($(this).val() === "") {
            if (valid) {
                // Give focus to the first invalid item
                $(this).focus();
            }
            var classes = $(this).attr("class");

            $(this).parents("dd").after("<dd class='mandatory "+ classes +"'>This is a required field</dd>");
            valid = false;
        }
    });
    $("#" + form + " :input.email").each(function() {
      if ($(this).val() !== "") {
        var isEmail = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[_a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i.test($(this).val());
        if (!isEmail) {
          if (valid) {
              // Give focus to the first invalid item
              $(this).focus();
          }
          var classes = $(this).attr("class");
          $(this).parents("dd").after("<dd class='mandatory "+ classes +"'>Please input a valid e-mail address</dd>");
          valid = false;
        }
      }
    });
    $("dd.mandatory+dd.mandatory").remove();

    return valid;
}

/* Colour switcher */

var colourSwitchTimer = false;
function colourSwitchEnter() {
  clearTimeout(colourSwitchTimer);

  var window_path = $("#colour-switcher .window img").attr("src").replace(/[^\/]*\.jpg$/, "");

  var window = $(this).attr("src").replace(/^.*\//, "");
  window = window.replace(/\.png$/, ".jpg");

  $("#colour-switcher .window img").attr("src", window_path + window);

  var name = $(this).attr("alt");
  $("#colour-switcher .window .name").text(name);
}
function colourSwitchLeave() {
  colourSwitchTimer = setTimeout(function() {
    var window_path = $("#colour-switcher .window img").attr("src").replace(/[^\/]*\.jpg$/, "");

    $("#colour-switcher .window img").attr("src", window_path + "white.jpg");
    $("#colour-switcher .window .name").text("RB7003 White");
  }, 150);
}
function colourSwitchPreload() {
  var window_path = $("#colour-switcher .window img").attr("src").replace(/[^\/]*\.jpg$/, "");

  $("#colour-switcher .chips img").each(function() {
    var window = $(this).attr("src").replace(/^.*\//, "");
    window = window.replace(/\.png$/, ".jpg");

    var cacheImage = document.createElement("img");
    cacheImage.src = window_path + window;
  });
}
