// JavaScript Document
//for the luxury props page.
function showHide(elm) {
	//the UL container
	elmUL=elm.parentNode.parentNode;
	for(i=0; i<elmUL.childNodes.length; i++) {//loops and hides all... except...
		if(elmUL.childNodes[i].nodeName=="LI") {//the LI Children are the content containers
			//the current LI
			elmLI=elmUL.childNodes[i];
			if(elmLI.childNodes[1]==null) {break;}
			if(elmLI.childNodes[1]==elm.parentNode.childNodes[1] && elm.parentNode.childNodes[1].style.display=="none") {//show the clicked item if not visible
				elm.parentNode.childNodes[1].style.display="inline-block";
			} else if(elmLI.childNodes[1].nodeName=="UL"){//otherwise, hide everything
				elmLI.childNodes[1].style.display="none";
			}
		}
	}
}
//checks for active session
function checkUser() {
	var html = jQuery.ajax({
	type: "POST",
	url: "http://"+location.host+"/pages/ajax.isSession.php",
	data: '',
	async: false
	}).responseText;
	//creates the login/out div
	var logDiv = document.createElement('div');
	logDiv.id = 'logOut';
	var topCenter=document.getElementById("topCenter");
	topCenter.appendChild(logDiv);
	//creates the link inside it
	if(html!="false") {//if logged in
		var a = logDiv.appendChild(document.createElement('a'));
		a.innerHTML=html+' - Log Out';
		a.onclick=sDie;
		a.href="javascript:;";
	} else {//if not logged in
		var a = logDiv.appendChild(document.createElement('a'));
		a.innerHTML='Register';
		a.href="http://www.pulsefactors.com/pages/register.php";
		//adds a mouse over to display/hide the form fields
		logDiv.onmouseover=MO_showLogin;
		logDiv.onmouseout=MO_hideLogin;
		//the login form
		logDiv.innerHTML+='<form action="'+location.href+'" method="post" name="loginOnlyForm" ID="loginOnlyForm" onsubmit="return submitLogin();">';
		var loginForm=document.getElementById("loginOnlyForm");
		loginForm.innerHTML+='<input type="submit" name="submit" value="Login" class="submitLoginOnly" >';
		var fieldsDiv = loginForm.appendChild(document.createElement('div'));
		fieldsDiv.id='fieldsDiv';
		fieldsDiv.innerHTML+='<p>Login: <input type="text" name="loginOnly" class="loginOnly" autocomplete="off" ></p>';
		fieldsDiv.innerHTML+='<p>Pass: <input type="password" name="passOnly" class="loginOnly" ></p>';
	}
	
}
function submitLogin() {
	//get the current values
	var loginOnly = document.forms['loginOnlyForm'].elements['loginOnly'].value; 
	var passOnly = document.forms['loginOnlyForm'].elements['passOnly'].value; 
	var html = jQuery.ajax({
	type: "POST",
	url: "http://"+location.host+"/pages/ajax.login.php",
	data: "login=" + loginOnly +'&pass='+passOnly,
	async: false
	}).responseText;
	if(html=='login') {//if valid login, take to mypulse
		window.location = "http://"+location.host+"/pages/myPulse.php";
	} else {//respond with invalid message
		var fieldsDiv= document.getElementById("fieldsDiv");
		invalid=document.getElementById("invalidLoginTxt");
		var p1 =document.createElement('p');
		p1.id='invalidLoginTxt';
		p1.innerHTML='Invalid '+html;
		if (invalid) {
			fieldsDiv.replaceChild(p1,invalid); 
		} else {
			fieldsDiv.appendChild(p1);
		}
	}
	return false;
}
//functions to show/hide the login at the top right
function MO_showLogin() {
	var loginOnlyFields=document.getElementById("fieldsDiv");
	loginOnlyFields.style.display = 'block';
	var logOutDiv=document.getElementById("logOut");
	logOutDiv.style.backgroundColor="#000000";
	logOutDiv.style.border="1px solid #FFFFFF";
}
function MO_hideLogin() {
	var loginOnlyFields=document.getElementById("fieldsDiv");
	loginOnlyFields.style.display = 'none';
	var logOutDiv=document.getElementById("logOut");
	logOutDiv.style.backgroundColor="transparent";
	logOutDiv.style.border="1px solid transparent";
}
addLoadEvent(checkUser);
//kills the session
function sDie() {
	var html = jQuery.ajax({
	type: "POST",
	url: "http://"+location.host+"/pages/ajax.ses_kill.php",
	data: '',
	async: false
	}).responseText;
	window.location.reload();
	//var logout= document.getElementById("logOut");
	//logout.innerHTML='<a href="http://"+location.host+"/pages/login.php">Login</a>';
}
function endFilter() {
	var html = jQuery.ajax({
	type: "POST",
	url: "ajax.ses_removeFilter.php",
	data: '',
	async: false
	}).responseText;
	//window.location.reload();
}

//emails the password to you.
function getPass() {
	var email = document.forms['loginForm'].elements['email'].value; 
	var html = jQuery.ajax({
	type: "POST",
	url: "ajax.returnPass.php",
	data: "email=" + email,
	async: false
	}).responseText;
	var feedback= document.getElementById("forgotPass");
	if(html=='Your pass has been emailed to you') {
		feedback.innerHTML=html;
	} else if(html=='That email does not exist in our database') {
		feedback.innerHTML='<p><a href="javascript:;" onclick="getPass();">Forgot your password?</a><br /><span class="smallResponseTxt">That email does not exist.</span></p>';
	} else {
		feedback.innerHTML='<p><a href="javascript:;" onclick="getPass();">Forgot your password?</a><br /><span class="smallResponseTxt">Please enter an email.</span></p>';
	}
}
//displays the POM
function myPOMpop(ID,pomORtip) {
	var html = jQuery.ajax({
	type: "POST",
	url: "ajax.getPOM.php",
	data: "ID=" + ID + "&pomORtip=" + pomORtip,
	async: false
	}).responseText;
	bodyH=document.body.offsetHeight;
	//scales the blackBackground/container to fill the entire page.
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer")
	blackBack.style.display="block";
	popContainer.style.display="block";
	//middle of the screen, unless image > screen height
	var textCenter=document.getElementById("textCenter");
	if(pomORtip=='POM') {
		textCenter.style.width="800px"; 
	} else {
		textCenter.style.width="400px"; 
	}
	//writes content
	textCenter.innerHTML=html;
	//current screen position
	var pageScroll=getPageScroll();
	if(bodyH>textCenter.offsetHeight+20) {
		var centerNothing=(bodyH-textCenter.offsetHeight)/2;
		textCenter.style.top=pageScroll[1]+centerNothing+"px"; 
	} else {
		textCenter.style.top=pageScroll[1]+20+"px"; 
	}
	//Finds the height of the content container
	var container = document.getElementById("container");
	var contY = container.offsetHeight;
	//resizes the two DIV's based on the container size + menu elements (134px)
	popContainer.style.height=contY+134+"px";
	blackBack.style.height=contY+134+"px";
	//adds the closeX
	var imgX = new Image();
	imgX.onload = function() {
		textCenter.innerHTML+='<img src="'+imgX.src+'" id="closeX" name="closeX" />';
	}
	imgX.src = "http://"+location.host+"/img/closebox.png";
}
// multiple onloads
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      func();
	  if (oldonload) {
        oldonload();
      }
    }
  }
}

//scales the side bar
function scaleContent() {
	var leftMenu= document.getElementById("leftMenu");
	var container = document.getElementById("container");
	if(leftMenu) {
		var contX = container.offsetHeight;
		var lmX=leftMenu.offsetHeight;
		if(lmX < contX) {
			leftMenu.style.height=contX+"px";
		}
	}
	//just for the myPulse Page
	var POM_box= document.getElementById("POM_box");
	if(POM_box) {
		var tipsBox = document.getElementById("tip_box");
		POM_box.style.height="auto";
		tipsBox.style.height="auto";
		if(POM_box) {
			//alert(tipsBox.firstChild.offsetHeight);
			var pomY = POM_box.offsetHeight;
			var tipsY = tipsBox.offsetHeight;
			if(pomY>tipsY) {
				tipsBox.style.height=pomY+"px";
			} else if (tipsY>pomY){
				POM_box.style.height=tipsY+"px";
			}
		}
	}
}

/* checks the link just clicked to see if it's the current page... if so, change the ID=self */
function linkCurrentPage() { 
	if (!document.getElementsByTagName) return;  
		 //oDiv=document.getElementById('divName') //if I want to limit it to a specific div
		 var links = document.getElementsByTagName("a"); 
		 var thispage = location.href; 
		 var thispage=thispage.substring(thispage.lastIndexOf("\/")+1, thispage.length); 
		 for (var i=0; i<links.length; i++) {  
		 var link = links[i]; 
		 thisHREF = link.getAttribute("href"); 
		if (thisHREF == thispage) { 
			 link.id = "self"; 
			 return; 
		 }
	 }  
} 
addLoadEvent(scaleContent);
addLoadEvent(linkCurrentPage);

//feedback function
function feedback(location) {
	var footer= document.getElementById("footer");
	footer.innerHTML+='<span id="split">|</span><a href="http://www.pulsefactors.com/pages/feedback.php?link='+location+'">Submit feedback</a>';
}
addLoadEvent(function() {
		feedback(location.href);
});  

function hide(elmnt){
	linkName=elmnt.parentNode;
	hiddenLink=linkName.lastChild;
	if (linkName.title=='active') {
		hiddenLink.style.display = 'none';
	}
}
function showSub(elm){
	/*linkName=elmnt.parentNode;
	if (linkName.title=='active') {
		var current=true;
	}
	hiddenLink=linkName.childNodes[linkName.childNodes.length-1];
	divChilds=linkName.parentNode;
	
	for (i=0; i<divChilds.childNodes.length; i++) {
		thisNode=divChilds.childNodes[i];
		if (thisNode.title=='active') {
			thisNode.childNodes[thisNode.childNodes.length-1].style.display = 'none';
			thisNode.firstChild.style.color='#bbc379';
			thisNode.title='';
		}
	}
	if (!current) {
		linkName.title='active';
		hiddenLink.style.display = 'block';
		elmnt.style.color='#8db9c6';
		scaleContent();
	}*/
	var container=elm.parentNode.parentNode;
	for(i=0; i<container.childNodes.length; i++) {//loops and hides all... except...
		if(container.childNodes[i].nodeName=="DIV") {//the LI Children are the content containers
			if(container.childNodes[i].childNodes[1]==elm.parentNode.childNodes[1] && elm.parentNode.childNodes[1].style.display=="none") {//show the clicked item if not visible
				elm.parentNode.childNodes[1].style.display="inline-block";
			} else {//otherwise, hide everything
				container.childNodes[i].childNodes[1].style.display="none";
			}
		}
	}
}
function showHome(elm){
	if(elmnt.parentNode.nodeName=='P') {
		elmnt.parentNode.title='active';
	} else if (elmnt.parentNode.nodeName=='SPAN') {
		elmnt.parentNode.parentNode.title='active';
	}
	var linksCol= document.getElementById("col1");
	for (i=0; i<linksCol.childNodes.length; i++) {
		var thisNode=linksCol.childNodes[i];
		if(thisNode.nodeName=='P') {
			for (j=0; j<thisNode.childNodes.length; j++) {
				var spans=thisNode.childNodes[j];
				if(spans.id=='hiddenCopy') {
					if(spans.parentNode.title=='active') {
					spans.style.display = 'block';
					}
				}
				if(spans.id=='startCopy') {
					spans.style.display = 'none';
				}
				
			}
		}
	}
}

//determines the scoll position... used for placing popup images
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
//shows the video popup
function videoPop(videoName,size,basePath) {
	//size must be lowercase
	size=size.toLowerCase(); 
	videoName=videoName.parentNode.parentNode.id;
	//scales the blackBackground/container to fill the entire page.
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer")
	blackBack.style.display="block";
	popContainer.style.display="block";
	//adds the video container... to be replaced by swf embedd
	var popCenter=document.getElementById("popCenter");
	popCenter.innerHTML+='<div id="videoContainer"></div>';
	//Finds the height of the content container
	var container = document.getElementById("container");
	var contY = container.offsetHeight;
	//resizes the two DIV's based on the container size + menu elements (134px)
	popContainer.style.height=contY+134+"px";
	blackBack.style.height=contY+134+"px";
	
	//decides the size
	if(size=='sm'){vWidth=420; vHeight=236;}
	if(size=='med'){vWidth=656; vHeight=376;}
	if(size=='lrg'){vWidth=720; vHeight=405;}
	//scales center/white border based on size
	var popCenter=document.getElementById("popCenter");
	popCenter.style.width=vWidth+"px";
	popCenter.style.height=vHeight+"px";
	var imgX = new Image();
	imgX.onload = function() {
		popCenter.innerHTML+='<img src="'+imgX.src+'" id="closeX" name="closeX" />';
	
	
	//middle of the screen, unless image > screen height
	var pageScroll =getPageScroll();
	bodyH=document.body.offsetHeight;
	if(bodyH>vHeight+20) {
		var centerPic=(bodyH-vHeight)/2;
		popCenter.style.top=pageScroll[1]+centerPic+"px"; 
	} else {
		popCenter.style.top=pageScroll[1]+"px"; 
	}
	
	//sets/inserts the video
	//inits
	var flashvars = {
		MM_ComponentVersion: "1",
		skinName: basePath+"img/video/Clear_Skin_2",
		autoPlay: "true",
		autoRewind: "true",
		streamName: size+'/'+videoName
	};
	//var flashvars = false;
	var params = {
		scale: "noscale",
		salign: "tl"
	};	
	var attributes = {
		id: "Vid",
		name: "Vid"
	};
	//flashvars.streamName = size+'/'+videoName;
	swfobject.embedSWF(basePath+"img/video/FLVPlayer_Progressive.swf", "videoContainer", vWidth, vHeight, "9.0.0", false, flashvars, params, attributes);
	}
	imgX.src = "http://www.pulsefactors.com/img/closebox.png";
}
//shows the still popup images
function showFull(args,movie) {
	bodyH=document.body.offsetHeight;
	//scales the blackBackground/container to fill the entire page.
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer")
	blackBack.style.display="block";
	popContainer.style.display="block";
	//middle of the screen, unless image > screen height
	var popCenter=document.getElementById("popCenter")
	//current screen position
	var pageScroll=getPageScroll();
	if(bodyH>popCenter.offsetHeight+20) {
		var centerNothing=(bodyH-popCenter.offsetHeight)/2;
		popCenter.style.top=pageScroll[1]+centerNothing+"px"; 
	} else {
		popCenter.style.top=pageScroll[1]+"px"; 
	}
	//Finds the height of the content container
	var container = document.getElementById("container");
	var contY = container.offsetHeight;
	//resizes the two DIV's based on the container size + menu elements (134px)
	popContainer.style.height=contY+134+"px";
	blackBack.style.height=contY+134+"px";
	//if arguments are supplied by the function, add images, add loading gif
	if (args) {//finds just the name of the image thumbnail that was clicked
		if(args.nodeName!="IMG" && args.parentNode.id!='mapLinks') {//if the element isn't an image then we can't pull it's source... Find the image!
			var thisPath=args.firstChild.src.substring(0,args.firstChild.src.lastIndexOf("\/")-4);
			var thisImg=args.firstChild.src.substring(args.firstChild.src.lastIndexOf("\/")+1);
		} else if (args.src) {
			var thisPath=args.src.substring(0,args.src.lastIndexOf("\/")-4);
			var thisImg=args.src.substring(args.src.lastIndexOf("\/")+1);
		}
		
		//the element that is centered... Holds the popup image
		//var popCenter=document.getElementById("popCenter");
		//new image element
		var img = new Image();
		var imgX = new Image();
		var imgBlocker = new Image();
		//when image is loaded
		img.onload = function() {
			
			//writes the image once it's loaded
			popCenter.innerHTML='<img src="'+img.src+'" id="popImg" name="popImg" />';
			//adds a border if this is the graph pages... if not graph pages, leave border at 0.
			var xtraBorder=0;
			if(args.parentNode.id=='graphContainer') {
				var popImg=document.getElementById("popImg");
				popImg.style.border='15px solid #000000';
				var xtraBorder=30;
			}
			//finds the demensions
			popCenter.style.width=img.width+xtraBorder+"px";
			popCenter.style.height=img.height+xtraBorder+"px";
			//middle of the screen, unless image > screen height
			if(bodyH>img.height+20) {
				var centerPic=(bodyH-img.height)/2;
				popCenter.style.top=pageScroll[1]+centerPic+"px"; 
			} else {
				popCenter.style.top=pageScroll[1]+"px"; 
			}
			//adds the closeX
			imgX.onload = function() {
				popCenter.innerHTML+='<img src="'+imgX.src+'" id="closeX" name="closeX" />';
			}
			imgX.src = "http://"+location.host+"/img/closebox.png";
			//puts up a transparant gif over the pop'd up image... hiding it.
			imgBlocker.onload = function() {
				popCenter.innerHTML+='<img src="'+imgBlocker.src+'" id="imgBlocker" name="imgBlocker" width="'+img.width+'" height="'+img.height+'" />';
			}
			imgBlocker.src = "http://"+location.host+"/img/spacer.gif";
		};
		//writes the loading gif
		popCenter.innerHTML='<img src="http://www.pulsefactors.com/img/loading.gif" id="loading" name="loading" />';
		var popLoading=document.getElementById("loading");
		popLoading.style.marginTop=popCenter.offsetHeight/2+"px";
		if(args.parentNode.id=='neighborhoodPics' || args.parentNode.id=='wideCol') {//If the image clicked was in the neighborhood page
			img.src = "../../img/Neighborhoods/full/"+thisImg+"";
		} else if(args.parentNode.id=='topDetail') { //else if the image was on the properties detail page
			img.src = "../img/properties/lrg/"+thisImg+"";
		} else if(args.parentNode.id=='images') { //else if the image was on the properties detail, paid category
			img.src = thisPath+'/lrg/'+thisImg;
		} else if (args.parentNode.id=='graphContainer') {//graphs page
			img.src = "../../img/expertAdvice/graphs/lrg/"+thisImg+"";
		} else if (args.parentNode.id=='mapLinks') {//sf map
			img.src='../img/properties/PulseFactors_SF_Final.gif';	
		}
		
		
	} /*else { // if the image is already placed on the page, ignore preloading and just display the popup
		var popCenter=document.getElementById("popCenter");
		var popImg=document.getElementById("popImg");
		popCenter.style.width=popImg.offsetWidth+"px";
		popCenter.style.height=popImg.offsetHeight+"px";
		//40px from the top of the current view screen
		popCenter.style.top=pageScroll[1]+40+"px"; 
	}*/
}
//hides the popup image element
function hideFull() {
	var blackBack=document.getElementById("blackBack");
	var popContainer=document.getElementById("popContainer");
	var popCenter=document.getElementById("popCenter");
	blackBack.style.display="none";
	popContainer.style.display="none";
	video=document.getElementById('Vid');
	if(video) {popCenter.innerHTML='<div id="videoContainer"></div>';}
}
//applies the onclick functions to the videos page
function addVidLinks(baseDiv, basePath) {
	//body container
	var wideCol= document.getElementById(baseDiv);
	//loops all it's children
	for(i=0; i<wideCol.childNodes.length; i++) {
		//only accepts div's with an ID
		var mainMovieDiv=wideCol.childNodes[i];
		if(mainMovieDiv.nodeName=='DIV' && mainMovieDiv.id) {
			//loops the movie div's children
			for (j=0; j<mainMovieDiv.childNodes.length; j++) {
				var divSizes=mainMovieDiv.childNodes[j];
				//only accepts a div... class="vidSizes"
				if(divSizes.nodeName=='DIV') {
					for (h=0; h<divSizes.childNodes.length; h++) {
						var mooVlinks=divSizes.childNodes[h];
						if(mooVlinks.nodeName=='A') {
							if(mooVlinks.firstChild.nodeName=="IMG") {
								mooVlinks.onclick=function() {
									videoPop(this,'med',basePath);
								}
							} else if (mooVlinks.firstChild.nodeValue) {
								mooVlinks.onclick=function() {
									videoPop(this,this.firstChild.nodeValue,basePath);
								}
							}
						}
					}
				}
			}
		}
	}
}