/** Tile Utility Functions **/

		//make sure these stay in sync with PHP equivalents in serve-tile.php
		var WS_TILE_MIN_WIDTH = 225;
		var WS_TILE_LAYOUT_SWITCH_THRESHOLD = 450;
		
		var WS_TILE_H_DEFAULT_HEIGHT = 286;
		var WS_TILE_H_MIN_HEIGHT = 145;
		var WS_TILE_V_BASE_HEIGHT = 235;
		
		var WS_TILE_V_DEFAULT_LIST_LIMIT = 6;
		var WS_TILE_V_MIN_LIST_LIMIT = 2;
		var WS_TILE_MAX_LIST_LENGTH = 13;
		var WS_TILE_RESULT_LINE_HEIGHT = 18;
		
		var WS_TILE_FOOTER_LINE_HEIGHT = 18;
		var WS_TILE_FOOTER_2_LINE_THRESHOLD = 400;
		
		var WS_TILE_FOOTER_HIDDEN = 100;
		var WS_TILE_FOOTER_ADDRESS = 101;
		
		function ws_tileOptions(defaultHeight, minHeight, maxHeight, numFooterLines) {
			this.defaultHeight = defaultHeight;
			this.minHeight = minHeight;
			this.maxHeight = maxHeight;
			this.footerLines = numFooterLines;
		}
		function ws_footerInfo(footerX, footerY, footerX2, inputX, inputY, inputW, numFooterLines) {
			this.footerX = footerX;
			this.footerY = footerY;
			this.footerX2 = footerX2;
	  	this.inputX = inputX;
	  	this.inputY = inputY; 
			this.inputW = inputW;
			this.footerLines = numFooterLines;
		}
		function ws_rangeObj(minVal, maxVal) {
			this.minVal = minVal;
			this.maxVal = maxVal;
		}
		
		function ws_getTileOptions(width, layout, footerType) {	

			if (layout==null || layout=="auto")
				layout = ws_getDefaultLayout(width);
			
			//default to horizontal
			var defaultHeight = WS_TILE_H_DEFAULT_HEIGHT;
			var minHeight = WS_TILE_H_MIN_HEIGHT;
			var maxHeight = WS_TILE_H_DEFAULT_HEIGHT

			if ( layout == "vertical") 
		  {
		    defaultHeight = WS_TILE_V_BASE_HEIGHT + WS_TILE_RESULT_LINE_HEIGHT*WS_TILE_V_DEFAULT_LIST_LIMIT;
				minHeight = WS_TILE_V_BASE_HEIGHT + WS_TILE_RESULT_LINE_HEIGHT*WS_TILE_V_MIN_LIST_LIMIT;
		    maxHeight = WS_TILE_V_BASE_HEIGHT + WS_TILE_RESULT_LINE_HEIGHT*WS_TILE_MAX_LIST_LENGTH;
		  }
			//how much vertical space will the footer require?
			var numFooterLines = 1;			
			if ( footerType == WS_TILE_FOOTER_HIDDEN )
				numFooterLines = 0;	
			else if (width < WS_TILE_FOOTER_2_LINE_THRESHOLD)
				numFooterLines = 2;
			
			//if footer not on one line, need to correct height for that:
			var heightCorrection = (numFooterLines - 1) * WS_TILE_FOOTER_LINE_HEIGHT;
			defaultHeight += heightCorrection;
			minHeight += heightCorrection;
			maxHeight += heightCorrection;
			
			return new ws_tileOptions(defaultHeight, minHeight, maxHeight, numFooterLines);
		}
		
		function ws_getFooterInfo(width, layout, height, footerType){

			//default to horizontal
			var footerX = 8;
			var footerX2 = width-4;
			
		  if ( layout == "vertical") 
		  {
				footerX = 3;
				footerX2 = width-3;
		  }

			//default to single line footer
			var footerY = height - WS_TILE_FOOTER_LINE_HEIGHT;
			var inputX = 225;
			var inputY = 0;
			var numFooterLines = 1;
			
			if (width < WS_TILE_FOOTER_2_LINE_THRESHOLD)
			{
				footerY -= WS_TILE_FOOTER_LINE_HEIGHT;
				inputX = 0;
				inputY += WS_TILE_FOOTER_LINE_HEIGHT;
				numFooterLines = 2;
			}
			
			var inputW = footerX2 - footerX - 32 - inputX; //avail width - button - text-space 
			
			return new ws_footerInfo(footerX, footerY, footerX2, inputX, inputY, inputW, numFooterLines);
		}

		function ws_getDefaultTileHeight(width, layout, footerType) {
			var options = ws_getTileOptions(width, layout, footerType);
			return options.defaultHeight;
		}
		
		function ws_getIFrameHeight(width, layout) {
			var options = ws_getTileOptions(width, layout, WS_TILE_FOOTER_HIDDEN);
			return options.defaultHeight - WS_TILE_FOOTER_LINE_HEIGHT;
		}
		
		function ws_getIFrameHeightRange(width, layout) {
			var options = ws_getTileOptions(width, layout, WS_TILE_FOOTER_HIDDEN);
			return new ws_rangeObj(options.minHeight, options.maxHeight);
		}
		
		function ws_getDefaultLayout(width){
			return ( Number(width) < WS_TILE_LAYOUT_SWITCH_THRESHOLD) ? "vertical" : "horizontal";
		} 
function ws_var_exists(varname)
{
  return typeof( window[ varname ] ) != "undefined";
}

function ws_escape(value)
{
  return escape(value.toString().replace(/#/g, ""));
}

function ws_show_address(){
  var address = document.getElementById("ws-street").value;
  var url = "http://www.walkscore.com/get-score.php?street=" + escape(address) + "&utm_campaign=tilefooter&utm_medium=address_search";
  window.open(url, "Walk_Score");
}

function ws_innerclean_elem(elem) {
	if (!elem || !elem.style)
  	return;
	elem.style.textAlign = "left";
	elem.style.textDecoration = "none";
	elem.style.textAlign = "left";
	elem.style.padding = 0;
	elem.style.fontSizeAdjust = "none";
	elem.style.fontStretch = "normal";
	elem.style.fontStyle="normal";
	elem.style.fontVariant = "normal";
  elem.style.letterSpacing = "normal";
	elem.style.wordSpacing = "normal";
	elem.style.textTransform = "none";
	elem.style.verticalAlign = "baseline";
	elem.style.textIndent = 0;
	elem.style.textShadow = "none";
	elem.style.whiteSpace = "normal";
  elem.style.backgroundImage = "none";
  elem.style.backgroundColor = "transparent";
}
function ws_outerclean_elem(elem) {
	if (!elem || !elem.style)
  	return;
	elem.style.cssFloat = "left";
	elem.style.margin = 0;
	elem.style.outline = "none";
}

function ws_fullclean_elem(elem){
	if (!elem || !elem.style)
  	return;
	ws_outerclean_elem(elem);
	ws_innerclean_elem(elem);
	elem.style.border = "none";
}

function ws_show_tile(width, layout, height, hideFooter, params)
{
  //do some css and dynamic layout:
  var tile = document.getElementById("ws-walkscore-tile");
  if (tile) {
    tile.style.position = "relative";
    tile.style.textAlign = "left";
	}
    
  //default to 1-line footer -- set iframe height to accomodate
  var iframeHeight = ws_height - WS_TILE_FOOTER_LINE_HEIGHT;			
    
  if (hideFooter)
  {
    iframeHeight += WS_TILE_FOOTER_LINE_HEIGHT;
    var footer = document.getElementById("ws-footer");
    if (footer)
      footer.style.display = "none";
  }
  else {
    var footerInfo = ws_getFooterInfo(width, layout, height, WS_TILE_FOOTER_ADDRESS);

    if (footerInfo.footerLines == 2)
      iframeHeight -= WS_TILE_FOOTER_LINE_HEIGHT;
    
    var footer = document.getElementById("ws-footer");
    if (footer) {
      ws_fullclean_elem(footer);
      footer.style.position = "absolute";
      footer.style.display = "block";
      footer.style.left = footerInfo.footerX+"px";
      footer.style.top = footerInfo.footerY+"px";
      footer.style.width = (footerInfo.footerX2 - footerInfo.footerX) +"px";	
      footer.style.font = "11px/9px Verdana, Arial, Helvetica, sans-serif";
    }
    
    var form = document.getElementById("ws-form");
    if (form) {
      ws_fullclean_elem(form);
      form.onsubmit=function(){ ws_show_address(); return false; }		
		}
   	
    var input = document.getElementById("ws-street");
    if (input) {
      ws_innerclean_elem(input);
      ws_outerclean_elem(input);
      input.style.position = "absolute";
      input.style.left = footerInfo.inputX + "px";
      input.style.top = footerInfo.inputY + "px";
      input.style.width = footerInfo.inputW + "px";
      input.style.height = "13px";
      input.style.border = "1px solid #aaa";
      input.style.color = "#000";
      input.style.backgroundColor = "#fff";
      input.style.font = "11px/9px Verdana, Arial, Helvetica, sans-serif";
      input.value='Enter an address';
      input.onfocus=function() { if (this.value=='Enter an address') this.value=''; }
		}

    var button = document.getElementById("ws-go");
    if (button) {
    	ws_fullclean_elem(button);
      button.style.position = "absolute";
      button.style.right = "0px";
      button.style.top = footerInfo.inputY + "px";
		}
       
    var link = document.getElementById("ws-a");
    if (link) {
      ws_fullclean_elem(link);
      link.style.fontWeight = "bold";
    }
  }
              
  var tile = document.getElementById("ws-walkscore-tile");
	if (tile) {
    ws_innerclean_elem(tile);
    tile.style.width = width+"px";
    tile.style.height = height+"px";
      
    params += "&height=" + iframeHeight;    
    params += "&footheight=" + (height-iframeHeight);
    
    iframe = document.createElement('iframe');
    ws_fullclean_elem(iframe);
    iframe.marginHeight = "0";
    iframe.marginWidth = "0";
    iframe.width=width + "px";
    iframe.height=iframeHeight + "px";
    iframe.frameBorder="0";
    iframe.scrolling = "no";
    iframe.style.border = 0;
    tile.appendChild(iframe);
    iframe.src = "http://www.walkscore.com/serve-walkscore-tile.php?" + params;
  }
}

//inline code to prepare to display the tile

//build param string using any variables that were defined on hosting page
var ws_params = "";

if (!ws_var_exists("ws_wsid")) 
	var ws_wsid = "none";
ws_params += "wsid=" + ws_escape(ws_wsid);

if (ws_var_exists("ws_address"))
{
  ws_params += "&street=" + ws_escape(ws_address);
}
if (ws_var_exists("ws_lat") && ws_var_exists("ws_lon"))
{
  ws_params += "&lat=" + ws_escape(ws_lat) + "&lng=" + ws_escape(ws_lon);
}

//width
if (!ws_var_exists("ws_width")) 
	var ws_width = "500";
ws_width = String(ws_width).replace(/px/g, "");
ws_params += "&width=" + ws_escape(ws_width);

//layout
if (!ws_var_exists("ws_layout")) 
	var ws_layout = ws_getDefaultLayout(ws_width); 
ws_params += "&layout=" + ws_escape(ws_layout);

//distance units
if (ws_var_exists("ws_distance_units"))
	ws_params += "&units=" + ws_escape(ws_distance_units);
      
//height
if (!ws_var_exists("ws_height")) 
  ws_height = ws_getDefaultTileHeight( Number(ws_width), ws_layout, WS_TILE_FOOTER_ADDRESS);
ws_height = Number(ws_height);

if (ws_var_exists("ws_hide_scores_below")) ws_params += "&hide_scores_below=" + ws_escape(ws_hide_scores_below);

if (!ws_var_exists("ws_hide_footer")) ws_hide_footer = false;    

//color parameters for iframe
if (ws_var_exists("ws_background_color")) ws_params += "&bg_col=" + ws_escape(ws_background_color);
if (ws_var_exists("ws_score_color")) ws_params += "&score_col=" + ws_escape(ws_score_color);
if (ws_var_exists("ws_link_color")) ws_params += "&link_col=" + ws_escape(ws_link_color);
if (ws_var_exists("ws_link_hover_color")) ws_params += "&link_hov_col=" + ws_escape(ws_link_hover_color);
if (ws_var_exists("ws_secondary_color")) ws_params += "&head_col=" + ws_escape(ws_secondary_color);
if (ws_var_exists("ws_category_color")) ws_params += "&category_col=" + ws_escape(ws_category_color);
if (ws_var_exists("ws_result_color")) ws_params += "&result_col=" + ws_escape(ws_result_color);
if (ws_var_exists("ws_map_frame_color")) ws_params += "&map_frame_col=" + ws_escape(ws_map_frame_color);
if (ws_var_exists("ws_no_link_logo")) ws_params += "&unlink_logo=" + ws_escape(ws_no_link_logo);
if (ws_var_exists("ws_hide_bigger_map")) ws_params += "&hide_bigger_map=" + ws_escape(ws_hide_bigger_map);
if (ws_var_exists("ws_disable_street_view")) ws_params += "&disable_street_view=" + ws_escape(ws_disable_street_view);


//write the iframe and display the tile:
ws_show_tile(ws_width, ws_layout, ws_height, ws_hide_footer, ws_params);


//footer color customization: 

if (ws_var_exists("ws_background_color")) {
  var ws_tile = document.getElementById("ws-walkscore-tile");
  if (ws_tile) ws_tile.style.backgroundColor = "#" + ws_escape(ws_background_color);
}

if (ws_var_exists("ws_score_color")) {
  var ws_link = document.getElementById("ws-a");
  if (ws_link) ws_link.style.color = "#" + ws_escape(ws_score_color);
}

var ws_input = document.getElementById("ws-street");
if (ws_input) {
  if (ws_var_exists("ws_address_box_frame_color"))
    ws_input.style.borderColor = "#" + ws_escape(ws_address_box_frame_color);
  if (ws_var_exists("ws_address_box_bg_color"))
    ws_input.style.backgroundColor = "#" + ws_escape(ws_address_box_bg_color);
  if (!ws_var_exists("ws_address_box_text_color"))
  	ws_address_box_text_color = "757575";
  ws_input.style.color = "#" + ws_escape(ws_address_box_text_color);
}