/* 
required JsonService.js, dojo, profile_badge.css
These two are required near the top of the document rendered (not temp_)
*/
document.write('<div ID="temp_ROLLOVER_SHORT" style="display:none;" class="Rollover"></div>')
document.write('<div ID="temp_ROLLOVER" style="display:none;" class="Rollover"></div>')
document.write('<div ID="BADGE" class="ProfileBadge" style="display:none;"><div class=Loading>Loading Profile...</div></div>')
document.write('<form action="/profile/messages" name="sendmsg" method="post" enctype="multipart/form-date" style="margin:0px;">\n'+
							 '<input type="hidden" name="action" value="newMessage" /><input type="hidden" name="to" value="" /><input type="hidden" name="subject" value="" />\n'+
							 '<input type="hidden" name="selected" value="" /><input type="hidden" name="back_to_page" value="'+document.location+'" /></form>')
function SendMessage(PUBLIC_SUBID){
	F=document.sendmsg
	F.to.value=PUBLIC_SUBID
	F.submit()
}
// onMouseOver="RollMsg(event,'[POMSG]',1)"
API_URI_BADGE='/api/member'
API_ACTION_BADGE='getBadge'
popids="ROLLOVER"
function GetObj(id){return document.getElementById(id)}
function Badge(e,popID,timeout,leftAlign,ParentScrollId){//mouseObj,popID,timeout,leftAlign,ParentScrollId
	// set up the obj moused over as srcObj
	var srcObj;
	if (!e) var e = window.event;
	if (e.target) srcObj = e.target;
	else if (e.srcElement) srcObj = e.srcElement;
	if (srcObj.nodeType == 3) // defeat Safari bug
		srcObj = targ.parentNode;
	if(!timeout){timeout=null}
	if(!leftAlign){leftAlign=0}
	if(!ParentScrollId){ParentScrollId=null}
	KillTimeout() // kill prev badges to show (kills all under .5 sec)
	myTimeEvt=setTimeout(function(){BadgeSetup(srcObj,popID,timeout,leftAlign,ParentScrollId)},500)
}
function BadgeSetup(srcObj,popID,timeout,leftAlign,ParentScrollId){//mouseObj,popID,timeout
	newDIV='DIV_'+popID
	if(!GetObj(newDIV)){
		if(popids==""){popids=newDIV}else{popids=popids+","+newDIV}
	}
	if(timeout){myTimeEvt=setTimeout("HidePopUps()",timeout);}
	if(!leftAlign){leftAlign=0}
	if(!ParentScrollId){ParentScrollId=null}
	CreateDIV('BADGE',newDIV)
	PositionPopMsg(srcObj,newDIV,-50-leftAlign,0,ParentScrollId)
	GetBadge(popID)
	GetObj(newDIV).style.display=""
}
function GetBadge(popID){
	bdg=GetObj('DIV_'+popID);
	bdg_content=bdg.innerHTML;
	if(bdg_content.length < 80){
		jsonReq = new ecnext.JsonService({uri:API_URI_BADGE});
		var dResult = jsonReq.sendMessage({msg:{action:API_ACTION_BADGE,publicSubId:popID}});
		dResult.addCallback(function(result) {
			public_subid=result.public_subid;
			BadgeObj=GetObj('DIV_'+public_subid);
			BadgeObj.innerHTML=result.html;
			return result;
		});
		dResult.addErrback(function(error) 	 {
			alert("There was an error retrieving the data.");	return error;
		});
	}
}

function PrivateBrowse(onoff){
	jsonReq = new ecnext.JsonService({uri:'/api/member/private_browsing_mode'});
	var dResult = jsonReq.sendMessage({ msg:{action:'turn'+onoff.toLowerCase()} });
	dResult.addCallback(function(result) {
			pbm="On";if(result.mode=="on"){pbm="Off"};GetObj('pb_status').innerHTML=pbm;
	}); 
	dResult.addErrback(function(error){alert(error)}); //return error
}

// create copy of DIV for abs. pos.
function CreateDIV(srcID,popID,container){
	myDiv=GetObj(popID)
	if (!myDiv){
		if(!container){container='middle_content'} //target
		ParentObj=GetObj(container);
		SrcObj=GetObj(srcID);
		newNode = document.createElement("div");
		newNode.setAttribute("id",popID);
		newNode.innerHTML=SrcObj.innerHTML
		newNode.className=SrcObj.className;
		newNodeStyle=newNode.style
		SrcStyle=SrcObj.style
		for(x=0; x <= SrcStyle.length; x++){ //copy style vals
			var str = "newNodeStyle."+SrcStyle[x]+" = SrcObj.style."+SrcStyle[x]+";";
			try{eval(str)}
			catch(e){}
		}
		if(srcID!="BADGE"){
			newNodeStyle.width=SrcStyle.width // for IE
		}
		newNodeStyle.display=SrcStyle.display // for IE
		ParentObj.appendChild(newNode);
	}
}
function WriteRolloverDivs(){ //make sure they exist
	CreateDIV("temp_ROLLOVER_SHORT","ROLLOVER_SHORT")
	CreateDIV("temp_ROLLOVER","ROLLOVER")
}
function Pos(xLeft,yTop) {
	return function(target, eventCoords) {
		target.style.left = (eventCoords[0] +xLeft) + 'px';
		target.style.top = (eventCoords[1] +yTop) + 'px';
	};
}
function HidePopUps(timeout){
	KillTimeout();
	if(timeout){
		myTimeEvt=setTimeout("HidePopUps()",timeout);
	} else {
		idsArr=popids.split(",");
		arrLen=idsArr.length
		if(arrLen > 0){
			for (x=0; x<arrLen; x++){
				obj=GetObj(idsArr[x]);
				if(obj){
					if(obj.style.display==""){
						obj.style.display="none"
						//if(x.match("DIV_")){alert('clear data');obj.innerHTML=""}
					}
				}
			}
		}
	}
}
function SHOW(ID){GetObj(ID).className='show'}
function HIDE(ID){GetObj(ID).className='hide'}
function HidePopUp(ID){obj=GetObj(ID);
	if(obj.style.display==""){obj.style.display="none"}
}
	
ready="";
function DOMready(){ready="complete"; return ready;}
dojo.addOnLoad(function(){DOMready();WriteRolloverDivs();})

myTimeEvt=""
function RollMsg(srcObj,msg,timeout){
	KillTimeout() // kill prev rollovers to show (kills all under .5 sec)
	if(timeout){
		myTimeEvt=setTimeout(function(){RollMsgSetup(srcObj,msg,timeout)},500)
	}else{
		myTimeEvt=setTimeout(function(){RollMsgSetup(srcObj,msg)},500)
	}
}

function RollMsgSetup(srcObj,msg,timeout){
	//if(myTimeEvt != ""){clearTimeout(myTimeEvt)} // if any prev rollovers...
  if(ready=="complete"){ // needed for IE7 load issue
		RollObjName='ROLLOVER'
		if(timeout){
			RollObjName='ROLLOVER_SHORT'
			HidePopUp(RollObjName); // hide any displayed
			myTimeEvt=setTimeout("HidePopUp(RollObjName)",1000); // sets this one to hide soon
		}
		obj=GetObj(RollObjName)
		obj.innerHTML=msg
		PositionPopMsg(srcObj,RollObjName,-4,18)
		obj.style.display=""
	}
}

function HIDEstyle(ID,timeout){
	if(timeout){
		KillTimeout()
		myTimeEvt=setTimeout(function(){HIDEstyle(ID)},timeout);
	}else{
		HidePopUp(ID)
	}
}
function KillTimeout(){if(myTimeEvt != ""){clearTimeout(myTimeEvt)}}

function getAbsolutePosition(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}
function PositionPopMsg(srcObj,ID,xLeft,yTop,ParentScrollId){
	posobj=GetObj(ID)
	if(!xLeft){xLeft=0}
	if(!yTop){yTop=0}
	yOffSetScroll=0;
	if(ParentScrollId){
		yOffSetScroll=GetObj(ParentScrollId).scrollTop
	}
	// get DIV abs pos or mouse pos
	r=getAbsolutePosition(srcObj)
	LTpx=r.x;TPpx=r.y;
	if(isNaN(LTpx)){
		var coords = findCoords(event);
		posobj.style.top = coords[1] + yTop - yOffSetScroll + "px";
		posobj.style.left = coords[0] + xLeft + "px"
	} else {
		posobj.style.top = TPpx + yTop - yOffSetScroll + "px";
		posobj.style.left = LTpx + xLeft + "px"
	}
	//alert('LT:'+LTpx+' TP:'+TPpx)
}
