/* --------------------------------- */
/*         SOPHIAKNOWS: JS           */
/* --------------------------------- */
/*         TRAIL DATA JS OO          */
/* --------------------------------- */
/*  Created:  2006-05-18             */
/*  Issued:   2006-05-18             */
/*  Modified: 2006-05-18             */
/*  Component: JS/DATA/OOP           */
/*  Copyright (c) 2006               */
/*  Tony Pisarra, SophiaKnows        */
/*  http://www.sophiaknows.com       */
/* --------------------------------- */
/* --------------------------------- */

// print alias of document.write

function print(str) {
	document.write(str);
	}


// SECTION CONSTRUCTOR
function Section(id,name,start,end,map) {
	this.id=id;
	this.name=name;
	this.start=start;
	this.end=end;
	this.map=map;
	this.total = returnPrecision1Float(this.end - this.start);
	this.shelters=new Array();
	this.towns=new Array();
	this.features=new Array();
	this.hostels=new Array();
	this.maintainers=new Array();
	}

Section.prototype.isInSection = function(m) {
	return (parseInt(m) >= parseInt(this.start) && parseInt(m) <= parseInt(this.end) );
	}

// SHELTER CONSTRUCTOR
function Shelter(name,sname,mile,type,elevation,county,state,lat,lon,north,south,off,notes,club,capacity,fee) {
	this.name=name;
	this.sname=sname;
	this.mile=mile;
	this.type=type;
	this.elevation=elevation;
	this.county=county;
	this.state=state;
	this.lat=lat;
	this.lon=lon;
	this.north=north;
	this.south=south;
	this.off=off;
	this.club=club;
	this.notes=notes;
	this.club=club;
	this.capacity=capacity;
	this.fee=fee;
	this.select_label = this.name+" ("+this.mile+" mi)";
	this.label=this.name;
	this.target="shelters";
	this.poplink="";
	this.info="";
	this.info_tabs = new Array();
	this.html=""
	this.point="";
	this.marker="";
	this.icon ="";
	this.active=0;
	}

// TOWN CONSTRUCTOR
function Town(name,zip,mile,off,lat,lon,north,south,hours,phone) {
	this.name=name;
	this.zip=zip;
	this.mile=mile;
	this.off=off;
	this.lat=lat;
	this.lon=lon;
	this.north=north;
	this.south=south;	
	this.hours=hours;
	this.phone=phone;
	this.select_label = this.name+" "+this.zip+" ("+this.mile+" mi)";
	this.target="towns";
	this.info_tabs = new Array();
	this.services = new Array();
	this.label=this.name+" "+this.zip;
	this.poplink="";
	this.info="";
	this.html=""
	this.point="";
	this.marker="";
	this.icon = "";
	this.active=0;	
	}

// FEATURE CONSTRUCTOR
function Feature(name,section,state,elevation,lat,lon,mile) {
	this.section=section;
	this.name=name;
	this.state=state;
	this.elevation=elevation;
	this.lat=lat;
	this.lon=lon;
	this.mile=mile;
	this.select_label = this.name+" ("+this.elevation+")";
	this.label=this.name;
	this.target="features";
	this.poplink="";
	this.info="";
	this.html=""	
	this.info_tabs = new Array();
	this.point="";
	this.marker="";
	this.icon ="";
	this.active=0;
	}

// HOSTEL CONSTRUCTOR
function Hostel(name,location,mile,services,type,lat,lon) {
	this.name=name;
	this.location=location;
	this.mile=mile;
	this.services=services;
	this.type=type;	
	this.lat=lat;
	this.lon=lon;
	this.point="";
	this.marker="";
	this.icon = "";
	this.info="";
	this.html=""
	this.active=0;	
	}

// forecast constructor
function Forecast(lat,lon,date,min,max,text,icon) {
	this.lat=lat;
	this.lon=lon;
	this.date=date;
	this.min=min;
	this.max=max;
	this.text=text;
	this.icon=icon;
	}

// SHELTER: XHTML: INFO 1
Shelter.prototype.xhtml = function() {
	var xhtml = "<table class='datatable' cellspacing=1 cellpadding=0 width=250>";
	    xhtml +="<tr><td valign=top style='padding-right:7px;' width=50%>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + pfloat(this.mile,1) +" mi <\/div>To Spngr:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + pfloat((2174.6-this.mile),1) +" mi <\/div>To Kthdn:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + this.north +" mi <\/div>Nxt North:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + this.south +" mi <\/div>Nxt South:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>"+((this.off)?this.off:"0.0") +" mi<\/div>Off Trail:<\/div>";
	    xhtml +="<td valign=top style='padding-left:7px;' width=50%>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + pfloat(this.lat,5) + "<\/div> Lat:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + pfloat(this.lon,5) + "<\/div> Lon:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + this.elevation + "<\/div> Elev:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + return_maptech_link(this.lat,this.lon) + "<\/div> Map1:<\/div>";
	    xhtml +="<div style=padding-bottom:1px><div class=fright>" + return_topozone_link(this.lat,this.lon) + "<\/div> Map2:<\/div>";
    	xhtml +="<\/table>";
 	return xhtml;
 	}

// TOWN: XHTML: INFO TAB 1
Town.prototype.xhtml = function() {
	var xhtml = "<table class='datatable' cellspacing=1 cellpadding=1 width=220>";
	    xhtml +="<tr><td>To Springer:<td align=right>" + pfloat(this.mile,1) +" mi";
	    xhtml +="<tr><td>To Katahdin:<td align=right>" + pfloat((2174.6-this.mile),1) +" mi";
	    xhtml +="<tr><td>Next North:&nbsp;<td align=right>" + this.north +" mi";
	    xhtml +="<tr><td>Next South:&nbsp;<td align=right>" + this.south +" mi";
	    xhtml +="<tr><td>Off Trail:&nbsp;<td align=right>" + this.off +" mi";
    	xhtml +="<\/table>";
 	return xhtml;
 	}

// FEATURE: XHTML: INFO TAB 1
Feature.prototype.xhtml = function() {
	this.label = this.name;
	var xhtml = "<table class='datatable' cellspacing=1 cellpadding=1 width=220>";
	    xhtml +="<tr><td>Elevation:<td align=left>&nbsp;" + this.elevation;
	    xhtml +=" <td align=right>"+pfloat((parseInt(this.elevation.replace(/[^\d]/g,""))*.3),1)+" m";
	    xhtml +="<tr><td>Lat/Lon:<td align=left>&nbsp;" + pfloat(this.lat,4) +"&deg;N<td align=right>&nbsp;"+ pfloat(this.lon,4) +"&deg;W";
//	    xhtml +="<tr><td>Longitude:&nbsp;<td align=left>&nbsp;" + pfloat(this.lon,4) +"&deg;W";
	    xhtml +="<tr><td>Alt Map 1:<td align=left colspan=2>&nbsp;" + return_maptech_link(this.lat,this.lon);
	    xhtml +="<tr><td>Alt Map 2:<td align=left colspan=2>&nbsp;" + return_topozone_link(this.lat,this.lon);
	    xhtml +="<\/table>";
 	return xhtml;
 	}

// SHELTER DETAIL TABLE: INFO TAB 2
Shelter.prototype.detail_table = function() {
	var xhtml = "<table class='datatable' cellspacing=1 cellpadding=1 width=240>";
		if(forecasts && forecasts[this.lat+"/"+this.lon]) {
			f = forecasts[this.lat+"/"+this.lon];
		    xhtml +="<tr><td valign=top>Weather:<td align=left>" + return_weather_link(this.lat,this.lon,(f.min +"&deg;-" + f.max +"&deg; &nbsp; "+f.text));			
			}
	    xhtml +="<tr><td valign=top>Capacity:&nbsp;<td align=left>" + this.capacity;
	    xhtml +="<tr><td valign=top>Maintainer:&nbsp;<td align=left><a href=#>" + this.club + "<\/a>";
	    xhtml +="<tr><td valign=top>Fee Site:<td align=left>" + this.fee;
	    xhtml +="<tr><td valign=top>Notes:&nbsp;<td align=left>" + this.notes;
    	xhtml +="<\/table>";
 	return xhtml;
	
	}

// TOWN DETAIL TABLE: INFO TAB 2
Town.prototype.detail_table = function() {
	var xhtml = "<table class='datatable' cellspacing=1 cellpadding=1 width=240>";
	    xhtml +="<tr><td valign=top>P.O. Hours:&nbsp;<td align=left>" + this.hours;
	    xhtml +="<tr><td valign=top>P.O. Phone:&nbsp;<td align=left>" + this.phone + "<\/x>";
		if(forecasts && forecasts[this.lat+"/"+this.lon]) {
			f = forecasts[this.lat+"/"+this.lon];
		    xhtml +="<tr><td valign=top>Weather:&nbsp<td align=left>" + return_weather_link(this.lat,this.lon,(f.min +"&deg;-" + f.max +"&deg; &nbsp; "+f.text));			
			}
   	xhtml +="<\/table>";
 	return xhtml;
	
	}

// SHELTER DETAIL TABLE: INIT INFOTABS
Shelter.prototype.init_infotabs = function() {
    this.info_tabs.length=0;
	this.info_tabs[this.info_tabs.length]=new GInfoWindowTab(this.catlabel, "<b><span style='letter-spacing:1px;'><a href=\"http://www.sophiaknows.com/atdb/atdb-detail.php?target="+this.target+"&key="+(this.index)+"\" target=\"info_window\" onclick=\"return pop_tool(this.href,650,500);\">" + this.label + "<\/a><\/b><\/span> " + this.info);
	this.info_tabs[this.info_tabs.length]=new GInfoWindowTab("Details",this.detail_table());
	return this.info_tabs;
	}
// TOWN DETAIL TABLE: INIT INFOTABS
Town.prototype.init_infotabs = function() {
    this.info_tabs.length=0;
    var hstr =this.getservices();
	this.info_tabs[this.info_tabs.length] = new GInfoWindowTab(this.catlabel, "<b>" + this.label + "<\/b> " + this.info);
	this.info_tabs[this.info_tabs.length] = new GInfoWindowTab("Details",this.detail_table());
//	this.info_tabs[this.info_tabs.length] = new GInfoWindowTab("Weather",mkForecastTab(this));
	if(hstr) this.info_tabs[this.info_tabs.length] = new GInfoWindowTab("Hostels", hstr);
	return this.info_tabs;
	}
// FEATURE DETAIL TABLE: INIT INFOTABS
Feature.prototype.init_infotabs = function() {
    this.info_tabs.length=0;
	this.info_tabs[this.info_tabs.length]=new GInfoWindowTab(this.catlabel, "<b><span style='letter-spacing:1px;'><a href=\"http://www.sophiaknows.com/atdb/atdb-detail.php?target="+this.target+"&key="+(this.index)+"\" target=\"info_window\" onclick=\"return pop_tool(this.href,650,500);\">" + this.label + "<\/a><\/b><\/span> " + this.info);
	this.info_tabs[this.info_tabs.length]=new GInfoWindowTab("Weather",mkForecastTab(this));
	return this.info_tabs;
	}


Town.prototype.show = function() {
	this.marker.openInfoWindowHtml("<b>" + this.label + "<\/b> " + this.info);
	}

Town.prototype.tabshow = function() {
	var infoTabs = this.init_infotabs();
	this.marker.openInfoWindowTabsHtml(infoTabs);
	var tselect = document.forms[0].elements[this.target+'_select']
	if(tselect.options[this.key]) tselect.selectedIndex=this.key;
	}

Town.prototype.getservices = function() {
	var hstr="";
 	this.gethostels();
 	for(s in this.services) { 
 		hstr+="<div style='padding-bottom:8px;width:280px;'><b>"+hostels[this.services[s]].name+"<\/b>: ";
 		hstr+=hostels[this.services[s]].location+" (";
 		hstr+=hostels[this.services[s]].services+")<\/div>";
 		}
 	return hstr;
 	}

Town.prototype.gethostels = function() {
	this.services.length=0;
	for(h in hostels) {
		if(this.lat == hostels[h].lat && this.lon == hostels[h].lon) this.services[this.services.length] = h;
		}
	}

// FEATURE: GENERIC INFO SHOW
Feature.prototype.show = function() {
	this.marker.openInfoWindowHtml("<b>" + this.label + "<\/b> " + this.info);
	}
// FEATURE: GENERIC TABBED INFO SHOW
Feature.prototype.tabshow = function() {
	var infoTabs = this.init_infotabs();
	this.marker.openInfoWindowTabsHtml(infoTabs);
	var tselect = document.forms[0].elements[this.target+'_select']
	var tselect = document.forms[0].elements[this.target+'_select']
	if(tselect.options[this.key]) tselect.selectedIndex=this.key;
	}

// SHELTER: GENERIC INFO SHOW
Shelter.prototype.show = function() {
	this.marker.openInfoWindowHtml("<b>" + this.label + "<\/b> " + this.info);
	}
// SHELTER: GENERIC TABBED INFO SHOW
Shelter.prototype.tabshow = function() {
	var infoTabs = this.init_infotabs();
	this.marker.openInfoWindowTabsHtml(infoTabs);
	var tselect = document.forms[0].elements[this.target+'_select']
	if(tselect.options[this.key]) tselect.selectedIndex=this.key;
	}



// GENERIC FORECAST TAB CONTENTS
function mkForecastTab(p) {
	var fcast=forecasts[p.lat+'/'+p.lon];
    if(fcast) {
    	return "<div style=width:200px><div style=float:left;padding-right:15px;>"+return_weather_link(p.lat,p.lon,"<img src='"+fcast.icon+"'height=60 width=60 border=0/>")+"<\/div>"+fcast.min+"&deg;-"+fcast.max+"&deg; F<p>"+fcast.text+"<\/div>";
    	}
  	return "<div style=width:200px>No Forecast For This Position<\/div>";
	}

// GENERIC FORECAST IFRAME CONTENTS
function mkForecastIframe(p) {
    if(p.lat) {
    	return "<iframe border=0 style='border:none;height:100px;width:100%;' src='http://sophiaknows.com/weather/weather_embed.js.html?lat="+p.lat+"&lon="+p.lon+"'><\/iframe>";
    	}
  	return "<div style=width:200px>No Forecast For This Position<\/div>";
	}



// GLOBALS

function return_poplink(p) {
	var args = return_poplink.arguments;
	var poplink = "<a href=\"gmap_navby.html?lat="+p.lat+"&lon="+p.lon+"\" "
	poplink+= "onclick=\"return pop_tool(this,700,450)\" title=\"View Detail\">";
	poplink+= ((args[1])? args[1] : p.name)+"<\/a>";
	return poplink;
	}

function return_info_link(target,key,label) {
	return "<a href=\"http://www.sophiaknows.com/atdb/atdb-detail.php?target="+target+"&key="+key+"\" onclick=\"return pop_tool(this.href,700,500)\" target=\"infowindow\" title=\"View Waypoint Detail\">"+label+"<\/a>";
	}


function return_topozone_link(lat,lon) {
	return "<a href=\"http://www.topozone.com/map.asp?lat="+lat+"&lon="+lon+"&datum=NAD83&s=50&size=l\" target=\"topowindow\">TopoZone.Com<\/a>";
	}

function return_maptech_link(lat,lon) {
	return "<a href=\"http://mapserver.maptech.com/homepage/index.cfm?lat="+lat+"&lon="+lon+"&scale=24000&type=1&size=S&zoom=100\" target=\"maptechwindow\">MapTech.Com<\/a>";
	}


function return_gmap_link(lat,lon,label) {
	return "<a href=\"http://sophiaknows.com/atdb/gmap_navby.html?lat="+lat+"&lon="+lon+"\" target=\"gmapwindow\" onclick=\"return pop_tool(this.href,700,500)\" title=\"ATDB GMap Detail\">"+label+"<\/a>";
	}

function return_mslive_link(lat,lon,label) {
	return "<a href=\"http://local.live.com/default.aspx?wip=2&v=2&style=h&rtp=~&&cp="+lat+"~"+lon+"&lvl=12\" target=\"mslivewindow\" onclick=\"return pop_tool(this.href,700,500)\" title=\"MS Live Map\">MS Local.Live.Com<\/a>";
	}

function return_weather_link(lat,lon,label) {
	return "<a href=\"http://sophiaknows.com/weather/weather.js.html?lat="+lat+"&lon="+lon+"\" target=\"noaawindow\" onclick=\"return pop_tool(this.href,700,150)\"  title=\"7 Day NOAA Forecast\">"+label+"<\/a>";
	}


function return_weatherquery($lat,$lon) {
	$wq="http://sophiaknows.com/weather/weather_embed.js.html?";
	$wq+="lat="+$lat;
	$wq+="&lon="+$lon;
	return $wq;
	}

// SINGLE PRECISION NUMBER NEW


function pfloat(f,p) {
	var str = Math.round(parseFloat(f)*Math.pow(10,p))/Math.pow(10,p)+"";
	var postfix="";
	str+=((str.indexOf('.')<0)?".0":"");
	str_array = str.split('.');
	for(c=0;c<p-str_array[1].length;c++) postfix+="0";
	return str_array[0]+"."+str_array[1].substring(0,p)+postfix;
	}


function precision1_float(f) {
	f=f+"";
	if(f.match(/\.0$/)) {
		return f;
		}
	if(f.indexOf(".")<0) {
		return f+".0";
		} else {
			return (Math.round(parseFloat(f)*10)/10);
			}
	}
// SINGLE PRECISION NUMBER OLD
function returnPrecision1Float(theFloat) {
	revisedFloat="";
	theFloat=theFloat+"";
	if(theFloat.indexOf(".")<0) {
		return theFloat+".0";
		} else {
			return (Math.round(parseFloat(theFloat)*10)/10);
			}
	}


function zero_prefix(str) {
	var xstr=str+"";
	if(xstr.match(/^[\d\.]+$/)) {
		prefix="";
		prelen= 10 - ((xstr.indexOf(".")>-1)?xstr.indexOf("."):xstr.length);
		for(e=0;e<prelen;e++) {prefix=prefix+"0";}
		xstr = prefix+""+str;
		}
	return xstr;	
	}




// POPUP: (function: PopTool) 
function open_infowindow(obj) {
	if(obj=="") return false;
	obj=eval(obj);
	infourl="http://www.sophiaknows.com/atdb/atdb-detail.php?target="+obj.target+"&key="+obj.key;
	pop_tool(infourl,700,550);
	}


// POPUP: (function: PopTool) 
function open_focastwindow(obj) {
	if(obj=="") return false;
	obj=eval(obj);
	focasturl="http://sophiaknows.com/weather/weather.js.html?lat="+obj.lat+"&lon="+obj.lon;
	pop_tool(focasturl,750,150);
	}
// POPUP: (function: PopTool) 
function open_gmapwindow(obj) {
	if(obj=="") return false;
	obj=eval(obj);
	gmapurl="http://sophiaknows.com/atdb/gmap_navby.html?lat="+obj.lat+"&lon="+obj.lon;
	pop_tool(gmapurl,750,450);
	}



// POPUP: (function: PopTool) 

/* POPUP: (function: PopTool) */
function pop_tool(a) {
   var argv=pop_tool.arguments;
   var rstamp = new Date();
   var params="width=" + ((argv[1])?argv[1]:600);
       params+=",height="+((argv[2])?argv[2]:450);
       params+=",scrollbars=1,resizable=1";
   var x_target = 'new'+rstamp.getTime();
   var x_url = a;
   x_target=((x_target)?x_target:"_blank");
   var popWindow=window.open(x_url,x_target,params);	
   popWindow.focus();
   return false;
   }


