function refreshResource(){
    formattedLink = $("#rightcolumn").attr("formattedLink");
 
	if ((formattedLink == null)||(formattedLink == "")) {
		ref = $("#rightcolumn").attr("tocRef");
    	ouvrage = $("#rightcolumn").attr("ouvrage");	

		$.get("/resource/getFormattedLink.jsp",
		 {'ref': ref,'ouvCode' : ouvrage},
		 function(data){
		 	window.location.href=window.location.href.replace(/.*$/,data);
		 });
    } else {
    	window.location.href=window.location.href.replace(/.*$/,formattedLink);
	}
} 

function openResource(tocId,ref,contextPath,highlight,formattedLink){
	var ouvrage = $("#rightcolumn").attr("ouvrage");
	if ($("#rightcolumn").size() == 0) {
    	url = "/ressources/" + ouvrage + "/" + ref;
    	window.location.href=url;
    }
        
	if ($("#rightcolumn").attr("divMode") == "true")
    	openResourceDivMode(tocId,ref,contextPath,highlight,formattedLink);
	else
		openResourceIframeMode(tocId,ref);
		
	$("#rightcolumn").attr("tocId",tocId);
	$("#rightcolumn").attr("tocRef",ref);
	$("#rightcolumn").attr("formattedLink",formattedLink);
	
    $("span[@class*=selectedTocNode]").removeClass("selectedTocNode");
    $("#TOCLINK-" + tocId).addClass("selectedTocNode");
	log(ref,ouvrage,'HTML', "");

}
function openResourceIframeMode(tocId,ref){
    var ouvrage = $("#rightcolumn").attr("ouvrage");
    $("#rightcolumn").attr('src','/ResourceFS/' + ouvrage + 'fs/' + ref );
    nodeClick(tocId,true);
}

function openResourceDivMode(tocId,ref,contextPath,highlight,formattedLink){
    var ouvrage = $("#rightcolumn").attr("ouvrage");
    var imagePath = $("#rightcolumn").attr("imagePath");
    var ouvrageUrlPart = "ouv." + ouvrage + "/";
	
	var anchor = "";
	if (ref.match(/#/))
    	anchor = ref.split('#')[1];

    htmlLink = ref.split('#')[0];
		
    if($("#rightcolumn").attr("htmlLink") != htmlLink) {
    	 toggleLoader("#TOCIMG-" + tocId,imagePath + "/loading.gif");
         $.ajax({
                type: "GET",
                url: contextPath + "/ResourceFS/" + ouvrage + "fs/" + ouvrageUrlPart + htmlLink + "?hlt=" + highlight, 
                dataType: "html",
                success: function(msg){
                    $("#rightcolumn").children().remove();
                    $("#rightcolumn").append(msg);
                    nodeClick(tocId,true);
                    if (anchor != "") {
                    	  $("#rightcolumn").find("img").oneload(
	                          function() {
	                          	$("a[@name*=" + anchor + "]").IdmScrollTo(1,-45);
	                          }
	                      );
                    } else {
                    	scroll(0,0);
                    	$("#rightcolumn").find("a[@href]").click(function() { return jScroll(this); });
                    }
                    
                 }
            });
     } else {
     	nodeClick(tocId,true);
        if (anchor != "")
     		$("a[@name*=" + anchor + "]").IdmScrollTo(500,-45); 
     }
	$("#rightcolumn").attr("htmlLink",htmlLink);
	
}
        
function toggleNode(tocId){
	imagePath = $("#rightcolumn").attr("imagePath");
    if($("#TOCIMG-" + tocId).size() == 1) {
	    if($("#TOC-" + tocId).attr("open") == "false") {
	        $("#TOCIMG-" + tocId).attr("src",imagePath + "/moins.gif");
	        $("#TOC-" + tocId).attr("open","true");
	    } else {
	        $("#TOCIMG-" + tocId).attr("src",imagePath + "/plus.gif");
	        $("#TOC-" + tocId).attr("open","false");
	    }
	    $("#TOC-" + tocId).toggle();
    }
}

function showNode(tocId){
	imagePath = $("#rightcolumn").attr("imagePath");
    $("#TOCIMG-" + tocId).attr("src",imagePath + "/moins.gif");
	$("#TOC-" + tocId).attr("open","true");
    $("#TOC-" + tocId).show();
}
    
function nodeClick(tocId, showOnly){ 
	imagePath = $("#rightcolumn").attr("imagePath");
    ouvrage = $("#rightcolumn").attr("ouvrage");
    
    if($("#TOC-" + tocId).text() == '') {
        $("#TOCIMG-" + tocId).attr("src",imagePath + "/loading.gif");
        $.ajax({
            type: "POST",
            url:"/resource/getTocChildren.jsp",
            data: "tocId=" + tocId + "&ouvCode=" + ouvrage + "&imgPath=" + imagePath,
            dataType: "html",
            success: function(msg){
                $("#TOC-" + tocId).append(msg);
                if (showOnly == true)
                	showNode(tocId);
               	else
               		toggleNode(tocId);
    			$("#TOC-" + tocId).find("a").Tooltip({delay: 0,showURL: false});
             }
        });
    } else {
    	if (showOnly == true)
        	showNode(tocId);
        else
        	toggleNode(tocId);
    }
}
    
function initTree(ouvrage,tocId,parentIds,contextPath,imagePath){
    $("#rightcolumn").attr("ouvrage",ouvrage);
    $("#rightcolumn").attr("imagePath",imagePath);
    var parentId = parentIds.pop();
    if (parentId != null) {
    	$("#TOCIMG-" + tocId).attr("src",imagePath + "/loading.gif");
        $.ajax({
            type: "POST",
            url: contextPath + "/resource/getTocChildren.jsp",
            data: "tocId=" + parentId + "&ouvCode=" + ouvrage +"&imgPath=" + imagePath,
            dataType: "html",
            success: function(msg){
                $("#TOC-" + parentId).append(msg);
                $("#TOC-" + parentId).find("a").Tooltip({delay: 0,showURL: false});
                initTree(ouvrage,tocId,parentIds,contextPath,imagePath);
                toggleNode(parentId,imagePath);
                if (parentIds.length == 0) {
	                $("#TOCLINK-" + tocId).addClass("selectedTocNode");
	            }
             }
        });
    }
}
    
   