$(function() {
  var match = location.href.match(/([^\/]*).html/);
  if (match && match[1] != "index") {
    // if we're on a project page...
    var project = match[1];
    var img_path = "images/" + project + "/";
    $("#project_image").css("background-image", "url('" + img_path + "1.jpg')");

    $(".buttons").mouseover(function(e) {
      var tar = $(e.target)
      var filename = tar.attr("src").split(/buttons\//)[1];
      $("#project_image").bg(img_path + filename)
      var replace = tar.attr("src").replace(/\./, "_hover.");
      tar.attr("src", replace);
    })
    $(".buttons").mouseout(function(e){
      var tar = $(e.target);
      var replace = tar.attr("src").replace(/_hover/, "");
      $(e.target).attr("src", replace);
    });
    $("#project_image").click(function(e) {
      var src = $(e.target).css("background-image");
      var img = src.match(/\/(\d+).jpg/)[1];
      var project = location.href.match(/\/([^\/]+)\.html/)[1];
      var opts = "location=no,menubar=no,scrollbars=no,status=no,titlebar=no,toolbar=no,resizable=no,width=800,height=600";
      window.open("zoom.html?img="+img+"&project="+project, "Zoom", opts);
    });
    $("#before_rollover").mouseover(function() {
      $("#project_image").css("background-position", "0 420px");
    }).mouseout(function() {
      $("#project_image").css("background-position", "0 0");
    })

    $.fn.bg = function(src) {
      return $(this).css("background-image", "url('" + src + "')");
    };    
  } else {
    // we're on the homepage so swap images...
    var _i = 2;
    function _img_change() {
      $img = $("#home_left img");
      $img.animate({"opacity": "0"}, 1000, function() {
        $img.attr("src", "images/home/" + _i + ".jpg");
        $img.animate({"opacity": "1"}, 1000);
        _i++;
        if (_i == 8) _i = 1;
      });
    };
    //change 6000 if you want to change how fast the images are swapped
    setInterval(_img_change, 6000);
  };
});

function preload(project,num) {
  for (var i=1;i<num+1;i++) {
    var src = "images/"+project+"/"+i+".jpg";
    var img = "<img style='height:0px' src='" + src + "' />";
    $("body").append(img);
  };
};
