/* $Id: specialist.js 229 2009-07-27 09:24:09Z edin.coralic $ */

var Specialist = {

	map: null,
	map_container: null,
	single: false,
	map_x: 0,
	map_y: 0,
	map_zoom: 0,
	map_xy_marker: null,
	map_markers: null,
	direction: null,

	run: function()
	{
		Specialist.generateMapMarkersArray();
		Specialist.generateAjaxLoader();

		if (GBrowserIsCompatible()) {
			Specialist.map_x = $('.specialist input[name="google_map_my_location_x"]').val();
			Specialist.map_y = $('.specialist input[name="google_map_my_location_y"]').val();
			var zoom = 10;

			Specialist.map_container = $('.specialist .google_map:first');
			Specialist.single = Specialist.map_container.hasClass('single');

			Specialist.map = new GMap2(Specialist.map_container.get(0));
			Specialist.map.setCenter(new GLatLng(Specialist.map_x, Specialist.map_y), zoom);
			Specialist.map.addControl(new GSmallZoomControl());

			Specialist.map_zoom = Specialist.map.getZoom();

			Specialist.allowed_bounds = new GLatLngBounds(new GLatLng(49.5,-10), new GLatLng(59,2.6));

			// Overwrite the getMinimumResolution() and getMaximumResolution() methods
			var mt = Specialist.map.getMapTypes();
			for (var i=0; i < mt.length; i++) {
				mt[i].getMinimumResolution = function() {return 5;}
			}

			$('.specialist .google_map_search input[type="button"]').bind('click', Specialist.searchClick);
			$('.specialist .google_map_search input[name="address"]').bind('keypress', Specialist.searchKeyPress);

			Specialist.setSize();
			Specialist.setIcons();
			Specialist.setMarker();
			Specialist.setXmlSpecialists();
			Specialist.setListJumps();
			if (Specialist.single) {
				Specialist.setDirection();
			} else {
				Specialist.fetchSpecialist();
			}
		}
	},

	generateMapMarkersArray: function()
	{
		var markers = new Array();
		for (i=0; i < 17; i++) {
			markers[i] = new Array();
		}
		Specialist.map_markers = markers;
	},

	generateAjaxLoader: function()
	{
		$('.specialist .items').before('<img class="ajax_loader" src="/media/dsg/ajax_loader.gif" alt="" />');
		Specialist.loader = $('.specialist img.ajax_loader');
		Specialist.loader.hide();
	},

	setSize: function()
	{
		/*
		var items = $('.specialist div.items');
		var height = items.height() > 300 ? items.height() : 300;
		Specialist.map_container.css('height', height +'px');
		Specialist.map.checkResize();
		*/
	},

	setMarker: function(zoom_used)
	{
		if (Specialist.map_xy_marker != null) {
			Specialist.map.removeOverlay(Specialist.map_xy_marker);
		}
		var point = new GLatLng(Specialist.map_x, Specialist.map_y);
		if (!zoom_used) {
			Specialist.map.panTo(point);
		}
		Specialist.map_xy_marker = new GMarker(point, Specialist.iconRed);
		Specialist.map.addOverlay(Specialist.map_xy_marker);
	},

	searchClick: function(event)
	{
		Specialist.search();
	},
	searchKeyPress: function(event)
	{
		if (event.which == 13) {
			Specialist.search();
		}
	},
	search: function()
	{
		var address = $('.specialist .google_map_search input[name="address"]').val();

		$.ajax({
			url: this.href,
			dataType: 'json',
			type: 'GET',
			data: {
				plugin: 'specialist',
				mode: 'fetchAddressLocation',
				address: address
			},
			success: function(data) {
				if (data && (data['x'] || data['y'])) {
					Specialist.map_x = data['x'];
					Specialist.map_y = data['y'];
					Specialist.setMarker();
					if (Specialist.single) {
						Specialist.setDirection();
					}
				}
			}
		});
	},

	setIcons: function()
	{
		var iconBlue = new GIcon();
		iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
		iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
		iconBlue.iconSize = new GSize(12, 20);
		iconBlue.shadowSize = new GSize(22, 20);
		iconBlue.iconAnchor = new GPoint(6, 20);
		iconBlue.infoWindowAnchor = new GPoint(5, 1);
		Specialist.iconBlue = iconBlue;

		var iconRed = new GIcon();
		iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
		iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
		iconRed.iconSize = new GSize(12, 20);
		iconRed.shadowSize = new GSize(22, 20);
		iconRed.iconAnchor = new GPoint(6, 20);
		iconRed.infoWindowAnchor = new GPoint(5, 1);
		Specialist.iconRed = iconRed;
	},

	setXmlSpecialists: function()
	{
		var url = location.href + (location.href.indexOf('?') == -1 ? '?' : '&') + 'specialist=xml';

		GDownloadUrl(url, function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var name = markers[i].getAttribute("name");
				var zoom = parseInt(markers[i].getAttribute("zoom"));
				var address = markers[i].getAttribute("address");
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
				var marker = Specialist.setXmlSpecialistsCreateMarker(point, name, address);
				Specialist.map_markers[zoom][i] = marker;
				//Specialist.map.addOverlay(marker);
			}
			Specialist.setMapMarkers();
		});
	},
	setXmlSpecialistsCreateMarker: function(point, name, address)
	{
		var marker = new GMarker(point, Specialist.iconBlue);
		var html = "<b>" + name + "</b><br />" + address +'<div class="spacer" style="height:2em"></div>';
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	},

	setMapMarkers: function()
	{
		Specialist.map.clearOverlays();
		Specialist.setMarker(true);
		jQuery.each(Specialist.map_markers, function(zoom, val) {
			jQuery.each(val, function(i, marker) {
				if (marker) {
					Specialist.map.removeOverlay(marker);
					if (Specialist.map_zoom >= zoom) {
						Specialist.map.addOverlay(marker);
					}
				}
			});
		});
	},

	setListJumps: function()
	{
		$('.specialist .items img.icon').bind('click', Specialist.setListJumpsClick);
	},
	setListJumpsClick: function(event)
	{
		var xy = this.alt.split(',');
		var point = new GLatLng(xy[0], xy[1]);
		Specialist.map.panTo(point);
	},

	fetchSpecialist: function()
	{
		//GEvent.bind(Specialist.map, "move", Specialist, Specialist.checkBounds);
		GEvent.bind(Specialist.map, "moveend", Specialist, Specialist.fetchSpecialistOnMoveEnd);
		Specialist.fetchSpecialistOnMoveEnd(null);
	},
	fetchSpecialistOnMoveEnd: function(event)
	{
		var bounds = Specialist.map.getBounds();
		var zoom = Specialist.map.getZoom();

		if (Specialist.map_zoom != zoom) {
			Specialist.map_zoom = zoom;
			Specialist.setMapMarkers();
		}

		$.ajax({
			url: this.href,
			dataType: 'html',
			type: 'GET',
			data: {
				plugin: 'specialist',
				mode: 'fetchSpecialistByBounds',
				sw: bounds.getSouthWest().toUrlValue(8),
				ne: bounds.getNorthEast().toUrlValue(8),
				my: Specialist.map_x +','+ Specialist.map_y,
				zoom: zoom,
				brand_id: $('.specialist input[name="brand_id"]').val()
			},
			beforeSend: function() {
				Specialist.loader.show();
			},
			success: function(data) {
				$('.specialist .items').empty().html(data);
				$('.specialist .items img[src$=".png"]').ifixpng();
				Specialist.setListJumps();
				Specialist.setSize();
			},
			complete: function() {
				Specialist.loader.hide();
			}
		});
	},

	setDirection: function()
	{
		if (Specialist.direction == null) {
			Specialist.direction = new GDirections(Specialist.map, $('.specialist .google_map_direction:first').get(0));
		}
		Specialist.direction.clear();
		var from = $('.specialist input[name="address"]').val();
		var to = $('.specialist .items .address:first').text();
		Specialist.direction.load('from: '+ from +' to: '+ to);
	},

	checkBounds: function()
	{
		if (Specialist.allowedBounds.contains(map.getCenter())) {
			return;
		}

		var C = map.getCenter();
		var X = C.lng();
		var Y = C.lat();

		var AmaxX = allowedBounds.getNorthEast().lng();
		var AmaxY = allowedBounds.getNorthEast().lat();
		var AminX = allowedBounds.getSouthWest().lng();
		var AminY = allowedBounds.getSouthWest().lat();

		if (X < AminX) {X = AminX;}
		if (X > AmaxX) {X = AmaxX;}
		if (Y < AminY) {Y = AminY;}
		if (Y > AmaxY) {Y = AmaxY;}

		map.setCenter(new GLatLng(Y,X));
	}

}
StartUp(Specialist);