/**
Vertigo Tip by www.vertigo-project.com
*/
this.vtip = function() {    
    this.xOffset = 10; // x distance from mouse
    this.yOffset = 10; // y distance from mouse       
    
    $("area").unbind().hover(    
        function(e) {
            this.t = this.title;
            this.title = ''; 
            this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
            
            $('body').append( '<p id="vtip"><img id="vtipArrow" />' + this.t + '</p>' );
                        
            $('p#vtip #vtipArrow').attr("src", 'images/vtip_arrow.png');
            $('p#vtip').css("top", this.top+"px").css("left", this.left+"px").show();//.fadeIn("slow");
            
        },
        function() {
            this.title = this.t;
            $("p#vtip").fadeOut("slow").remove();
        }
    ).mousemove(
        function(e) {
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
                         
            $("p#vtip").css("top", this.top+"px").css("left", this.left+"px");
        }
    );            
    
};
/* --vertigo tip */

var setGIcon = function (icon, ifile) {
    icon.image = S_URL_IMG + '/markers/' + ifile;
    icon.shadow = S_URL_IMG + '/markers/marker_shadow.png';
	icon.transparent = S_URL_IMG + 'markers/marker_transparent.png';    
	icon.iconSize = new GSize(31, 37);
    icon.shadowSize = new GSize(66, 37);
	icon.iconAnchor = new GPoint(16,37);
	icon.infoWindowAnchor = new GPoint(16,0);
	icon.imageMap = [18,0,21,1,23,2,24,3,26,4,26,5,27,6,28,7,28,8,29,9,29,10,29,11,30,12,30,13,30,14,30,15,30,16,30,17,30,18,29,19,29,20,29,21,28,22,28,23,28,24,27,25,27,26,26,27,25,28,24,29,23,30,21,31,20,32,19,33,18,34,17,35,16,36,9,36,8,35,7,34,6,33,6,32,4,31,3,30,2,29,1,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,1,4,2,3,3,2,5,1,8,0];
}
var ICON_HOUSE = new GIcon();
setGIcon(ICON_HOUSE, 'marker_default.png');
var ICON_SUPERMARKET = new GIcon();
setGIcon(ICON_HOUSE, 'marker_default.png');

var createSpotMarker = function (spotLat, spotLng, spotIcon, spotUrl, infoHtml) {
	var point = new GLatLng(parseFloat(spotLat),
							parseFloat(spotLng));					
	var marker = new GMarker(point, {
		icon: spotIcon,
		draggable: false
	});
	marker.tooltip = '<div class="tooltip"><nobr>'+infoHtml+'<\/nobr><\/div>';
	GEvent.addListener(marker, "mouseover", function() {
		//marker.openInfoWindowHtml(infoHtml);
		showTooltip(marker);
	});
	GEvent.addListener(marker,"mouseout", function() {
		//marker.closeInfoWindow();
		tooltip.style.visibility="hidden";
	});
	GEvent.addListener(marker,"click", function() {
		window.location.href = spotUrl;
	}); 
	return marker;	
}
var showTooltip = function (marker) {
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}

var bindMapControls = function (map) {
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    $('#mapctl_zoomout').bind('click', function () {
        map.zoomOut();
    });
    $('#mapctl_zoomin').bind('click', function () {
        map.zoomIn();
    });
    $('#mapctl_typemap').bind('click', function () {
        map.setMapType(G_NORMAL_MAP);
    });
    $('#mapctl_typesatellite').bind('click', function () {
        map.setMapType(G_SATELLITE_MAP);
    });
    $('#mapctl_typehybrid').bind('click', function () {
        map.setMapType(G_HYBRID_MAP);
    });
}

function loadSpotlistMap (spotArray) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("spotlist-map"));
		map.setCenter(new GLatLng(46.830134,1.80175), 5);
		map.disableScrollWheelZoom();

		var tooltip = document.createElement("div");
		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
		tooltip.style.visibility="hidden";		
		
		bindMapControls(map);
		$('.scrollable-map').scrollFollow({
			speed: 300
		});
		for (var item in spotArray) {
			var html = '<div class="spot_infowindow"><h4>'+spotArray[item][3]+'</h4><p>afficher</p></div>';
			var marker = createSpotMarker(
								spotArray[item][0], 
								spotArray[item][1],
								ICON_HOUSE,
								spotArray[item][2],
								html);
			map.addOverlay(marker);             
		}		
    }
}

var bind_live_actions = function () {  
  $(".display_cities").live("click", function () {
      $("#citiesfulllist").toggle("fast");
  });
}

var bind_search_events = function() {
	if ($("#search_input").val() == '') {
		$("#search_input").val('magasin, ville');
		$("#search_input").css('color', '#999');
	}
	$("#search_input").bind("focus", function() {
		if ($("#search_input").val() == 'magasin, ville') {
			$("#search_input").val('');
			$("#search_input").css('color', 'black');
		}
	});
	$("#search_input").bind("blur", function() {
		if ($("#search_input").val() == '') {
			$("#search_input").val('magasin, ville');
			$("#search_input").css('color', '#999');
		}
	});	
	$("#frm-search").submit(function() {
		if (($("#search_input").val() == 'magasin, ville') || ($("#search_input").val() == '')) {
			$("#search_input").val('').focus();
			return false;
		}	
	});	

}

function initSpotMap(lat, lng) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("spot_map_container"));
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GLargeMapControl3D());  
		map.addControl(new GMapTypeControl());       
		var point = new GLatLng(lat, lng);
		map.setCenter(point, 15);
		//map.enableScrollWheelZoom();	
		var marker = new GMarker(point, {
			draggable: false
		});
		map.addOverlay(marker);
	}
}  

/*
* 
* document ready
* objectif: factoriser au maximum pour que document.ready n'exécute que des fonctions mères et de setup
* 
*/
$(document).ready(function () {
  bind_live_actions();
  bind_search_events();
  vtip();
  $(".tooltip").tooltip({position:'bottom left', delay: '50', offset:[-25, -5]});
});



