// based on gxmarker.1.js
var overlay = new Array();

function GxMarkerNamespace() {

var n4=(document.layers);
var n6=(document.getElementById&&!document.all);
var ie=(document.all);
var o6=(navigator.appName.indexOf("Opera") != -1);
var safari=(navigator.userAgent.indexOf("Safari") != -1);

function GxMarker( a, b, tooltip, clickURL) {
    this.inheritFrom = GMarker;
    this.inheritFrom(a,b);
    if ( typeof tooltip != "undefined" ) {
        this.setTooltip( tooltip );
    }
	this.clickURL = clickURL;
}

GxMarker.prototype = new GMarker(new GLatLng(1,1));

GxMarker.prototype.setTooltip = function( string ) {
    this.tooltip = new Object();
    //this.tooltip.opacity  = 70;
    this.tooltip.contents = string;
};

GxMarker.prototype.initialize = function( a ) {
    try {
        GMarker.prototype.initialize.call(this, a);
        // Setup the mouse over/out events
		GEvent.bind(this, "mouseover", this, this.onMouseOver);
		GEvent.bind(this, "mouseout",  this, this.onMouseOut);
		GEvent.bindDom(this, "click",  this, this.onClick);
		this.map = a;
    } catch(e) {
	;
   }
}

GxMarker.prototype.remove = function( a ) {
    GMarker.prototype.remove.call(this);
    if ( this.tooltipObject )
        //this.map.div.removeChild(this.tooltipObject);
        document.body.removeChild(this.tooltipObject);
}

GxMarker.prototype.onInfoWindowOpen = function() {
    this.hideMouseOver();
    GMarker.prototype.onInfoWindowOpen.call(this);
}

GxMarker.prototype.onMouseMove = function() {
alert("here");
}

GxMarker.prototype.onClick = function() {
	if (this.clickURL)
	{
		this.hideMouseOver();
		GET(this.clickURL, 'all_page_contain');
	}
}

GxMarker.prototype.onMouseOver = function() {
    this.showMouseOver();
};

GxMarker.prototype.onMouseOut = function() {
    this.hideMouseOver();
};

GxMarker.prototype.showMouseOver = function() {
    if ( this.tooltip ) {
        if ( typeof this.tooltipObject == "undefined" ) {
            this.tooltipObject = document.createElement("div");
            this.tooltipObject.style.display    = "none";
            this.tooltipObject.style.position   = "absolute";
            this.tooltipObject.style.background = "#ffffff";
            this.tooltipObject.style.padding    = "0";
            this.tooltipObject.style.margin     = "0";
            this.tooltipObject.style.zIndex     = 50000;
            this.tooltipObject.innerHTML        = "<div class=\"markerTooltip\">" + this.tooltip.contents + "</div>";

/*
            var opacity = this.tooltip.opacity / 100;
            this.tooltipObject.style.MozOpacity = opacity;
            this.tooltipObject.style.filter     = "alpha(opacity=" + this.tooltip.opacity + ")";
            this.tooltipObject.style.opacity    = opacity;
*/

            document.body.appendChild(this.tooltipObject);
        }

		function getElementPosition(elem) {
		    var offsetTrail = elem;
		    var offsetLeft = 0;
		    var offsetTop = 0;
		    while (offsetTrail) {
		        offsetLeft += offsetTrail.offsetLeft;
		        offsetTop += offsetTrail.offsetTop;
		        offsetTrail = offsetTrail.offsetParent;
		    }
		    if (navigator.userAgent.indexOf("Mac") != -1 && 
		        typeof document.body.leftMargin != "undefined") {
		        offsetLeft += document.body.leftMargin;
		        offsetTop += document.body.topMargin;
		    }
		    return {left:offsetLeft, top:offsetTop};
		}


		var tlcLatLng = this.map.fromContainerPixelToLatLng(new GPoint(0,0), true);
		var tlcDivPixel = this.map.fromLatLngToDivPixel(tlcLatLng);
		var pointDivPixel = this.map.fromLatLngToDivPixel(this.getPoint());
		var c = new GPoint(pointDivPixel.x-tlcDivPixel.x, pointDivPixel.y-tlcDivPixel.y);
		var mapPos = getElementPosition(this.map.getContainer());

		this.tooltipObject.style.left = "-1000px";
        this.tooltipObject.style.top = "-1000px";
        this.tooltipObject.style.display = "block";

        var tipLeft = c.x - this.getIcon().iconAnchor.x + mapPos.left;
        if (tipLeft > (document.body.offsetWidth/2)) {
        	tipLeft -= this.tooltipObject.offsetWidth;
        } else {
        	tipLeft += this.getIcon().iconSize.width;
        }
        this.tooltipObject.style.left = tipLeft + "px";

        var tipTop = c.y - this.getIcon().iconAnchor.y + mapPos.top;
        if ((c.y + this.map.getContainer().offsetTop) > (this.map.getContainer().offsetHeight/2)) {
        	tipTop -= this.tooltipObject.offsetHeight;
        } else {
        	tipTop += this.getIcon().iconSize.height;
        }
        this.tooltipObject.style.top = tipTop + "px";
    }
}

GxMarker.prototype.hideMouseOver = function() {
    if ( typeof this.tooltipObject != "undefined" ) {
        this.tooltipObject.style.display = "none";
    }
}

function makeInterface(a) {
    var b = a || window;
    b.GxMarker = GxMarker;
}

makeInterface();
}

GxMarkerNamespace();

function showPopupIndex(index) {
	overlay[index].showMouseOver();
	//highlightMarker = new GMarker(overlay[index].getPoint(), createSliceIcon(index, SLICE_STYLE_HIGHLIGHTED));
	//map.addOverlay(highlightMarker);
}

function hidePopupIndex(index) {

	overlay[index].hideMouseOver();
	//map.removeOverlay(highlightMarker);
}


/*****************************/
/*** fonctions utilitaires ***/
/*****************************/

function load_without_controls()
{
  load('false');
}

function insideCreateIcon(style)
{
	var icon_other = new GIcon();

	if (style == 'main')
	{
	  icon_other.image = "http://www.athem.fr/im/logo_map.png";
	  //icon_other.shadow = "http://www.adven.fr/~wikinsider/beta2/im/wiki_shadow.png";
	}
	else
	{
	  icon_other.image = "http://www.athem.fr/im/logo_map.png";
	  //icon_other.shadow = "http://www.adven.fr/~wikinsider/beta2/im/wiki_shadow.png";
	}
	icon_other.iconSize = new GSize(39, 48);
	//icon_other.shadowSize = new GSize(20, 20);
	icon_other.iconAnchor = new GPoint(20, 48);
	icon_other.infoWindowAnchor = new GPoint(5, 1);
	return (icon_other);
}

var map = null;
/*var main_text = null;
var main_lat = null;
var main_long = null;
var main_id = null;*/
var map_main_object = null;

/* recupere taille de la carte pour connaie pos des 4 coins */
function getMapWindowInfos(Container)
{
	var adjBounds = {};
	var center = map.getCenter();
	var lat = center.lng();
	var long = center.lat();
	var topDead = 25;
	var leftDead = 10;
	var rightDead = 5;
 	var bounds = map.getBounds();
	var lonPerPix = (bounds.getNorthEast().lng() - bounds.getSouthWest().lng())/Container.clientWidth;
	var latPerPix = (bounds.getNorthEast().lat() - bounds.getSouthWest().lat())/Container.clientHeight;
	var type = null;
	
	adjBounds['minlon'] = bounds.getSouthWest().lng() + leftDead*lonPerPix;
	adjBounds['maxlon'] = bounds.getNorthEast().lng() - rightDead*lonPerPix;
	adjBounds['minlat'] = bounds.getSouthWest().lat();
	adjBounds['maxlat'] = bounds.getNorthEast().lat() - topDead*latPerPix;
	adjBounds['long'] = long;
	adjBounds['lat'] = lat;
	if (document.getElementById('see_type'))
	{
		adjBounds['type'] = document.getElementById('see_type').value;
	}

	return adjBounds;
}

/* appele qd on bouge la carte, recharge les points */
function updateNearMap()
{
;
}

/* initialisation de la carte */
function insideInitMap(controls, long, lat)
{
    GUnload();
    var point = new GLatLng(long, lat, 17);
    map = new GMap2(document.getElementById("map"));
    //GEvent.addListener(map, "moveend", updateNearMap);
    if (controls == 'true')
      {
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
      }
    else
      {
	map.addControl(new GSmallZoomControl());
      }
      map.setCenter(point, 16);
   copyrightTimer = window.setInterval('makeCopyrightSmaller()', 150);
    updateNearMap();
}

/* positionne à */
function CenterMapTo(long, lat)
{
    var point = new GLatLng(long, lat, 17);
    map.setCenter(point, 16);
}

/* reduit le copyright */
function makeCopyrightSmaller() {
/*
	if (map.isLoaded()) 
	{
		for(var i = 0; i < map.getContainer().childNodes.length; ++i) {
			if(map.getContainer().childNodes[i].innerHTML.indexOf(String.fromCharCode(169)) !== -1){
      			map.getContainer().childNodes[i].style.fontSize = '9px';
      			window.clearInterval(copyrightTimer);
      			break;
    		}
    	}
  	}
*/
}

/** creation du marqueur **/
function insideCreateMarker(infos)
{
	var point = new GLatLng(infos.long, infos.lat, 17);
	var icon = insideCreateIcon(infos.type);


	var txt = "<div class='detailMapPopup'><img src='"+infos.picture+"' /><div style='margin-top:3px;'><span class='detailMapText'>"+infos.nom+"</span></div>";
	
	txt += "<div style='height:0px; clear: both; font-size:0px;'></div></div>";

	//overlay[infos.number] = new GxMarker(point, icon, txt, infos.link);
	var new_log = new GxMarker(point, icon, txt, infos.link);

	map.addOverlay(new_log);
	/*map.addOverlay(createMarker(point_tmp, txt, icon_other));*/
}


function encode_tab_to_get(param)
{
	var str = "";

	for (var i in param)
	{
		str = str+"&"+i+"="+param[i];		
	}
	return (str);
}
