// nice single global var
var tll = new Object;

// make sure the page is centered
function fixWidth () {
	if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
		ourWidth = window.innerWidth;
		ourHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
		ourWidth = document.documentElement.clientWidth;
		ourHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
		ourWidth = document.body.clientWidth;
		ourHeight = document.body.clientHeight;
	}

	// var ourWidth = document.body.clientWidth;
	var leftMargin = (ourWidth - 900) / 2;
	if (leftMargin > 0) {
		document.getElementById('mainArea').style.left = leftMargin + 'px';
		leftMargin += 20;
		// document.getElementById('bottomRule').style.left = leftMargin + 'px';
	} else {
		document.getElementById('mainArea').style.left = '0px';
		// document.getElementById('bottomRule').style.left = '0px';
	}
	
	// do the height too
	// var ourHeight = document.body.clientHeight;
	var topMargin = ourHeight - 200;
/*	if (topMargin > 0) {
		document.getElementById('mainContent').style.height = topMargin + 'px';
	} else {
		document.getElementById('mainContent').style.height = '0px';
	}
*/
}

// for the services page
function showHideInfo (divID) {
	var theDetails = document.getElementById(divID);
	if (theDetails.style.display == 'none') {
		theDetails.style.display = 'block';
	} else {
		theDetails.style.display = 'none';
	}
}

// how does our site flow?
// set up the data structure to 
tll.siteStructure = new Array();
	tll.siteStructure['welcome'] = 'Welcome';
	tll.siteStructure['available'] = 'Available Homes';
	tll.siteStructure['apply'] = 'Online Lease Application';
	tll.siteStructure['services'] = 'Services &amp; Rates';
	tll.siteStructure['sample_docs'] = 'Example Contracts';
	tll.siteStructure['faq_clients'] = 'New Client FAQ';
	tll.siteStructure['faq_tenants'] = 'New Resident FAQ';
	tll.siteStructure['rules'] = 'Rules of the Road';
	tll.siteStructure['sec_deposits'] = 'About Security Deposits';
	tll.siteStructure['team'] = 'Meet Our Team';
	tll.siteStructure['contact'] = 'Contact Us';
	tll.siteStructure['request'] = 'Request Maintenance';
	tll.siteStructure['portal'] = 'Client Portal';	
tll.subTitles = new Array();
	tll.subTitles['welcome'] = 'The Right Property Managers Make the Difference!';
	tll.subTitles['available'] = 'Homes Available for Lease';
	tll.subTitles['apply'] = 'Online Lease Application - Please Read Carefully';
	tll.subTitles['services'] = 'Our Services &amp; Rates - Let\'s Get Right Down To It';
	tll.subTitles['sample_docs'] = 'Our Example Contracts - No One Likes Surprises';
	tll.subTitles['faq_clients'] = 'New Client/Investor Frequently Asked Questions (FAQ)';
	tll.subTitles['faq_tenants'] = 'New Resident Frequently Asked Questions (FAQ)';
	tll.subTitles['rules'] = 'Everyone Can Be A Winner If We Play By the Rules';
	tll.subTitles['sec_deposits'] = 'Our Policies on Security Deposits';
	tll.subTitles['team'] = 'Meet Our Team (Introducing the All-Stars)';
	tll.subTitles['contact'] = 'Contact Us - We\'re Excited to Hear From You';
	tll.subTitles['request'] = 'For Residents: Request Maintenance or Service';
	tll.subTitles['portal'] = 'Client Portal: Keep Your Eye on Us!';	

// space to cache the content
tll.mainContent = new Array();
	
// now plug in our navigation pieces
function setNavArea () {
	var navArea = document.getElementById('navWrapper');
	for (var n in tll.siteStructure) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id',n);
		newDiv.innerHTML = '<a href="#' + n + '" class="navLink">' + tll.siteStructure[n] + '</a>';
			newDiv.setAttribute('onMouseOver','changeButtonsOn(this)');
			newDiv.setAttribute('onMouseOut','changeButtonsOff(this)');
		if (n == 'welcome') {
			newDiv.setAttribute('style','border-top: 1px solid #003399');
		}
		navArea.appendChild(newDiv);
		newDiv.className='navOff';
	}
}	

// change button handler
function changeButtonsOn(button) {
	if (button.className == 'navOff') {
		button.className = 'navOn';
	}
}
function changeButtonsOff(button) {
	if (button.className == 'navOn') {
		button.className = 'navOff';
	}
}

// know our place
var currentAnchor = document.location.hash;


function trafficController () {  
	// has it changed?
	if (currentAnchor != document.location.hash) { // it changed
		// only do this once
		currentAnchor = document.location.hash;  
		if (currentAnchor !='') { // execute the js
			changeTheMainArea(currentAnchor);
		}
	}		
	
}


// here is our content-grabber
function changeTheMainArea (target) {
	target = target.replace(/#/,'');
	
	// jump to the top
	scroll(0,0);

	// color the active button
	for (var n in tll.siteStructure) { 
		if (n == target) {
			document.getElementById(n).className = 'navSelected';
		} else {
			document.getElementById(n).className = 'navOff';
		}
	}
	
	// fix the sub-title
	tll.subTitle.innerHTML = tll.subTitles[target];
	
	// make them wait
	tll.theArea.innerHTML = 'Loading...';
	
	// do we already have it?  if so, just slap it in
	if (tll.mainContent[target] != null) {
		tll.theArea.innerHTML = tll.mainContent[target];
		if (target == 'welcome') { // get featured listing
			featuredPropertyCached()
		}
	} else { // everything else we grab with the ajax	
		AjaxRequest.get({
			'url':'content/' + target + '.html',
			'onSuccess':function(req){ 
				// get it in there
				tll.theArea.innerHTML = req.responseText;
				
				// welcome area? get the featured properties
				if (target == 'welcome') {
					featuredProperty();
				
				// application page needs options
				} else if (target == 'apply') {
					var appOptionArea = document.getElementById('applicationOptions');
					if (tll.applicationOptions.match(/option/)) { // has one, good
						appOptionArea.innerHTML = '<b>Apply Here:</b>  <select onchange="applyJump(this)"><option>Select Desired Home for Lease Application</option>' + tll.applicationOptions + '</select>';
					} else { // none
						appOptionArea.innerHTML = 'No Homes are Currently Available for Application - Please Check Back Soon.';
					}
				}
				// cache it too
				tll.mainContent[target] = req.responseText;
			}
		});		
	}
}

// function to send them to the lease application
function applyJump (menu) {
	if (menu.options[menu.options.selectedIndex].value != null) {
		window.open(menu.options[menu.options.selectedIndex].value);
		menu.options.selectedIndex = 0;
	}
}

// function to get the featured property for the main page
var featuredListingsArray;
function featuredProperty () {
	// snatch the divs
	AjaxRequest.get({
		'url':'content/featured.html',
		'onSuccess':function(req){ 
			// put it in the body
			var featuredListingsHTML = req.responseText;

			// break it up
			featuredListingsArray = featuredListingsHTML.split('BREAK__HERE');
			
			// now pick one at random,
			var randOne = Math.floor ( Math.random() * featuredListingsArray.length ); 

			// stick it in there -- avoid duplicates / multiple
			if (tll.theArea.innerHTML.match(/FEATURED HOME/) == null) {
				tll.theArea.innerHTML = featuredListingsArray[randOne] + tll.theArea.innerHTML;
			}
		}
	});		
}

// function if welcome page is already cached
function featuredPropertyCached () {
	// now pick one at random,
	var randOne = Math.floor ( Math.random() * featuredListingsArray.length ); 
	// stick it in there
	tll.theArea.innerHTML = featuredListingsArray[randOne] + tll.theArea.innerHTML;
}

// crank up the trafficController to handle the navigation
function crankThisPage () {
	setNavArea();
	fixWidth();
	
	// keep us where we're going
	setInterval("trafficController()", 300);
	
	// the destination for our content
	tll.theArea = document.getElementById('mainContent');
	
	tll.subTitle = document.getElementById('subTitle');
	
	// get the application options
	AjaxRequest.get({
		'url':'content/appOptions.html',
		'onSuccess':function(req){ 
			tll.applicationOptions = req.responseText;
		}
	});
	// set first location
	if (!document.location.hash) {
		document.location.hash = '#welcome';
	}
	changeTheMainArea(document.location.hash);
}

// contact form submissions
function checkrequired (which) {
	var submitButton = document.getElementById('submitButton');
	var pass = true;
	var whichHTML;
	for ( var i = 0;i<which.length;i++) {
		if (which.elements[i].name == 'which') {
			whichHTML = which.elements[i].value;
		}
		
		// test that it is filled-in
		if (which.elements[i].type != 'select-one' && which.elements[i].value == '' && pass == true) {
			alert("Sorry, all fields must be filled in.");
			submitButton.value = 'Click Once to Get Us Working on this Request';
			pass = false;
		}
	}

	if (pass) {
		AjaxRequest.submit(which,{
			'onComplete':function(req){ 
				if (req.responseText) {
					tll.theArea.innerHTML = req.responseText;
				} else {
					tll.theArea.innerHTML = '<center>We will be in touch shortly.</center><br/>';
					tll.theArea.innerHTML += '<center><a href="javascript:changeTheMainArea(\'' + whichHTML + '\')">Click here to see the form again.</a></center>';
				}
				// appropriate subtitle
				if (req.responseText.match(/ERROR/)) {
					tll.subTitle.innerHTML = 'Oops! There is a Slight Problem';
				} else {				
					tll.subTitle.innerHTML = 'Thanks! Your Message Has Been Sent!';
				}
			}
		});
	}


	return false;
}


// check username/password for the owner portal
function checkOwnerUN (which) {
	// get the operative fields
	var ownerUN = document.getElementById('ownerUN').value;
	var ownerPass = document.getElementById('ownerPass').value;
	var loginButton = document.getElementById('ownerLogin');
	var pass = true;
	
	// if either is blank, barf
	if (!ownerUN || !ownerPass) {
		loginButton.value = 'Username and Password Required';
		loginButton.className = 'angryButton';
		pass = false;
	}

	if (pass) {
		AjaxRequest.submit(which,{
			'onSuccess':function(req){ 
				// appropriate reaction
				if (req.responseText.match(/ERROR/)) {
					loginButton.value = 'Incorrect Username or Password';
					loginButton.className = 'angryButton';
				} else {				
					// easy sub to get the portal oueces
					getAPortalPiece(req.responseText);
				}
			}
		});
	}
	return false;
}

// grab and place portal html
function getAPortalPiece (portalPiece) {
	
	// book it
	AjaxRequest.get({
		'url':'/cgi-bin/portals/portals.cgi?gimme=' + portalPiece,
		'onSuccess':function(req){ 
			tll.theArea.innerHTML = req.responseText;
			// cache it too
			tll.mainContent['portal'] = req.responseText;
			// subtitle too
			tll.subTitle.innerHTML = document.getElementById('useTitle').innerHTML;
			tll.subTitles['portal'] = document.getElementById('useTitle').innerHTML;
			
		}
	});		
}
