﻿// JavaScript Document
var firstMenuElement = new Array(4);
var mnTimer,levelOfParam

	// load the menu (called either from the flash menus or from reload menus function
	function LoadMenuWithSubs(hrefParams,Level,CallAsp,CrrMenuID) {
		var AjaxObj = new Ajax(CallAsp+"?Level="+Level, 
			{method: 'get',update: $("MenuHolder"+Level), onComplete: 
				function() { 
					levelOfParam = parseInt(Level)+1 // save the level for call back from flash menu.
					initSubMenu(parseInt(Level)+1,hrefParams);
				} 
			}
		)
		AjaxObj.request();	
	}

	// build the flash menu in the new div holder
    function initSubMenu(Level) {
		if (Level == 4) { // for level for the width will be larger
			widthForFlash = "244"
		} else {
			widthForFlash = "196"
		}
		var so = new SWFObject("menus.swf?Lang="+lang+"&Level="+Level, "menus"+Level,widthForFlash , "199", "8", "");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess","sameDomain")
		so.write("MenuCol"+Level);
		if ($("noFlash") != null) {
			$("noFlash").style.visibility = "visible"
		}
	}

	function removeDataFromNextMenus(loadedLevel,startFromNextLevel) {
		if (loadedLevel <3) {
			for (i=(loadedLevel + startFromNextLevel);i<5;i++) {
				if (typeof(getMovieName('menus'+i).callRemoveData()) == 'function' ) {
					getMovieName('menus'+i).callRemoveData()
				}
			}
		}
	}
	
	// this functions is used in news -> for getting next and pre menus!
	function getPreNextPage(Level,Dir) {
		return getMovieName("menus"+levelOfParam).callFindPageByNextPreIndex(Dir)
	}
	
	// load flash logo
	function LoadLogo() {
		var so = new SWFObject("logo_anim_clear.swf", "mainPage", "800", "100%", "8", "");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.write("divLogo");
	}
	
	//*********************************************************************************************
	var CrrParam,LoadParamsData, ContLoading 

	function initMenus(startLang) {		
		
		// check if there are params 
		if (startLang != "FromClicked") {
			$("LangHeb").style.display = 'none'
		}
		var thisURL = document.location.href;
		ContLoading = false
		upperURL = ""
		if (startLang!="he" && thisURL.indexOf("#")==-1) {
			thisURL = thisURL + "#"+startLang+"/"}
		// check if there are params for this URL
		if (thisURL.indexOf("#")>-1) {
			arrParams = thisURL.split("#")
			Params = arrParams[1] 
			lastLang = lang
			lang =  Params.substring(0,Params.indexOf("/"))
			Params = Params.substring(Params.indexOf("/")+1)
			// check if the URL if for wordpress (in wordpress we open a new index and not useing the ajax structure)
			if (Params!="") {
				ContLoading = true
			}
			if (lastLang != lang) {
				changeLang(document,lang)
			} else {
				reloadMenus()
			}
		} else {
			// check for default lang-. if not hebrew change lang
			reloadMenus()
		}
	}


	function initKeywords() {
		  var metaArray = document.getElementsByName('keywords');
		  for (var i=0; i<metaArray.length; i++) {
			 document.getElementsByName('keywords')[i].content=$('MetaKeywords'+lang).value;
		  }		
	}
	
	function initLogo() {
		var so = new SWFObject("logo_anim_clear.swf", "logo_anim_clear", "119", "111", "8", "");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.write("divLogo");
	}
	
	function LoadHomePage() {
		var AjaxObj = new Ajax(upperURL+"FirstPage.asp?Lang="+lang, {method: 'get',update: $("Page") , onComplete: function() {initFirstPage();restartMenus()} } )
		AjaxObj.request();
	}
	
	function restartMenus() {
		replaceBckPng($("imgFadeBackground"),"images/bg_grad_portfolio_nolines.png","images/bg_grad_general_nolines.png","images/bg_grad_general_nolines.png")
		GeneralPageLoading("","",$("Title").value,$("MetaDesc").value)
	}

	function GeneralPageLoading(prjName,paramsForURL,Title,MetaDesc) {
		setBrowserUrl(lang+"/"+paramsForURL)
		SetTitle(prjName,Title)		
		if (document.getElementsByName) {
		  var metaArray = document.getElementsByName('description');
		  for (var i=0; i<metaArray.length; i++) {
			 document.getElementsByName('description')[i].content=MetaDesc
		  }
		}
		
		correctPNG()
	}
	var crrMenuID
	// load the page - called from the last menus level
	function LoadPage(URL,PageID,Level,MenuID,LastParams,prjName,Title,MetaDesc) {
		crrMenuID = MenuID
		removeDataFromNextMenus(parseInt(Level),1)
		var AjaxObj = new Ajax(upperURL+URL+PageID+"&Level="+Level+"&MenuID="+MenuID+"&Lang="+lang, {method: 'get',update: $("Page"), 
			onComplete: function() {
				ReplaceLangDep()
				if (MenuID == "1") {
					initProject()
					replaceBckPng($("imgFadeBackground"),"images/bg_grad_general_nolines.png","images/bg_grad_portfolio_nolines.png","images/bg_grad_portfolio_nolines.png")
				} else {
					replaceBckPng($("imgFadeBackground"),"images/bg_grad_portfolio_nolines.png","images/bg_grad_general_nolines.png","images/bg_grad_general_nolines.png")
					if (PageID == "43") {
						initTeam()
					} else {
						if (PageID =="108") { // 108= clients 
							initScrollerText("ClientsData")
						} else {
							if (MenuID != "5" && PageID != "117") { // 5 = contact 
								initPage(MenuID,PageID,Level)
								if (MenuID == "112") { // team worker
									initPic($("maxPics").value,true,1)
									initScrollerText("TeamWorkerText")
								}
								if (MenuID == "2") {
									replaceBckPng($("imgFadeBackground"),"images/bg_grad_general_nolines.png","images/bg_grad_portfolio_nolines.png","images/bg_grad_portfolio_nolines.png")
									initScrollerText("CaseItemData")
								}
							} else {
								if (PageID == "117") {
									arrangeForLoadingFiles();
								}
							}
						}
					}
				}
				initFlashes(MenuID)
				GeneralPageLoading(prjName,LastParams+"P"+PageID,Title,MetaDesc)
			}} 
		)
		AjaxObj.request();
	}
	
	function replaceBckPng(imgID,fromClr,ToClr,imgFileName) {
		// if the png was replace with span (by correctPNG function for ie browsers not supporting png, replace outer html)
		if ($(imgID).src != null) {
			$(imgID).src = imgFileName
		} else {
			if ($(imgID).outerHTML != null) {
				crrOuter = $(imgID).outerHTML
				crrOuter = crrOuter.replace(fromClr,ToClr)
				$(imgID).outerHTML = crrOuter
			} else {
				$(imgID).src = imgFileName
			}
		}
	}
	
	function SetTitle(Name,Title) {
		if (Title == "" || Title == null) {
			if (lang == "Heb") {
				document.title = "אופן"
			} else {
				document.title = "Open"
			}
			if (Name !="") {		
				document.title = document.title +" - " +Name.replace("&#39;","'")
			}
		} else {
			document.title = Title.replace("&#39;","'")
		}
	}

	function replaceAllChar(strMenu) {
		strMenu = strMenu.replace(/&nbsp;/g," ")		
		strMenu = strMenu.replace(/&amp;/g,"&")
		return strMenu
	}	

	// Handle and Load all Params when loading the index with params
	function GetNewParamOfMenuInURL() {
		if (Params.indexOf("_")<0) {
			CrrParam = Params.substring(Params.indexOf("=")+1)
		} else {
			CrrParam = Params.substring(0,Params.indexOf("_"))
		}
		if (Params.indexOf("_")>0) {
			Params = Params.substring(Params.indexOf("_")+1)
			ContLoading = true
		} else {
			ContLoading = false
		}
		return CrrParam
	}

		
	function reloadMenus() {
		LoadedMenusXML = 0
		initSubMenu(1)
		initSubMenu(2)
		initSubMenu(3)
		initSubMenu(4)		
		initKeywords()
	}
	
	function reLoadFirstPage() {
		removeDataFromNextMenus(1,1)
		getMovieName('menus1').callLoadMenuData(0,1)		
	}
	// this function is been called from the flash after loading last menu (level 4)
	function cmplLoadAllInitData() {
		LoadedMenusXML = LoadedMenusXML + 1
		if (LoadedMenusXML == 4) {
			getMovieName('menus1').callLoadMenuData(0,1)		
		}
	}
	
	function ExecuteLoadingFromMenuByMenuID(CrrParam,calledFromUrlParams) {
		// Call the menu in the flash and execute it's click function
		getMovieName("menus"+levelOfParam).callClickOnMenu(CrrParam)
		
	}
	
	// this function is been called from the flash menu, when it's data has been loaded
	function cmplLoadingFlashMenu(Level,ParentMenuID) {
		// if menu was load by URL -> get new sub menu from URL. or if clicked on Alphabet(79) then load a-z (80)
		levelOfParam = Level
		StartDeleteNextMenus = 0
		if (ContLoading || ParentMenuID==1 || ParentMenuID==79) {
			if (ContLoading) {
				paramMenuID = GetNewParamOfMenuInURL()
			} else {
				if (ParentMenuID==79) {
					paramMenuID = "80"
				} else {
					paramMenuID = "22"
				}
				StartDeleteNextMenus = 2
			}
			ExecuteLoadingFromMenuByMenuID(paramMenuID,true)
		} else { 
			if (levelOfParam ==1) {
				LoadHomePage()
			} 
			StartDeleteNextMenus = 1			
		}
		if (StartDeleteNextMenus >0) {
			removeDataFromNextMenus(parseInt(Level),StartDeleteNextMenus)
		}
	}
	
	
	function fireClickEvent(control) { 
		if (document.all) { 
			control.fireEvent("onclick"); 
		} else { 
			var clickEvent = document.createEvent("MouseEvents"); 
			clickEvent.initEvent("click", false, true); 
			control.dispatchEvent(clickEvent); 
		} 
	} 
//*********************************************************************************************
