// constructor
function nmaaCore() {

}

// class objects
nmaaCore.prototype.rpcLib = null;
// page dom objects
nmaaCore.prototype.mainMapObj = null;
nmaaCore.prototype.apartmentMapIcon = null;
nmaaCore.prototype.aptListObj = null;
nmaaCore.prototype.aptFlyerObj = null;
nmaaCore.prototype.mainContentObj = null;
// page vars
nmaaCore.prototype.imageArray = new Array();
nmaaCore.prototype.dynMenuOn = true;	
nmaaCore.prototype.docStyles = null;
nmaaCore.prototype.browserName = 'msie';
nmaaCore.prototype.browserVersion = '6.0';

// class functions

// initalize main page
nmaaCore.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[1].style.background;
		downArrowBG = downArrowBG.substring(downArrowBG.indexOf('\(')+1,downArrowBG.indexOf('\)'));
		downArrowHeight = this.docStyles[1].style.height.replace('px','');
		downArrowWidth = this.docStyles[1].style.width.replace('px','');
		rightArrowBG = this.docStyles[2].style.background;
		rightArrowBG = rightArrowBG.substring(rightArrowBG.indexOf('\(')+1,rightArrowBG.indexOf('\)'));
		rightArrowHeight = this.docStyles[2].style.height.replace('px','');
		rightArrowWidth = this.docStyles[2].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(11,10);
		this.imageArray["menu-bg"] = new Image(10,11);
		this.imageArray["right-arrow"].src = "";
		this.imageArray["down-arrow"].src = "";
		this.imageArray["menu-bg"].src = "";
		
	}

	//start slideShow
	slideShow.initShow();
	
}

nmaaCore.prototype.getSearchForm = function (formType, formMessage, style) {
		var searchContent = "";
		searchContent += '<table cellpadding="0" cellspacing="0" broder="0" align="left"><tr><form method="post" action="" onSubmit="return nmaaCore.doSearch(this);">'
		searchContent += '<input type="hidden" name="formType" value=' + formType + '><td align=center class="' + style + '">';
		searchContent += formMessage + '&nbsp;<input type="text" name="searchQuery" style="font: normal 6pt arial;">';
		searchContent += ' &nbsp;<input type="submit" value="Go" style="font: normal 6pt arial;"> </td></form></tr></table>';

		return searchContent;
}

nmaaCore.prototype.doSearch = function(formObj) {
	formType = formObj.formType.value;
	query = formObj.searchQuery.value;
	if(formType == "googleSearch") {
		queryString = "http://www.google.com/search?as_q=" + query + "&as_sitesearch=http://www.nmapartment.com";
	} else if (formType == "upcSearch") {
		queryString = "http://www.bernco.gov/property/default.asp?qpaction=search&type=parcel&Parcel=" + query+ "&year=2004&dpage=profile2&Submit=Search";
	} else if (formType == "cityWater") {
		if (document.createElement && (form = document.createElement('form'))) {
			//form.action = "http://www.cabq.gov/utbonline/enquiry.jsp";
			form.action = "http://pecos.cabq.gov/psc/wss/CUSTOMER/CIS/c/CI_WSS.CI_WSS_ACCT_INFO.GBL";
			form.target = "_blank";
			form.method = 'POST';
			
			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'CU_WSS_SRCH_VW_ACCT_ID';
			input.value = query;
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICType';
			input.value = "Panel";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICElementN um';
			input.value = "0";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICStateNum';
			input.value = "7";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICAction';
			input.value = "None";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICXPos';
			input.value = "0";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICYPos';
			input.value = "0";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICFocus';
			input.value = "";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICChanged';
			input.value = "-1";
			form.appendChild(input);

			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'ICResumbit';
			input.value = "1";
			form.appendChild(input);

			document.body.appendChild(form);
			form.submit()
		}	
		return false;
	} else {
		queryString = "http://www.google.com/search?as_q=" + query + "&as_sitesearch=http://www.nmapartment.com";
	}

	window.open(queryString,'search','');	
	
	return false;
}

nmaaCore.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;
	}
}

nmaaCore.prototype.hideNav = function() {
	subnavObj.style.display = "";
}


nmaaCore.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;
}

nmaaCore.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);
}

nmaaCore.prototype.resetContent = function() {
	slideShow.showEnabled = true;
	//this.mainContentObj.innerHTML = defaultContentArea;
}

nmaaCore.prototype.showProperty = function(propKey) {
	winUrl = "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');
}

nmaaCore.prototype.createMarker = function(lat, long, icon, infoContent) {

	point = new GLatLng(lat, long);
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(infoContent);
		
	});

	return marker;
}

nmaaCore.prototype.showAddress = function(address, infoContent) {
	if (nmaaCore.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);
						nmaaCore.mainMapObj.addOverlay(marker);
						GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(address + "<br>" + infoContent);  });
					}
				}
			);
	} else {
		alert('geocoder not found');
	}
}

nmaaCore.prototype.changeMainContentArea = function(newUrl, urlParams) {
	this.rpcLib.doHttpRequest(newUrl, urlParams, this.mainContentObj.id);
}

nmaaCore.prototype.changeAptOrder = function(newOrder) {
	this.rpcLib.doHttpRequest('inc/layout/apartmentsListings.php', '?aptSort=' + newOrder, this.aptListObj.id);
}

nmaaCore.prototype.showMiniFlyer = function(propertyCode) {
	this.changeMainContentArea('inc/layout/miniPropFlyer.php', '?code=' + propertyCode);
}