﻿// JavaScript Document
function getMouseXY(e) {
	if(!e) e= window.event
	try{ // grab the x-y pos.s if browser is IE
		mouseX = e.clientX 
		mouseY = e.clientY 
	}catch(e){  // grab the x-y pos.s if browser is NS
		try{
			mouseX = e.pageX
			mouseY = e.pageY
		}catch(e){}
	}  
	// catch possible negative values in NS4
	if (mouseX < 0){mouseX = 0}
	if (mouseY < 0){mouseY = 0}  
	
	return true
}

var popArr = new Array;
var domPopUpStart = function() {
	var bwh = getPageInnerSize()
	var i
	
	if(popArr.length == 0){
		var css = document.createElement("LINK")
		css.type = "text/css"
		css.rel = "stylesheet"
		css.href = "/css/lightbox.css"
		document.getElementsByTagName("HEAD")[0].appendChild(css)
		for(i=0;i<4;i++){
			popArr[i] = this.cloneNode(true)
			popArr[i].id+=i
			document.body.appendChild(popArr[i])
			popArr[i].startMe= domPopUpStart
			popArr[i].startMe()
		}		
	}
	var ME = this
	ME.isOpen = false
	var divs = ME.getElementsByTagName("DIV")
	var topResizer 
	var footResizer
	var stage
	var closeBut = ME.getElementsByTagName("IMG")[0]
	var title1 = ME.getElementsByTagName("H2")[0]
	for(i=0; i<divs.length; i++){
		switch(divs[i].id){
			case "top": topResizer = divs[i] ; break;
			case "foot": footResizer = divs[i] ; break;
			case "popContent": stage = divs[i] ; break;
		}
	}
	ME.stage = stage 
	ME.moveTo =  function(x,y){
		var tmpX = x 
		var tmpY = y + ME.offsetHeight 
		if (tmpX < 10 ) x = 10 
		if (tmpY+ME.offsetHeight > bwh.height -30 ) y= tmpY - ME.offsetHeight+30
		ME.style.top = y.toString() + "px"
		ME.style.left = x.toString() + "px"
	}
	ME.setSize = function(w,h){
		if(w){
			if(!isNaN(w)){
				ME.style.width = w.toString() + "px"
				topResizer.childNodes[0].style.width = (w-5).toString()+ "px"
				footResizer.childNodes[0].style.width = (w-5).toString() + "px"
			}
		}
	}
	ME.closePop= function(){
		
		ME.isOpen = false
		ME.style.visibility = "hidden"
		if(ME.onClose) ME.onClose()
		stage.innerHTML = ""
		
		ME.onClose = false
		
	}
	ME.openPopUp= function(title, html){
		if(ME.onOpen) ME.onOpen()
		ME.style.visibility = "visible"
		stage.innerHTML = html
		ME.setTitle(title)
		ME.isOpen = true
	}
	ME.setTitle = function(p){title1.innerHTML = p}
	ME.closePop()
	closeBut.onclick=ME.closePop
	
	ME.style.zIndex=100+ (30*popArr.length)
	
	
}//domPopUpStart


document.onclick = function(){
	//alert(mouseX + "/" + mouseY)	
	
	
}
var domPrompt 
var pageHolder = document.createElement("DIV")
pageHolder.init = function(){
	var ME=this
	var coverAll =  document.createElement("DIV")
	merge (coverAll.style, {position:'absolute', display:'block', zIndex:100, left:'0px', top:'0px',backgroundColor:'#000',opacity:'0.5', filter:'alpha(opacity=50)'})
	merge (ME.style, {position:'relative', display:'block', zIndex:100, left:'0px', top:'0px',zIndex:1, overflow:'auto'})
	var bScroll 
	var isOpen = false
	var sBody = document.body
	var sHtml = document.getElementsByTagName("HTML")[0]
	var fChilde
	ME.setPage=function(toggel){
		
		if(toggel){
			if(isOpen){ return coverAll; }
			bScroll = sHtml.scrollTop + sBody.scrollTop
			sBody.style.overflow = "hidden"
			sHtml.style.overflow = "hidden"
			var bwh = getPageInnerSize()
			var n=0
			merge (ME.style, {width:bwh.width+'px', height:bwh.height+'px'})
			merge (coverAll.style, {width:bwh.width+'px', height:bwh.height+'px'})
			while(sBody.childNodes[n].tagName!="DIV")n++
			fChilde =fChilde ||  sBody.childNodes[n]
			sBody.appendChild(ME)
			ME.appendChild(fChilde)
			setMyScroll()	
			clearTimeout(s)
			s = setTimeout(setMyScroll,100)
			sHtml.scrollTop = 0
			sBody.scrollTop = 0
			sBody.appendChild(coverAll)
			isOpen = true
			return coverAll
			
		}else{
			bScroll = ME.scrollTop
			
			sHtml.scrollTop = bScroll
			sBody.scrollTop = bScroll 
			sBody.appendChild(ME.childNodes[0])
			sBody.style.overflow = ""
			sHtml.style.overflow = ""
			sHtml.scrollTop = bScroll
			sBody.scrollTop = bScroll 
			deleteNode(ME)
			deleteNode(coverAll)
			isOpen = false
			return coverAll
			
			
		}
	}
	var s
	var playResize = false
	var onresizeEvent = function(){
		if(!playResize && isOpen){
			onresizeEvent = true
			clearTimeout(s)
			s= setTimeout(resizeAction,100)
		}
		
	}
	var resizeAction = function(){
		var bwh = getPageInnerSize()
		merge (ME.style, {width:bwh.width+'px', height:bwh.height+'px'})
		merge (coverAll.style, {width:bwh.width+'px', height:bwh.height+'px'})
		playResize = false
	}
	var setMyScroll = function(){
		ME.scrollTop = bScroll
		
	}
	document.body.onresize  = onresizeEvent
	window.onresize = onresizeEvent
	
	
	
}
pageHolder.init()

function modDialog(title, html, width, top, left, type, isDialog ){
	var bwh = getPageInnerSize()
	var isDialog = isDialog || "dialog"
	if(isDialog=="dialog") var  coverAll = pageHolder.setPage(true)
	var win=0
	var n
	for(n=0; n<popArr.length; n++){
		win = n
		if(!popArr[n].isOpen)n= popArr.length; 
	}
	
	popArr[win].style.zIndex = 120 + (3*win)
	if(isDialog=="dialog"){
		coverAll.style.zIndex= popArr[win].style.zIndex - 2
		popArr[win].onOpen = function(){ coverAll.style.display="block";}
	}
	popArr[win].onClose = function(){
		if(isDialog=="dialog") if(win>0){coverAll.style.zIndex=popArr[win-1].style.zIndex - 2 }
		for(n=0; n<popArr.length; n++){ if(popArr[n].isOpen) return;}
		if(isDialog=="dialog")pageHolder.setPage(false)
	}
	width = width || 450
	popArr[win].setSize(width)
	popArr[win].openPopUp(title || "", html || "")
	//top = top? top-160 : (myBodyHeight /2) - (domPrompt.offsetHeight/2) -60
	//left = left ||  (myBodyWidth /2) + (width/2) -80
	
	popArr[win].style.top="-5000px"
	function moveTo(){
		
		top = top? top-10 : (bwh.height /2) - parseInt(popArr[win].offsetHeight/2) -10
		if(top+popArr[win].offsetHeight >  bwh.height){
			
			tmpTop = bwh.height - popArr[win].offsetHeight-10
			top = tmpTop<-10? -10 : tmpTop
		}
		left = left ||  (bwh.width /2) - (width/2) +10
		popArr[win].moveTo(left,top)
		
	}
	setTimeout(moveTo, 150)
	return popArr[win]
}// modDialog

function openDialogMousePoz(title, html, width, centerTop,centerLeft){
	getMouseXY()
	var top = (!centerTop)? mouseY: 100;
	var left = (!centerLeft)?mouseX- parseInt(width/3):0
	return  modDialog(title, html, width, top, left )
	
}
function Alert(p){
		var bwh = getPageInnerSize()
		var top1 = (bwh.height /2)  -170
		p="<p class=\"alertMessage\">"+p+"</p>"
		return modDialog("", p, 400)
} //Alert()

function modLessDialog(title, html, width, top, left ){
	
	var win=0
	var popObj = $("#domProptUpObj")[0]
	
	popObj.style.zIndex = 120 + (3*win)
	popObj.onClose = function(){
		for(n=0; n<popArr.length; n++){ if(popArr[n].isOpen) return;}
	}
	width = width || 450
	popObj.setSize(width)
	popObj.openPopUp(title || "", html || "<br><br><br>")
	//top = top? top-160 : (myBodyHeight /2) - (domPrompt.offsetHeight/2) -60
	//left = left ||  (myBodyWidth /2) + (width/2) -80
	
	popObj.style.top="-5000px"
	function moveTo(){
		var bwh = getPageInnerSize()
		
		
		var bwh = getPageInnerSize()
		var scrlTop = document.body.scrollTop
		var doc_html = document.getElementsByTagName("HTML")[0]
		bwh.width += document.body.scrollLeft + doc_html.scrollLeft
		bwh.height += document.body.scrollTop + doc_html.scrollTop
		
		top = top? top : (bwh.height /2) - parseInt(popObj.offsetHeight/2) -10
		left = left ||  (bwh.width /2) - (width/2) +10
		
		if(left+popObj.offsetWidth>bwh.width){
			left -= (left+popObj.offsetWidth-bwh.width)
		}
		
		if(top+popObj.offsetHeight>bwh.height){
			trace(top)
			top -= (top+popObj.offsetHeight-bwh.height)+20
			trace(top)
		}
		popObj.moveTo(left,top)
		
	}
	setTimeout(moveTo, 200)
	return popObj
	
	
}
function openLightBoxMousePoz(e,title, html, width, centerTop, centerLeft){
	getMouseXY(e)
	var scrlTop = document.body.scrollTop
	var doc_html = document.getElementsByTagName("HTML")[0]
	scrlTop+= doc_html.scrollTop
	var top = (!centerTop)? mouseY+scrlTop-80: 100;
	var left = (!centerLeft)?mouseX- parseInt(width/3):0
	return  modLessDialog(title, html, width, top, left,null,"aa" )
	
}

// calling format:
// OpenForm({id:"969", title:"טופס צור קשר", width: "333", height:"347"})
function OpenForm(formObj) {
	var width = formObj.width || '800';
	var height = formObj.height || '535';
	var p = "<iframe framespacing=0 marginwidth=0 marginheight=0 marging frameborder=0 scrolling=no width='" + width + "' height='" + height + "' src='/contact_light.asp'>" + formObj.id + "</iframe>";
	return modDialog(formObj.title, p, parseInt(width) + 19)
}
function OpenFormEn(formObj) {
	var width = formObj.width || '800';
	var height = formObj.height || '535';
	var p = "<iframe framespacing=0 marginwidth=0 marginheight=0 marging frameborder=0 scrolling=no width='" + width + "' height='" + height + "' src='/en/contact_light.asp'>" + formObj.id + "</iframe>";
	return modDialog(formObj.title, p, parseInt(width) + 19)
} 

