

var wsj_mexico_map = {
	initialize:function(locations, mapdiv, map_options, options) {
		this.options = options
		map_options = (!map_options) ? {}: map_options;
		this.mapdiv = document.getElementById(mapdiv);
		this.locations = locations;
		/*
		this.locations.todays_category = (_category) ? _category :
			this.locations.todays_category;
			
		this.locations.todays_location = (_site) ? 
			this.locations.locations[_category][_site].coord : this.locations.todays_location;
		*/
		this.locations.update_today(_category, _site);	
		this.coord = this.locations.get_todays_coord();
		map_options = this.locations.options;
		map_options.center = this.locations.get_todays_latLng();
		this.db(map_options);
		for (var m in map_options) { 
			this.db(m + " : " + map_options[m]);	
		}
		this.map = new google.maps.Map(this.mapdiv, map_options);
		if (!map_options.no_markers) { 
			this.setup_markers();
		}
		this.locations.create_location_index();
		return;
		//this.map.setCenter(this.locations.get_next_latLng());
		if (this.options.auto_cycle) {
			this.auto_cycle(this.options.auto_cycle); 
		}
		this.map_options = map_options;
	},
	update_today:function(category, site, go) { 
		this.db(category);
		this.db(site);
		this.db("***");
		this.options.auto_cycle.category = (category) ? 
			category : this.options.auto_cycle.category;
		this.locations.update_today(category, site);
		if (go) { this.auto_cycle(this.options.auto_cycle)}
	},
	auto_cycle:function(options) {
		if (!this.options.auto_cycle) { return ; }
		var ts = this;
		var c = ts.locations.get_current(options.category);
		ts.cycle_start_index = (typeof(ts.cycle_start_index)=='undefined') ? c[1] : ts.cycle_start_index;

		var m = ts.setup_marker(
			c[0], c[1]
			);
		ts.open_info_window(
			c[0], c[1], m, ts);
		google.maps.event.addListener(ts.map, 'click', function() { 
			clearInterval(ts.cycle_int);	
		});
		ts.cycle_start=true;
		if (setInterval) { 
				this.cycle_int = setInterval(function() {
				ts.centerTo(ts, options);
				var c = ts.locations.get_current(options.category);
				if (c[1] == ts.cycle_start_index && 
					ts.cycle_start==false) { 
					ts.map.setCenter(ts.locations.get_latLng(ts.locations.todays_location));
					ts.info.close();
					ts.map.setZoom(11);
					clearInterval(ts.cycle_int);
					return;	
				}
				ts.cycle_start = false;
				var m = ts.setup_marker(
					c[0], c[1]
					);
				ts.open_info_window(
					c[0], c[1], 
					m, ts
					);
			}, 
			options.time);
		}
	}, 
	centerTo:function(ts, options) {
		var nc
		if (options.category) { 
			nc = ts.locations.get_next_latLng(true);
		} else { 
			nc = ts.locations.get_next_latLng();
		}
		ts.map.setCenter(nc);
	},
	setup_markers:function() { 
		var marker;
		this.markers = [];
		for (var i in this.locations.locations) {
			for (var ix=0; ix< this.locations.locations[i].length; ix++) {
				marker = this.setup_marker(i, ix) ; 
				this.markers.push(marker);
				this.setup_info_window(i, ix, marker); 
			} 
			/*
			this.db(i);
			//marker = this.setup_marker(i, 0);
			this.markers.push(marker);
			this.setup_info_window(i, 0, marker);
			marker = this.setup_marker(i, 1);
			this.markers.push(marker);
			this.setup_info_window(i, 1, marker);
			*/
		}
	}, 
	setup_marker:function(i, ii) { 
		var marker; 
		var opt = this.locations.get_marker_options(i, ii);
		if (!opt.position) { return false} 
		opt.map = this.map;
		opt.minWidth = 150;
		opt.minHeight = 150;
		opt.maxWidth = 200;
		opt.maxHeight = 150;
		marker = new google.maps.Marker(opt);
		var xd = this;
		var oi = this.open_info_window;
		google.maps.event.addListener(marker, 
			'click', 
			function() { oi(i, ii, marker, xd) } );
		return marker
	}, 
	setup_info_window:function(i, ii, marker) {
		var info;
		var opt = this.locations.get_info_window_options(i,ii); 
		info = new google.maps.InfoWindow(opt);
		
	},
	open_info_window:function(i, ii, marker, xd) {
		var opt = xd.locations.get_info_window_options(i, ii); 
		opt.minWidth = 150;
		opt.minHeight = 150;
		opt.maxWidth = 200;
		opt.maxHeight = 150;
		if (xd.info) { xd.info.close()};
		xd.info = new google.maps.InfoWindow(opt);
		xd.info.open(xd.map, marker);
		
	},
	db:function(v) { 
		try{ 
			console.log(v); 
		} catch(e) { 
		}
	}
	
}

