// JavaScript Document

//*************************************SWAP IMAGE**********************************************//
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//***************************GENERAL FUNCTIONS ********************************
//returns the base of the page
function getBase(){
	var base;

	if(document.URL.search(/www/) != -1)
		base = "http://www.nechama.org.il/";
	else
		base = "http://nechama.org.il/";
	return	base;
}
//returns the clients browser name
function getBrowser(){
	var browser;
	if(navigator.appName.search("Netscape")!=-1)
		browser = "Firefox";
	if (navigator.userAgent.search("MSIE 7.0")!= -1)
		browser = "IE7";
	if (navigator.userAgent.search("MSIE 6.0") != -1)
		browser = "IE6";
	return browser;
}
//returns the first subdirectory after the base
function getFirstSubDir(){
	var firstSubDir = (document.URL.slice(getBase().length).split("/"))[0];
	return firstSubDir;
}
//returns the filename in the URL
function getFileName(){
	var fileName = document.URL.slice(document.URL.lastIndexOf("/")+1);
	fileName = (fileName == "") ? ".index.html" : fileName ;
	return fileName;
}

//*************************LINKING STYLESHEETS**********************************************
//1. links a main styleshhet: http://nechama.org.il/style.css for all the pages
//2. links a general stylesheet for each subdirectory:
//   http://nechama.org.il/[subdir]/env/[subdir].css
//3. links a specific stylesheet for each file
//   http://nechama.org.il/[subdir]/env/[filename].css
//****************************************************************************************
function setCssOld ()
{	
	var fileName = "style";
	var path = "";
	var slices = new Array()
	if (fileName == "ie") alert("שגיאה");

	slices = document.URL.slice(getBase().length).split("/");
	//alert("<link href="+ path + "env/" + fileName + ".css rel=stylesheet type=text/css />");
	document.write("<link href="+ path + "env/" + fileName + ".css rel=stylesheet type=text/css />");
	for(i=0; i<slices.length; i++){
		if(slices[i].indexOf(".html") != -1)
			fileName = slices[i].slice(0,slices[i].indexOf(".html"));
		else{
			if (slices[i] == "")
				fileName = "index"; 
			else{
				fileName = slices[i];
				path += slices[i];
				path += "/";
			}
		}
		
		//if the filename contains just numbers add dynamic.css for dynamic content
		fileNameFinal = fileName;
		for(j=0; j<fileName.length; j++){
			x = fileName;
			if ((fileName.charCodeAt(j) > 47) && (fileName.charCodeAt(j) < 58))
				fileNameFinal = "dynamic";
			else
				fileNameFinal = fileName;
		}
//		if (fileNameFinal == "ie") alert("שגיאה");
//
//		//alert("<link href="+ path + "env/" + fileNameFinal + ".css rel=stylesheet type=text/css />");
//		document.write("<link href="+ path + "env/" + fileNameFinal + ".css rel=stylesheet type=text/css />");

		if (fileNameFinal != "ie")
		{
			document.write("<link href="+ path + "env/" + fileNameFinal + ".css rel=stylesheet type=text/css />");
		}
	}
}
//----------------------------------------------------------------------------------------------------------------
function setCss ()
{	
	var fileName = "style";
	var path = "";
	var slices = new Array()
	if (fileName == "ie") alert("שגיאה");

	var url=document.URL;
	var le=url.indexOf(getBase());
	if (le ==-1)
	{
		var le=url.indexOf("www");
		url="http://"+url.substring(le, url.length);
		le=url.indexOf("+");
		url=url.substring(0, le);
	}
	slices = url.slice(getBase().length).split("/");

	document.write("<link href="+ path + "env/" + fileName + ".css rel=stylesheet type=text/css />");
	for(i=0; i<slices.length; i++){
		if(slices[i].indexOf(".html") != -1)
			fileName = slices[i].slice(0,slices[i].indexOf(".html"));
		else{
			if (slices[i] == "")
				fileName = "index"; 
			else{
				fileName = slices[i];
				path += slices[i];
				path += "/";
			}
		}
		
		//if the filename contains just numbers add dynamic.css for dynamic content
		fileNameFinal = fileName;
		for(j=0; j<fileName.length; j++){
			x = fileName;
			if ((fileName.charCodeAt(j) > 47) && (fileName.charCodeAt(j) < 58))
				fileNameFinal = "dynamic";
			else
				fileNameFinal = fileName;
		}

		if (fileNameFinal != "ie")
		{
			document.write("<link href="+ path + "env/" + fileNameFinal + ".css rel=stylesheet type=text/css />");
		}
	}
	
}
//******************************* SET BLUE NAVIGATION BAR ********************************
//1. turns off the blue navigation button of the current sector (subdirectory)
//****************************************************************************************
function setBlueNavigation(){
	if (document.URL == getBase())
		return;

	var navigation = new Array("pages","cantillation","nechama","about","commentators","acronym" );
	var alt = new Array("מאגר הגיליונות המלא","שאלות בטעמי המקרא","על נחמה ליבוביץ","מהם הגיליונות של נחמה?","מידע בסיסי על הפרשנים הנזכרים בגיליונות","קיצורים וראשי תבות" );

	var currSubDir = document.URL.slice(getBase().length);
	if(currSubDir.indexOf("haftarot.html") != -1 || currSubDir.indexOf("special") !=  -1 || currSubDir.indexOf("guidance.html") != -1 || currSubDir.indexOf("shnaton") != -1)
		currSubDir = "pages/";
	else if (currSubDir.indexOf("/") != currSubDir.lastIndexOf("/") || currSubDir.indexOf(".html") >= 0)
		return;
	
	currSubDir = currSubDir.slice(0,currSubDir.indexOf("/"));	

	var currSubDirNum = -1;

	if (currSubDir == "cgi-bin")
	{
		currSubDir = document.URL.slice(getBase().length+8);
		currSubDir = currSubDir.slice(0, currSubDir.indexOf(".pl"));
	}

	for(i=0; i<navigation.length; i++)
		if(currSubDir == navigation[i])
			currSubDirNum = i;

	if (currSubDirNum != -1)
	{		
		var liArray = document.getElementById("blueMenu").getElementsByTagName("li");
		liArray[currSubDirNum].innerHTML = "<img src=\"images/header-footer/blueBtn" + (currSubDirNum+1) + "_over.jpg\" alt='" + alt[currSubDirNum] +"' />";
	}

}//end of setBlueNavig

//***************************GO BACK TO THE GILAYON ********************************
//1. function for the "בחזרה לגיליון" button from pages of answer and guidance
//****************************************************************************************
function backToGilayon(){
	var newURL = getBase() + "pages/" + getFileName();	
	document.URL = newURL;
}
//***************************GO TO THE GUIDAINCE PAGE ***********************************
//1. function for the "לאלון ההדרכה" button. Go to the guidaince page of the current gilayon.
//****************************************************************************************
function toGuadiance(){
	var newURL = getBase() + "guidance/" + getFileName();	
	document.URL = newURL;
}
//***************************GO TO THE ANSWERS PAGE ***********************************
//1. function for the "הסברים ותשובות" button. Go to the answer page of the current gilayon.
//****************************************************************************************
function toAnswer(){
	var newURL = getBase() + "answer/" + getFileName();	
	document.URL = newURL;
}
//***************************SHOW MORE PAGES ***********************************
//1. function for the "גיליונות נוספים בנושא זה" button. Display a gilyon list which is relevant to the current gilyon.
//****************************************************************************************
function showMore(){
	var disp = document.getElementById("morePages").className;
	if (disp == "display")
		document.getElementById("morePages").className = "undisplay";
	else 
		document.getElementById("morePages").className = "display";
}
function collapseForum(){
	if(document.getElementById("forumMenu"))
		document.getElementById("forumMenu").style.display = "none";
}

function collapseForum2(obj, e){
	e.cancelBubble = true;
	
	//Handle IE
	if(getBrowser() == "IE6" || getBrowser() == "IE7"){
		if(e.toElement == "[object]")
			obj.style.display = "none";
	}
	//Handle normal browsers
	else
		obj.style.display = "none";
}


function collapse(obj, e){
	e.cancelBubble = true;
	obj.style.display = "none";
}

function expand(obj){
	obj.parentNode.getElementsByTagName("div")[0].style.display = "block";
}
//***************************HIDE SEARCH BAR ***********************************
//1. Hide the earcg option of the orange navigation bar
//****************************************************************************************
function hideSearch(){
	document.getElementById("searchMask").className = "display";
	document.getElementById("searchTxt").className = "undisplay";
	document.getElementById("searchArrow").className = "undisplay";
}












