$(document).ready(function() {
		    $("#files_hider").
		    click(function() {
			    $("#files").addClass("hidden");
			  });

		    $(".img_sel").
		    click(function() {
			    resetfiles();

			    $("#files").
 			      removeClass("hidden").
 			      data("kind", $(this).attr("id").slice(0, -2));

			    $("#files li").
			      click(function() {
				    resetfiles();
				    $(this).removeClass("highlighted");
				    $(this).addClass("selected");
				    populate();
				  }).
			      hover(function () {
				    if($(this).hasClass("selected") != true) {
				      $(this).addClass("highlighted");
				    }
				  },  function () {
				    if($(this).hasClass("selected") != true) {
				      $(this).removeClass("highlighted");
				    }
				  });
			  });
		  });

var resetfiles = function() {
  $("#files li").
  removeClass("highlighted").
  removeClass("selected")
}

var populate = function() {
  $("#files li.selected:first").
  each(function() {
	 kind =  $("#files").data("kind");
	 selfile = $(this).text();
	 sel = "#" + kind + "_i";
	 base = selfile.slice(0, -4);

	 $(sel).val( selfile );

	 if(kind != "ovf" && $("#ovf_i").text() == "" && existsp(base + ".ovf")) {
	   $("#ovf_i").val( base + ".ovf" );
	 }

	 if(kind != "zip" && $("#zip_i").text() == "" && existsp(base + ".zip")) {
	   $("#zip_i").val( base + ".zip" );
	 }

	 if(kind != "deb" && $("#deb_i").text() == "" && existsp(base + ".deb")) {
	   $("#deb_i").val( base + ".deb" );
	 }
       });
}

var existsp = function(n) {
  return $("#files li").filter(function() { return $(this).text() == n; }).size() > 0;
}

var releases = function() {
  $(".sidebar_label a").
    each(function() {
         $(this).click(function() {
	   idn = $(this).attr('id');
	   rx = /sidebar_link_(\d+)/;
	   ver = rx.exec(idn)[1];
           window.location = "/a/release/" + ver;
         });
       });
}

var admin_releases = function() {
  $(".sidebar_label a").
  each(function() {
	 node = $(this).get(0);
	 idn = $(this).attr('id');
	 rx = /sidebar_link_(\d+)/;
	 ver = rx.exec(idn)[1];
	 $.getJSON("/a/admin/releases/" + ver + "/json",
		   function(release) {
	             jQuery("#sidebar_link_" + release['id']).
		     click(function() {
			     $("#name_i").val(release['name']);
			     $("#id_i").val(release['id']);
			     $("#ovf_i").val(release['ovf']);
			     $("#zip_i").val(release['zip']);
			     $("#deb_i").val(release['deb']);

			     if(release['published']) {
			       $("#published_i").attr('checked', true);
			     } else {
			       $("#published_i").attr('checked', false);

			     }

			     $("#birthday_i").val(release['birthday']);

			     relnotes = release['releasenotes'];
			     $("#releasenotes_i").text(relnotes);

			     $("#edit_release_form").attr("action", "/a/admin/releases/edit");
			     return(false);
			   });
		   });
       }
      );
}

$(document).ready(function() {
  $("a#check_all").click(function() {
   $("input.input_checkbox", $(this).closest("form")).each(function() {
     $(this).attr('checked', true);
   });
    return false;
  });

  $("a#uncheck_all").click(function() {
   $("input.input_checkbox", $(this).closest("form")).each(function() {
     $(this).attr('checked', false);
   });
    return false;
  });

});