// if Shared SSL, the web application root is /daatemet
var vPath = (location.protocol == "http:") ? "/" : "/daatemet/";

var langCookie = "LANG";

function ifExists(urlstr){
	var underConstPage = "/construction.html";
	var nonExistArr = new Array (	"/daathalacha/ru_gentiles1.html",
					"/daathalacha/ru_gentiles2.html",
					"/daathalacha/ru_gentiles3.html",
					"/daathalacha/ru_gentiles4.html",
					"/daathalacha/ru_gentiles5.html",
					"/parasha/ru_masaey.html",
					"/parasha/ru_devarim.html",		
					"/parasha/ru_vet.html",		
					"/parasha/ru_ekev.html",		
					"/parasha/ru_reeh.html",		
					"/parasha/ru_shoftim.html",	
					"/parasha/ru_ki_tetze.html",	
					"/parasha/ru_ki_tavo.html",	
					"/parasha/ru_nitzavim.html",
					"/parasha/ru_vayelech.html",	
					"/parasha/ru_haazinu.html",
					"/parasha/ru_zot.html",
					"/talmudicissues/ru_amoraim.html",
					"/talmudicissues/ru_holy_writ.html",
					"/talmudicissues/ru_strange.html",
					"/talmudicissues/ru_ink.html",
					"/media/audio/ru_index.html"
								    );
	for (var i=0; i<nonExistArr.length; i++){
		if (urlstr == nonExistArr[i]){
			return underConstPage;
		}
	}
	return urlstr;
}

function getCookie(NameOfCookie){
  if (document.cookie.length > 0){
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1){ 
     begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
    } 
  }
	return null;
}

function setCookie(NameOfCookie, value, expiredays) {
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) + "; path=/" +
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function changeLang(i){
// BEGIN TEMPORARY
setCookie(langCookie,i,365);
location.href = (i == "ru") ? "/" + i + "_index.html" : "/index.cfm";
// END TEMPORARY
/*
	lang = i;
	var re,newstr,vExists;
	var str = location.pathname;
	if (lang.length > 0){
		if (lang.length == 2){	// i.e. it's "en","he" or "ru"
			setCookie(langCookie,lang,365);
			if (str.indexOf("en_") != -1){
				re = /en_/gi;
				newstr=str.replace(re, lang + "_");
				location = ifExists(newstr);
			}else if (str.indexOf("ru_") != -1){
				re = /ru_/gi;
				newstr=str.replace(re, lang + "_");
				location = ifExists(newstr);
			}else if (str.indexOf("he_") != -1){
				re = /he_/gi;
				newstr=str.replace(re, lang + "_");
				location = ifExists(newstr);
			}else{
				location.href = location.href;
			}
		}else{					// i.e. it's a link
			location = lang;
		}
	}
*/
}

function displayMeta(){
	var metaStr = "<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>";
	metaStr += "<META HTTP-EQUIV='expires' CONTENT='0'>";
	var chr;
	if (lang == "en"){
		chr = "ISO-8859-1";
	}else if (lang == "ru"){
		chr = "Windows-1251";
	}else{
		chr = "VISUAL"; //"ISO-8859-8";
	}
	metaStr += '<META http-equiv="Content-type" content="text/html; charset=' + chr +'">';
	metaStr += '<LINK REL="STYLESHEET" HREF="' + vPath + 'styles/main.css">';
	metaStr += '<LINK REL="STYLESHEET" HREF="' + vPath + 'styles/' + lang + '_style.css">';
	return metaStr;
}

var lang = getCookie(langCookie);

if ((lang == null) || (lang == "")) {
	// set English as default language
	setCookie(langCookie,'en',365);
	lang = 'en';
}
var alignment = (lang == "he") ? "right" : "left";
var direction = (lang == "he") ? "RTL" : "LTR";
