/// <reference path="../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />

/**
 *
 * GoogleMaps
 *
 * This class used to google maps functionality for the SC64 solution
 *
 */

// Check that namespace into which the Class definition will be creates has been defined & if not then create

if (!mrm.global.isNamespaceDefined("mrm.cms.display")) mrm.global.createNamespace("mrm.cms.display", "1.0");

mrm.cms.display.GoogleMaps = Object.subClass
(
	{
		/*
		=============================
		PROPERTIES
		=============================
		*/
		INITIAL_LON : 0,
		INITIAL_LAT : 0,
		INITIAL_ZOOM : 0,
		MAP_DOM_CONTAINER_ID : 'map_canvas',
		MARKERS : new Array(),
		CURRENT_INFO_WINDOW : null,		
		
		/*
		=============================
		CONSTRUCTOR
		=============================
		*/
		"init": function() {
			//store the scope pointer
			var _this = this;

			//bind the event used to display the map
			$(document).bind("MRM-JAVASCRIPT-RUN", function(jqEvent) {
				_this.DisplayMap(jqEvent);
			});
			
			//bind the event used to configure the defaults used for the google map
			$(document).bind("MRM-JAVASCRIPT-INSERT-MAP-DEFAULTS", function(jqEvent, args) {
				_this.SetMapDefaults(jqEvent, args);
			});
			
			//bind the event used to load a new marker into the marker array
			$(document).bind("MRM-JAVASCRIPT-INSERT-MAP-DATA", function(jqEvent,args) {
				_this.CreateMarkerDataObject(jqEvent,args);
			});
		},
		
		/*
		=============================
		METHODS 
		=============================
		*/
		//Method used to configure the default settings for this instance of the google map
		"SetMapDefaults" : function(jqEvent, args) {
			//store the scope pointer
			var _this = this;
			
			//set the map defaults
			_this.INITIAL_LON = parseFloat(args.initialLong);
			_this.INITIAL_LAT = parseFloat(args.initialLat);
			_this.INITIAL_ZOOM = parseFloat(args.initialZoom);
		},		
		
		//method used to add the details of another marker to the marker array
		"CreateMarkerDataObject" : function(jqEvent, args) {
			//create scope pointer
			_this = this;
			
			//add the args to the index at the specified index
			_this.MARKERS[args.index] = args;			
		},
		
		//method used to display the map
		"DisplayMap" : function(jqEvent) {
			//create the scope pointer
			_this = this;
			
			//load the map as all of the data should now be present
			var map = _this.LoadMap();
			
			//load all of the markers in the MARKERS array			
			_this.LoadMarkers(map);
		},

		
		//Method used to load the map object and move it to the required lon-lat
		"LoadMap" : function() {
			//create the scope pointer
			var _this = this;
			
			//create map load latlong
			var latlng = new google.maps.LatLng(_this.INITIAL_LAT,_this.INITIAL_LON);
			
			//configure the initial lat long location of the map to be centre1
			var myOptions = {
				zoom: _this.INITIAL_ZOOM,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
				,scrollwheel: false
			}
			var map = new google.maps.Map(document.getElementById(_this.MAP_DOM_CONTAINER_ID), myOptions);		    
			
			return map;		
		},
		
		//loads the markers that have been selected
		"LoadMarkers": function(map) {
			//create the scope pointer
			_this = this;
			
			//iterate the markers array outputting each to the map object	
			for(var i=0; i<_this.MARKERS.length; i++) {
				//pass the instance of the args into the LoadMarker method
				_this.LoadMarker(_this.MARKERS[i].lon,
					_this.MARKERS[i].lat,
					_this.MARKERS[i].icon,
					_this.MARKERS[i].title,
					_this.MARKERS[i].html,
					map);
			}
		},
		
		//Method used to load a single marker object onto the map object that has been passed in
		"LoadMarker" : function(lon,lat,image,title,html,map) {
			//create the scope pointer
			_this = this;
			
			//create the latlng object
			var latlng = new google.maps.LatLng(lat,lon);
			
			//create the marker and add it to the map object
			var marker = new google.maps.Marker({
				position: latlng, 
				map: map, 
				title:title,
				icon: image
			});
			
			//create the inforwindow object
			var infowindow = new google.maps.InfoWindow({ content: html });
			
			//add the click handler to the map to render the infowindow for the correct marker
			google.maps.event.addListener(marker, 'click', function() {
				if(_this.CURRENT_INFO_WINDOW) {
					_this.CURRENT_INFO_WINDOW.close();
				}
			  
				_this.CURRENT_INFO_WINDOW = infowindow;
				infowindow.open(map,marker);
				_this.PngFixGoogleLocations();
			});

			
		},
	
		"PngFixGoogleLocations" : function()
		{
			var ASHXFILE = "img[src*='.ashx']";
			var BLANKGIF = "/common/assets/images/_blank.gif";
			var MAPLOCATIONS_M = ".location-container div a " + ASHXFILE;
			var MAPLOCATIONS_S = ".location-container-single div a " + ASHXFILE;
			var IE6BROWSER = ($.browser.msie && $.browser.version < 7) ? true : false;

			//If IE6 
			if (IE6BROWSER) {
				//If container has multiple logos
				$(MAPLOCATIONS_M).each(function(index) {
					
					//Get image src for each image
					var ImageSrc = $(this).attr("src");

					//Set the src of the filter attr to the src of the image
					var filter = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + ImageSrc + "', sizingMethod='image')";
		
						//Set src to blank gif to hide the original greyed image
						$(this).attr("src", BLANKGIF);
						//Set filter to show png fixed image
						$(this).attr("style", filter);		
				});

				//If container has only a single logo
				$(MAPLOCATIONS_S).each(function(index) {
					//Get image src for each image
					var ImageSrc = $(this).attr("src");

					//Set the src of the filter attr to the src of the image
					var filter = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + ImageSrc + "', sizingMethod='image')";
		
						//Set src to blank gif to hide the original greyed image
						$(this).attr("src", BLANKGIF);
						//Set filter to show png fixed image
						$(this).attr("style", filter);		
				});

			}

		}

});
