vanilla.namespace('carl');
carl.resizing =
{
    MIN_CONTENT_HEIGHT : 450,

    getHeightWithoutContent : function()
    {
	var userAgent = window.navigator.userAgent;  
	if ( userAgent.indexOf("Safari/") >= 0 )
	{
	    return 270;
	}
	
	return 252;
    },
    
    resize : function()
    {
	var height = $("#heightRef").offset().top;
	height -= this.getHeightWithoutContent();

	if ( height > this.MIN_CONTENT_HEIGHT )
	{
	    $('#contentContainer').height(height);
	}
	else
	{
	    $('#contentContainer').height("auto");
	}
    },
    
    init : function()
    {
	$(document.createElement("DIV")).html("&nbsp;")
	    .attr("id", "heightRef")
	    .css
	    (
		{
		    position:"absolute", 
		    bottom:0
		}
	    )
	    .appendTo($("body"));

	$(window).resize
	(
	    function()
	    {
		carl.resizing.resize();	    
	    }
	)
	.resize();

	var index;
	if ( index = document.location.href.indexOf('#') > 0 )
	{
	    document.location.href = document.location.href;
	}
    }
};

carl.menu =
{
    init : function()
    {
	var fv= function()
	{
	    carl.menu.overState(this);
	};

	var fo = function()
	{
	    carl.menu.outState(this);
	};

	// on récupère l'URL de la page
	var url	= document.location.href;
	var _this = this;

	$("#topNavigation li").hover
	(
	    function()
	    {
		$(this).toggleClass("over", true);
	    },
	    function()
	    {
		$(this).toggleClass("over", false);
	    }
	)
	.filter(":has(>ul)").addClass("directory");

	// gestion des seconds niveaux
	$("#topNavigation > ul > li > a").each
	(
	    function()
	    {
		var dir = _this.getDirectory(this.href);	
		if ( url.indexOf(dir) == 0 )
		{
		    $(this).parent("li").addClass("selected");
		}
	    }
	);
    },

    generateLeftNavigation : function(url)
    {
	var selected    = null;
	var inDirectory = null;

	if ( url )
	{
	    if ( url.charAt(0) != '/' )
	    {
		url = "/" + url;
	    }

	    var host    = document.location.host;
	    var port    = document.location.port;
	    var protocol= document.location.protocol;

	    if ( protocol == 'file:' )
	    {
		var tmp = document.location.href;

		var begin = tmp.indexOf('/fr/gmao');
		if ( begin < 0 )
		{
		    begin = tmp.indexOf('/en/cmms');
		}

		if ( begin > 0 )
		{
		    url = tmp.substring(0, begin) + url;
		}
		else
		{
		    url = document.location.href;
		}
	    }
	    else if ( !port || port == 80 )
	    {
		url = protocol + "//" + host + url;
	    }
	    else
	    {
		url = protocol + "//" + host + ":" + port + url;
	    }
	}
	else
	{
	    // on récupère l'URL de la page
	    url = document.location.href;
	}

	var dir	    = this.getDirectory(url);
	var sharp   = url.indexOf('#');
	if ( sharp > 0 )
	{
	    url = url.substring(0, sharp);
	}

	var inter = url.indexOf('?');
	if ( inter > 0 )
	{
	    url = url.substring(0, inter);
	}

	var _this = this;
	$("#topNavigation li").not(":has(>ul)").not(":has(.redi)").each
	(
	    function()
	    {
		var a = $(this).find(">a").get(0);
		if ( !a )
		{
		    return;
		}

		if ( a.href == url )
		{
		    selected = this;
		}

		if ( _this.getDirectory(a.href) == dir )
		{
		    inDirectory = this;
		}
	    }
	);

	if ( !selected && !inDirectory )
	{
	    var html = $("#ambianceText h4:first").html()
	    if ( html )
	    {
		this.drawLeftTitle(url, html, 2);
	    }

	    document.write('<ul></ul>');
	    return;
	}

	var ref = (selected ? selected : inDirectory);

	var revertPath = new Array();
	var parent = ref.parentNode.parentNode;
	while ( parent.id != 'topNavigation' )
	{
	    revertPath.push(parent);
	    parent = parent.parentNode.parentNode;
	}

	if ( !revertPath.length )
	{
	    this.drawLeftTitleFromLi(ref, 2);
	    document.write('<ul></ul>');
	    return;
	}

	// on dessine le path
	var i, j;
	for ( i = revertPath.length - 1, j = 0 ; i >= 0 ; i--, j++ )
	{
	    this.drawLeftTitleFromLi(revertPath[i], j+2);
	}
	
	// on dessine le même niveau
	document.write('<ul>');
	var li = ref.parentNode.firstChild;
	do
	{
	    if ( li.tagName != 'LI' )
	    {
		continue;
	    }

	    var link = $(li).find(">a").get(0);
	    if ( !link )
	    {
		continue;
	    }

	    var last = $(li).find("+li").eq(0).is(".separator");

	    var className = "";
	    if ( li == selected )
	    {
		className += " selected";
	    }
	    if ( last )
	    {
		className += " last";
	    }

	    document.write('<li class="' + className + '">');
	    document.write('<a href="' + link.href + '">' + link.innerHTML + '</a>');
	    document.write('</li>');
	}
	while ( li = li.nextSibling );

	document.write('</ul>');
    },

    drawLeftTitleFromLi : function(li, level)
    {
	var link = $(li).find(">a").get(0);
	if ( link )
	{
	    this.drawLeftTitle(link.href, link.innerHTML, level);
	}
    },

    drawLeftTitle : function(href, text, level)
    {
	document.write('<h' + (level) + '>');
	document.write('<a href="' + href + '">' + text + '</a>');
	document.write('</h' + (level) + '>');
    },

    nextHomonym : function(node)
    {
	var n = node;
	while ( n = n.nextSibling )
	{
	    if ( n.tagName == node.tagName )
	    {
		return n;
	    }
	}


	return null;
    },

    getDirectory : function(url)
    {
	var path	    = url.split('/');
	var file	    = path[path.length - 1];
	var directory   = url.substring(0, url.length - file.length);

	return directory;
    }
};

carl.image =
{
    lightbox : function()
    {
	var path = "";
	var src = $("head script[src*='carlscript']").eq(0).attr("src");
	if ( src )
	{
	    path = src.substr(0, src.indexOf("script/carlscript"));
	}

	$("a[rel='lightbox']").lightBox
	(
	    {
		imageLoading	: path + 'image_common08/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev	: path + 'image_common08/lightbox/lightbox-btn-prev.gif',
		imageBtnNext	: path + 'image_common08/lightbox/lightbox-btn-next.gif',
		imageBtnClose	: path + 'image_common08/lightbox/lightbox-btn-close.gif',
		imageBlank	: path + 'image_common08/lightbox/lightbox-blank.gif'
	    }
	);
    },

    preload : function(src)
    {
	var img = new Image();
	img.src = src;
    },

    initSwaping : function(image, src, link, title)
    {
	image = $("#" + image);

	var old = image.get(0).src;
	this.preload(src);

	$(image).mouseover
	(
	    function()
	    {
		this.src = src;
	    }
	)
	.mouseout
	(
	    function()
	    {
		this.src = old;
	    }
	)
	.click
	(
	    function()
	    {
		document.location.href = link; 
	    }
	)
	.css("style", "pointer").attr("title", title);
    }
};

carl.language =
{
    init : function()
    {
	var popup = $(document.createElement("UL")).addClass("popup");
	$("ul#languages li:not(.home)").each
	(
	    function()
	    {
		var li = $(this).clone();
		li.find("span").html(li.find("a").attr("title"));
		li.hover
		(
		    function()
		    {
			$(this).toggleClass("over", true);	
		    },
		    function()
		    {
			$(this).toggleClass("over", false);	
		    }
		);

		if ( $(this).is(".selected") )
		{
		    li.removeClass("selected").prependTo(popup);
		}
		else
		{
		    li.appendTo(popup);
		    $(this).remove();
		}
	    }
	);

	popup.appendTo($("ul#languages > li.selected"));

	var selected	= $("ul#languages > li.selected");
	var popupHeight = -1;
	var oneHeight	= -1;

	selected.hover
	(
	    function()
	    {
		if ( popupHeight < 0 )
		{
		    popupHeight = popup.height();
		    oneHeight	= popupHeight / popup.find("li").length;
		}

		popup.stop(true, true).css("overflow", "hidden")
		    .height(oneHeight).fadeIn("fast")
		    .animate({height:popupHeight}, "fast");
	    },

	    function()
	    {
		popup.stop(true, true)
		    .css("overflow", "hidden")
		    .fadeOut("fast");
	    }
	);

	selected.find("> a span").html("Languages");

	$("ul#languages").show();
    }
};

$(document).ready
(
    function()
    {
	carl.resizing.init();
	carl.menu.init();
	carl.image.lightbox();
	carl.language.init();
    }
);

isFlashInstalled = function()
{
    return FlashDetect.installed;
};
