/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
  if (element.addEventListener) {
    element.addEventListener(eventType, lamdaFunction, useCapture);
    return true;
  } else if (element.attachEvent) {
    var r = element.attachEvent('on' + eventType, lamdaFunction);
    return r;
  } else {
    return false;
  }
}

/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */

addEvent(window, 'load', init, false);

function init() {
  var formInputs = document.getElementsByTagName('input');
  for (var i = 0; i < formInputs.length; i++) {
    var theInput = formInputs[i];
    
    if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
      /* Add event handlers */          
      addEvent(theInput, 'focus', clearDefaultText, false);
      addEvent(theInput, 'blur', replaceDefaultText, false);
      
      /* Save the current value */
      if (theInput.value != '') {
        theInput.defaultText = theInput.value;
      }
    }
  }
}

function clearDefaultText(e) {
  var target = window.event ? window.event.srcElement : e ? e.target : null;
  if (!target) return;
  
  if (target.value == target.defaultText) {
    target.value = '';
  }
}

function replaceDefaultText(e) {
  var target = window.event ? window.event.srcElement : e ? e.target : null;
  if (!target) return;
  
  if (target.value == '' && target.defaultText) {
    target.value = target.defaultText;
  }
}


/* Store locator */

function prepareLinks() {
  // Get all area tags in map and attach onClick event
  var counties = document.getElementsByTagName('area');
  for (var i = 0; i < counties.length; i++) {
    counties[i].onclick = function() {
      return showStores(this);
    }
  }
}

function showStores(county) {
  // Display the div with same Id as alt attribute of area tag.
  var countyName = county.getAttribute('alt');
  var stores = document.getElementById(countyName);
  stores.style.display = 'block'; 

  // Hide all the other county divs
  var counties = document.getElementsByTagName('area');
  for (var i = 0; i < counties.length; i++) {
    var countiesToHide = counties[i].getAttribute('alt');
    if (document.getElementById(countiesToHide) && countiesToHide != stores.id) {
      document.getElementById(countiesToHide).style.display = '';
    }
  }
  // Don't follow the link
  return false;
}


