var CurrentParent="";
var CurrentPage="";
var activeLayers=new Array();
var tid; //handle to an interval defined to mimic sleep while PreLoadIndex is not defined

var bw = whichBrs();
if( bw == "Firefox"){
	window.captureEvents(Event.KEYPRESS);
	window.document.onkeypress=KeyPressed;
}

function PreLoadPages()
{	
	try {
		if (typeof PreLoadIndex != "undefined"){
			if (tid!=null) { clearInterval(tid); tid=null; }
			
			var Frame =window.frames['TextPreLoader'];
			if(typeof Frame == 'undefined')
				return;
			
			if(PreLoadIndex<PageNames.length){
				if(PreLoadIndex>-1 && eval('typeof '+PageNames[PreLoadIndex]+'Text')=='undefined' && Frame && Frame.document)
					eval(PageNames[PreLoadIndex] + 'Text = "' + Frame.document.body.innerHTML.replace(/\"/g,"").replace(/\r/g,"").replace(/\n/g,"") + '";' );
				
				PreLoadIndex++;
				if ( Frame &&  PreLoadIndex<PageNames.length)
					Frame.location = "Text/"+PageNames[PreLoadIndex]+".htm";
				
			}
		}
	} catch(e){}
}
tid = setInterval('PreLoadPages()', 100);
var firstTime = true;
function SetDiv()
{
	try{
		if(firstTime && CheckQuery() != null )
			return;
		var Frame = document.getElementById? document.getElementById('TextLoader'): document.all? document.all['TextLoader']: null;
		if(Frame && typeof Frame.src != "undefined" )
			var PageName = Frame.src.substring(Frame.src.lastIndexOf("/")+1,Frame.src.lastIndexOf(".")).replace(".htm","");
		
		if(PageName != null && PageName != "")
			if(window.frames && window.frames['TextLoader'] && window.frames['TextLoader'].document )
				if( (eval('typeof '+ PageName + 'Text') == 'undefined') || ( eval( PageName + 'Text')==null ) )
					eval(PageName + 'Text = "' + window.frames['TextLoader'].document.body.innerHTML.replace(/\"/g,"").replace(/"/g,"&quot;").replace(/\r/g,"").replace(/\n/g,"") + '";' );
				
		var txtDiv = document.getElementById? document.getElementById('Text'): document.all? document.all['Text']: null;
		if (  txtDiv && typeof txtDiv.innerHTML != "undefined" ) 
			txtDiv.innerHTML = eval(PageName + 'Text');
	}
	catch(e){}
}

//set the start page- The call to this function is in PreloadImages
function SetStartPage()
{
	try{
		if(firstTime)
			firstTime = false;
		else
			return false;
			
		var page = CheckQuery();	
		if( typeof page != 'undefined' && page != null){
			SetActivePage(page);
			return true;
		}
	}
	catch(e){return false;}
	return false;
}

function CheckQuery(){
	var query = location.search;
	try{
		var page = null;
		if(query.length>1)
		{
			query = query.substring(1); //remove the "?"
			var allKeys = query.split("&");
			for(var i=0; i<allKeys.length; i++)
			{
				var keyPair = allKeys[i].split("=");
				if(keyPair.length == 2)
					if(keyPair[0].toLowerCase() == "page")
						if(keyPair[1]!=""){
							var index = IndexOfPageName(keyPair[1])
							if(index!=-1)
								return keyPair[1];
							else
								return null;
						}
			}
		}
	}
	catch(e){return null;}
	return null;
}

function SetCurrentParent(PageName)
{
	if(document.getElementById(PageName+"Level")!=null)
		if(document.getElementById(PageName+"Level").value==1)
			CurrentParent = PageName;
}
function RemoveCurrentBtn()
{
	SetNormalImg(document.getElementById(CurrentBtn));
}
function SetActivePage(PageID)
{
	hideAll();
	CurrentPage = PageID;
	if(document.getElementById(PageID+"Level")!=null){
		if(document.getElementById(PageID+"Level").value==1)//just if main-menu:highlight buttun
			SetActiveButton(document.getElementById(PageID));
		else{
				hideLevel(2);//hide all subs
				SetActiveButton(document.getElementById(CurrentParent));
				
		}
	}
	SetDynamicContent(PageID);
}

function OnDemand(PageID){
	var i = IndexOfPageName(PageID);	
	if(i >=0){
		if(eval('typeof ' + PageNames[i] + 'Text') == 'undefined' ){
			//alert("laod...");
			document.getElementById('TextLoader').src = "Text/"+PageNames[i]+".htm";
		}
		else{
			//alert("var...");
			document.getElementById('Text').innerHTML = eval(PageNames[i]+'Text');
		}
	}
}

function IndexOfPageName(PageName){
	var i;
	for(i=0; i<PageNames.length; i++)
		if(PageNames[i] == PageName)
			return i;
	return -1;
}

function KeyPressed(e)
{
	
	var i;
	var count=0
	var key;

	if(window.event)
		key = event.keyCode;
	else key=e.which;
	
	if(key == 37 || key == 39){
		var direction = key == 37 ? -1 : 1;
		
		try{
			i = IndexOfPageName(CurrentPage);
			i = GetNearbyPageIndex(i,direction);
			while( document.getElementById(PageNames[i]+"Level") == null && count<PageNames.length){//no level==>not in menu!!!
				i = GetNearbyPageIndex(i,direction);
				count++;
			}
			if(count==PageNames.length) return //no pages in menu
			
			//if this index is of parent with childs-must skip on it		
			if(i!=PageNames.length){ // can't be parent without childrens in last index	
				while(i>=0)
					if( document.getElementById(PageNames[i]+"Level") != null && document.getElementById(PageNames[i+1]+"Level")) //next one is not in menu ==> current isn't parent with children
						if( document.getElementById(PageNames[i]+"Level").value < document.getElementById(PageNames[i+1]+"Level").value) //if current level smaller than next ==> this is a parent with childs
							i = GetNearbyPageIndex(i,direction); //go to closer page, in fit direction 
						else break;
					else break;
			}
			//if current page is a child- must find its ancient parent -to set the 'CurrentParent' & highlight its bottun
			if( document.getElementById(PageNames[i]+"Level") != null)	
				if( document.getElementById(PageNames[i]+"Level").value != 1) //this is not an ancient parent-find its parent
					CurrentParent = FindParent(i);
			
			//set current page to be active
			SetActivePage(PageNames[i]);
		}
		catch(e){}
	}
}

//get closer page's index- according to direction
function GetNearbyPageIndex(index,direction){
	if(direction == -1)
		return GetPreviousPageIndex(index) ;
	else
		return GetNextPageIndex(index);
}

//get index of next page.
//IMPORTANT:
// 1. On PageNames.length-1 it returns 0.
// 2. On index that bigger than PageNames.length-1, it return -1.
function GetNextPageIndex(index)
{
	if (index == PageNames.length-1)
		return 0;
	else if(index>=0 && index<PageNames.length)
		return index+1;
	else return -1;
}

//get index of previous page.
//IMPORTANT:
// 1. On 0  it returns PageNames.length-1.
// 2. On index that bigger than PageNames.length-1, it return -1.
function GetPreviousPageIndex(index)
{
	if (index ==0 )
		return PageNames.length-1;
	else if(index>0 && index<PageNames.length)
		return index-1;
	else return -1;
}

//find ancient parent of a page
// if this page is an ancient parent OR not in menu- return null
function FindParent(PageIndex)
{
	if( document.getElementById(PageNames[PageIndex]+"Level") != null){
		if( document.getElementById(PageNames[PageIndex]+"Level").value == 1) //this is an ancient parent
			return null;
	}
	else return null; //this page is not in menu
			
	for(var i=PageIndex;i>=0;i--) //go back to find the ancient parent, must have level=1.
		if( document.getElementById(PageNames[i]+"Level") != null)
			if( document.getElementById(PageNames[i]+"Level").value == 1)
				return PageNames[i];
	return null; //no parent was found!!!! - a problem with the menu!!!
}


function SetPic(PicId,src)
{
	try{
		document.getElementById(PicId).src  = src;}
	catch(e){}
}

function SetDynamicContent(PageName)
{
	try{
		if(document.getElementById(PageName+"Level")!=null){
			if(document.getElementById(PageName+"Level").value == 1)
				document.getElementById("PagePic").src = "TecLaw/images/"+ PageName+ "Pic.gif";
			else if (CurrentParent!=null && CurrentParent != "")
				document.getElementById("PagePic").src = "TecLaw/images/"+ CurrentParent+ "Pic.gif";
		}
		else
			document.getElementById("PagePic").src = "TecLaw/images/"+ PageName+ "Pic.gif";

		OnDemand(PageName);
	}
	catch(e){}
}
