/**
 * @author Greg Pasquariello
 */
Ext.namespace("Society");

Society.VicinityMap = function(config) {
	this.config = config;
	
	if (!this.config.id) {
		throw "ID must be specified for Society.VicinityMap";
	}
}

Society.VicinityMap.prototype.load = function() {
	Ext.Ajax.request({
		url: "/components/Map/services/getNearbyCompanies.php",
		success: this.onLoadComplete,
	   	params: { cid: this.config.companyId, ctype: this.config.companyType, pid: this.config.propertyId },
		scope: this
	});
}


Society.VicinityMap.prototype.onListItemClick = function(evt, src, options) {
	var i = options.pointIndex;
	
	if (this.config.map && this.config.points) {
		var point = this.config.points[i];
		if (point) {
			var mergedAddress = ((point.latitude != "" && point.longitude != "") ? point.latitude + ", " + point.longitude : "");
			
			var html = "";
			
			html += "<div>" + "<b><a href='" + point.link + "'>" + point.name + "</a></b>";		
			
			// Do not display addresses for properties, can display address for ski, golf, restaurant though
			if (point.industryType != 7) {
				html += "<br/>" + point.address;
				if (point.address2) html += "<br/>" + point.address2;	
			}
			html += "<br/>" + point.city + (point.state ? ", " + point.state : "") + " " + (point.zip ? point.zip : "");
			html += (point.country_name ? "<br/>" + point.country_name : "");
			html += "<br/><br/>" + point.phone;
			html += "<br/><br/>";
			html += "Get directions <a href='http://maps.google.com?daddr=" + mergedAddress + "' target='_blank'>to here</a> or <a href='http://maps.google.com?saddr=" + mergedAddress + "' target='_blank'>from here</a>"
			html += "</div>";
			
			this.config.map.showInfoWindowForIndex(i, html);
		}
	}
}

//
// Invoked when the form load is completed successfully
//
Society.VicinityMap.prototype.onLoadComplete = function(result) {
	if (result.status == 200) {
		result = eval("((" + result.responseText + "))");
		if (result.success == true) {
			var tab;
			var tableBody;
			
			if (this.config.listdiv) {
				tab = this.config.listdiv.insertHtml("beforeEnd", "<table width='100%' cellspacing='3' cellpadding='3'></table>", true);
				//tableBody = this.config.listdiv.insertHtml("beforeEnd", "<tbody></tbody>", true);
				tableBody = tab.insertHtml("beforeEnd", "<tbody></tbody>", true);
			}
			
			this.config.points = new Array();
			
			var validItemCount = 0;
			for (var i = 0; i < result.data.companies.length; i++) {
				var iconImage;
				var shadowImage;
				var blnValidItem;
				
				blnValidItem = false;
				switch (result.data.companies[i].industryType) {
					case "6": /* golf */
						iconImage = "/media/images/icon_golf-" + (i + 1) + ".png";
						shadowImage = "/media/images/icon_golf_shadow.png";
						
						urlName = result.data.companies[i].name;
						if (urlName) {
							urlName = urlName.replace(/ /g, "+");
						}
						
						link = "/golf/" + urlName;
						blnValidItem = true;
						break;
					case "7": /* vacation rental */
						iconImage = "/media/images/icon_vacation-" + (i + 1) + ".png";
						shadowImage = "/media/images/icon_vacation_shadow.png";
						link = "/vacation+rental/" + result.data.companies[i].id
						blnValidItem = true;
						break;
					case "8": /* ski */
						iconImage = "/media/images/icon_ski-" + (i + 1) + ".png";
						shadowImage = "/media/images/icon_ski_shadow.png";
						
						urlName = result.data.companies[i].name;
						if (urlName) {
							urlName = urlName.replace(/ /g, "+");
						}
						
						link = "/snow/" + urlName;
						blnValidItem = true;
						break;
					case "4": /* nightclub */
					case "9": /* restaurant/bar */
						iconImage = "/media/images/icon_restaurant-" + (i + 1) + ".png";
						shadowImage = "/media/images/icon_restaurant_shadow.png";
						link = "/restaurant/" + result.data.companies[i].id
						blnValidItem = true;
						break;
					case "10": /* lodge */
						iconImage = "/media/images/icon_lodge.png";
						shadowImage = "/media/images/icon_lodge_shadow.png";
						break;
				}
				
				if (blnValidItem == true) {
					validItemCount += 1;
				}
				
				// Limit number of items displayed in map and in list below map to 10
				if (validItemCount <= 10) {
					this.config.points[validItemCount-1] = {
						latitude: result.data.companies[i].latitude,
						longitude: result.data.companies[i].longitude,
						name: result.data.companies[i].name,
						phone: result.data.companies[i].phone,
						address: result.data.companies[i].address,
						address2: result.data.companies[i].address2,
						city: result.data.companies[i].city,
						state: result.data.companies[i].state,
						zip: result.data.companies[i].zip,
						country_name: result.data.companies[i].country_name,
						industryType: result.data.companies[i].industryType,
						link: link,
						icon: {
							//image: "http://maps.google.com/mapfiles/ms/micons/grn-pushpin.png",
							//shadow: "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png",
							image: iconImage,
							shadow: shadowImage,
							imageWidth: 25,
							imageHeight: 29,
							shadowWidth: 25,
							shadowHeight: 29,
							anchorX: 5,
							anchorY: 32,
							infoWindowAnchorX: 12,
							infoWindowAnchorY: 12
						}
					}
					
					if (tableBody) {
						//var row = tableBody.insertHtml("beforeEnd", "<tr><td style='vertical-align: middle; padding: 3px; margin: 3px;'><img src='" + result.data.companies[i].smallImage + "'></td><td style='vertical-align: middle; padding: 3px; margin: 3px;'>" + result.data.companies[i].name + "<br>" + "303-555-1212</td></tr>", true);
						var row = tableBody.insertHtml("beforeEnd", "<tr><td style='vertical-align: middle;'></td><td style='vertical-align: middle;'></td></tr>", true);
						
						var td1 = row.dom.childNodes[0];
						var td2 = row.dom.childNodes[1];
						
						td1 = Ext.get(td1);
						td2 = Ext.get(td2);
						
						if (result.data.companies[i].smallImage != "") {
							var image = td1.createChild({
								tag: 'img',
								src: result.data.companies[i].smallImage,
								width: 60,
								style: 'cursor: pointer;'
							});
							image.on("click", this.onListItemClick, this, {
								pointIndex: validItemCount-1
							});
						}
						
						var link = td2.createChild({
							tag: 'span',
							html: result.data.companies[i].name + "<br/>" + result.data.companies[i].phone,
							style: 'cursor: pointer;'
						});
						link.on("click", this.onListItemClick, this, {
							pointIndex: validItemCount-1
						});
					}
				}
			}
			
			this.config.map.addPoints(this.config.points);
		}
	}
}

//
// Invoked when the form load fails
//
Society.VicinityMap.prototype.onLoadFailure = function() {
	
}

Society.VicinityMap.prototype.onFilterChange = function() {
	var idx = this.config.filterDropdown.dom.selectedIndex;
	var selected = this.config.filterDropdown.dom.options[idx];
	
	this.config.map.clearPoints();
	this.config.companyType = selected.value;
	
	var parent = this.config.listdiv.dom;
	
	while ( parent.hasChildNodes() ) { 
		parent.removeChild(parent.firstChild);
	}

	this.load();
}

Society.VicinityMap.prototype.render = function() {
	if (this.config.host) {
		this.config.host = Ext.get(this.config.host);
		
		width = 776;
		
		this.config.mapdiv = this.config.host.createChild({
			tag: 'div',
			id: this.config.id + "_mapDiv",
			style: 'width: 100%; height: 300px'
		});
		
		this.config.filterDiv = this.config.host.createChild({
			tag: 'div',
			id: this.config.id + "_filterDiv",
			style: 'padding: 3px 0px 3px 0px; width: 100%; height: 26px; line-height: 26px; text-align: center; vertical-align: middle; background: white;',
			html: 'Show Nearby: '
		});
		
//		this.config.filterDiv.createChild({
//			tag: 'span',
//			html: 'Show Nearby: ',
//			style: 'background: white;'
//		});
		
		this.config.filterDropdown = this.config.filterDiv.createChild({
			tag: 'select',
			id: this.config.id + "_filterDropdown",
			children: [
				{tag: 'option', value: 0,  html: 'All'},
				{tag: 'option', value: 10, html: 'Lodge/Inn'},
				{tag: 'option', value: 9,  html: 'Restaurant/Bar'},
				{tag: 'option', value: 4,  html: 'Night Club'},
				{tag: 'option', value: 8,  html: 'Ski Resort'},
				{tag: 'option', value: 6,  html: 'Golf Resort'},
				{tag: 'option', value: 7,  html: 'Vacation Rental'}
			]
		});
		
		this.config.filterDropdown.on("change", this.onFilterChange, this);
		
		this.config.listdiv = this.config.host.createChild({
			tag: 'div',
			id: this.config.id + "_listDiv",
			style: 'width: 315px; overflow: auto;'
		});
		
		this.config.map = new p5.Map({
			name: 'mapControl1',
			renderTo: this.config.id +"_mapDiv",
			latitude: this.config.latitude,
			longitude: this.config.longitude,
			zoomlevel: this.config.zoomLevel,
			smallMapControl: true
		});
		
		this.load();
	}
	else throw "No host div specified for the vicinityMap";
}

