$(document).ready(function() {	
	$(".more-detail").live('click', function() {
	    if($("#stateSelection").is(":visible")) {
	        $(stateClicked).parent().css("border-color", "transparent");
	        $("#stateSelection").hide();
	
	        stateClicked = null;
	    }
	
	    //FIXME : Sous IE7 cela ne fonctionne pas...utiliser show() à un moment...
	    $("."+$(this).attr("id")).toggle();
	    
	    return false;
	});
	
	$(".c").live('click', function(){
	    if (typeof(stateClicked) != "undefined") {
	        $("#stateSelection").hide();
	        $(stateClicked).parent().css("border-color", "transparent");
	
	        if (stateClicked == this) {
	            stateClicked = null;
	            return false;
	        }
	    }
	
	    stateClicked = this;
	
	    $(this).parent().css("border-color", "red");
	
	    var link = $(this);
	    $("#stateSelection > li > a").each(function(){
	        $(this).attr("href", link.attr("href") + "/state/" + $(this).attr("id"));
	        $(this).css("display", link.find("img").attr("src") == $(this).find("img").attr("src") ? "none" : "block");
	    });
	
	    var position = $(this).parent().position();
	    $("#stateSelection")
	        .css("left", position.left + ($(this).parent().width() - $("#stateSelection").width())/2 )
	        .css("top", position.top - $("#stateSelection").height() - 1)
	        .show("slide", {direction : "down"});
	
	    return false;
	});
	
	$(".changeState").live('click', function(){
	    if(typeof(stateClicked) == "undefined") return false;
	
	    $(stateClicked).html('<img src="/skins/default/img/loading.gif" class="icon" />');
	    $(stateClicked).parent().css("border-color", "transparent");
	    $("#stateSelection").hide();
	
	    $.getJSON($(this).attr("href"), {format: "json"} , function(json){
	        $("#stateSelection > li > a").each(function(){
	            if ($(this).attr("id") == json.state) {
	                $(stateClicked).find("img").attr("src", $(this).find("img").attr("src"));
	
	                stateClicked = null;
	
	                return false;
	            }
	        });
	    });
	
	    return false;
	});
});