vanilla.namespace('carl');
carl.resizing =
{
	MIN_HEIGHT : 100,

	getHeightWithoutContent : function()
	{
	    var userAgent = window.navigator.userAgent;  
	    if ( userAgent.indexOf("Safari/") >= 0 )
	    {
		return 270;
	    }
	    
	    return 252;
	},
	
	resize : function()
	{
		var height = EL("heightRef").offsetTop;
		height -= this.getHeightWithoutContent();
		height = Math.max(this.MIN_HEIGHT, height);
			
		EL('contentContainer').style.height = height + 'px';
	},
	
	init : function()
	{
		var ref = document.createElement('DIV');
		ref.innerHTML="&nbsp";
		ref.id="heightRef";
		with(ref.style)
		{
			position = "absolute";
			bottom = "0";
		}

		document.body.appendChild(ref);

		var f = function()
		{
			carl.resizing.resize();
		};
		
		window.onresize = f;
		this.resize();

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

carl.menu =
{
	init : function()
	{
		var top 	= EL('topNavigation');
		var items 	= top.getElementsByTagName('li');
		
		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;

		for ( var i = 0 ; i < items.length ; i++ )
		{
			var li = items[i];
			
			li.onmouseover = fv;
			li.onmouseout = fo;

			if ( this.isSecondLevel(li) )
			{
				/*
				    Deuxième niveau
				*/

				var link = this.getLink(li);

				if ( link != null )
				{
					var directory = this.getDirectory(link.href);
					if ( url.indexOf(directory) == 0 )
					{
					    vanilla.html.DOM.addClassName(li, 'selected');
					}
				}
			}

			if ( this.hasChilds(li) )
			{
			    vanilla.html.DOM.addClassName(li, 'directory');
			}
		}
	},

	isSecondLevel : function(li)
	{
		return li.parentNode.parentNode == EL('topNavigation');
	},

	hasChilds : function(li)
	{
	    return (li.getElementsByTagName('li').length > 0);
	},
	
	overState : function(li)
	{
		if ( !vanilla.html.DOM.hasClassName(li, 'over') )
		{
			vanilla.html.DOM.addClassName(li, 'over');
		}
	},

	outState : function(li)
	{
		vanilla.html.DOM.removeClassName(li, 'over');
	},

	generateLeftNavigation : function(url)
	{
	    var top	    = EL('topNavigation');
	    var items	    = top.getElementsByTagName('li');
	    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);
	    }

	    for ( var i = 0 ; i < items.length ; i++ )
	    {
		var li	    = items[i];
		var link    = this.getLink(li);

		if ( link == null )
		{
		    continue;
		}

		if ( !vanilla.html.DOM.hasClassName(li, "redi") && link.href == url && !this.hasChilds(li) )
		{
		    selected = li;
		    break;
		}

		if ( this.getDirectory(link.href) == dir )
		{
		    inDirectory = li;
		}
	    }

	    if ( !selected && !inDirectory )
	    {
		// on récupère le titre
		var ambianceText = EL('ambianceText');	
		if ( !ambianceText )
		{
		    return;
		}

		var titles = ambianceText.getElementsByTagName('h4');
		if ( titles && titles.length )
		{
		    var t = titles[0];
		    this.drawLeftTitle(url, t.innerHTML, 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 = this.getLink(li);
		if ( !link )
		{
		    continue;
		}

		var next = this.nextHomonym(li);
		var last = next && vanilla.html.DOM.hasClassName(next, '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    = this.getLink(li);
	    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) + '>');
	},

	getLink : function(li)
	{
	    var link = li.firstChild;
	    while ( link != null && link.tagName != "A" )
	    {
		link = link.nextSibling;
	    }

	    return link;
	},

	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 =
{
    preload : function(src)
    {
	var img = new Image();
	img.src = src;
    },

    initSwaping : function(image, src, link, title)
    {
	image = EL(image);
	this.preload(src);
	var old = image.src;

	image.onmouseover = function()
	{
	    this.src = src;
	};

	image.onmouseout = function()
	{
	    this.src = old;
	};

	image.onclick = function()
	{
	    document.location.href = link; 
	}

	image.style.cursor = "pointer";
	image.title = title;
    }
}

window.onload = function()
{
	carl.resizing.init();
	carl.menu.init();
};

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