/* Globals*/
//HOME_URL="http://www.fulbright.org.il";
HOME_URL="http://fulbright2.davidrennert.com";


function init()
{



	preloadImages();
	
    //activateMenu('vertnav');
	
	/* Wipe off the first level1 and level2 li elements top border */
	wipeFirstLevelRedundentTopBorder();
	
	/* After page load we set the class for each second leverl LI element.
	The LI elements with child LI elements are different: they have an arrow 
	on their right to indicate the fact that they have sons. */
	setSecondLevelLiElementsWithSonsClass();

	activateMenu('nav');
	
	/* For some reason the second leverl menu white border does not show until a onMouseOver event.
	We simulate this event + a onMouseOut event as a hack */
	hackSecondLeverMenuBorderDosentShow();

	/* (someUrl)#name didn't go to the name location so we do it here */
	goToAnchor();
}

activateMenu = function(nav) {
    /* currentStyle restricts the Javascript to IE only */
	if (document.all && document.getElementById(nav).currentStyle) {  
		var navroot = document.getElementById(nav);
        /* Get all the list items within the menu */
        var lis=navroot.getElementsByTagName("LI");  
		for (i=0; i<lis.length; i++) {
            /* If it's a top level menu item */
			if(lis[i].parentNode.parentNode.tagName=='DIV')	
			{
				if(lis[i].lastChild.tagName=="UL"){	/* If the LI has another menu level */
					/* assign the function to the LI */
					lis[i].onmouseover=function() {		
					   /* display the inner menu */
					   this.lastChild.style.display="block";
					}
					lis[i].onmouseout=function() {                       
						this.lastChild.style.display="none";
					}
				}
			}
			else
			{
				if(lis[i].lastChild.tagName=="UL"){		/* If the LI has another menu level */
					/* assign the function to the LI */
					lis[i].onmouseover=function() {
					   /* display the inner menu */
					   this.lastChild.style.display="block";
						rollOverLi(this);
							
					}
					lis[i].onmouseout=function() {                       
						this.lastChild.style.display="none";
						rollOutLi(this);
					}
				}
				else									/* If the LI doesn't have another menu level */
				{
					lis[i].onmouseover=function() {		
					   /* display the inner menu */
						rollOverLi(this);
							
					}
					lis[i].onmouseout=function() {                       
						rollOutLi(this);
					}
				}

				/* Make it go to the link location the text link holds */
				lis[i].onclick=function() {                       
					document.location=this.childNodes[0].href;
					//document.location='http://xxx';
				}
			}
		}
	}
}

/* This function is called on a second or third level LI element onMouseOver event.
Params:
@obj: the A object the user put his mouse on */
function rollOverLi(obj)
{
	switch (obj.className)
	{
		
	    case 'liWithSons' :
       		/* Change parent LI class name */
			obj.className='liWithSonsOn';

			/* Change arrow image source */
			src=obj.firstChild.nextSibling.src;
			src=addOnString(src);
			obj.firstChild.nextSibling.src=src;
	        break;
		
	    case 'liWithNoSons' :
			obj.className='liWithNoSonsOn';
	        break;

		case 'lastLiWithNoSons' :
			obj.className='lastLiWithNoSonsOn';
			break;

		case 'lastLiWithSons' :
	   		obj.className='lastLiWithSonsOn';
	        break;
	
		default :
			if(obj.id=='xxx1')
				alert('default over reached');
	        /* Change parent LI class name */
			//obj.className='liWithNoSonsOn';
	}

	/* Change border style. For some reason this didnt work in the css file class definition 
	so we change it here.*/
//	if(obj.id=='xxx')
	aObjects=obj.getElementsByTagName("A");
	if(aObjects!=null && aObjects.length>0)
		obj=aObjects[0];
	else
		obj=obj;
	
	obj.style.borderLeftStyle='solid';
	obj.style.borderLeftColor='white';
	obj.style.borderLeftWidth='1px';
	obj.style.borderRightStyle='solid';
	obj.style.borderRightColor='white';
	obj.style.borderRightWidth='1px';
	obj.style.color='white';

}

/* This function is called on a second or third level LI element onMouseOout event.
Params:
@obj: the A object the user put his mouse on */
function rollOutLi(obj)
{

	switch (obj.className)
	{
	    case 'liWithSonsOn' :
       		/* Change LI class name */
			obj.className='liWithSons';

			/* Change arrow image source */
			src=obj.firstChild.nextSibling.src;
			src=removeOnString(src);
			obj.firstChild.nextSibling.src=src;
	        break;

	    case 'liWithNoSonsOn' :
			obj.className='liWithNoSons';
	        break;

		case 'lastLiWithNoSonsOn' :
	        /* Change parent LI class name */
			obj.className='lastLiWithNoSons';
	        break;
	    
		case 'lastLiWithSonsOn' :
	        /* Change parent LI class name */
			obj.className='lastLiWithSons';
	        break;
	    
		default :
	       	/* Change parent LI class name */
		if(obj.id=='xxx1')
			alert('default out reached because class name is ' + obj.className);
	}
	
	/* Change border style. For some reason this didnt work in the css file class definition 
	so we change it here.*/
		aObjects=obj.getElementsByTagName("A");
	if(aObjects!=null && aObjects.length>0)
		obj=aObjects[0];
	else
		obj=obj;
	
	obj.style.borderLeftStyle='solid';
	obj.style.borderLeftColor='#9CC3E2';
	obj.style.borderLeftWidth='1px';
	obj.style.borderRightStyle='solid';
	obj.style.borderRightColor='#9CC3E2';
	obj.style.borderRightWidth='1px';
	obj.style.color='#032F66';

	if(obj.id=='xxx1')
	{
		alert('out finised, ' + obj.className);
	}

}


function rollTopMenuImg(action,id)
{
	path="/"+IMAGES_ROOT+"/"+LANG+"topmenu/";
	
	if(action==1)
	    eval("document.top_menu_" + id  + ".src='" + path + id + "_on.gif'");
	else
		eval("document.top_menu_" + id  + ".src='" + path + id + ".gif'");
}


/* This function is called on a rollOver Event for the second levell menu. It is called from the
level1class UL element.
We walk the tree towards the root and get the image source of the first level menu.
Than we set it to be the rolled over image to cancel the first level onMouseOut which
takes out the active image.

we also set the li background image
*/
function rollOverLevel1(o)
{
	//alert('in');
	/* Get the image source and add the string "_on" in it */
	var s=o.previousSibling.firstChild.src;
	var on=s.substr(s.length-7,3);

	if(on!='_on')
	{
		var s1=s.substr(0,s.length-4);
		var s2=s.substr(s.length-4,s.length);

		/* Set the new image source */
		var rollOverImgSrc=s1+'_on'+s2;
		//o.parentNode.parentNode.previousSibling.childNodes[0].src=rollOverImgSrc;
		o.previousSibling.firstChild.src=rollOverImgSrc;
	}
}

/* Same logig as rollOverLevel1 */
function rollOutLevel1(o)
{
	//alert('in');
	/* Get the image source and remove the string "_on" from it */
	var s=o.previousSibling.firstChild.src;
	var on=s.substr(s.length-7,3);

	/* Switch to not highlighted image only if it's a highlighted image and if it isn't the selected
	top level category	*/
	if(on=='_on' && o.parentNode.id!='sel')
	{
		var s1=s.substr(0,s.length-7);
		var s2=s.substr(s.length-4,s.length);

		/* Set the new image source */
		var rollOverImgSrc=s1+s2;
		var s=o.previousSibling.firstChild.src=rollOverImgSrc;
	}
}




function rollOverLevel2()
{
    alert('in');
}


/* Wipe off the first level1 and level2 li elements top border */
function wipeFirstLevelRedundentTopBorder()
{
	ulElements=new Array;
	liElements=new Array;
	menuImages=new Array;

	/* Get all document ul elements */
	ulElements=document.getElementsByTagName("ul");

	/* Loop ul elements */
	for(i=0;i<ulElements.length;i++)
	{
		/* Check if it's the ul we want */
		if(ulElements[i].className=='level1class' || ulElements[i].className=='level2class')
		{
			/* Set the first li topBorder to 0 */
			ulElements[i].firstChild.firstChild.style.borderTopWidth=0;
			ulElements[i].firstChild.style.height='26px';
			ulElements[i].firstChild.firstChild.style.height='26px';
		}
	}    
}

/* Display top menu second level border */
function hackSecondLeverMenuBorderDosentShow()
{
	/* Since the level1 ul element did not get a border untill an onMouseOver event
	on one of it's LI elements we simulate it. We make 3 stesp:
	1. 1. Simulate a onMouseOver event for all level1 LI A elements.
	2. Set focus on some element.
	3. Simulate a onMouseOut event for all level1 LI A elements.	*/

	/* 1. Simulate a onMouseOver event for all level1 LI A elements */
	for(i=0;i<ulElements.length;i++)
	{
		/* Check if its the ul we want */
		if(ulElements[i].className=='level1class')
		{
			ulElements[i].firstChild.fireEvent("onMouseOver");

			/* Set bottom li bottom border */
			/* ulElements[i].lastChild.style.borderBottomStyle='solid';
			ulElements[i].lastChild.style.borderBottomWidth='1px';
			ulElements[i].lastChild.style.borderBottomColor='#9CC3E2'; */
			
		}
	}

	/* 2. (Hack. The triggering of the onMouseOver events didn't do the job the other way.
	Set focus on some element) 
	y=new Array();
	y=document.forms[0].childNodes;
	z=document.getElementById("img_top_search_submit");
	z.focus();*/

	/* Simulate a onMouseOut event for all level1 LI A elements */
	/* Loop all document UL elements */

	for(i=0;i<ulElements.length;i++)
	{
		/* Check if its the UL we want */
		if(ulElements[i].className=='level1class')
		{
			/* 
			liElements=ulElements[i].childNodes;
			for(j=0;j<liElements.length;j++)
			{
				liElements[j].childNodes[0].fireEvent("onMouseOut");
			} */
			ulElements[i].firstChild.fireEvent("onMouseOut");
		}
	}    
}

/* Set different class names for top menu second level LI elements:
One for those with child elements,
One for those without.
*/
function setSecondLevelLiElementsWithSonsClass()
{
	ulElements=new Array;
	liElements=new Array;
	menuImages=new Array;

	/* Get all document ul elements */
	ulElements=document.getElementsByTagName("ul");

	/* Loop ul elements */
	for(i=0;i<ulElements.length;i++)
	{
		/* Check if it's the ul we want */
		if(ulElements[i].className=='level1class')
		{

			/* Get the UL child elements */
			liElements=ulElements[i].childNodes;

			/* Loop the LI child elements and set their class */
			for(var j=0;j<liElements.length;j++)
			{
				/* Set the LI class */
				liElements[j].className='liWithSons';

				/* Create the arrow IMG element */
				/* Insert an image element before */
				imgArrow=document.createElement("img");
				if(j==0)
					imgArrow.className='img_arrow_first';
				else
					imgArrow.className='img_arrow';

				/* We insert the IMG element after the LI element's A child element and before 
				the LI elements's UL child element. We need a reference to the UL element
				for the insertBefore method. */
				ulElement=liElements[j].firstChild.nextSibling;

				path=DOMAIN+"/"+IMAGES_ROOT;
				path_en=path+"/"+LANG;
				
				if(liElements[j].firstChild.nextSibling)
					imgArrow.src=path_en+"/top_menu_arrow.gif";
				else
					/* Hack */
					imgArrow.src=path+'/trans.gif';

				/* Insert the image element we created before the UL element (and after the A element) */
				liElements[j].insertBefore(imgArrow,ulElement);

				if(j==liElements.length-1)
				{
					if(liElements[j].firstChild.nextSibling)
						liElements[j].className='lastLiWithSons';
					else
						liElements[j].className='lastLiWithNoSons';
				}
			}	
		}
		if(ulElements[i].className=='level2class')
		{
			liElements=ulElements[i].childNodes;

			/* Loop the LI child elements and set their class */
			for(var j=0;j<liElements.length;j++)
			{
				if(j!=liElements.length-1)
					liElements[j].className='liWithNoSons';
				else
					liElements[j].className='lastLiWithNoSons';

				
			}	
		}
	}    
}

function addOnString(str)
{
	var s1=str.substr(0,str.length-4);
	var s2=str.substr(str.length-4,str.length);
	var s3=rollOverImgSrc=s1+'_on'+s2;
	return s3;
}

function removeOnString(str)
{
	var s1=str.substr(0,str.length-7);
	var s2=str.substr(str.length-4,str.length);
	/* Set the new image source */
	var s3=s1+s2;
	return s3;
}

