var rsProperty = new Array();

var isLoadingPG = false;
var isLoadingResults = false;
var isLoadingBubbles = false;

var preBBL = 1;

function setResults(xml, pgTo) {
	if (xml.readyState == 4 || xml.readyState == "complete") {
		if (xml.responseText.length > 0) {
			var rsResults = eval(xml.responseText);
			buildMe(rsResults, pgTo);
		}
	}
}

function getResults(url, pgTo) {
	isLoadingResults = true;
	
	if (window.addEventListener) {
		var xml = GetXmlHttpObject(function() {
			xml.onreadystatechange = setResults(xml, pgTo);
		});
	}
	else {
		var xml = GetXmlHttpObject(function() {
			xml.onreadystatechange = function () {
				if (xml.readyState == 4 || xml.readyState == "complete") {
					if (xml.responseText.length > 0) {
						var rsResults = eval(xml.responseText);
						buildMe(rsResults, pgTo);
					}
				}
			}
		});
	}

	var sVars = '';

	xml.open ("POST", url , true);
	xml.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	xml.send (sVars);
}

function bblMe() {
	if (isLoadingBubbles == false) {
		//hiLight(this);
		//showBBL(this, '/pop/_property.asp?ID='+ this.title, 2, '', null, null, 1);
	}
}

function bblMeTopLine() {
	if (isLoadingBubbles == false) {
		//hiLight(this);
		//showBBL(this, '/pop/_property.asp?ID='+ this.title, 1, 'pink', null, null, 1);
	}
}

function bblMeBtmLine() {
	if (isLoadingBubbles == false) {
		//hiLight(this);
		//showBBL(this, '/pop/_property.asp?ID='+ this.title, 2, 'blue', null, null, 1);
	}
}

function doNextBBL(bblPG, bblIndex, rsResults) {
	isLoadingBubbles = true;
	
	if (rsResults[bblIndex]) {
		if (doNewStyle) {
			if (bblIndex < 6) {
				var bblColor = 'pink';
				var bblDir = 1;
			}
			else {
				var bblColor = 'blue';
				var bblDir = 2;
			}
		}
		else {
			var bblColor = '';
			var bblDir = 2;
		}
		
		var nxtBBL = buildBBL('lst'+ bblPG +'-'+ (bblIndex+1) +'Bubble', '/pop/_property.asp?ID='+ rsResults[bblIndex][0], bblDir, bblColor);
	}
	
	if ((bblIndex+1) < rsResults.length) {
		doNextBBL(bblPG, (bblIndex+1), rsResults);
	}
	else {
		isLoadingBubbles = false;
	}
}

function buildMe(rsResults, pgTo) {
	if (rsResults.length > 0) {
		var ul = document.createElement('ul');
		ul.id = 'pg'+ pgTo +'List';
		ul.className = 'listing listing-wide hideMe';

		if (pgVarsJS.length == 0) {
			var pgVarsMe = '?pg='+ pgTo;
		}
		else {
			var pgVarsMe = pgVarsJS +'&pg='+ pgTo;
		}

		for (i=0;i<rsResults.length;i++) {
			var pID = rsResults[i][0];
			var pTown = rsResults[i][1];
			var pThumb = rsResults[i][2];
			var pPrice = rsResults[i][3];
				
			var li = document.createElement('li');
			li.id = 'lst'+ pgTo +'-'+ (i+1);
			li.title = pID;
			
			if (doNewStyle) {
				if (i < 6) {
					li.onmouseover = bblMeTopLine;
				}
				else {
					li.onmouseover = bblMeBtmLine;
				}
			}
			else {
				li.onmouseover = bblMe;
			}
			
			//li.onmouseover = bblMe;
		
			var a = document.createElement('a');
			a.href = '/detail.asp'+ pgVarsMe +'&ID='+ pID;
			a.title = 'View Property';
			
			var s1 = document.createElement('span');
			s1.className = 'town';
			s1.innerHTML = pTown;
			a.appendChild(s1);
			
			var s2 = document.createElement('span');
			s2.className = 'photo';
			s2.style.backgroundImage = "url('http://pcms.davidbehan.com/_images/"+ sClientFolder +"/tn/"+ pThumb +"')";
			a.appendChild(s2);
			
			var s3 = document.createElement('span');
			s3.className = 'price';

			s3.innerHTML = pPrice;
			a.appendChild(s3);
			
			li.appendChild(a);
			ul.appendChild(li);
		}

		if (document.getElementById('wrapper')) {
			document.getElementById('wrapper').appendChild(ul);
		}
	}
	
	isLoadingResults = false;
	
	//if (navigator.appVersion.toLowerCase().indexOf('msie') == -1) {
	//	doNextBBL(pgTo, 0, rsResults);
	//}
}

function fadeIn(i, max, dir) {
	var timerInt = 20;
	
	var percent = 0;
	if (navigator.appVersion.toLowerCase().indexOf('msie') >= 0) {
		var animateMe = 100;
	}
	else {
		var animateMe = 40;
	}

	if (dir == null) {
		var nxtIndex = i+1;
	}
	else {
		var nxtIndex = i-1;
	}
	
	var timer = window.setInterval(function() {
		percent += animateMe;

		rsProperty[i].style.filter = 'alpha(opacity='+ percent +')';
		rsProperty[i].style.opacity = (percent/100);
		rsProperty[i].style.mozOpacity = (percent/100);
		
		rsProperty[i].className = '';

		if (percent >= 100) {
			window.clearInterval(timer);
			
			var proceed = false;
			
			if (dir == null) {
				if (nxtIndex < max) {
					proceed = true;
				}				
			}
			else {
				if (nxtIndex >= 0) {
					proceed = true;
				}
			}
			
			if (proceed) {
				fadeIn(nxtIndex, max, dir);
			}
			else {
				slideOut(pgBubble);				
				isLoadingPG = false;
			}
		}
	}, timerInt);
}

function fadeOut(i, max, dir, pgTo, pix) {
	var timerInt = 20;
	
	var percent = 100;
	if (navigator.appVersion.toLowerCase().indexOf('msie') >= 0) {
		var animateMe = 100;
	}
	else {
		var animateMe = 40;
	}

	if (dir == null) {
		var nxtIndex = i-1;
	}
	else {
		var nxtIndex = i+1;	
	}
		
	var timer = window.setInterval(function() {
		percent -= animateMe;

		rsProperty[i].style.filter = 'alpha(opacity='+ percent +')';
		rsProperty[i].style.opacity = (percent/100);
		rsProperty[i].style.mozOpacity = (percent/100);
		
		if (percent <= 0) {
			window.clearInterval(timer);
			rsProperty[i].className = 'hideMe';
			
			var proceed = false;
			
			if (dir == null) {
				if (nxtIndex >= 0) {
					proceed = true;
				}
			}
			else {
				if (nxtIndex < max) {
					proceed = true;
				}
			}
			
			if (proceed) {
				fadeOut(nxtIndex, max, dir, pgTo, pix);
			}
			else {
				var timer2 = window.setInterval(function() {
					if (!isLoadingResults) {
						window.clearInterval(timer2);
						
						var oblUL = document.getElementById('pg'+ pgTo +'List');
						var objLI = oblUL.getElementsByTagName('li');

						rsProperty = new Array();

						for (i=0;i<objLI.length;i++) {
							objLI[i].style.filter = 'alpha(opacity=0)';
							objLI[i].style.opacity = 0;
							objLI[i].style.mozOpacity = 0;

							rsProperty[rsProperty.length] = objLI[i];
						}

						oblUL.className = oblUL.className.replace('hideMe', '');

						oblUL.style.height = document.getElementById('pg'+ pgNum +'List').offsetHeight +'px';
						oblUL.style.width = document.getElementById('pg'+ pgNum +'List').offsetWidth +'px';

						if (pix == null) {
							document.getElementById('pg'+ pgNum +'List').parentNode.removeChild(document.getElementById('pg'+ pgNum +'List'));
							for (i=1;i<13;i++) {
								if (document.getElementById('lst'+ pgNum +'-'+ i +'Bubble')) {
									document.getElementById('lst'+ pgNum +'-'+ i +'Bubble').parentNode.removeChild(document.getElementById('lst'+ pgNum +'-'+ i +'Bubble'));
								}
							}
						}
						else {
							document.getElementById('slide'+ pgNum).className = 'hideMe';
							document.getElementById('slide'+ pgTo).className = 'section';
						}

						pgNum = pgTo;

						if (dir == null) {
							var sIndex = 0;
						}
						else {
							var sIndex = rsProperty.length-1;
						}

						fadeIn(sIndex, rsProperty.length, dir);
					}
				}, 50);
			}
		}
	}, timerInt);
}

function doPrevPG(obj, pix) {
	if (pix == null) {
		if ((pgNum-1) > 0) {
			return doLoad(document.getElementById('pg'+ (pgNum-1)), '/pop/_loading.asp?pg='+ (pgNum-1), (pgNum-1));
		}
		else {
			//showBBL(obj, '/pop/_loading.asp?pg=none', 1, 'blue', 1, 1);
			return false;
		}
	}
	else {
		if ((pgNum-1) > 0) {
			return doLoadPix(document.getElementById('pg'+ (pgNum-1)), (pgNum-1));
		}
		else {
			return doLoadPix(document.getElementById('pg'+ numPG), numPG);
			//return false;
		}
	}
}

function doNextPG(obj, pix) {
	if (pix == null) {
		if ((pgNum+1) <= numPG) {
			return doLoad(document.getElementById('pg'+ (pgNum+1)), '/pop/_loading.asp?pg='+ (pgNum+1), (pgNum+1));
		}
		else {
			//showBBL(obj, '/pop/_loading.asp?pg=none', 1, 'blue', 1, 1);
			return false;
		}
	}
	else {
		if ((pgNum+1) <= numPG) {
			return doLoadPix(document.getElementById('pg'+ (pgNum+1)), (pgNum+1));
		}
		else {
			return doLoadPix(document.getElementById('pg1'), 1);
			//return false;
		}
	}
}

function doLoad(obj, bblURL, pgTo, dir) {
	if (!isLoadingPG && pgNum != pgTo) {
		isLoadingPG = true;
		
		var oblUL = document.getElementById('pages');
		var objLI = oblUL.getElementsByTagName('a');
		
		for (i=0;i<objLI.length;i++) {
			if (objLI[i].id == 'pg'+ pgTo) {
				objLI[i].className = 'on';
			}
			else {
				objLI[i].className = '';
			}
		}
		
		oblUL = document.getElementById('pg'+ pgNum +'List');
		objLI = oblUL.getElementsByTagName('li');

		for (i=0;i<objLI.length;i++) {
			rsProperty[rsProperty.length] = objLI[i];
		}

		oblUL.style.height = oblUL.offsetHeight +'px';
		oblUL.style.width = oblUL.offsetWidth +'px';

		if (dir == null) {
			var sIndex = objLI.length-1;
		}
		else {
			var sIndex = 0;
		}

		if (!document.getElementById('pg'+ pgTo +'List')) {
			var rURL = '/_ssi/ajax/_getResults.asp'+ pgVars;
			if (rURL.indexOf('?') >= 0) {
				rURL = rURL +'&pg='+ pgTo;
			}
			else {
				rURL = rURL +'?pg='+ pgTo;
			}
			getResults(rURL, pgTo);
		}
		
		fadeOut(sIndex, objLI.length, dir, pgTo);
	}
	
	return false;
}

function doLoadPix(obj, pgTo, dir) {
	if (!isLoadingPG && pgNum != pgTo) {
		isLoadingPG = true;
		
		//alert('pg'+ pgNum +'List -> pg'+ pgTo +'List');
		
		oblUL = document.getElementById('pg'+ pgNum +'List');
		
		if (oblUL) {
			objLI = oblUL.getElementsByTagName('li');

			for (i=0;i<objLI.length;i++) {
				rsProperty[rsProperty.length] = objLI[i];
			}

			oblUL.style.height = oblUL.offsetHeight +'px';
			oblUL.style.width = oblUL.offsetWidth +'px';

			if (dir == null) {
				var sIndex = objLI.length-1;
			}
			else {
				var sIndex = 0;
			}

			fadeOut(sIndex, objLI.length, dir, pgTo, 1);
		}
	}
	
	return false;
}
