window.is_top=true; var map=null;
function $(e) { return typeof e == 'string' ? document.getElementById(e) : e; }
function E(e) { return document.createElement(e); }

function show(id,s) {
  var l = $(id);
  if (l) l.style.display = s ? s : 'block';
}

function hide(id) {
  var l = $(id);
  if (l) l.style.display = 'none';
}

function get_value(f) {
  var e = $(f);
  return e ? e.value : null;
}

function has_value(f) {
  return !is_empty(get_value(f));
}

function get_option(f) {
  var e = $(f);
  return e && e.selectedIndex > -1 ? e.options[e.selectedIndex].text : null;
}

function set_value(f,v) {
  var e = $(f);
  if (e) e.value = v;
}

function set_option(f,v) {
  var e = $(f); if (!e) return;
  var o = e.options; var n = o.length;
  for (var i=0; i<n; i++) if (o[i].text==v) {
    o[i].selected = true;
    break;
  }
}

function set_text(f,t) {
  var f = $(f);
  if (f) {
    if (f.innerText) f.innerText = t;
    else {
      f.innerHTML = '';
      f.appendChild(document.createTextNode(t));
    }
  }
}

function set_html(f,t) {
  var f = $(f);
  if (f) f.innerHTML = t;
}

function set_cookie(f,v,e) {
  if (is_empty(v)) { e = new Date(); e.setTime(e.getTime()-100000); }
  var d = e ? 'expires=' + e.toGMTString() + '; ' : '';
  document.cookie = f+"="+v+"; "+d+"path=/";
}

function get_cookie(f) {
  var cs = document.cookie.split(';'); for (var i in cs) { var nv = cs[i].split('=');
    if (string_trim(nv[0])==f) return nv.length>1 ? unescape(string_trim(nv[1])) : null; 
  }
  return null;
}

function copy_value(from,to) {
  set_value(to,get_value(from));
}

function has_class(e,c) {
  e = $(e); if (!e) return false;
  var p = e.className.split(" "); var l = p.length;
  for (var i=0; i<l; i++) if (p[i]==c) return true;
  return false;
}

function add_class(e,c) {
  e = $(e); if (!e || has_class(e,c)) return;
  e.className = e.className + ' ' + c;
  if (e.tagName.toLowerCase()=='td' && is_gecko()) e.parentNode.parentNode.innerHTML = e.parentNode.parentNode.innerHTML;
}

function remove_class(e,c) {
  e = $(e); if (!e) return;
  var p = e.className.split(" "); var l = p.length;
  for (var i=0; i<l; i++) if (p[i]==c) {
    if (p.splice) p.splice(i,1); else p[i] = null;
    e.className = p.join(" ");
    return;
  }
}

function blank_class(e,c) {
  e = $(e); if (!e) return;
  if (e.value=='') add_class(e,c);
  else remove_class(e,c);
}

function check_enter(event,handler) {
  if (event.keyCode == 13) {
    handler();
    return false;
  }
  return true;
}

function addEvent(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent) {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
}

function is_gecko() {
  var a = window.navigator.userAgent.toLowerCase();
  return a.indexOf('gecko')>=0 && a.indexOf('safari')<0;
}

function is_empty(v) {
  return !v || v=='';
}

function is_integer(event) {
  var key;
  if (event.keyCode) {
    key = event.keyCode;
  } else {
    key = event.which;
  }
  return is_digit(key) || is_control(key) || (is_nav(key) && !event.shiftKey);
}

function is_decimal(field, event) {
  var key;
  if (event.keyCode) {
    key = event.keyCode;
  } else {
    key = event.which;
  }
  return is_digit(key) || is_control(key) || (is_nav(key) && !event.shiftKey);
}

function is_digit(key) {
  return key > 47 && key < 58;
}

function is_control(key) {
  return key == 8 || key == 9 || key == 10 || key == 13 || key == 46 || key == 127;
}

function is_nav(key) {
  return key > 34 && key < 41;
}

function limit_area(f, limit) {
  var c = f.value.length - limit;
  if (c>0) {
    if (document.selection) { // ie
      var sel = document.selection.createRange();
      sel.moveStart('character', -c);
      sel.text = '';
    } else if (f.selectionStart || f.selectionStart=='0') { // gecko
      var s = Number(f.selectionStart);
      f.value = f.value.substring(0,s-c) + f.value.substring(s);
    } else {
      f.value = f.value.substring(0, limit);
    }
  }
}

function set_days(month, day) {
  if (month==0) return;
  var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[month-1];
  var selectedDay = day.selectedIndex == null ? 1 : day.selectedIndex;
  var oldDays = day.options.length;
  day.options.length = daysInMonth + 1;
  for (var i=oldDays; i <= daysInMonth; i++) day.options[i] = new Option(i,i);
  day.selectedIndex = Math.min( selectedDay, daysInMonth );
}

function string_trim(s) {
  return s ? s.replace(/^\s+|\s+$/g,'') : s;
}

function unserialize(s) {
  s = string_trim(s);
  if (is_empty(s)) return null;
  return un_serialize(s)[0];
}

function un_serialize(s) {
  switch (s.charAt(0)) {
    case 'N':
      return [null, s.substr(2)];
    case 's':
      var l = s.substring(2, s.indexOf(':',2));
      var n = Number(l), p = 0, j = l.length+4;
      while (p<n) {
        var c = s.charCodeAt(j++);
        p += c < 128 ? 1 : (c < 2048 ? 2 : (c < 65536 ? 3 : 4));
      }
      return [s.substring(l.length + 4, j), s.substr(j + 2)];
    case 'i':
    case 'd':
      var n = s.substring(2, s.indexOf(';',2));
      return [Number(n), s.substr(n.length + 3)];
    case 'b':
      var b = s.substr(2, 1) == 1;
      return [b, s.substr(4)];
    case 'a':
      var l = s.substring(2, s.indexOf(':',2));
      s = s.substr(l.length + 4);
      l = Number(l);
      var a = new Array();
      for (var i=0; i<l; i++) {
        var k = un_serialize(s);
        var v = un_serialize(k[1]);
        s = v[1];
        a[k[0]] = v[0];
      }
      return [a, s.substr(1)];
    case 'O':
      var l = s.substring(2, s.indexOf(':',2));
      var c = s.substr(l.length + 4,Number(l));
      s = s.substr(l.length + 6 + Number(l));
      var n = s.substr(0, s.indexOf(':'));
      s = s.substr(n.length + 2);
      n = Number(n);
      var o = new Object();
      for (var i=0; i<n; i++) {
        var property = null;
        var value    = null;
        var k = un_serialize(s);
        var v = un_serialize(k[1]);
        // strip access mods
        key[0] = key[0].replace(new RegExp('^\x00' + c + '\x00'), '').replace(new RegExp('^\x00\\*\x00'), '');
        s = v[1];
        o[k[0]] = v[0];
      }
      return [a, s.substr(1)];
    default:
      return [null, null];
  }
}

function word_count(f, limit, status, max) {
  var a = f.value.split(/\s+/); if (!a) a = [];
  if (a.length>0 && is_empty(a[0])) a.splice(0,1);
  if (a.length>0 && is_empty(a[a.length-1])) a.splice(a.length-1,1);
  if (a.length>limit && limit<max) set_text(status,"You have passed your word limit. Consider upgrading your account to leave a longer description.");
  else set_text(status,Math.max(limit - (a.length>0 && !is_empty(a[0]) ? a.length : 0),0) + " words remaining"); 
}

function count_words(f,limit,status,max) {
  f = $(f); word_count(f,limit,status,max);
  addEvent(f,'keyup',function() { word_count(f,limit,status,max); });
}

function map_init() {
  if (map) return false;
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GSmallMapControl());
  window.onunload=map_unload;
}

function map_center(points) {
  if (points.length==0) return;
  var nt = 180; var xt = -180; var ng = 180; var xg = -180; var z = 13;
  for (var i=0; i<points.length; i++) {
    var p = points[i];
    if (p.lat()<nt) nt = p.lat();
    if (p.lat()>xt) xt = p.lat();
    if (p.lng()<ng) ng = p.lng();
    if (p.lng()>xg) xg = p.lng();
  }
  if (points.length > 1) {
    var r = Math.max( 0.1, map.getSize().height / map.getSize().width );
    var s = Math.max( Math.abs( xt - nt ) / r, Math.abs( xg - ng )  );
    z = Math.min( 17, Math.max( 0, 17 - Math.round( Math.log( s / 0.003 ) / Math.LN2 ) ) );
  }
  map.setCenter(new GLatLng((xt + nt)/2,(xg + ng)/2),z);
}

function map_add_point(point, info, img) {
  var marker;
  if (img) { var icon = new GIcon(G_DEFAULT_ICON); icon.iconSize = new GSize(32, 32);icon.image = img; marker = new GMarker(point,{icon:icon}); }
  else marker = new GMarker(point);
  map.addOverlay(marker);
  if (info && info!='') GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(info);
  });
}

function map_set_points(d) {
  var a = unserialize(d);
  var t = [];
  if (a) for (i=0; i<a.length; i++) {
    t.push(new GLatLng(Number(a[i][0]),Number(a[i][1])));
  }
  map_center(t);
  for (i=0; i<t.length; i++) {
    map_add_point(t[i],a[i].length > 2 ? a[i][2] : null,a[i].length > 3 ? a[i][3] : null);
  }
}

function map_unload() {
  map=null;
  GUnload();
  window.onunload=null;
}

function sort(type,form) {
  if (!form) form = 0;
  var s = document.forms[form].sort;
  if (s.value==type+'Down') s.value = type+'Up';
  else s.value = type+'Down';
  document.forms[form].submit();
}

function advanced() {
  $('search_toggle').href = 'javascript:basic();';
  $('twistie').src = '/images/up.gif';
  show('advanced');
}

function basic() {
  $('search_toggle').href = 'javascript:advanced();';
  $('twistie').src = '/images/down.gif';
  hide('advanced');
  document.forms[0].ctry.value = '';
  document.forms[0].sls.value = '';
  document.forms[0].yrs.value = '';
  document.forms[0].emps.value = '';
}

function set_photo(i) {
  var m = $('main_photo').getElementsByTagName('img').item(0); var t = $('thumb_'+i).getElementsByTagName('img').item(0);
  m.src = t.src; m.title = t.title; set_text('caption',t.title);
}

function bookmark(id,level,name,dba) {
  set_cookie('bm',id+'|'+level+'|'+name+'|'+dba); bookmark_show();
}

function bookmark_show() {
  var c = get_cookie('bm'); if (!c) return; c = c.split('|');
  var d = $('bookmark'); if (!d) { d = E('div'); d.setAttribute('id','bookmark'); document.body.appendChild(d); }
  d.innerHTML = "<div><h2>Bookmarked Business</h2><p class='companyName "+c[1]+"'><a href='/profile/"+c[0]+"'>"+c[2]+"</a>"+
    (is_empty(c[3])?'':"<span>"+c[3]+"</span>") + "</p><a id='bm_close' href='javascript:bookmark_close();'><img src='/images/close.jpg'/></a></div>";
}

function bookmark_close() {
  set_cookie('bm',''); var d = $('bookmark'); if (d) d.parentNode.removeChild(d);
}

function next_level(p) {
  return p == 'platinum' ? 'gold' : (p == 'gold' ? 'silver' : (p == 'silver' ? 'compare' : (p == 'basic' ? 'platinum' : ($('basic') ? 'basic' : 'platinum'))));
}

function set_menu() {
  remove_class('platinum_menu','selected'); remove_class('gold_menu','selected'); remove_class('silver_menu','selected'); remove_class('basic_menu','selected'); remove_class('compare_menu','selected');
  add_class(window.aCurrent+'_menu','selected');
}

function preview(s) {
  window.aRotation = false; change_preview(s,0);
}

function remove_filter(c,a,e) {
  var to = $(e.to); if (to.style.removeAttribute) { to.style.removeAttribute('filter'); }
  if (e.to != e.from) hide(e.from);
}

function change_preview(to, delay) {
  var from = window.aCurrent; var delay = typeof delay == "undefined" ? 1 : delay;
  if (window.aFrom) window.aFrom.stop(true); if (window.aTo) window.aTo.stop(true);
  window.aFrom = new YAHOO.util.Anim(from, { opacity: { from:1.0, to:0.0 } }, delay); window.aFrom.animate();
  window.aTo = new YAHOO.util.Anim(to, { opacity: { from:0.0, to:1.0 } }, delay); show(to);
  window.aTo.onComplete.subscribe(remove_filter,{from:from, to:to}); window.aTo.animate();
  window.aCurrent = to; window.setTimeout("set_menu()",delay*500);
}

function rotate_preview() {
  if (!window.aRotation) return false;
  change_preview(next_level(window.aCurrent));
  start_rotation();
}

function start_rotation() {
  window.setTimeout("rotate_preview()",3000);
}

function start_animation() {
  window.aRotation = true; window.aCurrent = $('basic') ? 'basic' : 'platinum'; window.aFrom = null; window.aTo = null;
  new YAHOO.util.YUILoader({require: ["animation"],loadOptional: false,onSuccess: start_rotation,timeout: 10000,combine: true}).insert(); 
}

function process(r,f) {
  var s = /<script(.|\s)*?\/script>/img; var scripts = [];
  r = r.replace(s,function(v) { scripts.push(v.replace(/<\/?script(.|\s)*?>/img,'')); return ''; });
  f(r);
  for (var is=0; is<scripts.length; is++) { eval(scripts[is]); }
}

function ajax_init() {
  if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) {}
  return null;
}

function ajax_get(uri, target) {
  var x = ajax_init();
  if (!x) return true;
  x.open("GET", uri, true);
  x.onreadystatechange = function() { ajax_response(x,target); }
  x.send('');
}

function ajax_response(r,target) {
  if (r.readyState!=4) return;
  var d = r.responseText;
  process(d,function(t) {
    if (typeof target=="function") target(d);
    else set_html(target,t);
  });
  delete r;
}

function track(t) {
  ajax_get('/parts/click?target=' + encodeURIComponent(t),new function(d) {});
}

function alpha_links(base) {
  document.write("<ul id='alpha'><li><b>Jump To:</b></li>");
  for (var i=65;i<91;i++) {
    var s = String.fromCharCode(i);
    document.write("<li><a href='"+base+"?letter="+s+"'>"+s+"</a></li>");
  }
  document.write("<li><a href='"+base+"?letter=1'>Etc</a></li></ul>");
}