// constructor
function aaaCore() {

}

// class objects
aaaCore.prototype.rpcLib = null;
// page dom objects
aaaCore.prototype.mainMapObj = null;
aaaCore.prototype.apartmentMapIcon = null;
aaaCore.prototype.aptListObj = null;
aaaCore.prototype.aptFlyerObj = null;
aaaCore.prototype.mainContentObj = null;
// page vars
aaaCore.prototype.imageArray = new Array();
aaaCore.prototype.dynMenuOn = true;	
aaaCore.prototype.docStyles = null;
aaaCore.prototype.browserName = 'msie';
aaaCore.prototype.browserVersion = '6.0';
aaaCore.prototype.mapPointsArray = new Array();
aaaCore.prototype.mapSlideIndex = 0;
aaaCore.prototype.pauseMapSlide = false;
aaaCore.prototype.lastSelectedRow = "";
aaaCore.prototype.lastSelectedRowStyle = "";


// class functions

// initalize main page
aaaCore.prototype.initCore = function () {
	// preload images for top menu
	
	if(document.images) {
		this.dynMenuOn = true;	
		this.mainContentObj = document.getElementById('mainContentLayer');
		this.aptListObj = document.getElementById('aptListingListArea');
		

		if(this.browserName == 'msie') {
			this.docStyles = document.styleSheets[0].imports.item(0).rules;
		} else {
			docImport = document.styleSheets[0].cssRules.item(0).styleSheet.cssRules;
			this.docStyles = docImport;
		}
		
		downArrowBG = this.docStyles[2].style.background;
		downArrowBG = downArrowBG.substring(downArrowBG.indexOf('\(')+1,downArrowBG.indexOf('\)'));
		downArrowHeight = this.docStyles[2].style.height.replace('px','');
		downArrowWidth = this.docStyles[2].style.width.replace('px','');
		rightArrowBG = this.docStyles[3].style.background;
		rightArrowBG = rightArrowBG.substring(rightArrowBG.indexOf('\(')+1,rightArrowBG.indexOf('\)'));
		rightArrowHeight = this.docStyles[3].style.height.replace('px','');
		rightArrowWidth = this.docStyles[3].style.width.replace('px','');
			
		
		this.imageArray["right-arrow"] = new Image(rightArrowHeight,rightArrowWidth);
		this.imageArray["down-arrow"] = new Image(downArrowHeight,downArrowWidth);
		this.imageArray["menu-bg"] = new Image(10,11);
		this.imageArray["right-arrow"].src = rightArrowBG;
		this.imageArray["down-arrow"].src = downArrowBG;
		this.imageArray["menu-bg"].src = "";
		
	} else {
		this.dynMenuOn = false;		
		this.imageArray["right-arrow"] = new Image(10,11);
		this.imageArray["down-arrow"] = new Image(10,11);
		this.imageArray["menu-bg"] = new Image(10,11);
		this.imageArray["right-arrow"].src = "";
		this.imageArray["down-arrow"].src = "";
		this.imageArray["menu-bg"].src = "";
		
	}
	//initalize google map
	if (GBrowserIsCompatible()) {
		var geocoder = new GClientGeocoder();
		var map = new GMap2(document.getElementById("aptMap"));
		map.setCenter(new GLatLng(35.110703, -106.609991), 12);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		
		//create apartment icon
		smallIcon = new GIcon();
		smallIcon.image = "http://bluepacificdigital.com/earthquakeMaps/images/quakemarker-small.png";
		smallIcon.shadow = "http://bluepacificdigital.com/earthquakeMaps/images/quakemarker-shadow.png";
		smallIcon.iconSize = new GSize(8, 11);
		smallIcon.shadowSize = new GSize(8, 11);
		smallIcon.iconAnchor = new GPoint(3, 3);
		smallIcon.infoWindowAnchor = new GPoint(3, 3);		


		this.mainMapObj = map;
		this.apartmentMapIcon = smallIcon;
		
	} else {
		var geocoder = null;
		var map = null;
		this.mainMapObj = map;
		this.apartmentMapIcon = null;
		
	}
	//start slideShow
	this.resetTabArrows();
	this.mapSlideShow();
	this.showApartmentList();
	
}

aaaCore.prototype.mapSlideShow = function() {

	currMapArray = this.mapPointsArray[this.mapSlideIndex][0]; 
	currArrayIndex = this.mapPointsArray[this.mapSlideIndex][1];

	if(!aaaCore.pauseMapSlide) {
		aaaCore.mainMapObj.clearOverlays();
		
		if(document.getElementById("aptrow" + this.mapSlideIndex) != null) {
			if(this.lastSelectedRow != "") {
				this.lastSelectedRow.className = this.lastSelectedRowStyle;
			}
		
			this.lastSelectedRow = document.getElementById("aptrow" + this.mapSlideIndex);
			this.lastSelectedRowStyle = this.lastSelectedRow.className;
			this.lastSelectedRow.className = "altRow3";
		}
		
		address = currMapArray[3] + " " + currMapArray[4] + " " + currMapArray[5] + ", " + currMapArray[7] + " " + currMapArray[8] + " " + currMapArray[9]; 
		
		newInfoContent = this.buildInfoContent(currMapArray, currArrayIndex);
				
		aaaCore.showAddress(address, newInfoContent);

		
		aaaCore.mapSlideIndex = aaaCore.mapSlideIndex + 1;
		if(aaaCore.mapSlideIndex >= this.mapPointsArray.length) {
			aaaCore.mapSlideIndex = 0;
		}
	}
	setTimeout("aaaCore.mapSlideShow()",10000);
	
}

aaaCore.prototype.dumpMapPointArray = function() {
	if(this.mapPointsArray.length > 0) {
		for(i=0;i < this.mapPointsArray.length; i++) {
			currPoint = this.mapPointsArray[i];
			this.showAddress(currPoint[0], currPoint[1]);		
		}
	}
}


aaaCore.prototype.resetTabArrows = function() {
	
	for(i=1; i<= (tabArray.length-1); i=i+1) {
		currTab = tabArray[i];
		tabArrow = document.getElementById(currTab);
		tabArrow.className = '';
		tabArrow.src = this.imageArray["right-arrow"].src;
	}
}

aaaCore.prototype.hideNav = function() {
	subnavObj.style.display = "";
}


aaaCore.prototype.changeSubnav = function(subNav,tabName) {
	subnavObj = document.getElementById('headerContentArea');
	tabArrowObj = document.getElementById(tabName);
	currNavContent = subnavObj.innerHTML;
	this.resetTabArrows();
	tabArrowObj.src = this.imageArray["down-arrow"].src;
	subnavObj.style.display = "inline";
	subnavObj.style.left = tabArrowObj.offsetLeft + 'px';

	var currNavArray = subNav + "_subnav";
	
	//alert(currNavArray);
	
	if(subNav == "consulting") {
		currNavArray = consulting_subnav;
	} else if(subNav == "inventory") {
		currNavArray = inventory_subnav;
	} else if(subNav == "quicklinks") {
		currNavArray = quicklinks_subnav;
	} else if(subNav == "related") {
		currNavArray = related_subnav;
	} else if(subNav == "resources") {
		currNavArray = resources_subnav;
	} else if(subNav == "toolkit") {
		currNavArray = toolkit_subnav;
	} else if(subNav == "closing") {
		currNavArray = closing_subnav;
	} else if(subNav == "tour") {
		currNavArray = tour_subnav;
	} else {
		currNavArray = consulting_subnav;
	}
	subnavObj.innerHTML = "&nbsp;";
	//alert(currNavContent);
	newNavContent = "";
	//newNavContent += "<table width='90%' align=left cellpadding=2 cellspacing=0 border=0>";

	for(i=0; i  < currNavArray.length; i=i+1) {
		//newNavContent += "<tr><td>";
		newNavContent += '<div style="padding: 2px;"><img class="whiteRightArrow" src="images/spacer.gif" width="11" height="10" hspace=5 vspace=0 />';
		//newNavContent += '</td><td align=left class="headerbarLink">';
		newNavContent += currNavArray[i] + '</div>';
		if(i != (currNavArray.length-1)) {
			//newNavContent += "&nbsp;|&nbsp;";
		}
		//newNavContent += "</td></tr>";
	}  
	//newNavContent += "</table>";
	subnavObj.innerHTML = newNavContent;
	menuMousedOver = false;
}

aaaCore.prototype.changeContent = function(imageName, imageSize) {
	slideShow.showEnabled = false;
	alert(imageName);
	this.changeMainContentArea(imageName, imageSize);
	/*
	if(imageSize =="") {
		imageSize = 300;
	}
	contentArea = top.document.getElementById('mainContentImage');
	origHeight = contentArea.height;
	contentArea.src = imageName;
	if(contentArea.width > imageSize) {
		contentArea.width = imageSize;
	}
	*/
	//alert(imageName);
}

aaaCore.prototype.resetContent = function() {
	this.pauseMapSlide = false;
	
}

aaaCore.prototype.showProperty = function(propKey) {
	winUrl = "http://www.nmapartment.com/propertyPopup.php?propkey=" + propKey;
	window.open(winUrl,'props','width=860, height=720, status=0, toolbar=0, titlebar=0, scrollbars=0, statusbar=0, menubar=0,resizable=1');
}

aaaCore.prototype.createMarker = function(lat,longitude,icon,infoContent) {

	point = new GLatLng(lat, longitude);
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(infoContent);
		
	});

	return marker;
}

aaaCore.prototype.showAddress = function(address, infoContent) {
	if (aaaCore.geocoder) {
		geocoder = this.geocoder;
		geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						//alert(address + " not found");
					} else {
						//map.setCenter(point, 13);
						var marker = new GMarker(point);
						aaaCore.mainMapObj.addOverlay(marker);
						marker.openInfoWindowHtml(infoContent);  
						aaaCore.mainMapObj.panTo(point);
						
					}
				}
			);
	} else {
		alert('geocoder not found');
	}
}

aaaCore.prototype.changeMainContentArea = function(newUrl, urlParams) {
	this.rpcLib.doHttpRequest(newUrl, urlParams, this.mainContentObj.id);
}

aaaCore.prototype.changeAptOrder = function(newOrder) {
	this.rpcLib.doHttpRequest('inc/layout/apartmentsListings2.php', '?aptSort=' + newOrder, 'actionFrame');
	this.aptListObj.innerHTML = this.aptListObj.innerHTML;
	
}

aaaCore.prototype.showMiniFlyer = function(rowIndex) {
	
	currMapArray = this.mapPointsArray[rowIndex][0]; 
	currArrayIndex = this.mapPointsArray[rowIndex][1];
	
	if(this.lastSelectedRow != "") {
		this.lastSelectedRow.className = this.lastSelectedRowStyle;
	}

	this.lastSelectedRow = document.getElementById("aptrow" + rowIndex);
	this.lastSelectedRowStyle = this.lastSelectedRow.className;
	this.lastSelectedRow.className = "altRow3";
	
	address = currMapArray[3] + " " + currMapArray[4] + " " + currMapArray[5] + ", " + currMapArray[7] + " " + currMapArray[8] + " " + currMapArray[9]; 
	
	newInfoContent = this.buildInfoContent(currMapArray, currArrayIndex);
			
	this.pauseMapSlide = true;
	this.mainMapObj.clearOverlays();
	this.showAddress(address, newInfoContent);;
	
}

aaaCore.prototype.showApartmentList = function() {
	apartmentArray = this.mapPointsArray;
		this.aptListObj.innerHTML = "";
		// width="100%" cellpadding="2" cellspacing="0" border="0"	
		tempTable=document.createElement('table');
		tempTable.setAttribute('id','aptListTable');
		tempTable.setAttribute('width','100%');
		tempTable.setAttribute('cellpadding','2');
		tempTable.setAttribute('cellspacing','0');
		tempTable.setAttribute('border','0');
		tableObj = this.aptListObj.appendChild(tempTable);	

		tempRow = document.createElement('tr');
		tempRow.className = "header";
		tempTable.appendChild(tempRow);
		
		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(4)'>Address</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(7)'>City</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(8)'>State</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(10)'>Units</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(13)'>Price</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong><a href='javascript:aaaCore.changeAptOrder(12)'>Status</a></strong>";
		tempRow.appendChild(tempCell);

		tempCell = document.createElement('td');
		tempCell.innerHTML = "<strong>Photos</strong>";
		tempRow.appendChild(tempCell);


		for(i=0; i < apartmentArray.length; i++) {	
		
			if(i%2==1) {
				rowClass = "altRow1";
			} else {
				rowClass = "altRow2";
			}
			tempRow = document.createElement('tr');
			tempRow.setAttribute('id','aptrow' + i);
			tempRow.className = rowClass;
			tempTable.appendChild(tempRow);
			
			currRow = apartmentArray[i][0];
			
			// onMouseOver="aaaCore.showMiniFlyer(<?= $i ?>, this.id);" onMouseOut="aaaCore.resetContent()"
			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = "<a href='" + currRow[0] + "' target='top'>" + currRow[3] + ' ' + currRow[4] + ' ' + currRow[5] + "</a>";
			tempRow.appendChild(tempCell);
			
			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = currRow[7];
			tempRow.appendChild(tempCell);

			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = currRow[8];
			tempRow.appendChild(tempCell);
			
			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = currRow[10];
			tempRow.appendChild(tempCell);
			
			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = currRow[13];
			tempRow.appendChild(tempCell);
			
			tempCell = document.createElement('td');
			tempCell.setAttribute('onMouseOver','aaaCore.showMiniFlyer(' + i + ');');
			tempCell.setAttribute('onMouseOut','aaaCore.resetContent();');
			tempCell.innerHTML = currRow[12];
			tempRow.appendChild(tempCell);

			tempCell = document.createElement('td');
			tempCell.setAttribute('align','center');
			tempCell.innerHTML = "<span class='picsButton' onClick='aaaCore.showProperty(" + currRow[22] + ")'>&nbsp;&nbsp;&nbsp;&nbsp;</span>";
			tempRow.appendChild(tempCell);

		}
	this.aptListObj.innerHTML = this.aptListObj.innerHTML;
}

aaaCore.prototype.buildInfoContent = function(currMapArray, arrayIndex) {
	
	address = currMapArray[3] + " " + currMapArray[4] + " " + currMapArray[5] + ", " + currMapArray[7] + " " + currMapArray[8] + " " + currMapArray[9];
	addressLink = currMapArray[0];
	market = currMapArray[4];
	units = currMapArray[10]
	currStatus = currMapArray[12];
	price = currMapArray[13];
	photo = currMapArray[11];
	downpayment = currMapArray[14];
	grm = currMapArray[15];
	cap = currMapArray[17];
	agent = currMapArray[18];
	agentphone = currMapArray[19];
	csvIndex = currMapArray[22];

	newInfoContent = "<div style='width:320px;' align='left'><p>";
	newInfoContent = newInfoContent + "<img src='" + photo + "' width=120 height=100 align=right border=0 />";
	newInfoContent = newInfoContent + "<b><a href='" + addressLink + "' alt='Click for property flyer' target='_blank'>" + currMapArray[3] + " " + currMapArray[4] + " " + currMapArray[5] + ",<br> " + currMapArray[7] + " " + currMapArray[8] + " " + currMapArray[9] + "</a></b><br>";
	newInfoContent = newInfoContent + "<b>Market</b>:&nbsp;" + market + "<br>";
	newInfoContent = newInfoContent + "<b>Units</b>:&nbsp;" + units + "<br>";
	newInfoContent = newInfoContent + "<b>Status</b>:&nbsp;" + currStatus + "<br>";
	newInfoContent = newInfoContent + "<b>Price</b>:&nbsp;" + price + "<br>";
	newInfoContent = newInfoContent + "<b>Agent</b>:<br>" + agent + "<br>" + agentphone;
	
	//this.aptListObj.innerHTML = currMapArray;
	newInfoContent = newInfoContent + "</p></div>";
	
	return newInfoContent;
		
	
}