if (!CLO ) var CLO = {};

CLO.PropertyDetails = Class.create();
CLO.PropertyDetails.addMethods({
	options: null,
	locationMap: null,
	locationMapTab: null,
	activeTabClassName: null,
	aResetMap: null,
	aAddToShortlist: null,
	locationMapPins: [],
	_ieFixApplied: false,
	propertyDetailsTabControl: null,
	
	initialize:function(options) {
		this.options = $H({
			mapPosition: {lat: 0, longt: 0, alt: 8},
			locationMapTabID: "locationMapTab",
			locationMapDivID: "locationMap",
			activeTabClassName: "active",
			resetMapID: "resetMap",
			addToShortlistID: "addToShortlist",
			locationMapPinsList: "localMapLinkMarkers",
			markerLinkClassName: "markerLink",
			pinPathPrefix: "/images/map_",
			pinPathSuffix: ".png",
			myPropertyShortlistID: "myPropertyShortlist",
			shortlistPropertyPrefix: "#shortlistProperty_",
			mainImageID: "mainImage",
			mainImageDimensions: {width: 415, height: 290},
			tabControlContainerID: "propertyDetailsTabControl"
		}).merge(options || {});
		this.locationMapTab = $(this.options.get("locationMapTabID"));
				
		this.locationTabHandle = $(this.options.get("locationTabHandle"));
		this.aResetMap = $(this.options.get("resetMapID"));
		this.aAddToShortlist = $(this.options.get("addToShortlistID"));
		this.myPropertyShortlist = $(this.options.get("myPropertyShortlistID"));
				
		this.propertyDetailsTabControl = new CLO.TabControl(this.options.get("tabControlContainerID"));
		
		if(this.locationMapTab && ($(this.options.get("locationMapPinsList")))) {
			Event.observe(this.aResetMap, "click", this.aRestMap_onClick.bindAsEventListener(this));
			Event.observe(this.aAddToShortlist, "click", this.aAddToShortlist_onClick.bindAsEventListener(this));
			this.locationMapPins = this.getLocationMapPins($(this.options.get("locationMapPinsList")));
			this.loadMap();
			
			//IE Print Map Fix
			Event.observe(window, "beforeprint", this.window_onBeforePrint.bind(this));
			Event.observe(window, "afterprint", this.window_onAfterPrint.bind(this));
		}
	},
	
	loadMap: function() {
		var toggled = false;
		if(!this.locationMapTab.hasClassName(this.options.get("activeTabClassName"))) {toggled = true; this.locationMapTab.addClassName(this.options.get("activeTabClassName"))};
		Event.observe('locationTab', 'click', function() {
			setTimeout(function(){this.locationMap.gmap.checkResize()}.bind(this), 20);
		}.bind(this));
		this.locationMap = new CLO.PropertyDetails.LocationMap({
			locationMapDivID: this.options.get("locationMapDivID"),
			position: this.options.get("mapPosition"),
			pins: this.locationMapPins
		});
		this.locationMap.gmap.checkResize();
		if(toggled) {setTimeout(function() {this.locationMapTab.removeClassName(this.options.get("activeTabClassName"))}.bind(this), 20);};
	},
	
	getLocationMapPins: function(pinList) {
		var pins = [];
		if(pinList) {
			$A(pinList.select("." + this.options.get("markerLinkClassName"))).each(function(node){
				var p = {};
				var ll = node.readAttribute("rel").split(",");
				p.lat = ll[0];
				p.longt = ll[1];
				p.innerHTML = $(node.parentNode).down(".desc").innerHTML;
				p.iconURL = this.options.get("pinPathPrefix") + ll[2] + this.options.get("pinPathSuffix");
				p.anchor = node;			
				pins.push(p);
			}.bind(this));
		}
		return pins;
	},
	
	window_onBeforePrint: function() {
		if(!this.locationMapTab.hasClassName(this.options.get("activeTabClassName"))) {
			this.locationMapTab.addClassName(this.options.get("activeTabClassName"));
			this.locationMap.gmap.checkResize();
			this._ieFixApplied = true;
		} else {
			this._ieFixApplied = false;
		}
	},
	window_onAfterPrint: function() {
		if(this._ieFixApplied) {
			this.locationMapTab.removeClassName(this.options.get("activeTabClassName"));
		}
	},
	
	aRestMap_onClick: function(e) {
		Event.stop(e);
		this.locationMap.resetMap();
	},
	
	aAddToShortlist_onClick: function(e) {
		Event.stop(e);
		var a = Event.findElement(e, 'a')
		var pId = a.readAttribute('rel');
		new Ajax.Updater(this.myPropertyShortlist, a.href, {onComplete: function() {
			new Effect.Highlight(this.myPropertyShortlist.down('ul').down(this.options.get("shortlistPropertyPrefix") + pId).parentNode);
		}.bind(this)})
		new CLO.FlyTo(this.options.get("mainImageID"), this.myPropertyShortlist, {originalWidth: this.options.get("mainImageDimensions").width, originalHeight: this.options.get("mainImageDimensions").height});
	},
	
	locationMapTab_onClick: function() {
		this.locationMap.gmap.checkResize();
	}
});

CLO.PropertyDetails.LocationMap = Class.create();
CLO.PropertyDetails.LocationMap.addMethods({
	options: null,
	gmap: null,
	baseIcon: null,
	
	initialize: function(options) {
		this.options = $H({
			locationMapDivID: "locationMap",
			position: {lat: 0, longt: 0, alt: 12},
			pins: [],
			baseIconURL: "/images/map_shadow.png"
		}).merge(options || {});

		this.baseIcon = this.getBaseIcon(this.options.get("baseIconURL"));
		this.gmap = this.getGmap($(this.options.get("locationMapDivID")), this.options.get("position"));
		this.addPins(this.options.get("pins"));
	},
	
	getBaseIcon: function(baseIconURL) {
		var baseIcon = new GIcon();
		baseIcon.shadow = baseIconURL;
		baseIcon.iconSize = new GSize(24, 34);
		baseIcon.shadowSize = new GSize(36, 31);
		baseIcon.iconAnchor = new GPoint(24, 34);
		baseIcon.infoWindowAnchor = new GPoint(15, 10);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		return baseIcon;
	},
	
	getGmap: function(mapDiv, initialPosition) {
		gmap = new GMap2(mapDiv);
		gmap.addControl(new GSmallMapControl());
		gmap.addControl(new GMapTypeControl());
		gmap.setCenter(new GLatLng(initialPosition.lat, initialPosition.longt), initialPosition.alt);
		return gmap;
	},
	addPins: function(pins) {
		$A(pins).each(function(pin) {
			var icon = new GIcon(this.baseIcon);
			icon.image = pin.iconURL;
			var marker = new GMarker(new GLatLng(pin.lat, pin.longt), icon);
			this.gmap.addOverlay(marker);
			if(pin.anchor) {
				Event.observe(pin.anchor, "click", function(e) {
					marker.openInfoWindowHtml(pin.innerHTML);
					//new Effect.ScrollTo(this.options.get("locationMapDivID"), {offset: -150});
					Event.stop(e);
				}.bindAsEventListener(this));
			}
			GEvent.addListener(marker, 'click', function() {
				marker.openInfoWindowHtml(pin.innerHTML);
			});
		}.bind(this));
	},
	
	resetMap: function() {
		this.gmap.setCenter(new GLatLng(this.options.get("position").lat, this.options.get("position").longt), this.options.get("position").alt);
	}
});
