if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}


var mapVisible =false;
var firstTime = true;
var map = null;
var effectsDisabled = false;

function initialise()
{
	var mapElem = document.getElementById("map");
	
	if (!mapElem || 'Map' == mapElem.name) return;

	map = new GMap2(mapElem);

	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl ());
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GOverviewMapControl());

	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "/commun/images/pin.png";
	var markerOptions = { icon: icon };

	var bounds = new GLatLngBounds();
	var i = 0;
	points.forEach(function(point) {
		var marker = new GMarker(point[0], markerOptions);
		map.addOverlay(marker);
		bounds.extend(point[0]);
		GEvent.addListener(marker, "mouseover", function() {
			showInfoForMarker(marker, point);
		});

		GEvent.addListener(marker, "mouseout", function() {
			hideInfoBubble();
		});
		
		GEvent.addListener(marker, "click", function() {
			window.location.href = point[5];
		});
		
		$('.mapLink' + i).bind('click', i, function(e) {
			$.scrollTo("#showmap", 500, {offset: {top: 0}});
			showMap(function() {
				
				showInfoForMarker(marker, point);
			});
		});

		i = i + 1;
	});

	GEvent.addListener(map, "move", window.GMap_MoveStart);
	
	map.setCenter(bounds.getCenter());
	map.setZoom(map.getBoundsZoomLevel(bounds));

	
	
	
	$("#showmap a:first-child").click(function() {
		toggleMap();
	});

	/* The roll-down breaks google maps horribly on IE */
	if(jQuery.browser.msie) {
		effectsDisabled = true;
	}

	$("#showmap").show();
}
function GMap_MoveStart()
{
   hideInfoBubble() ;
}
var _balloon;

function Balloon(marker) {
	this._marker = marker;
	var div = document.createElement("div");
	div.className = 'tooltip';
	this._div = div;
	this._div.style.visibility = 'hidden';
	document.body.appendChild(div);
}

Balloon.prototype.setText = function(text) {
    this._text = text;
    this._div.innerHTML = text;
}


Balloon.prototype.show = function(){
   
	var markerPos = map.fromLatLngToContainerPixel(this._marker.getPoint());
	var iconAnchor = this._marker.getIcon().iconAnchor;
	var p = $("#map");
	var offset = p.offset();
	var yPos = offset.top + markerPos.y - this._div.clientHeight - 40;
	var xPos = Math.round(offset.left + markerPos.x - this._div.clientWidth / 2);
	this._div.style.top =  yPos + 'px';
	this._div.style.left =  xPos + 'px';
	this._div.style.visibility = 'visible';
	
}

Balloon.prototype.hide = function(){
	this._div.style.visibility = 'hidden';
}



function showInfoForMarker(marker, point)
{
/////// hide any bubbles if there are there 
hideInfoBubble();

var html = '\
    <div class="toolrel" valign="top"><a href="' + point[5] + '"><img class="thumb" src="' + point[3] + '"></a><div class="in"><a href="' + point[5] + '">' + point[1] +'</a><br>'  + point[2] +'<br>' + point[4] + '<\div>\
    <div class="tail"></div> \
    </div>';
marker.tooltipHtml = html;
_balloon = new Balloon(marker, marker.tooltipHtml);
marker.tooltip = _balloon;
_balloon.setText(marker.tooltipHtml);
_balloon.show();


}

function hideInfoBubble() 
{
  if(_balloon)
        _balloon.hide();
}


function showMap(callback)
{
	if(mapVisible) {
		if(callback != null) {
			callback();
		}
		return;
	}
	

	
	
	if(effectsDisabled) {
		$("#map").css('position', 'relative');
		$("#map").css('left', '0');		

		map.checkResize();

		if(callback != null) {
			callback();
		}		
	} else {
		if(firstTime) {
			$("#map").css('display', 'none');
			$("#map").css('position', 'relative');
			$("#map").css('left', '0');
		}

		firstTime = false;
		
		$("#map").slideDown("normal", function() {
			if(callback != null) {
				callback();
			}
			
			map.checkResize();
		});
	}

	mapVisible = true;
	$("#showmap a:first-child").html("Hide Map");
}

function hideMap()
{
	if(!mapVisible) return;
	if(effectsDisabled) {
		$("#map").css('position', 'absolute');
		$("#map").css('left', '-10000px');
	} else {
		$("#map").slideUp("normal");
	}
	mapVisible = false;
	$("#showmap a:first-child").html("Show Map");
}

function toggleMap()
{
	if(mapVisible) {
		hideMap();
	} else {
		showMap();
	}
}

$(document).ready(initialise);
