/* ------------------------------------------
Class YAPCMap 
------------------------------------------ */
function YAPCMap(coord1,coord2,zoom) 
{
	this.overlays = new Array();
	this.icons = new Array();
	this.gmap = new GMap2(document.getElementById("map"));

	// Add general controls
	this.gmap.addControl(new GLargeMapControl());
	
	// Add scale controls
	this.gmap.addControl(new GScaleControl());

	// Set map zoom and center
	this.gmap.setCenter(new GLatLng(coord1,coord2), zoom);


}
YAPCMap.prototype.overlays;
YAPCMap.prototype.gmap;
YAPCMap.prototype.icons;

YAPCMap.prototype.addline = function(coord1,coord2,type,name,color) {
  return this.overlays[this.overlays.length] = (new Line(this,coord1,coord2,type,name,color));
}

YAPCMap.prototype.addpoint = function(coord1,coord2,type,name,text,url,image) {
  return this.overlays[this.overlays.length] = (new Point(this,coord1,coord2,type,name,text,url,image));
}

YAPCMap.prototype.addicon = function(type,image,shadow,sizex,sizey,anchorx,anchory) {
	var newicon = new GIcon();
	newicon.image = image;
	if (shadow) {
		newicon.shadow = shadow;
		newicon.shadowSize = new GSize(sizex, sizey);
	}
	newicon.iconSize = new GSize(sizex, sizey);
	newicon.iconAnchor = new GPoint(anchorx, anchory);
	newicon.infoWindowAnchor = new GPoint(anchorx, anchory);
	this.icons[type] = newicon;
	return;
}

YAPCMap.prototype.showall = function() {
  for (var item = 0; item < this.overlays.length; item ++) {
    this.overlays[item].show();
  }
  return;
} 

YAPCMap.prototype.hideall = function() {
  for (var item = 0; item < this.overlays.length; item ++) {
    this.overlays[item].hide();
  }
  return;
}

YAPCMap.prototype.toggletype = function(type) {
  for (var i = 0; i < arguments.length; i++) {
    for (var item = 0; item < this.overlays.length; item ++) {
      if (this.overlays[item].type == arguments[i]) {
        this.overlays[item].toggle();
      }
    }
  }
}

YAPCMap.prototype.find = function(coord1,coord2) {
  for (var item = 0; item < this.overlays.length; item ++) {
    if (this.overlays[item].coord1 == coord1 && this.overlays[item].coord2 == coord2) {
      return this.overlays[item];
    }
  }
  return null;
}

/* ------------------------------------------
Class Overlay 
------------------------------------------ */

function Overlay() {
   this.status = 0;
   this.marker = null;
} 

Overlay.prototype.parent;
Overlay.prototype.status;
Overlay.prototype.coord1;
Overlay.prototype.coord2;
Overlay.prototype.marker;
Overlay.prototype.type;

Overlay.prototype.toggle = function() {
  if (this.status) {
    this.hide();
  } else {
    this.show();
  }
}
Overlay.prototype.hide = function() {
  if (this.status == 1) {
    this.parent.gmap.removeOverlay(this.marker);
    this.status = 0;
  }
}
Overlay.prototype.show = function() {
  if (this.status == 0) {
    this.parent.gmap.addOverlay(this.marker);
    this.status = 1;
  }
}

/* ------------------------------------------
Class Point extends Overlay
------------------------------------------ */

function Point(parent,coord1,coord2,type,name,text,url,image) 
{
  this.parent = parent;
  this.coord1 = coord1;
  this.coord2 = coord2;
  var marker = this.marker = new GMarker(new GLatLng(coord1,coord2),parent.icons[type]);

  var output = '<div class="mapinfo">';
  if (image) {
    output += '<img src="'+image+'" alt="'+name+'" />';
  }
  output += '<strong>'+name+'</strong><br/>'+text+'';
  if (url) {
    output += '<br><em><a href="'+url+'" target="_blank">Further information</a></em>';
  }
  output += '</div>';
  this.marker.title = name;
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(output);
  });
  this.type = type;
  this.show();
}

Point.prototype = new Overlay();
Point.prototype.output;

Point.prototype.focus = function() {
  this.show();
  this.parent.gmap.panTo(new GLatLng(this.coord1, this.coord2));
  GEvent.trigger(this.marker,'click');
  if (this.parent.gmap.getZoom() < 15) {
    this.parent.gmap.zoomIn();
  }
}

/* ------------------------------------------
Class Line extends Overlay
------------------------------------------ */

function Line(parent,coord1,coord2,type,name,color)
{
  this.parent = parent;
  this.coord1 = coord1;
  this.coord2 = coord2;
  this.marker = new GPolyline.fromEncoded({
    color:      color,
    weight:     7,
    points:     this.coord1,
    levels:     this.coord2,
    zoomFactor: 32,
    numLevels:  4
  });
  this.type = type;
  this.marker.title = name;
  this.show();
} 

Line.prototype = new Overlay();
