$(document).ready(function(){

  $("#close-house-photos").click(function(event){
    $("#house-photos-prev").attr("alt", last_house_photo);
    $("#house-photos-next").attr("alt", "1");
    $("#house-photos-current").attr("src", house_photos[0].src);
    $("#preload-house-photos").html("");
    $(".house-photos-rooms").parent("li").removeClass("up");
    $("#photo-gallery").hide();
    event.preventDefault();
  });

  $(".show-house-photos").click(function(event){
    $("#house-photos-"+house_photos[0].room).parent("li").addClass("up");
    $("#photo-gallery").show();
    $("#preload-house-photos").html('<img src="'+house_photos[last_house_photo].src+
                                    '" class="'+house_photos[last_house_photo].room+
                                    '" /><img src="'+house_photos[1].src+
                                    '" class="'+house_photos[1].room+'" />');
    event.preventDefault();
  });

  $("#house-photos-next").click(function(event){
    var off = Number($(this).attr("alt"));
    if (off >= house_photos.length)
      off = 0;
    var prev = off - 1;
    if (prev < 0)
      prev = last_house_photo;
    var next = off + 1;
    if (next >= house_photos.length)
      next = 0;
    
    $("#house-photos-current").attr("src", house_photos[off].src);
    $("#house-photos-current").attr("class", house_photos[off].room);
    if ($("#house-photos-"+house_photos[off].room).parent("li").hasClass("up")==false){  
      $(".house-photos-rooms").parent("li").removeClass("up");
      $("#house-photos-"+house_photos[off].room).parent("li").addClass("up");
    }
    $("#house-photos-prev").attr("alt", prev);
    $("#house-photos-next").attr("alt", next);
    $("#preload-house-photos").html('<img src="'+house_photos[next].src+
                                    '" class="'+house_photos[next].room+
                                    '" />');
    
    event.preventDefault();
  }); 

  $("#house-photos-prev").click(function(event){
    var off = Number($(this).attr("alt"));
    if (off >= house_photos.length)
      off = 0;
    var prev = off - 1;
    if (prev < 0)
      prev = last_house_photo;
    var next = off + 1;
    if (next >= house_photos.length)
      next = 0;

    $("#house-photos-current").attr("src", house_photos[off].src); 
    $("#house-photos-current").attr("class", house_photos[off].room);
    if ($("#house-photos-"+house_photos[off].room).parent("li").hasClass("up")==false){
      $(".house-photos-rooms").parent("li").removeClass("up");
      $("#house-photos-"+house_photos[off].room).parent("li").addClass("up");
    }
    $("#house-photos-prev").attr("alt", prev);
    $("#house-photos-next").attr("alt", next);
    $("#preload-house-photos").html('<img src="'+house_photos[prev].src+
                                    '" class="'+house_photos[prev].room+
                                    '" />');
    event.preventDefault();
  });

  $(".house-photos-rooms").click(function(event){
    var off = Number($(this).attr("alt"));
    if (off >= house_photos.length)
      off = 0;
    var prev = off - 1;
    if (prev < 0)
      prev = last_house_photo;
    var next = off + 1;
    if (next >= house_photos.length)
      next = 0;

    $("#house-photos-current").attr("src", house_photos[off].src);
    $("#house-photos-current").attr("class", house_photos[off].room);
    if ($("#house-photos-"+house_photos[off].room).parent("li").hasClass("up")==false){
      $(".house-photos-rooms").parent("li").removeClass("up");
      $("#house-photos-"+house_photos[off].room).parent("li").addClass("up");
    }
    $("#house-photos-prev").attr("alt", prev);
    $("#house-photos-next").attr("alt", next);
    $("#preload-house-photos").html('<img src="'+house_photos[prev].src+
                                    '" class="'+house_photos[prev].room+
                                    '" />'+
                                    '<img src="'+house_photos[next].src+
                                    '" class="'+house_photos[next].room+
                                    '" />');
    
    event.preventDefault();
  });
});

