/**
 * Fernand Braudel Institute website scripts.
 * By Gustavo Rezende Montesino.
 * Copyright (C)2006 Instituto Fernand Braudel de Economia Mundial
 **/

/**
 * initPage(): Initialization routine. Check for the frameset, activates the
 * menu and center the element whose id is "vp" (viewport).
 *
 * category: The id of the menu item to whose the document being initialized
 * attachs to.
 **/
function initPage(category)
{
	if (checkFrame() && (document.getElementById))
	{
		top.menu.setCurrentMenu(category);
	}
}

/**
 * getHeight(): Returns the height available on the callers frame
 *
 **/
function getHeight()
{
	if (self.innerHeight)
	{
		return(self.innerHeight);
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		return(document.documentElement.clientHeight);
	}
	else if (document.body)
	{
		return(document.body.clientHeight);
	}
}

/**
 * setHeight: Sets the height of an element
 *
 * element: Id of the element to resize
 * height: New height of elem
 **/
function setHeight(elementId, height)
{
	document.getElementById(elementId).style.height = height + 'px';
}	

/**
 * getWidth(): Returns the width available on the callers frame
 *
 **/
function getWidth()
{
	if (self.innerWidth)
	{
		return(self.innerWidth);
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		return(document.documentElement.clientWidth);
	}
	else if (document.body)
	{
		return(document.body.clientWidth);
	}
}

/**
 * setWidth: Sets the width of an element
 *
 * element: Id of the element to resize
 * width: New width of elem
 **/
function setWidth(elementId, width)
{
	document.getElementById(elementId).style.width = width + 'px';
}	

/**
 * center(): Centers a block (html) element through javascript/css/DOM
 * manipulation.
 *
 * elem: element to center.
 **/
function center(elem)
{
        hasDOM = document.getElementById && document.createElement;
        if (!(hasDOM))
                return;

        if (!(elem.parentNode))
                return;

/*
        if (!(elem.offsetWidth))
                return;
*/

        parentWidth = elem.parentNode.offsetWidth;
        Width = elem.clientWidth;

        /* Workarounds to firefox < 2.0 */
        if (parentWidth == 0)
                parentWidth = document.documentElement.offsetWidth;
        if (Width == 0)
                Width = elem.ofssetWidth;

        left = ((parentWidth - Width) - (parentWidth - Width) % 2) / 2;

        elem.style.left = left + 'px';
}

/**
 * alignWithMenu(): Aligns 'vp' with the left of a element with top.menu.topvp.left
 **/
function alignWithMenu()
{
/*
	while(!top.menu.loaded)
	{
		continue;
	}
*/
	left = top.menu.document.getElementById('topvp').style.left;
	document.getElementById('vp').style.left = left;
}

/**
 * checkFrame(): Insures that a page is opened inside the frameset
 *
 * Returns: False if we did redirect, true otherwise
 **/
function checkFrame()
{
        if (top.location.href == location.href)
	{
               	location.href = 'http://www.braudel.org.br/site.php?old=' + location.href;
		return false;
	}
	else
		return true;
}

/**
 * setCurrentMenu(): Set the class of the old current menu to 'active' and of
 * the truly current menu to 'current'. Please note that it must be called
 * on the menu itself (ie, top.menu.setCurrentMenu())
 *
 * CurrentId: id of the element which represents the current menu
 *
 * Returns: True on sucess, false otherwise
 **/

function setCurrentMenu(CurrentId)
{
        if (document.getElementById)
        {
                if (typeof(CurrentMenu) != "undefined")
		{
                        CurrentMenu.className = 'active';
                }
		
		if (CurrentId != '')
		{
			CurrentMenu = document.getElementById(CurrentId);
                	CurrentMenu.className = 'current';
		}

                return(true);
        }
        else
        {
                return(false);
        }
}

/**
 * setLanguage(): Set the language on which the site is to be shown
 * 
 * newlang: Language to show. currently pt or en
 */
 
function setLanguage(newlang)
{
	if (newlang == 'pt')
	{
		top.menu.location.href = 'top.php';
		top.main.location.href = 'main.php';
	}
	else
	{
		top.menu.location.href = newlang + '/top.php';
		top.main.location.href = newlang + '/main.php';
	}
}

/**
 * popup(): Shows a popup window
 *
 * file: file to open in the popup
 * width: width of the popup
 * height: height of the popup
 **/
function popup(file, width, height)
{
	options = 'width=' + width + ',height=' + height + ', statusbar=no';
	window.open(file, '', options);
}

