(function($) {

	prepareOnLoad();

	var b = true;

	$("div.cal_gp").unbind("all").bind("click", function() {

		var id = $(this).attr("rel");
		var self = this;
    var sport_facility_id;

		if (b == false) {
			b = true;
		} else {

			var act = $(".cal_gp").map(function() {
				return $(this).hasClass(".cal_active") ? this : null;
			});

			$("img", act).each(function() {
				this.src = this.src.replace(/ended|tobeplayed/gi, "").replace("small/", "small/tobeplayed");
			});

			sport_facility_id = (function() {
				return $("img:first", self)[0].src.split("/").reverse()[0].split(".")[0];
			})();


			$(".cal_gp").removeClass("cal_active");

			$(this).find("img").each(function() {
				this.src = this.src.replace(/ended|tobeplayed/gi, "").replace("small/", "small/ended");
			});

			$(this).removeClass("cal_tobeplayed").addClass("cal_active");

		}

		var url_sport_facility = "include/shared/2010/circuito/anagrafica/" + sport_facility_id + ".shtml";

		$.ajax({
			url: url_sport_facility,
			success: function(txt) {
			  
				$(".cal_info_container").html((function() {
				  
				  return $(txt)
				  .find("img")
				  .each(function() {
				    this.src=this.src.replace("(none)","");
				  })
				  .end();
				  
				})());
			}
		});

		$.ajax({
			url: "include/shared/2010/calendario/gp_prove/" + id + ".shtml",
			success: function(txt) {
				$(".cal_image_container").html($(txt).html());
			}
		});
	});

	$(".cal_active").trigger("click");

})(jQuery);
