/**************************************************************************************
*File: marker.js
*Contents: Marker Class.
*All rights reserved.
**************************************************************************************/

function Marker(xmlNode) {

   var p = xmlNode.getElementsByTagName("position")
   if(p.length != 0) {
      var lng = parseFloat(p[0].getAttribute("lng"));
      var lat = parseFloat(p[0].getAttribute("lat"));
      this.position = new GLatLng(lat,lng);
   }
   else 
      this.position = null; 

   this.shown = false;
   this.info_shown = false;
   this.marker = null;
};

function initMarker(map,photosPath) {
Marker.prototype.map = map;
Marker.prototype.photosPath = photosPath;
}


function createMarker() {
   var position = this.upper.position;
   var markerIcon = this.markerIcon;

   if(position == null) {
      position = this.fallBack;
   }

   if(markerIcon != null)
      var icon = createMarkerIcon(markerIcon);


   this.upper.marker  = new GMarker(position,{icon:icon,title:this.name});	
   var this_child = this;		
   GEvent.addListener(this.upper.marker, "click", function() {
	               if(this_child.upper.info_shown) {
	                  this_child.hide();
	               }
	               else {
		          this_child.upper.marker.openInfoWindowTabsHtml(this_child.toHtml());
		       }
		     });
					
   GEvent.addListener(this.upper.marker, "infowindowopen", function() {this_child.upper.info_shown = true;});					
   GEvent.addListener(this.upper.marker, "infowindowclose", function() {this_child.upper.info_shown = false;});
}

function showMarker() {
    if(!this.upper.shown) {
       this.upper.map.addOverlay(this.upper.marker);
       this.upper.shown = true;
    }
 }

function hideMarker() {
    if(this.upper.shown) {
       this.upper.map.removeOverlay(this.upper.marker);
       this.upper.shown = false;
    }
    
    if(this.upper.info_shown) {
       this.upper.map.closeInfoWindow();
       this.upper.info_shown = false;
    }
}

 function goToMarker() {
   var position = this.upper.position;
   if(position == null)
      position = this.fallBack;

   this.upper.map.panTo(position);
   this.show();
   this.upper.marker.openInfoWindowTabsHtml(this.toHtml()); 
}

function toggleMarker() {
   if(this.upper.shown)
      this.show();
   else 
      this.hide();
}


function getOptionalProperty(xmlNode,tag) {
   var elem = xmlNode.getElementsByTagName(tag);
   if(elem.length != 0) {
      return elem[0].firstChild.nodeValue;
   }
   return null;
}

function createMarkerIcon(markerIcon) {
   var theIcon = new GIcon();
   theIcon.image = markerIcon.image;
   theIcon.shadow = markerIcon.shadow;
   theIcon.iconSize = new GSize(markerIcon.iw, markerIcon.ih);
   theIcon.shadowSize = new GSize(markerIcon.sw, markerIcon.sh);
   theIcon.iconAnchor = new GPoint(markerIcon.ax, markerIcon.ay);
   theIcon.infoWindowAnchor = new GPoint(markerIcon.ix, markerIcon.iy);

  return theIcon;
  
}
