// 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;
}

/**
 * This function will connect to 'saveFile.php' via ajax which will
 * write the data to a file.
 */
function saveData()
{
	var request = makeRequest() ;
	var prepData = prepareData() ;
	//alert(prepData) ;
	var url = 'saveFile.php' ;
	//alert(url) ;
	postData(request, prepData, url) ;
	positionOutput() ;
	slidingDiv('open') ;

}

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

function changeState(object)
{
	var targetObj ;
	var targetParent ;
	
	//check the browser
	if(browserDetect() == "Internet Explorer")
		targetObj = object.nextSibling ;
	else
		targetObj = object.nextSibling.nextSibling ;
	
	targetParent = object.parentNode ;
	//alert(targetParent.id) ;
	
	var currentState = targetObj.style.display ;
	//var objectBg = object.style.background ;
	if(currentState == "")
		fixSidebarDisplayBug() ;

	currentState = targetObj.style.display ;

	var targetIconHolder = object.childNodes[0].childNodes[0] ;
	
	if(currentState == "block")
	{	//alert("block " + targetObj.id) ;
		targetObj.style.display = "none" ;
		//targetParent.style.marginRight = 10 + "px" ;
		targetIconHolder.src = "../_images/triangle.gif" ;

	}
	else if(currentState == "none")
	{	//alert("none " + targetObj.id) ;
		targetObj.style.display = "block" ;
		//targetParent.style.marginRight = 0 ;
		targetIconHolder.src = "../_images/opentriangle.gif" ;
		
		//if the xml file is not loaded yet, load it
		if((targetParent.id == "infoLoc" || targetParent.id == "search") && !RYE_XML_DOC_OTHER_CENTER)
		{
			loadMapXml("informationBuildings.xml", "infoLocationList", "infoLoc") ;
		}
	}
}

function slidingDiv(object, state)
{
	alert(typeof object) ;
	alert(object.nextSibling.nextSibling.id) ;
    //speed for each frame
    var speed = Math.round(500 / 100);
    var timer = 0;
	
	var divMinHeight = 1 ;
	var divMaxHeight = 205 ;

	var targetObj = object.nextSibling.nextSibling ;
	
	if(state == "open")
	{
		for(i = (divMinHeight + 1); i <= divMaxHeight ; i++)
		{
			setTimeout("changeDimension(" + i + ",'" + targetObj + "', 'open')",(timer * speed));
			timer++;
		}
	}
	else if(state == "close")
	{
		for(i = (divMaxHeight - 1); i >= divMinHeight ; i--)
		{
			setTimeout("changeDimension(" + i + ",'" + targetObj + "', 'close')",(timer * speed));
			timer++;
		}		
	}
	//document.getElementById('windowContent').style.visibility = "visible" ;
}

function changeDimension(height, object, status)
{
    //var object = document.getElementById(id).style;
	var value = height + "px" ;
    object.style.height = value ;
	
	//if(height == 350)
		//document.getElementById('windowContent').style.visibility = "visible" ;
	 //if(height == 25)
		//document.getElementById('saveStatus').style.visibility = "hidden" ;
}

/**
 * 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;
}


function resizeMap()
{
	var mapHolder = document.getElementById('mapHolder') ;
	
	var availW ;
	var availH ;
	
	if(document.all){
      availW = document.body.clientWidth;
      availH = document.body.clientHeight;
   }else{
      availW = innerWidth;
      availH = innerHeight;
   }
   /*alert(availW) ;
   alert("changing") ;*/
   mapHolder.style.width = availW - 255 + "px" ;
   //mapHolder.style.height = availH - 115 + "px" ;
   /*alert("done") ;*/

}

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()
{
	if(document.getElementById('searchContent'))
		document.getElementById('searchContent').style.display = "none" ;
	if(document.getElementById('infoLocContent'))
		document.getElementById('infoLocContent').style.display = "none" ;
	if(document.getElementById('relevantInfoContent'))
		document.getElementById('relevantInfoContent').style.display = "none" ;
	if(document.getElementById('classroomContent'))
		document.getElementById('classroomContent').style.display = "block" ;
}