// Pre-Load Images

Image1= new Image(143,24);
Image1.src = "images/saved_search_button_on.gif";

Image2= new Image(31,32);
Image2.src = "images/video_arrow_left_on.gif";

Image3= new Image(31,32);
Image3.src = "images/video_arrow_right_on.gif";

Image4= new Image(213,68);
Image4.src = "images/faq_selector_arrow.gif";

Image5= new Image(16,16);
Image5.src = "images/icon_plus_on.gif";

Image6= new Image(16,16);
Image6.src = "images/icon_minus.gif";

Image7= new Image(16,16);
Image7.src = "images/icon_minus_on.gif";

// Find Foreclosure Setup

window.onload = findSetup;

function findSetup() {
	var c = Math.floor(Math.random()*11);
	if ( document.getElementById('last-update')) {
		document.getElementById('last-update').innerHTML = c;
	}
	if ( document.getElementById('finder') ) {
		document.getElementById('finder').focus();
	}
}

// FAQ Selector

var l = new Array('foreclosures', 'rent-to-own', 'membership');
var n = new Array('faq-fc', 'faq-rto', 'faq-member');
var m=6;
var intFAQ;
var fader;

function swap_faq(obj, faq) {
	for (i = 0; i < l.length; i++) {
		document.getElementById(l[i]).style.backgroundImage = "url(images/separator_lg.gif)";
		document.getElementById(l[i]).style.backgroundPosition = "bottom";
		document.getElementById(l[i]).style.color = "#303742";
		document.getElementById(l[i]).style.fontSize = "14px";
		document.getElementById(l[i]).style.cursor = "pointer";
	}
	for (i = 0; i < n.length; i++) {
		document.getElementById(n[i]).style.display = "none";
		document.getElementById(n[i]).style.opacity = .05;
	}
	document.getElementById(obj).style.backgroundImage = "url(images/faq_selector_arrow.png)";
	document.getElementById(obj).style.color = "#ffffff";
	document.getElementById(obj).style.fontSize = "17px";
	document.getElementById(obj).style.cursor = "default";
	fader = faq;
	clearInterval(intFAQ);
	m=6;
	fadeFAQ();
}

function setOpacity() {
	if (m<=100) {
		document.getElementById(fader).style.opacity = m / 100;
		m=m*1.1;	
	}
	else {
		document.getElementById(fader).style.opacity = 1;
		clearInterval(intFAQ);
	}	
}

function fadeFAQ() {
	intFAQ=setInterval("setOpacity()",20);
	document.getElementById(fader).style.display = "block";
}

// FAQ Expander

function show_question(obj) {
	if (obj.className == "off") {
		obj.className = "on";
	}
	else {
		obj.className = "off";
	}
}

// Toggle Map & Listings View

function toggle_map(obj, site) {
	document.getElementById('map-on').style.display = "none";
	document.getElementById('list-on').style.display = "none";
	document.getElementById('fh-map').style.display = "none";
	document.getElementById('fh-list').style.display = "none";
	document.getElementById(obj).style.display = "block";
	document.getElementById(site).style.display = "block";

}

// Button Toggle

function toggle_button(obj) {
	var a = document.getElementById(obj);
	if(a.style.display == 'block') {
		a.style.display = 'none';
	}
	else {
		a.style.display = 'block';
	}
}

// Saved Search Drop Down

var savedSearch = {
	fetched: false,	
	e: 0,
	f: 0,
    activate: function() {
    	savedSearch.f = 1;
    	dojo.byId('saved-searches-button').style.backgroundImage = "url(images/saved_search_button_on.gif)";
    	savedSearch.show();
    },	
	show: function() {
	    var ss = savedSearch;
    	if (ss.f == 1) {
    		ss.e = 1;
    		if( !ss.fetched ) {
    			ss.fetched = true;
    			ss.doFetch();
    		}

    		dojo.byId('saved-searches').style.display = "block";		
    	}
    },
	needsFetch:function () {
	  fetched = false;  
	},
	doFetch: function() {
		args = {
			url : "/web/savedSearches/brief.do",
			content: {
			    no_cache: new Date().getTime()
			},
			load : function(data, xhr) {
				var savedSearchesDiv = dojo.byId('saved-searches');
				savedSearchesDiv.innerHTML = data;
				var count = dojo.query("#saved-searches li.saved_search").length;
				dojo.query("#saved-searches-button h6")[0].innerHTML = count;
				setTimeout('hide_ss()', 5000);
			},
			error : function(a, b) {
				setTimeout('hide_ss()', 5000);
				showError(a, b, '[10]');
			}
		};
		dojo.xhrPost(args);
	},
	onmouseover: function(evt) {
		dojo.query(".saved-search.selected").removeClass("selected");
		console.dir(evt.target);
		evt.target.className = "saved-search selected";
	},
	select : function(id) {
		window.location = "/web/search.do?savedSearchId="+id;
	},
	deleteSearch : function(search_id) {
	    savedSearch.fetched = false;
		var args = {
				url : '/web/ajax/savedSearches/delete.do',
			content : {id: search_id},
			load : function(){
				savedSearch.doFetch();
			},
			handleAs : "json-comment-filtered",
			error : function(a, b) {
				if ( console && console.log) {
					console.log(a + b + '[158]');
				}
			}
		};
		var requestObj = dojo.xhrPost(args);
	}
};

window.activate_ss = savedSearch.activate;



window.show_ss = savedSearch.show;

window.hide_ss = function() {
	if (savedSearch.e == 0) {
		dojo.byId('saved-searches').style.display = "none";
		dojo.byId('saved-searches-button').style.backgroundImage = "url(images/saved_search_button.gif)";
		savedSearch.f = 0;
	}
}


window.toggle_ss_off = function() {
	savedSearch.e = 0;
	setTimeout('hide_ss()', 2000);
}

// Video Scroller

var p=0;
var w=0;
var h=0;
var q=7;
var intShow;
var intHide;

function right() {
        //alert('p:'+p+',h:'+h+',w:'+h);
	if (p==h && w==0) {
		clearInterval(intShow);
		h=600;
		w=1;
		intShow=setInterval("show()",10);
            //alert('1:: p:'+p+',h:'+h+',w:'+h);
	}
	else if (p==h && w==1) {
            return;
		/*clearInterval(intShow);
		w=2;
		h=h+600;
		intShow=setInterval("show()",10);*/
            //alert('2:: p:'+p+',h:'+h+',w:'+h);
	}

}

function show() {
	if (p<h) {
		p=p+q;
		document.getElementById('videos').style.left = (-(p))+"px";
	}
	else {
		clearInterval(intShow);
		document.getElementById('videos').style.left = (-(h))+"px";
		p=h;
	}
}

function left() {
	if (p==h && w==1) {
		clearInterval(intShow);
		w=0;
		h=0;
		intHide=setInterval("hide()",10);
	}
	if (p==h && w==2) {
		clearInterval(intShow);
		w=1;
		h=h-600;
		intHide=setInterval("hide()",10);
	}
}

function hide() {
	if (p>h) {
		p=p-q;
		document.getElementById('videos').style.left = (-(p))+"px";
	}
	else {
		clearInterval(intHide);
		document.getElementById('videos').style.left = (-(h))+"px";
		p=h;
	}
}

// Forums Accordion

var s=5;
var t=25;

function $(d){
	return document.getElementById(d);
}

function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

function sh(d,v){
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.height=v;
	}
}

function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}

function et(d){
	d = $(d);
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
	}
}

function cl(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}

function ex(d){
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
	}
}

function cc(n,v){
	s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
}

function Accordian(d,s,tc){
	l=$(d).getElementsByTagName('div');
	c=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){c.push(h);}
	}
	sel=null;
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			d=$(h.substr(0,h.indexOf('-'))+'-content');
			d.style.display='none';
			d.style.overflow='hidden';
			d.maxh =sh(d);
			d.s=(s==undefined)? 7 : s;
			h=$(h);
			h.tc=tc;
			h.c=c;
			h.onclick = function(){
				for(i=0;i<this.c.length;i++){
					cn=this.c[i];
					n=cn.substr(0,cn.indexOf('-'));
					if((n+'-header')==this.id){
						ex($(n+'-content'));
						n=$(n+'-header');
						cc(n,'__');
						n.className=n.className+' '+n.tc;
					}else{
						cl($(n+'-content'));
						cc($(n+'-header'),'');
					}
				}
			}
			if(h.className.match(/selected+/)!=undefined){ sel=h;}
		}
	}
	if(sel!=undefined){sel.onclick();}
}

// Message Modal

var modal={};

function T$(i){return document.getElementById(i)}

modal.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='modalbox';
				m=document.createElement('div'); m.id='modalmask';
				b=document.createElement('div'); b.id='modalcontent';
				document.body.appendChild(m); document.body.appendChild(p); p.appendChild(b);
				m.onclick=modal.box.hide; window.onresize=modal.box.resize; f=1
			}
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if(t){setTimeout(function(){modal.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){modal.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h,4)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			modal.box.alpha(p,-1,0,3)
		},
		resize:function(){
			modal.box.pos(); modal.box.mask()
		},
		mask:function(){
			m.style.height=modal.page.theight()+'px';
			m.style.width=''; m.style.width=modal.page.twidth()+'px'
		},
		pos:function(){
			var t=(modal.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+modal.page.top())+'px';
			p.style.left=(modal.page.width()/2)-(p.offsetWidth/2)+'px'
		},
		alpha:function(e,d,a,s){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){modal.box.twalpha(e,a,d,s)},20)
		},
		twalpha:function(e,a,d,s){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?modal.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100,5):modal.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=o+Math.ceil(Math.abs(a-o)/s)*d;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h,s){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?-1:1, hd=(oh-ho>h)?-1:1;
			e.si=setInterval(function(){modal.box.twsize(e,w,wo,wd,h,ho,hd,s)},20)
		},
		twsize:function(e,w,wo,wd,h,ho,hd,s){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){e.style.width=ow+(Math.ceil(Math.abs(w-ow)/s)*wd)+'px'}
				if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px'}
				this.pos()
			}
		}
	}
}();

modal.page=function(){
	return{
		top:function(){return document.body.scrollTop||document.documentElement.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight},
		theight:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight))
		},
		twidth:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();

// Mortgage Calculator

var j = 350;
var intSlide;

function calculate() { 
	form = document.calc; 
	LoanAmount = form.LoanAmount.value;
	DownPayment = 0;
	AnnualInterestRate = form.InterestRate.value/100;
	Years = form.NumberOfYears.value;
	MonthRate = AnnualInterestRate/12; 
	NumPayments = Years*12;
	Prin = LoanAmount-DownPayment;
	MonthPayment = Math.floor((Prin*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100;
	document.getElementById('NumberOfPayments').innerHTML = NumPayments;
	document.getElementById('MonthlyPayment').innerHTML = '$' + MonthPayment;	
	if (j<450) {
		intSlide=setInterval("slide()",10);
	}
}

function slide () {
	if (j<450) {
		j=j+5;
		document.getElementById('na-rates').style.height = j + 'px';
	}
	else {
		clearInterval(intSlide);
		document.getElementById('na-rates').style.height = 450 +'px';
	}
}