/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 * @version 1.0
 * The Tooltip class is an addon designed for the Google
 * Maps GMarker class.
 */

/**
 * @constructor
 * @param {GMarker} marker
 * @param {String} text
 * @param {Number} padding
 */
function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(this.text_));
	div.className = 'tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

Tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}

/*
GEvent.addListener(map, "click", function(overlay, latlng) {
          if (latlng) {
            marker = new GMarker(latlng, {draggable:true});
            GEvent.addListener(marker, "click", function() {
              var html = "<table>" +
                         "<tr><td>Titel:</td> <td><input type='text' id='title'/> </td> </tr>" +
                         "<tr><td>Beschreibung:</td> <td><input type='textarea' id='description'/></td> </tr>" +
                         "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData(marker)'/></td></tr>";

              marker.openInfoWindow(html);
            });
            map.addOverlay(marker);
          }
        });
*/

function saveData(marker_new) {
	  //var marker = marker;
	  var tourid = tourid;
      var title = escape(document.getElementById("title").value);
      var description = escape(document.getElementById("description").value);
      var latlng = marker_new.getLatLng();
      var lat = latlng.lat();
      var lng = latlng.lng();

      var url = "insertPOI.php?insertpoi=1&tourid=" + tourid + " &title=" + title + "&description=" + description +
                "&lat=" + lat + "&lng=" + lng;
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 1) {
          marker_new.closeInfoWindow();
          document.getElementById("message").innerHTML = "Location added.";
        }
      });
    }

  function deleteMarker(id, markernum) {
  		alert("Sind Sie sicher, dass Sie diesen Marker löschen möchten?");
  		var id = id;
  		//var markernum = markernum;
  		//marker = markernum;
  		marker.hide(markernum);
  		document.getElementById("marker"+markernum).style.visibility = "hidden";
  		var url = "deletePOI.php?deletepoi=1&id="+id;
  		GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 1) {
          marker.closeInfoWindow();
          document.getElementById("message").innerHTML = "Location added.";
        }
      });
  }


// A function to create the marker and set up the event window
function createMarker(point, title, html, tooltip, icon, directions) {
	var tooltip_text = tooltip;
	//var icon = icon;
	//var marker = new GMarker(point, {icon:icon, title:tooltip});

	// pruefen, ob Icon-Objekt existiert

	//var marker = new GMarker(point, {icon:icon, title:tooltip_text});
	var marker = new GMarker(point, {icon:icon});
	var title = title;
	var html = html;
	var directions = directions;
	
	if(directions == 1){
		// The info window version with the "to here" form open
    	to_htmls[i] = html + '<div class="infowindow-route">Routenplanung: <b class="active">Hierher</b> - <a href="javascript:fromhere(' + i + ')">Von hier</a>' +
           '<br><div class="form-title">Startadresse:</div><form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="Startadresse eingeben" onclick="this.value=\'\'" /><br>' +
           '<div class="form-example">Bsp.:&nbsp;(Musterstr. 2, 12345 Musterhausen)</div><INPUT value="Route anzeigen" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/></div>';
        // The info window version with the "from here" form open
        from_htmls[i] = html + '<div class="infowindow-route">Routenplanung: <a href="javascript:tohere(' + i + ')">Hierher</a> - <b class="active">Von hier</b>' +
           '<br><div class="form-title">Zieladresse:</div><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="Zieladresse eingeben" onclick="this.value=\'\'" /><br>' +
           '<div class="form-example">Bsp.:&nbsp;(Musterstr. 2, 12345 Musterhausen)</div><INPUT value="Route anzeigen" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/></div>';

    	html = html + '<div class="infowindow-route">Routenplanung: <a href="javascript:tohere('+i+')">Hierher</a> - <a href="javascript:fromhere('+i+')">Von hier</a></div>';
	}
	
	// wrap content with div container
	html = '<div class="infowindow-wrapper">' + html + '</div>';
	to_htmls[i] = '<div class="infowindow-wrapper">' + to_htmls[i] + '</div>';
	from_htmls[i] = '<div class="infowindow-wrapper">' + from_htmls[i] + '</div>';
	
	var tooltip = new Tooltip(marker,tooltip_text,4);
    marker.tooltip = tooltip;
    map.addOverlay(marker);
    map.addOverlay(tooltip);

    GEvent.addListener(marker,'mouseover',function(){ this.tooltip.show(); });
    GEvent.addListener(marker,'mouseout',function(){ this.tooltip.hide(); });

	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});

	// Sidebar zusammenstellen
	gmarkers[i] = marker;
	//gmarkers[i].push(marker);
	htmls[i] = html;
	//side_bar_html += '<a href="javascript:openMarker(' + i + ')">' + title + '</a><br>';
	i++;

	//map.addOverlay(new GMarker(point, myicon));

	return marker;
}

// A function to create the marker and set up the event window
function createTabbedMarker(point, title, tab1_title, tab1_content, tab2_title, tab2_content, tooltip, icon, directions) {
	var tooltip_text = tooltip;
	//var icon = icon;
	//var marker = new GMarker(point, {icon:icon, title:tooltip});

	// pruefen, ob Icon-Objekt existiert

	//var marker = new GMarker(point, {icon:icon, title:tooltip_text});
	var tabmarker = new GMarker(point, {icon:icon});
	var title = title;
	var html = html;
	var directions = directions;
	
	var tab1_title = tab1_title;
	var tab1_content = tab1_content;
	var tab2_title = tab2_title;
	var tab2_content = tab2_content;
	
	/*
	var infotabs = [
		new GInfoWindowTab("Tab 1",tab1),
		new GInfoWindowTab("Nummer 2",tab2)
	];
	*/

	if(directions == 1){
		// The info window version with the "to here" form open
    	tab2_content_to = tab2_content + '<div class="infowindow-route">Routenplanung: <b class="active">Hierher</b> - <a href="javascript:fromhereTabbed(' + i + ')">Von hier</a>' +
           '<br><div class="form-title">Startadresse:</div><form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="Startadresse eingeben" onclick="this.value=\'\'" /><br>' +
           '<div class="form-example">Bsp.:&nbsp;(Musterstr. 2, 12345 Musterhausen)</div><INPUT value="Route anzeigen" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/></div>';
         //to_htmls_tab1[i] = tab1_content;
         
        // The info window version with the "from here" form open
        tab2_content_from = tab2_content + '<div class="infowindow-route">Routenplanung: <a href="javascript:tohereTabbed(' + i + ')">Hierher</a> - <b class="active">Von hier</b>' +
           '<br><div class="form-title">Zieladresse:</div><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="Zieladresse eingeben" onclick="this.value=\'\'" /><br>' +
           '<div class="form-example">Bsp.:&nbsp;(Musterstr. 2, 12345 Musterhausen)</div><INPUT value="Route anzeigen" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/></div>';
		
		tab2_content = tab2_content + '<div class="infowindow-route">Routenplanung: <a href="javascript:tohereTabbed('+i+')">Hierher</a> - <a href="javascript:fromhereTabbed('+i+')">Von hier</a></div>';
	}
	
	tab1_content = '<div class="infowindow-wrapper">' + tab1_content + '</div>';
	tab2_content = '<div class="infowindow-wrapper">' + tab2_content + '</div>';
	tab2_content_to = '<div class="infowindow-wrapper">' + tab2_content_to + '</div>';
	tab2_content_from = '<div class="infowindow-wrapper">' + tab2_content_from + '</div>';
	
	html = [
		new GInfoWindowTab(tab1_title, tab1_content),
		new GInfoWindowTab(tab2_title, tab2_content)
	];
	
	to_htmls[i] = [
		new GInfoWindowTab(tab1_title, tab1_content),
		new GInfoWindowTab(tab2_title, tab2_content_to)
	];
	
	from_htmls[i] = [
		new GInfoWindowTab(tab1_title, tab1_content),
		new GInfoWindowTab(tab2_title, tab2_content_from)
	];

	var tooltip = new Tooltip(tabmarker,tooltip_text,4);
    tabmarker.tooltip = tooltip;
    map.addOverlay(tabmarker);
    map.addOverlay(tooltip);

    GEvent.addListener(tabmarker,'mouseover',function(){ this.tooltip.show(); });
    GEvent.addListener(tabmarker,'mouseout',function(){ this.tooltip.hide(); });

	GEvent.addListener(tabmarker, "click", function() {
	  tabmarker.openInfoWindowTabsHtml(html,1);
	});
	
	// Sidebar zusammenstellen
	gmarkers[i] = tabmarker;
	//gmarkers[i].push(marker);
	htmls[i] = html;
	//side_bar_html += '<a href="javascript:openMarker(' + i + ')">' + title + '</a><br>';
	i++;

	//map.addOverlay(new GMarker(point, myicon));

	return tabmarker;
}


/**
 * function openMarker
 *
 * opens the corresponding info window to a map marker and is called
 * via the sidebar with the unique marker-id used in gmarkers array
 *
 */
function openMarker(i){
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function openTabbedMarker(i){
	gmarkers[i].openInfoWindowTabsHtml(htmls[i]);
}

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

// functions that open the directions forms
function tohereTabbed(i) {
	gmarkers[i].openInfoWindowTabsHtml(to_htmls[i],{selectedTab:1});
}

function fromhereTabbed(i) {
	gmarkers[i].openInfoWindowTabsHtml(from_htmls[i],{selectedTab:1});
}

function showTooltip(sidebarLink){
    if(!(this.isInfoWindowOpen)){
        gmarkers[sidebarLink].tooltip.show();
    }
}

function hideTooltip(sidebarLink){
    gmarkers[sidebarLink].tooltip.hide();
}

// To make polygons printable...
/*
$$("div.gmnoprint").each( function (div, index) {
         div.removeClassName('gmnoprint');

});
*/

