			
var action;
IE4 = (document.all && !document.getElementById) ? true : false;
NS4 = (document.layers) ? true : false;
IE5 = (document.all && document.getElementById) ? true : false;
NS6 = (!document.all && document.getElementById) ? true : false;

function showDetails(action)
{
   x = 0;
   toggleImgLink(arguments[x]);
//   changeText(arguments[x]);
    if (IE4) { prefix = document.all(arguments[x]).style; }
    if (NS6 || IE5) { prefix = document.getElementById(arguments[x]).style }
    if (NS4){ prefix = document.layers(arguments[x]); }

    if (prefix.display == "")
    {
       prefix.display = "none";
    }
    else
    {
       prefix.display = "";
    }
}

function getprefix(item) 
{
    if (IE4) { prefix = document.all(item).style; }
    if (NS6 || IE5) { prefix = document.getElementById(item).style }
    if (NS4){ prefix = document.layers(item); }
    return prefix;
}

function showFlightDetails(flightgroup,pluslink,minuslink,pluslink2,minuslink2)
{
    //prefix = getprefix(flightgroup);
    //prefix.display = "";
    
    var divs = document.getElementsByTagName('div');
    for (var x=0;x<divs.length;x++) {
        if (divs[x].id.indexOf(flightgroup+'F')!=-1) {
            prefix = getprefix(divs[x].id)
            prefix.display="";
        }
    }
    
    prefix = getprefix(pluslink);    
    prefix.display = "none";
    
    prefix = getprefix(minuslink);    
    prefix.display = "";

    prefix = getprefix(pluslink2);    
    prefix.display = "none";
    
    prefix = getprefix(minuslink2);    
    prefix.display = "";
}

function hideFlightDetails(flightgroup,pluslink,minuslink,pluslink2,minuslink2)
{
    //prefix = getprefix(flightgroup);
    //prefix.display = "none";
    
    var divs = document.getElementsByTagName('div');
    for (var x=0;x<divs.length;x++) {
        if (divs[x].id.indexOf(flightgroup+'F')!=-1) {
            prefix = getprefix(divs[x].id)
            prefix.display="none";
        }
    }
    
    prefix = getprefix(pluslink);    
    prefix.display = "";
    
    prefix = getprefix(minuslink);    
    prefix.display = "none";

    prefix = getprefix(pluslink2);    
    prefix.display = "";
    
    prefix = getprefix(minuslink2);    
    prefix.display = "none";
}


function toggleImgLink2(link)
{
	// link += "img";
	// alert(link);
	var imgref;
	if ((imgref = document.getElementById(link)) != null ) {
		if (imgref.src.indexOf("plus") > -1 ) {
			imgref.src = "/images/minus.gif";
			imgref.title = "Collapse details";
		}
		else {
			imgref.src = "/images/plus.gif";
			imgref.title = "Expand details";
		}
	}
}

function changeText(link)
{
  link += "link";
  linkText = document.getElementById(link);
  linkText.firstChild.nodeValue;

  if (linkText.firstChild.nodeValue == "[+]")
  {
    linkText.firstChild.nodeValue = "[-]";
  }
  else
  {
    linkText.firstChild.nodeValue = "[+]";
  }
}

function toggleImgLink(link)
{
	link += "img";
	// alert(link);
	var imgref;
	if ((imgref = document.getElementById(link)) != null ) {
		if (imgref.src.indexOf("plus") > -1 ) {
			imgref.src = "/images/minus.gif";
			imgref.title = "Collapse details";
		}
		else {
			imgref.src = "/images/plus.gif";
			imgref.title = "Expand details";
		}
	}
}

function WeatherClickC(){
	WCDivF.style.display = "none";
	WCDivC.style.display = "";
}
function WeatherClickF(){
	WCDivF.style.display = "";
	WCDivC.style.display = "none";
}

var c_isIE = (navigator.userAgent.indexOf('MSIE') != -1);

function _loadMozillaCompatLayer(w) {
function GetNonTextNode(n) {
    try {
        while (n && n.nodeType!=1) {
            n = n.parentNode;
        }
    }
    catch (ex) {
        n = null;
    }
    return n;
}

w.attachEvent = function(eventName, handler) {
    this.addEventListener(eventName, handler, false);
}
w.detachEvent = function(eventName, handler) {
    this.removeEventListener(eventName, handler, false);
}
var attachEventProxy = function(eventName, eventHandler) {
    eventHandler._mozillaEventHandler = function(e) {
        window.event = e;
        eventHandler();
        return e.returnValue;
    };
    this.addEventListener(eventName.slice(2), eventHandler._mozillaEventHandler, false);
}

var detachEventProxy = function (eventName, eventHandler) {
    if (eventHandler._mozillaEventHandler) {
        var mozillaEventHandler = eventHandler._mozillaEventHandler;
        delete eventHandler._mozillaEventHandler;

        this.removeEventListener(eventName.slice(2), mozillaEventHandler, false);
    }
}

w.attachEvent = attachEventProxy;
w.detachEvent = detachEventProxy;
w.HTMLDocument.prototype.attachEvent = attachEventProxy;
w.HTMLDocument.prototype.detachEvent = detachEventProxy;
w.HTMLElement.prototype.attachEvent = attachEventProxy;
w.HTMLElement.prototype.detachEvent = detachEventProxy;

w.Event.prototype.__defineGetter__('srcElement', function () {
        var n = GetNonTextNode(this.target);
        return n;
    });
w.Event.prototype.__defineSetter__('cancelBubble', function (v) {
        if (v) {
            this.stopPropagation();
        }
    });

function GetLocation(el) {
    var c = { x : 0, y : 0 };
    while (el) {
        c.x += el.offsetLeft;
        c.y += el.offsetTop;
        el = el.offsetParent;
    }
    return c;
}
w.Event.prototype.__defineGetter__('offsetX', function () {
        return window.pageXOffset + this.clientX - GetLocation(this.srcElement).x;
    });
w.Event.prototype.__defineGetter__('offsetY', function () {
        return window.pageYOffset + this.clientY - GetLocation(this.srcElement).y;
    });
w.Event.prototype.__defineSetter__('returnValue', function (v) {
        if (!v) {
            this.preventDefault();
        }
        this.cancelDefault = v;
        return v;
    });
w.Event.prototype.__defineGetter__('returnValue', function() {
        return this.cancelDefault;
    });
}

if (!c_isIE) {
_loadMozillaCompatLayer(window);
}


var divMap;
var divMapDefaultHTML;
var map = null;
var gmap = null;
var gicon = [];
for (var x=1;x<=4;x++) {
	gicon[x] = new GIcon();
	gicon[x].image = 've/gi'+x+'.png';
	gicon[x].iconSize = new GSize(8,8);
	gicon[x].iconAnchor = new GPoint(4,4);
}
var gsicon = [];
for (var x=1;x<=4;x++) {
	gsicon[x] = new GIcon();
	gsicon[x].image = 've/gis'+x+'.png';
	gsicon[x].iconSize = new GSize(10,10);
	gsicon[x].iconAnchor = new GPoint(5,5);
}
var gclicon = new GIcon();
gclicon.image = 've/marker3.png';
gclicon.iconSize = new GSize(12,20);
gclicon.iconAnchor = new GPoint(6,20);

var ss = 0;
var aip = 0;

function onSuperStalkClick() {
	if ((null==map) && (null==gmap)) return;
	if (0==ss) {
		SuperStalkOn();
	} else {
		SuperStalkOff();
	}
}

function SuperStalkOn() {
	if ((null==map) && (null==gmap)) return;
	
	var link = document.getElementById('sslink');
	if (null!=link) {
		ss = 1;
		link.innerText = 'Click here to reset map';
	}
	if (ve) {map.SetMapStyle('h');}
	if (gm) {gmap.setMapType(G_HYBRID_MAP);}
	showPushpins(0);
	if (ve) {map.SetCenterAndZoom(new VELatLong(latitude,longitude),19);}
	if (gm) {
		gmap.panTo(new GLatLng(latitude,longitude));
		gmap.setZoom(17);
	}
}
  
function SuperStalkOff() { 
	if ((null==map) && (null==gmap)) return;

	var link = document.getElementById('sslink');
	if (null!=link) {
		ss = 0;
		link.innerText = 'Click here for super-scary stalker mode';
	}
	if (ve) {map.SetMapStyle(mapstyle);}
	if (gm) {gmap.setMapType(gmaptype);}
	showPushpins(0);
	if (ve) {map.SetCenterAndZoom(new VELatLong(latitude,longitude),zoom);}
	if (gm) {
		gmap.panTo(new GLatLng(latitude,longitude));
		gmap.setZoom(gzoom);
	}	
}

function onMapReset() {
	SuperStalkOff();
}

var mapview = {init: false, latmin: latitude, latmax: latitude, lonmin: longitude, lonmax: longitude}

function ResetMapMinMax() {
	mapview.latmin = latitude;
	mapview.latmax = latitude;
	mapview.lonmin = longitude;
	mapview.lonmax = longitude;
	mapview.init = true;
}

function ClearMapMinMax() {
	mapview.init =false;
}

function AdjustMapMinMax(lat,lon) {
	if (mapview.init) {
		if (mapview.latmin > lat) {mapview.latmin=lat;}
		if (mapview.latmax < lat) {mapview.latmax=lat;}
		if (mapview.lonmin > lon) {mapview.lonmin=lon;}
		if (mapview.lonmax < lon) {mapview.lonmax=lon;}
	} else {
		mapview.latmin=lat;
		mapview.latmax=lat;
		mapview.lonmin=lon;
		mapview.lonmax=lon;
		mapview.init=true;
	}
}

function SetMapViewport() {
	if (mapview.init) {
		var latborder=(mapview.latmax-mapview.latmin)/10;
		var lonborder=(mapview.lonmax-mapview.lonmin)/10;
		if (ve) {
			var locs = new Array;
			var loc = new VELatLong(mapview.latmin-latborder, mapview.lonmin-lonborder);
			locs.push(loc);
			var loc = new VELatLong(mapview.latmax+latborder, mapview.lonmax+lonborder);
			locs.push(loc);
			map.SetMapView(locs);
		}
		if (gm) {
			bounds = new GLatLngBounds(
							new GLatLng(mapview.latmin-latborder,mapview.lonmin-lonborder),
							new GLatLng(mapview.latmax+latborder,mapview.lonmax+lonborder)
						);
			gmap.panTo(bounds.getCenter());
			gmap.getBoundsZoomLevel(bounds,new GSize(500,420));
		}
	}
}

function showPushpins(hours) {
	showAnimatedPushpins(hours)
}

function MapClearPoints() {
	if (ve) {map.DeleteAllPushpins();map.DeleteAllPolylines();}
	if (gm) {gmap.clearOverlays();}
}

function MapCenter() {
	if (ve) {map.SetCenter(new VELatLong(latitude,longitude));}
	if (gm) {gmap.panTo(new GLatLng(latitude,longitude));}
}

function showAnimatedPushpins(numhours) {
	if ((null==map) && (null==gmap)) return;
	if (1==aip) return;
	MapClearPoints();
	ClearMapMinMax();
	var startpin = -1;
	// var ddl = document.getElementById('ddlShowTrip');
	//  if (!(null==ddl)) {ddl.selectedIndex = 0;}

	if (numhours>0) {
		for (var x=0;x<lhcount;x++) {
			startpin=x;
			AdjustMapMinMax(lhdata[x].lat,lhdata[x].lon);
		}
		SetMapViewport();
		if (startpin>-1) {
			showAnimatedPin(startpin,-1);
		}
	}
	makeCLPin();
	if (numhours<=0) {
		MapCenter();
	}
}

function showTrip(num) {
	showAnimatedTrip(num);
	return;
	if (null==map) return;
	MapClearPoints();
	ClearMapMinMax();
	if (num>0) {
		for (var x=lhcount-1; x>=0; x--) {
			if (lhdata[x].trip==num) {
				makePin(x);
			}
		}
	}
	SetMapViewport();
}

function showAnimatedPin(x,num) {
	aip=1;
	var delay;
	if ((-1==num)|(lhdata[x].trip==num)) {
		makePin(x);
		if (x>0) {
			if (lhdata[x].isstop) {delay=1000;} else {delay=100;}
			x--;
			setTimeout("showAnimatedPin("+x+","+num+")",delay);
		} else {
			aip=0;
		}
	} else {
		if (x>0) {
			x--;
			showAnimatedPin(x,num);
		} else {
			aip=0;
		}
	}
}

function showAnimatedTrip(num) {
	if ((null==map) && (null==gmap)) return;
	if (1==aip) return;
	MapClearPoints();
	ClearMapMinMax();
	var firstpin = -1;
	if (num>0) {
		for (var x = 0; x<lhcount; x++) {
			if (lhdata[x].trip==num) {
				firstpin = x;
				AdjustMapMinMax(lhdata[x].lat,lhdata[x].lon);
			}
		}
	}
	SetMapViewport();
	if (firstpin>-1) {
		showAnimatedPin(firstpin,num);
	}
}

function makePin(x) {
	if (lhdata[x].isstop) {
		if (ve) {
			var pin = new VEPushpin('pushpinlh'+x,
										new VELatLong(lhdata[x].lat,lhdata[x].lon),
										've/pp'+lhdata[x].color+'s.png',
										lhdata[x].time,
										'Stopped',
										'pushpin'+lhdata[x].color+'s',
										'pptitle',
										'ppdetail'
										);
            map.AddPushpin(pin);

			//var pushpinElement = map.AddPushpin('pushpinlh'+x,lhdata[x].lat,lhdata[x].lon,10,10,'pushpin'+lhdata[x].color+'s','&nbsp;');
			//pushpinElement.title = 'Stopped at ' + lhdata[x].time;
			//pushpinElement.attachEvent('onclick', onPushpinClick);
		}
		if (gm) {
			var point = new GLatLng(lhdata[x].lat,lhdata[x].lon);
			var marker = new GMarker(point,gsicon[lhdata[x].color]);
			gmap.addOverlay(marker);
		}
	} else {
		if (ve) {
			var pin = new VEPushpin('pushpinlh'+x,
										new VELatLong(lhdata[x].lat,lhdata[x].lon),
										've/pp'+lhdata[x].color+'.png',
										lhdata[x].time,
										'In Transit, Speed: ' + lhdata[x].speed + ' mph',
										'pushpin'+lhdata[x].color,
										'pptitle',
										'ppdetail'
										);
            map.AddPushpin(pin);

			//var pushpinElement = map.AddPushpin('pushpinlh'+x,lhdata[x].lat,lhdata[x].lon,8,8,'pushpin'+lhdata[x].color,'&nbsp;');
			//pushpinElement.title = lhdata[x].speed + ' mph at ' + lhdata[x].time;
			//pushpinElement.attachEvent('onclick', onPushpinClick);
		}
		if (gm) {
			var point = new GLatLng(lhdata[x].lat,lhdata[x].lon);
			var marker = new GMarker(point,gsicon[lhdata[x].color]);
			gmap.addOverlay(marker);
		}
	}
	AdjustMapMinMax(lhdata[x].lat,lhdata[x].lon);
}

function makeCLPin() {
	if (usinggpsdata) {
		if (ve) {
			var pin = new VEPushpin('pushpincl',
										new VELatLong(latitude,longitude),
										've/pp.png?v=2',
										'Current Location',
										'',
										'pushpincl',
										'pptitle',
										'ppdetail'
										);
            map.AddPushpin(pin);

			//var pushpinElement = map.AddPushpin('pushpincl', latitude, longitude, 12, 40, 'pushpin', '&nbsp;');
			//pushpinElement.title = 'Current Location';
			//pushpinElement.attachEvent('onclick', onPushpinClick);
		}
		if (gm) {
			var point = new GLatLng(latitude,longitude);
			var marker = new GMarker(point,gclicon);
			gmap.addOverlay(marker);
		}
	}
	AdjustMapMinMax(latitude,longitude);
}

function onPushpinClick() {
	var ddl = document.getElementById('ddlShowTrip');

	if (event.srcElement.id=='pushpincl') {
		showPushpins(0);
		if (!(null==ddl)) {ddl.selectedIndex = 0;}
	} else {
		var x = event.srcElement.id.substring(9) * 1;
		showTrip(lhdata[x].trip);
		if (!(null==ddl)) {ddl.selectedIndex = lhdata[x].trip;}
	}
}

function fixColumns() {
	var maxwidth=0;
	try {maxwidth=Math.max(maxwidth,colCL.offsetWidth);} catch(e){}
	try {maxwidth=Math.max(maxwidth,colAA.offsetWidth);} catch(e){}
	try {maxwidth=Math.max(maxwidth,colIF.offsetWidth);} catch(e){}
	try {maxwidth=Math.max(maxwidth,colRF.offsetWidth);} catch(e){}
	try {maxwidth=Math.max(maxwidth,colFL.offsetWidth);} catch(e){}
	try {colCL.width = maxwidth;} catch(e){}
	try {colAA.width = maxwidth;} catch(e){}
	try {colIF.width = maxwidth;} catch(e){}
	try {colRF.width = maxwidth;} catch(e){}
	try {colFL.width = maxwidth;} catch(e){}
}


function UseVE() {
	ve=true;
	gm=false;
	DrawMap();
	return true;
}

function UseGM() {
	ve=false;
	gm=true;
	DrawMap();
	return true;
}

function DrawMap() {
	divMap.innerHTML = divMapDefaultHTML;
	var width = 500;
	var height = 420;

	map = null;
	gmap = null;

	if (ve) {
		// Virtual Earth v4
		map=new VEMap('divMap');
		map.LoadMap(new VELatLong(latitude, longitude), zoom, mapstyle ,false, VEMapMode.Mode2D, false);

	}

	if (gm) {
		gmap = new GMap2(divMap);
		gmap.addControl(new GLargeMapControl());
		gmap.addControl(new GMapTypeControl());
		gmap.addControl(new GScaleControl());
		gmap.setCenter(new GLatLng(latitude,longitude),gzoom,gmaptype);
	}

	onMapReset();
}

function onPageLoad() {
    var body = document.getElementsByTagName('body')[0];

	fixColumns();
	
	divMap = document.getElementById('divMap');
	divMapDefaultHTML = divMap.innerHTML;

	var radioUseVE = document.getElementById('radioUseVE');
	radioUseVE.checked = ve;
	var radioUseGM = document.getElementById('radioUseGM');
	radioUseGM.checked = gm;

	DrawMap();
	
	var ddl = document.getElementById('ddlShowTrip');
	if (!(null==ddl)) {
        ddl.attachEvent('onchange', onSelectTrip);
	}
}

function onSelectTrip() {
	if (-1==event.srcElement.value) {
		showPushpins(1);
	} else {
		showTrip(event.srcElement.value);
	}
}

//window.onresize=null;
