/*
masterPOD.20147.multiPanel.js
masterPOD.20155.mpLogin(xObj)
masterPOD.20414.authenticate(memory)
masterPOD.20393.getMemory(memories)
masterPOD.20395.getMemories()
masterPOD.20394.setMemory(memory)
masterPOD.20359.remember(id,type)
masterPOD.20580.mpLogout(xObj)
masterPOD.20152.mpBlock(xObj)
masterPOD.20157.mpUpdate(xObj)
masterPOD.20224.mpConfirm()
masterPOD.20313.mpAttach()
masterPOD.20163.mpPanelComplete(xStr)
masterPOD.20343.mpPort(xPOD)
masterPOD.20344.mpGetPanel(pnlName)
masterPOD.20150.mpOnload()
masterPOD.20350.mpSize(pnlName,w,h)
masterPOD.20164.mpShowHide(pnlName,mode)
masterPOD.20352.getDragHandle(pnl)
masterPOD.20353.dragPanel(event)
masterPOD.20351.draggingPanel(e)
masterPOD.20354.dropPanel()
masterPOD.20355.getPobj(n)
masterPOD.20358.encryptlet(cryptee,pivot)
*/
window.multiPanel={}
//getMemory()
//alert("multiPanel.js window.multiPanel "+window.multiPanel)
function mpLogin(xObj){
	scriptid=20155;errObj="mpLogin"
xObj.panel='login'
xObj.modal=1
var tk=getCookie("token")
if(tk)xObj.token=tk
multiPanel.showHide("mpDiv",1)//0 hide, 1, fullScrn, 2 at cursor
multiPanel.port(xObj)
return false
/* grab token for encryptlet if exists */
function getCookie(cStr){
	if(!document.cookie.length)return false
	eStr=cStr+"="
	var c0=document.cookie.split(eStr)[1]
	if(!c0)return false
	return c0.split(";")[0]
}

/*var mPstyle=$("multiPanelNode").style
//mPstyle.top=mPstyle.left=0
//var mP=$("multiPanel")
//alert("mP.login "+mP.login+" xPODwrite(xPOD) "+xPODwrite(xPOD))
mP.login(xPODwrite(xPOD))   
*/
function authenticate(memory){
	scriptid=20414;errObj="authenticate"
var tk=parseInt(session.token)		;		t("    session.token "+tk)
var newMemory=0
for(var x in memory){
	t("mem1 "+memory[x])
	var mem=unescape(memory[x])
	t("unescape "+mem)
	var memSplit=mem.split(".")
	if(memSplit.length==1)continue 
	eStr="select sessionStart as tmLast from session where id="+memSplit[1]
	var rs=dc.Execute(eStr) ; 	t("rs.eof "+rs.eof+" for  "+eStr)
	if(rs.eof)continue
	var tmLast=new Date(rs("tmLast").value)
	tmLast=tmLast.getTime()	
	
	var memSum=memSplit[0]
	memSum=parseInt(memSum)
	t(" authenticate memSum: "+memSum)

	// get last session start
	var testDate=new Date();testDate.setTime(tmLast)
/*	eStr=testDate.getMonth()+"/"+testDate.getDate()+"/"+testDate.getYear()+" "+testDate.getHours()+":"+testDate.getMinutes()+":"+testDate.getSeconds()+":"+	testDate.getMilliseconds()
//	t("  authenticate tmLast back to date:\n  "+eStr+"\n")
	if(session.dataSITE=="masterpod"){
		var whatTime=1822+tk+mem
		var testDate=new Date();testDate.setTime(whatTime)
		eStr=testDate.getMonth()+"/"+testDate.getDate()+"/"+testDate.getYear()+" "+testDate.getHours()+":"+testDate.getMinutes()+":"+testDate.getSeconds()+":"+	testDate.getMilliseconds()
		t("Time that would have made it work:\n  "+eStr+"\n")
	}
*/	
	var id=tmLast-tk-memSum
	t("     authenticate tmLast-tk-mem=id")
	t("     authenticate "+tmLast+"-"+tk+"-"+memSum+"="+id)
	eStr="select [_name] from tree where id="+id 
	rs=dc.Execute(eStr)
	t("not found (rs.eof) "+rs.eof)
	if(rs.eof)continue
	t("session."+x+"id="+id)
	session[x+"id"]=id
	t("session."+x+"Name="+rs("_name").value)
	session[x+"Name"]=rs("_name").value
	if(!newMemory)newMemory={}
	newMemory[x]=id 
} 
return newMemory
//if(newMemory)setMemory(newMemory)
	/*
	var tsTm=tk+mem+20349
	var tsDate=new Date(tsTm+tk)
	eStr="          date extracted from mem "+(tsDate.getDate())+" "+tsDate.getHours()+":"+
	tsDate.getMinutes()+":"+tsDate.getSeconds()
	t(eStr)
	*/
	


}
function getMemory(memories){
	scriptid=20393;errObj="getMemory"
/*
return a memory object
type - hash
do ths by calling getMemories wchi return an array of 
type=hash pairs.

*/
//return {}
memories=String(memories).split("&")
var memory={}  //bytetherapy@gmail.com
//var memories=getMemories()
//alert("getMemory memories "+memories)
var substance=0
t("getMemory memories.length "+memories.length)
for(var x=0;x<memories.length;x++){
	var memItem=memories[x]
	if(memItem=="")continue
	t(" getMemory memItem: "+memItem)
	memItem=memItem.split("=")
	memory[memItem[0]]=memItem[1]
	substance=1
}
if(!substance)return false
return memory
}
function getMemories(){
	scriptid=20395;errObj="getMemories"
/*
in mulitpnael.dpod too
return array from remember cookie
bytetherapy@gmail.com
*/
if((typeof document)=='undefined'){
	var memories=String(Request.Cookies("remember"))
}else{
	var cookie=document.cookie
	var memories=cookie.split("remember=")
	if(memories.length==1)return []
	memories=memories[1].split(";")[0]
}
memories=memories.split("&")
return memories
}
function setMemory(memory){
	scriptid=20394;errObj="setMemory"
if((typeof document)=='undefined'){
	var tk=parseInt(session.token)
	eStr="select sessionStart from session where id="+session.sessionid
	var rs=dc.Execute(eStr)  	
	var tm=new Date(rs("sessionStart").value)
 	tm=tm.getTime()	
	var tk=parseInt(session.token)	
	for(var x in memory){
		var id=memory[x]
		newMemory=tm-tk-id
		//var mem=String(tm-tk-id)+"."+session.sessionid
		//String(tmLast-tk-id)+"."+sessionid
		t("Response.Cookies(remember)("+x+")="+String(newMemory)+"."+session.sessionid)
		Response.Cookies("remember")(x)=String(newMemory)+"."+session.sessionid
	}
	Response.Cookies("remember").expires="Jan 2, 2020"
}else{// \\ dont see where multiPanel uses this //
	document.cookie=mStr
	var mStr="remember="  //bytetherapy@gmail.com
	var operator=""
	//memory.swimmer=83483475;memory.runner=343453453
	for(var x in memory){
		mStr+=operator+x+"="+memory[x]
		operator="&"
	}
	if(operator=="")return false
	mStr+=";expires=Thu,2 Dec 2022 2:2:22 UTC;path=/"
	//t(mStr)//document.cookie="remember=xxx;expires=Thu,2 Dec 1900 2:2:22 UTC;path=/"
}
}
function remember(id,type){
	scriptid=20359;errObj="remember"
/* 
** ALERT: onSession PARENT!!! **
bill@bytetherapy.com
1. subtract toekn from time
2. multiply by userid */
t("remembering")
eStr="select sessionStart from session where id="+session.sessionid
var rs=dc.Execute(eStr) 
//t(" remember sessionStart "+eStr+": "+rs("sessionStart").value)
var tm=new Date(rs("sessionStart").value)
var tm=tm.getTime()

var tk=parseInt(session.token)
//t("  remember session.token "+tk)
//t("   remember time "+tm)
id=parseInt(id) 
var mem=String(tm-tk-id)+"."+session.sessionid
t(" remembering mem "+mem+" for type "+type) 
//t("    remembertm-tk-id=mem")
//t("     remember"+tm+"-"+tk+"-"+id+"="+mem)
//t("      remember Response.Cookies(remember)="+mem)
Response.Cookies("remember")(type)=mem
Response.Cookies("remember").Expires="Jan 2, 2020"
return mem
}
}
function mpLogout(xObj){
	scriptid=20580;errObj="mpLogout"
xObj.panel='logout'
xObj.modal=1
multiPanel.showHide("mpDiv",1)//0 hide, 1, fullScrn, 2 at cursor
multiPanel.port(xObj)
return false
}
function mpBlock(xObj){
	scriptid=20152;errObj="mpBlock"
if(xObj){//  1 means show - 0 means hide 
	if(multiPanel.blocking)return false
	multiPanel.blocking=true
	xObj.panel='block'
	multiPanel.showHide("mpDiv",1)//0 hide, 1, fullScrn, 2 at cursor
	multiPanel.port(xObj)
}else{ 
	if(!multiPanel.blocking)return false
	multiPanel.blocking=false
	multiPanel.port({panel:"block"})   /* no props tells swf to hide it */
	multiPanel.showHide("mpDiv",0)
}
return false




var attNode=$("attNode")
var mpDiv=$("mpDiv")
if(!mpDiv){alert('NO PANEL!');return false}
if(xPOD){//  1 means show - 0 means hide 
	if(window.abortEvent&&window.abortEvent=="true")return false
	mpDiv.style.top=50    
	mpDiv.style.left=0
//	xPOD.line=1
var mpSWF=$("mpSWF")
	xPOD.panel='block'
	xStr=xPODwrite(xPOD)
	mpSWF.show(xStr)
}else{ 
	mpSWF.hide("panel|block")   
//	alert(multiPanel.hide)
	multiPanel.stow()// multiPanel.js
}  
}
function mpUpdate(xObj){
	scriptid=20157;errObj="mpUpdate"
if(!xObj){multiPanel.block();return false}
xObj.action='update'
xObj.panel='up'
//t(xPODwrite(xPOD))
multiPanel.showHide("mpDiv",2)//0 hide, 1, fullScrn, 2 at cursor
multiPanel.port(xObj) 

//if(!multiPanel){alert('NO PANEL!');return false}
/*var xObj={}
xObj.pid=arguments[0]
xPOD.action='update'
xPOD.panel='up'
xObj.callBack='upCallBack'
xObj.pname='Personal Image'
xObj.scriptid=28139
*/  
}
function mpConfirm(){
	scriptid=20224;errObj="mpConfirm"

alert("mpConfirm Im mpConfirm!")
}
function mpAttach(){
	scriptid=20313;errObj="mpAttach"
var v=147

if(Prototype.Browser.IE){
	eStr="<div id='mpDiv' style=';"+
	"position:absolute;z-index:100;top:0;left:0;width:100%;height:100%;overflow:hidden"+
	"'><object id='mpSWF' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' "+
	"style='width:100%;height:100%'>"+
	"<param name='movie' value='1/masterpod.20222."+v+".swf'/>"+
	"<param name='wmode' value='transparent'/>"+
	"<param name='quality' value='high'/>"+
	"</object></div>"
	var div=document.createElement("div")
	document.body.appendChild(div)
	div.style.position='absolute'
	div.style.zIndex='100'
	div.style.height="100%"
	div.style.top=0
	div.innerHTML=eStr
/*
CONQUERING IE'S CSS FLICK-PUSH BUG..
so when an absolute posuitioned el is attached.. ie will expand the bottom of
the browser unless outerHTML'd -in which case it jumps.

The above combnation is the only one that works.. specifically:
created and appended BEFORE styled.
'top' style '0' to anchor it to the top
lastly.. innerHTML it.
*/	
	
//	div.outerHTML=eStr
}else{
	eStr="<div id='mpDiv' style='position:absolute;z-index:100;top:0;left:0;width:100%;height:100%'>"
	eStr+="<embed id='mpSWF' name='mpSWF' wmode='transparent' quality='high' "+
	"style='width:100%;height:100%' "+
	"src='1/masterpod.20222."+v+".swf' "+
	"type='application/x-shockwave-flash' "+
	"</embed></div>"
	var div=document.createElement("div")
	div.innerHTML=eStr
	document.body.appendChild(div.firstChild)
}

}
function mpPanelComplete(xStr){
	scriptid=20163;errObj="mpPanelComplete"
// called by swf with escaped xPOD
//multiPanel.panelCount--
// \\\TODO: granularity to sup html panels
multiPanel.showHide("mpDiv",0) //bytetherapy@gmail.com mpDiv mpSWF
xObj=xPODread(unescape(xStr))			//("xStr: "+xStr)
//alert("mpPanelComplete ")
if(xObj.remember){//unused (remembered on server)
	var memory=getMemory()
	//alert("memory["+xObj.authType+"]="+xObj.remember)
	memory[xObj.authType]=xObj.remember
	//alert(xObj.authType+"="+memory[xObj.authType])
	setMemory(memory)
	//alert("xPOD.remember "+xObj.remember)
	delete xObj.remember
}
//diver@waterman.com
var callBack=xObj.callBack
//t("callBack "+callBack+"   window[callBack]  "+window[callBack])
delete xObj.callBack
//alert("callBack "+callBack)
if(window[callBack])window[callBack](xObj)
//alert("callBack "+callBack)
}
function mpPort(xPOD){
	scriptid=20343;errObj="mpPort"
if(!xPOD.panel){
	t('no  xPOD.panel - aborting')
	return false
}
//multiPanel.panelCount++ // for when we implement multiple panels
var mpSWF=mpGetPanel("mpSWF")
//mpSWF)
//var mPstyle=$("multiPanelNode").style
//mpSWF.style.top=mpSWF.style.left=20
//alert("mP.login "+mP.login+" xPODwrite(xPOD) "+xPODwrite(xPOD))

var xStr=xPODwrite(xPOD)
 
mpSWF.show(xStr) // <-- talk to the swf
mpSWF.focus()
}
function mpGetPanel(pnlName){
	scriptid=20344;errObj="mpGetPanel"
var pnl=$(pnlName)
///if(!pnl)t("no "+pnlName+" found")
return pnl
}
function mpOnload(){
	scriptid=20150;errObj="mpOnload"
multiPanel.block=mpBlock
multiPanel.login=mpLogin
multiPanel.logout=mpLogout
multiPanel.encryptlet=encryptlet
multiPanel.update=mpUpdate
multiPanel.panelComplete=mpPanelComplete
multiPanel.port=mpPort
multiPanel.showHide=mpShowHide
multiPanel.confirm=mpConfirm
//multiPanel.panelCount=0
multiPanel.shadow=4
if(document.addEventListener){
    document.addEventListener('DOMContentLoaded', mpAttach,false)
}else{// old IE - cant use attachEvent.. 
	window.occupied=window.onload
	window.onload=function(){
		if(window.occupied)window.occupied()
		mpAttach()
	} 
}
return false

/*
Event.observe(window,'load',function(){Event.observe('signinForm','submit',checkForm)})
function AddListener(obj, functionName, functionCode) {
if (obj.addEventListener) {
obj.addEventListener (functionName, functionCode, false );
} else if (obj.attachEvent) {
obj.attachEvent( "on" + functionName, functionCode);
} else {
AddEvent(obj, functionName, functionCode);
}
}
//    function AddEvent(obj, functionName, functionCode)......
AddEvent(document.documentElement, "load", body_OnLoad);
function body_OnLoad() {
//do stuff
}
//document.body.appendChild(div)
new Ajax.Request('/multiPanel.4.1.xsl',{method:'post',
onSuccess: function(transport){plateLoaded(transport)},
onFailure: function(){alert('An error occurred!..')} 
}); 
function plateLoaded(transport){
	//alert(transport.responseXML.firstChild.firstChild.nextSibling.firstChild.xml)
	//var r=transport.responseXML.firstChild.firstChild.nextSibling.nextSibling.firstChild
	var r=extract(transport.responseXML.firstChild)
	//r.className="mpConfirm"
	var div=document.createElement("div")
	div.innerHTML=r.xml
	document.body.appendChild(div)
}	
function extract(xml){
	try{
	if(xml.getAttribute("id"))return xml
	var node=xml.firstChild
	while(node){
		if(extract(node))return node
		node=node.nextSibling
	}
	}catch(e){}//xml.getAttribute!=null&&
	return false
}	
*/

}
function mpSize(pnlName,w,h){
	scriptid=20350;errObj="mpSize"
var pnl=mpGetPanel(pnlName)
w=parseInt(w)
h=parseInt(h)
pnl.style.width=w//+shadow*2//+shadow set mpOnload
pnl.style.height=h
//alert("w "+w+"    h "+h)
if(multiPanel.dragHandle){
	var hndl=multiPanel.dragHandle
	hndl.style.left=parseInt(pnl.style.left)+w-20
}

}
function mpShowHide(pnlName,mode){
	scriptid=20164;errObj="mpShowHide"
/* ARGS
0: string - id of element ("mpSWF")
1: 0 hide, 1, fullScrn, 2 at cursor
ex: mpPanelShowHide("mpSWF",1)  - show full screen
*/
//t(pnlName+" "+mode)

var pnl=mpGetPanel(pnlName)  
//for(var x in Event){alert(x+": "+Event(x))}

if(!pnl){alert("No "+pnlName+" Panel");return false}
 
if(!mode){
	pnl.style.top=pnl.style.left=-10000
	if(multiPanel.dragHandle)multiPanel.dragHandle.style.top=multiPanel.dragHandle.style.left=-10000
}else if(mode==1){
	pnl.style.top=pnl.style.left=0
	pnl.parentNode.style.width="100%"
//	pnl.parentNode.style.height=pnl.parentNode.style.width="100%"
	if(pnl.firstChild)pnl.firstChild.focus()

}else{
//	alert("Event.clientX  "+Event.clientX)
	if(Event.clientX){
		pnl.style.left=Event.clientX 
		pnl.style.top=Event.clientY
	}else{
		pnl.style.left=pnl.style.top=0
		pnl.style.left=pnl.style.top=200
	}
	var drgHndl=getDragHandle(pnl)
	if(pnl.firstChild)pnl.firstChild.focus()
}
}
function getDragHandle(pnl){
	scriptid=20352;errObj="getDragHandle"
if(multiPanel.dragHandle)return multiPanel.dragHandle
var hndl=multiPanel.dragHandle=document.createElement("img")	
var p=getPobj(pnl)

hndl.style.position="absolute"
//hndl.src="1/masterpod.20337.1.jpg"
hndl.src="1/0.png"
hndl.style.zIndex=1000
hndl.style.height=40
hndl.style.width=16
//hndl.style.left=p.x+p.w-200 - redundant to mpSize
hndl.style.top=p.y+24
document.body.appendChild(hndl)
hndl.onmousedown=function(e){
	if(!Prototype.Browser.IE)event=e
	dragPanel(event)
	if(Prototype.Browser.IE){
		document.attachEvent("onmousemove",draggingPanel) 
		document.attachEvent("onmouseup",dropPanel) 
	}else{ 
		document.addEventListener('mousemove',draggingPanel,false)
		document.addEventListener('mouseup',dropPanel,false)
	}
	return false
}


}
function dragPanel(event){
	scriptid=20353;errObj="dragPanel"
var pnl=$("mpDiv")
var p=getPobj(pnl)
multiPanel.dragOffset=[event.clientX-p.x,event.clientY-p.y]
return false
}
function draggingPanel(e){
	scriptid=20351;errObj="draggingPanel"
if(!Prototype.Browser.IE)event=e
var pnl=$("mpDiv")
hndl=multiPanel.dragHandle
var p=getPobj(pnl)
var off=multiPanel.dragOffset
pnl.style.left=p.x=event.clientX-off[0]
hndl.style.left=p.x+p.w-parseInt(hndl.style.width)
hndl.style.top=event.clientY-off[1]+24
pnl.style.top=event.clientY-off[1]
return false
}
function dropPanel(){
	scriptid=20354;errObj="dropPanel"
if(Prototype.Browser.IE){
	document.detachEvent("onmousemove",draggingPanel)
	document.detachEvent("onmouseup",dropPanel)
}else{ 
	document.removeEventListener("mousemove",draggingPanel,false)
	document.removeEventListener("mouseup",dropPanel,false)
}
return false
}
function getPobj(n){
	scriptid=20355;errObj="getPobj"
var p={}
p.w=parseInt(n.style.width)
p.h=parseInt(n.style.height)
p.x=parseInt(n.style.left)
p.y=parseInt(n.style.top)
return p
}
function encryptlet(cryptee,pivot){
	scriptid=20358;errObj="encryptlet"
var crypted=''
for(var x=0;x<cryptee.length;x++){ 
	var charInt=cryptee.charCodeAt(x)
	// knock off unprintables at start of unicode
	charInt-=33 
	// get printable at coorespnding posit from pivot
	var pivotChar=pivot.charCodeAt(x%pivot.length) 
	charInt+=parseInt(pivotChar)
	if(charInt>94)charInt-=94 // loop if past last printable (127-33)
	charInt+=33 
	crypted+=String.fromCharCode(charInt)
}	
return escape(crypted)
}
mpOnload()