﻿// required jquery.js, util.js

	var s_mapResult = {
	
		mapResult: null,
		currentPage: 0,  //zero-based
		pageSize: 5,
		centerLat: '', // 1st listing geo, so detached map could center
		centerLng: '',
		format: 1,
		
		init: function(mapResult) {
			this.mapResult = mapResult;
		},
	
		printPaging: function(elmtId) {
			var html = "";
			if (this.currentPage == 0)
				html += "&lt; Previous&nbsp; | &nbsp;";
			else
				html += "<a href=\"javascript:s_mapResult.printPage(" + (this.currentPage-1) + ")\">&lt; Previous</a>&nbsp; | &nbsp;";
			var lastPage = Math.round(this.mapResult.listings.length / this.pageSize);
			var beginPage = this.currentPage;
			var endPage = this.currentPage;
			while (beginPage > 0 && beginPage > this.currentPage - 4)
				beginPage--;
			while (endPage < lastPage-1 && endPage < this.currentPage + 4)
				endPage++;
			for (var i = beginPage; i <= endPage; i++)
			{
				if (i == this.currentPage)
					html += (i+1) + "&nbsp;";
				else
					html += "<a href=\"javascript:s_mapResult.printPage(" + i + ")\">" + (i+1) + "</a>&nbsp;";
			}
			html += " of " + lastPage + "&nbsp; | &nbsp; ";
			if (this.currentPage == lastPage)
				html += "Next &gt;";
			else
				html += "<a href=\"javascript:s_mapResult.printPage(" + (this.currentPage+1) + ")\">Next &gt;</a>";
			$("#" + elmtId).html(html);
		},
		
		printPage: function(page) {
			this.currentPage = page;
			$("#listingSelectionView").show();
			this.printPaging("listingPaging");
			// Print Listings
			var html = "";
			var listings = s_arrayutil.getPage(this.mapResult.listings, this.currentPage, this.pageSize);
      
      // find center
      var lats = 0.0;
      var lngs = 0.0;
      for (var n = 0; n < listings.length; n++) {
		    lats += parseFloat(listings[n].latitude);
        lngs += parseFloat(listings[n].longitude);
			}
      this.centerLat = lats / listings.length;
	    this.centerLng = lngs / listings.length;
	    
			for(var i=0; i<listings.length; i++) {
				if (this.format == 1)
					html += this.printListing1(i+1+(this.currentPage*this.pageSize), listings[i]);
				else if (this.format == 2)
					html += this.printListing2(i+1+(this.currentPage*this.pageSize), listings[i]);
			}
			$("#listingContainer").html(html);
		},
		
		removePage: function() {
			this.currentPage = 0;
			$("#listingSelectionView").hide();
			$("#listingPaging").html("");
			$("#listingContainer").html("");
		},
		
		printListing1: function(indx, listing) {
			var ret = "<div class=\"listing_container\">" +
					"<div class=\"" + this.getPinClass(listing) + "\">" + indx +
						"<input type=\"checkbox\" name=\"listing\" value=\"" + listing.mlsNum + "\" id=\"listing_" + listing.mlsNum + "\" /></div>" +
					"<div class=\"listing_image_container\">" +
						"<a href=\"javascript:s_mapResult.getDetail('" + listing.mlsNum + "')\">" +
							"<img src=\"" + this.getPhotoLink(listing) + "\" /></a></div>" +
					"<div class=\"listing_info_container\">" +
						"<div class=\"listing_info\">";
			if (listing.openHouse && listing.openHouse != "")
				ret += "<div class=\"open_house_display_time\">Open House: " + listing.openHouse + "</div>";
			ret += "<div class=\"price\">$" + listing.listPrice + "</div>" +
							"<div class=\"address\">" + listing.address + "</div>" + 
							"<div class=\"address\">" + listing.city + ", " + listing.state + " " + listing.zipCode + "</div>" +
							"<div class=\"beds_and_baths\">" + listing.briefInfo + "</div>" +
							"<div class=\"mls_info\">MLS# " + listing.mlsNum + " - Status: " + listing.status + "</div>";
							//"<div class=\"IDX_courtesy_in_row\">" + listing.courtesy + "</div>";
			if (this.mapResult.mlsLogo != "" && !listing.ownListing)
				ret += "<div class=\"IDX_required_logo_in_results\">" + "<img src=\"" + this.mapResult.mlsLogo + "\" /></div>";
			ret += "</div>" +
					"</div>" +
					"<div class=\"listing_nav_container\">" +
						"<ul>" +
							"<li><a href=\"javascript:s_mapResult.getDetail('" + listing.mlsNum + "')\" class=\"results_button1\">Details</a></li>";
			if (!listing.saved)
				ret += "<li><a href=\"javascript:s_mapResult.getSave('" + listing.mlsNum + "')\" class=\"results_button2\">Save Property</a></li>";
			ret += "<li><a href=\"javascript:newwindow('SlideShow.aspx?mlsnum=" + listing.mlsNum +
								"&mlsTableID=" + listing.mlsTable + "')\" class=\"results_button3\">Slideshow</a></li>";
			if (listing.vTour && listing.vTour != "")
				ret += "<li><a href=\"" + listing.vTour + "\" target=\"_blank\" class=\"results_button4\">Virtual Tour</a></li>";
			ret += "</ul>" +
					"</div>" +
				"</div>";
			return ret;
		},
		
		printListing2: function(indx, listing) {
			var ret = "<div class=\"listing_container\" onmouseover=\"javascript:s_mapResult.showTooltip('" + listing.mlsNum + "')\" onmouseout=\"javascript:s_mapResult.hideTooltip('" + listing.mlsNum + "')\">" +
					"<div class=\"" + this.getPinClass(listing) + "\">" + indx +
						"<input type=\"checkbox\" name=\"mlsnums\" value=\"" + listing.mlsTable + "___" + listing.mlsNum + "\" id=\"mlsnum_" + listing.mlsNum + "\" /></div>" +
					"<div class=\"listing_image_container\">" +
						"<a href=\"javascript:s_mapResult.getDetail('" + listing.mlsNum + "')\">" +
							"<img src=\"" + this.getPhotoLink(listing) + "\" /></a></div>" +
					"<div class=\"listing_info_container\">" +
						"<div class=\"listing_info\">";
			if (listing.openHouse && listing.openHouse != "")
				ret += "<div class=\"open_house_display_time\">Open House: " + listing.openHouse + "</div>";
			ret += "<div class=\"price\">$" + listing.listPrice + "</div>" +
							"<div class=\"address\">" + listing.address + "</div>" + 
							"<div class=\"address\">" + listing.city + ", " + listing.state + " " + listing.zipCode + "</div>" +
							"<div class=\"beds_and_baths\">" + listing.briefInfo + "</div>" +
							"<div class=\"mls_info\">MLS# " + listing.mlsNum + " - Status: " + listing.status + "</div>";
							//"<div class=\"IDX_courtesy_in_row\">" + listing.courtesy + "</div>";
			if (this.mapResult.mlsLogo != "" && !listing.ownListing)
				ret += "<div class=\"IDX_required_logo_in_results\">" + "<img src=\"" + this.mapResult.mlsLogo + "\" /></div>";
			ret += "</div>" +
					"</div>" +
					"<div class=\"listing_nav_container\">" +
						"<ul>" +
							"<li><a href=\"javascript:s_mapResult.getDetail('" + listing.mlsNum + "')\" class=\"results_button1\">Details</a></li>";
			if (listing.saved)				
				ret += "<li><a id=\"saveItem_" + listing.mlsTable + "___" + listing.mlsNum + "\" href=\"javascript:popUp.removeItem('" + listing.mlsTable + "___" + listing.mlsNum + "', false)\" class=\"results_button2_saved\">Remove Property</a></li>";
			else
				ret += "<li><a id=\"saveItem_" + listing.mlsTable + "___" + listing.mlsNum + "\" href=\"javascript:popUp.saveItem('" + listing.mlsTable + "___" + listing.mlsNum + "', false)\" class=\"results_button2\">Save Property</a></li>";
			ret += "<li><a href=\"javascript:newwindow('SlideShow.aspx?mlsnum=" + listing.mlsNum +
								"&mlsTableID=" + listing.mlsTable + "')\" class=\"results_button3\">Slideshow</a></li>";
			if (listing.vTour && listing.vTour != "")
				ret += "<li><a href=\"" + listing.vTour + "\" target=\"_blank\" class=\"results_button4\">Virtual Tour</a></li>";
			ret += "</ul>" +
					"</div>" +
				"</div>";
			return ret;
		},
		
    showTooltip: function(mlsnum) {
      var tt = document.getElementById('tt_' + mlsnum);
      if (tt != null) {
        tt.style.visibility = "visible";
      }
    },

    hideTooltip: function(mlsnum) {
      var tt = document.getElementById('tt_' + mlsnum);
      if (tt != null) {
        tt.style.visibility = "hidden";
      }
    },

		printInfoBox: function(mlsNum, beakDirection) {
			var listing = this.getListing(mlsNum);
			var ret = "<div class=\"Map_1_bubble_outer_container\"><div class=\"Map_1_bubble_container\">" +
								"<a href=\"javascript:s_mapResult.getDetail('" + mlsNum + "')\">" +
								"<img class=\"Map_1_bubble_img\" src=\"" + this.getPhotoLink(listing) + "\" />" +
								"<div class=\"Map_1_bubble_info_area\">" + 
									"<div class=\"Map_1_bubble_price\">$" + listing.listPrice + "</div>" + 
									"<div class=\"Map_1_bubble_address\">" + listing.address + ",<br />" + listing.city + ", " + listing.state + " " + listing.zipCode + "</div>" +
									"<div class=\"Map_1_bubble_info\">" + listing.briefInfo + "</div>" + 
									"<div class=\"Map_1_bubble_mls\">MLS#: " + listing.mlsNum + "</div></div></a>" +
							"<div class=\"Map_1_bubble_buttons_container\">" +
								"<a href=\"javascript:s_mapResult.getDetail('" + mlsNum + "')\">Details</a>" +
								"&nbsp; | &nbsp;<a href=\"javascript:s_mapResult.getSave('" + listing.mlsNum + "')\">Save Listing</a>" +
							//"&nbsp; | &nbsp;<a href=\"#\">10 Additional Photos</a>" +
							"</div></div>";
			if (beakDirection != "")
				ret += "<div class=\"Map_1_bubble_beak_" + beakDirection + "\"><img src=\"http://elements6.superlativestudio.com/AppInterface/Default/MapSearch/images/beak_" + beakDirection + ".gif\" /></div>";
			ret += "</div>";
			return ret;
		},
		
		printInfoBoxNoLinks: function(mlsNum, beakDirection) {
			var listing = this.getListing(mlsNum);
			var ret = "<div class=\"Map_1_bubble_outer_container\"><div class=\"Map_1_bubble_container\">" +
								"<a href=\"javascript:s_mapResult.getDetail('" + mlsNum + "')\">" +
								"<img class=\"Map_1_bubble_img\" src=\"" + this.getPhotoLink(listing) + "\" />" +
								"<div class=\"Map_1_bubble_info_area\">" + 
									"<div class=\"Map_1_bubble_price\">$" + listing.listPrice + "</div>" + 
									"<div class=\"Map_1_bubble_address\">" + listing.address + ",<br />" + listing.city + ", " + listing.state + " " + listing.zipCode + "</div>" +
									"<div class=\"Map_1_bubble_info\">" + listing.briefInfo + "</div>" + 
									"<div class=\"Map_1_bubble_mls\">MLS#: " + listing.mlsNum + "</div></div></a>" +
							"</div>";
			if (beakDirection != "")
				ret += "<div class=\"Map_1_bubble_beak_" + beakDirection + "\"><img src=\"http://elements6.superlativestudio.com/AppInterface/Default/MapSearch/images/beak_" + beakDirection + ".gif\" /></div>";
			ret += "</div>";
			return ret;
		},
		
		getListing: function(mlsNum) {
			for (var i=0; i<this.mapResult.listings.length; i++) {
				if (this.mapResult.listings[i].mlsNum == mlsNum)
					return this.mapResult.listings[i];
			}
			return null;
		},
		
		getCustomIcon: function(indx, listing) {
			if (typeof showInfoBox == "function")
				return "<div id='pinListing" + listing.mlsNum + "' class='map_" + this.getPinClass(listing) + "' onClick=\"showInfoBox('pinListing" + listing.mlsNum + "')\">" + indx + "</div>";
			else
				return "<div id='pinListing" + listing.mlsNum + "' class='map_" + this.getPinClass(listing) + "'>" + indx + "</div>";
		},
		
		getPinClass: function(listing) {
			switch(listing.iconType) {
				case "all":
					return "select_blue";
				case "new":
					return "select_green";
				case "own":
					return "select_purple";
				case "openhouse":
					return "select_red";
				case "saved":
					return "select_orange";
				default:
					return "select_blue";
			}
		},
		
		getPhotoLink: function(listing) {
			return "ListingPhoto.aspx?mlstableid=" + listing.mlsTable + "&mlsnum=" + listing.mlsNum;
		},
		
		getDetailLink: function(listing) {
			return "IDXDetail.aspx?sp=y&pu=y&segmentid=" + $("#segmentid").val() + "&mlstableid=" + 
				listing.mlsTable + "&mlsnum=" + listing.mlsNum + "&uid=" + $("#uid").val() + "&rid=" + 
				$("#rid").val() + "&sid=" + $("#sid").val() + "&sponsorid=" + $("#sponsorid").val() + "&htmlfile=" + 
				$("#htmlfile").val();
		},
		
		getDetail: function(mlsNum) {
			var url = this.getDetailLink(this.getListing(mlsNum));
			var detailWin = window.open(url, 'detailWindow', 'width=600,height=600,resizable=yes,scrollbars=yes');
		},
		
		getSaveLink: function(listing) {
			return "/Summary/SavedItems.aspx?op=add&uid=" + $("#sid").val() + "&rid=" + $("#rid").val() + 
				"&mlstableid=" + listing.mlsTable + "&mlsnum=" + listing.mlsNum;
		},
		
		getSave: function(mlsNum) {
			window.open(this.getSaveLink(this.getListing(mlsNum)));
		},
		
		saveSearch: function() {
			var url = "/Summary/SavedSearches.aspx?op=add&" + s_arrayutil.serializeForm('searchForm');
			window.open(url,'map_save_search','width=700,height=400,resizable=yes,scrollbars=yes');
		},
		
		changeSegment: function(segmentid, scriptFile) {
			var intSegID = parseInt(segmentid);
			if (intSegID == -1) return;
			window.location = scriptFile + "?uid=" + $("#uid").val() + "&segmentid=" + intSegID + "&rid=" + $("#rid").val() +
				"&sid=" + $("#sid").val() + "&sponsorid=" + $("#sponsorid").val() + "&htmlfile=" + $("#htmlfile").val();
		},
		
		doAction: function(elmt, mlsTableID) {
			if (elmt.value == "") return;
			var mlsNums = "";
			var inputs = document.getElementsByTagName("input");
			for (var i=0; i<inputs.length; i++) {
				if (inputs[i].type == "checkbox" && inputs[i].name == "listing" && inputs[i].checked)
					mlsNums += "," + inputs[i].value;
			}
			if (mlsNums == "") {
				alert("No Listing Selected.\nPlease select one or more listings.");
				return;
			}
			else
				mlsNums = mlsNums.substring(1);
			switch(elmt.value) {
				case "save":
					window.open("/Summary/SavedItems.aspx?op=add&uid=" + $("#sid").val() + "&rid=" + $("#rid").val() + 
						"&mlstableid=" + mlsTableID + "&mlsnum=" + mlsNums);
					break;
				default:
					break;
			}
			elmt.value = "";
		}
	}
	
