// JavaScript Document

/*http://blog.firetree.net/2005/07/04/javascript-find-position/*/
/**
 * This function will find the location of x coordinate of
 * the object relative to the page.
 *
 * @param  - Object whose x coordinate need to find
 * @return - The x coordinate of the object
 */
 
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
  
/*http://blog.firetree.net/2005/07/04/javascript-find-position/*/
/**
 * This function will find the location of y coordinate of
 * the object relative to the page.
 *
 * @param  - Object whose y coordinate need to find
 * @return - The y coordinate of the object
 */
 
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}


function matchMapBackground(object)
{
	document.getElementById("mapHolder").className = object.parentNode.className ;
	//object.parentNode.style.margin = "10px 0 10px 0" ;
}

/**
 * Detect what kind of browser the user is using.
 * @return - Returns the name of the browser.
 */
function browserDetect()
{
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;
	//alert(detect) ;
	
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari" ;
	else if (checkIt('omniweb')) browser = "OmniWeb" ;
	else if (checkIt('opera')) browser = "Opera" ;
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab" ;
	else if (checkIt('msie'))
	{
		browser = "Internet Explorer" ;
		return browser ;
	}
	else if(checkIt('firefox'))
	{
		browser = "Firefox" ;
		return browser ;
	}
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator" ;
		version = detect.charAt(8);
	}
	else
	{
		browser = "An unknown browser";
		return browser ;
	}	
	
}

/**
 * A helper function for the browserDetect().
 */
function checkIt(string)
{
	var detect = navigator.userAgent.toLowerCase();
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}


//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function resizeMap()
{
	var mapHolder = document.getElementById('mapHolder') ;
	var sidebar = document.getElementById('sidebar') ;
	
	var myWidth = 0, myHeight = 0, sidebarWidth = 0 ;

	sidebarWidth = sidebar.style.width ;
	/**
	 * Since the widthValue is returned as a string
	 * i.e: "300px", we need to parse it and return
	 * the number only. So, using the lastIndexOf(..),
	 * find where the string "px" starts. Then using
	 * the substring(a, b), extract the number from
	 * the widthValue.
	 */
	sidebarWidth = sidebarWidth.substring(0, sidebarWidth.lastIndexOf("px")) ;
	//convert it into number by multiplying with 1
	sidebarWidth = (sidebarWidth * 1) + 60 ;

	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && ( document.body.clientWidth || document.body.clientHeight ))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//window.alert( 'Width = ' + myWidth + ' Height = ' + myHeight );
	mapHolder.style.width = myWidth - sidebarWidth + "px" ;
	mapHolder.style.height = myHeight - 125 + "px" ;
	//sidebar.style.height = myHeight - 150 + "px" ;
}

function displayLoadingInfo(displayState)
{
	var loadingObj = document.getElementById("loadingInfo") ;
	if(displayState == "on")
	{
		loadingObj.style.display = "block" ;
	}
	else if(displayState == "off")
	{
		loadingObj.style.display = "none" ;
	}
}

function fixSidebarDisplayBug()
{
	document.getElementById('searchContent').style.display = "none" ;
	document.getElementById('infoLocContent').style.display = "none" ;
	document.getElementById('relevantInfoContent').style.display = "none" ;
	document.getElementById('classroomContent').style.display = "block" ;
}