/* Basic Functions */
/*******************/

// Checking if element/event/object is defined
function isSet (_element)
{
	if (typeof(_element) == "undefined" || _element === null)
		return false;
	
	return true;
}

function appendAsFirst (_element, _parent)
{
	if(_parent.firstChild)
		_parent.insertBefore(_element,_parent.firstChild);
	else
		_parent.appendChild(_element);
}

function centerScreen (_element)
{
	var centerX, centerY;
	if( self.innerHeight )
	{
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientHeight )
	{
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	}
	else if( document.body )
	{
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}

	_element.style.left = ((centerX / 2) - (_element.offsetWidth / 2)) + "px";
	_element.style.top 	= ((centerY / 2) - (_element.offsetHeight / 2)) + "px";
}

// Returnning the element that called this event
function getElement (_event)
{
	if (_event == null) _event = window.event; // necessary for many browsers (for using `window.event`)	
	
	// IE
	if (isSet (_event.srcElement))
		return _event.srcElement;
	
	// FF, Chrome
	return _event.target;
}

// Getting the next brother
function getNextSibling(_element)
{
	// Checking that _element is not null
	if (_element === null)
		return null;
	
	// Getting next brother
	var next	=	_element.nextSibling;
	
	// Checking that is not null
	if (next === null)
		return null;
	
	// Checking brother's legitimacy
	if (next.nodeType === 1)
		return next;
	
	// Keep getting the next brother
	return getNextSibling(next);
}

// Getting the previous brother
function getProvSibling(_element)
{
	// Checking that _element is not null
	if (_element === null)
		return null;
	
	// Getting next brother
	var prev	=	_element.previousSibling;
	
	// Checking that is not null
	if (prev === null)
		return null;
	
	// Checking brother's legitimacy
	if (prev.nodeType === 1)
		return prev;
	
	// Keep getting the next brother
	return getProvSibling(prev);
}

// Changing opacity for elements (Opacity from 0 to 100)
function set_opacity(_element, _opacity)
{
	if (isSet(_element.style.opacity))
		_element.style.opacity = (_opacity / 100);
	
	_element.style.filter='alpha(opacity = '+_opacity+')';
	
	element = _element;
	opacity = _opacity + 10;
	if (_opacity < 100)
		setTimeout ("set_opacity(element, opacity);", 100);
}

// set button as active button
function activeMenuButton (id)
{
	document.getElementById(id).className="activelink";
}

function sizeFormat(size) 
{
		size = parseFloat(size);
		
		if (isNaN (size))
			return "-";
		
    if(size < 1024) 
			return size + " bytes"; 
    else if(size < (1024*1024)) 
    { 
        size = Math.round(size / 1024); 
        return size + " KB"; 
    } 
    else if(size<(1024*1024*1024)) 
    { 
        size = Math.round(size/(1024*1024)); 
        return size + " MB"; 
    } 
    else
    {
        size = Math.round(size/(1024*1024*1024)); 
        return size + " GB"; 
    }
}

function string_max_length (str, max)
{
	if (str.length > max)
		return str.substr (0, max-4) + "...";
	
	return str;
}

function print_r(theObj)
{
	var str = "";
	for (var i in theObj)
		str += i + " => " + theObj[i] + "\r\n";
	alert (str);
}
	

/***************/
/* Basic fixes */
/***************/
// Fixes XMLHttpRequest to work with most browsers.
if (! isSet (XMLHttpRequest))
{
  XMLHttpRequest = function()
  {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }     catch(e) {};
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }  catch(e) {};
    throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.");
  };
}
