//***********************************************************
// Trail Locator Project
// The Trans Canada Trail
// http://www.tctrail.ca
// 
// Coding by Stephane Simard
// ssoutbox-tctrail@yahoo.com
//************************************************************


//-------------------------------------------------------------------------------------------
// SEGMENT CLASS

function classSegment(points, type, overlay)
{
	this.points = points;
	this.type = type;
	this.overlay = overlay;
}


//-------------------------------------------------------------------------------------------
// TRAIL CLASS

function classTrail(name, number, province, swBounds, neBounds)
{
	this.name = name;
	this.number = number;
	this.province = province;
	this.sw = swBounds;
	this.ne = neBounds;	
	
	this.isMapped = false;
	this.isHighlight = false;
	this.segmentArrayIndex = 0;
	this.segmentArray = new Array();
	
}

	classTrail.prototype.addNewSegment = function(points, type, overlay) 
	{
			this.segmentArray[this.segmentArrayIndex++] = new classSegment(points, type, overlay);
	}
	
	classTrail.prototype.resetOverlay = function() 
	{			
			for (var s=0; s<this.segmentArrayIndex; s++)
			{
				this.segmentArray[s].points = null;
				this.segmentArray[s].overlay = null;
				this.segmentArray[s].type = null;
			}
						
			this.segmentArray.length = 0;
			this.segmentArrayIndex = 0;
			this.isMapped = false;		
	}
	
	// INSTANCE
	var trailListIndex = 0;
	var trailsList = new Array();
	function addNewTrail(name, number, province, swBounds, neBounds) {
		trailsList[trailListIndex++] = new classTrail(name, number, province, swBounds, neBounds);
	}


//-------------------------------------------------------------------------------------------
// PROVINCES CLASS

function classProvince(abreviationCode, name, ylat, xlong, zoomView, swBounds, neBounds)
{
	this.code = abreviationCode;
	this.name = name;
	this.ylat = ylat;
	this.xlong = xlong;
	this.zoom = zoomView;
	this.sw = swBounds;
	this.ne = neBounds;
}

	// INSTANCE
	var provincesListIndex = 0;
	var provincesList = new Array();
	function addNewProvince(abreviationCode, name, ylat, xlong, zoomView, swBounds, neBounds) {
	    provincesList[provincesListIndex++] = new classProvince(abreviationCode, name, ylat, xlong, zoomView, swBounds, neBounds);
	}


//-------------------------------------------------------------------------------------------
// HIGHLIGHT  CLASS

function classHighlight()
{
	this.trailIndex = 0;
	this.overlayIndex = 0;
	this.overlay = new Array();
}

	classHighlight.prototype.addNewOverlay = function(poly) 
	{
		this.overlay[this.overlayIndex++] = poly;
	}
	
	// INSTANCE
	var highlight = new classHighlight();
	
	
//-------------------------------------------------------------------------------------------
// INFOWIN  CLASS

function classInfoWin(map, css, infoImg) 
{
        this.map = map;
		this.css = css;
		this.infoImg = infoImg;
		this.fudge = 3;
		this.visible = false;
        this.ie = false;
        var agent = navigator.userAgent.toLowerCase();
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1))
		{
			this.ie = true
			this.fudge = 0;
		}
} 
    
      classInfoWin.prototype = new GOverlay();

      classInfoWin.prototype.initialize = function(map) 
	  {
        var div1 = document.createElement("div");
        div1.style.position = "absolute";
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
        var div2 = document.createElement("div");
        div2.style.position = "absolute";
        div2.style.width = "24px";
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
        this.div1 = div1;
        this.div2 = div2;
      }

      classInfoWin.prototype.openOnMap = function(point, html, offset) 
	  {
        this.offset = offset||new GPoint(0,0);
        this.point = point;
        this.div1.innerHTML = '<div class="' + this.css + '"><nobr>' + html + '</nobr></div>';
        
		if(this.infoImg != "")
		{
			if (this.ie && this.infoImg.toLowerCase().indexOf(".png")>-1) 
			{
	          var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ this.infoImg +"', sizingMethod='scale');";
	          this.div2.innerHTML = '<div style="height:24px; width:24px; ' + loader + '" ></div>';
	        
			}else
	          this.div2.innerHTML = '<img src="' + this.infoImg + '" width="24" height="24">';
        }
        var z = GOverlay.getZIndex(this.point.lat());
        this.div1.style.zIndex = z;
        this.div2.style.zIndex = z+1;
        this.visible = true;
        this.show();
        this.redraw(true);
      }
      
      classInfoWin.prototype.openOnMarker = function(marker,html) 
	  {
		var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
        var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
		
        this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
      }
      
      classInfoWin.prototype.redraw = function(force) {
        if (!this.visible) {return;}
        var p = this.map.fromLatLngToDivPixel(this.point);
        this.div2.style.left   = (p.x + this.offset.x) + "px";
        this.div2.style.bottom = (-p.y + this.offset.y - this.fudge) + "px";
        this.div1.style.left   = (p.x + this.offset.x + -10) + "px";
        this.div1.style.bottom = (-p.y + this.offset.y + 23) + "px";
      }

      classInfoWin.prototype.remove = function() {
        this.div1.parentNode.removeChild(this.div1);
        this.div2.parentNode.removeChild(this.div2);
        this.visible = false;
      }

      classInfoWin.prototype.show = function() {
        this.div1.style.display="";
        this.div2.style.display="";
        this.visible = true;
      }
	  
	  classInfoWin.prototype.showme = function(points) {
        this.div1.style.display="";
        this.div2.style.display="";
		var p = this.map.fromLatLngToDivPixel(this.point);
        this.div2.style.left   = (p.x + this.offset.x) + "px";
        this.div2.style.bottom = (-p.y + this.offset.y - this.fudge) + "px";
        this.div1.style.left   = (p.x + this.offset.x + -10) + "px";
        this.div1.style.bottom = (-p.y + this.offset.y + 23) + "px";
        this.visible = true;
      }
      
      classInfoWin.prototype.hide = function() {
        this.div1.style.display="none";
        this.div2.style.display="none";
        this.visible = false;
      }
      
      classInfoWin.prototype.isHidden = function() {
        return !this.visible;
      }
     
	 classInfoWin.prototype.zindex = function(zin) {
        var z = GOverlay.getZIndex(this.point.lat());
        this.div1.style.zIndex = z+zin;
        this.div2.style.zIndex = z+1+zin;
      }
