// :hover screws up IE6 in this case, so just do this everywhere.
Event.observe(window, 'load', function () {
  var button = $('get_now_button');
  
  if ( button ) {
    button.onmouseover = function () { button.addClassName('hover') };
    button.onmouseout = function () { button.removeClassName('hover') };
  }
});

// Only use the following in IE:
function fix_hover ( node ) {
  var old_mouseenter = node.onmouseenter || function () {};
  var old_mouseleave = node.onmouseleave || function () {};
  
  node.onmouseenter = function () {
    if ( ! /\bhover\b/.test(this.className) ) {
      this.className = this.className.replace(/\S+/g, "$& $&_hover")
        + " hover";
    }
    
    old_mouseenter.apply(this, arguments);
  }
  
  node.onmouseleave = function () {
    this.className = this.className.replace(/\S+_hover|\bhover\b/g, "");
    
    old_mouseleave.apply(this, arguments);
  }
}

function center_page_vertically () {
  // 560 is the height of the homepage content. 73 is the height of the logo,
  // which extends above the framework div.
  var height = window.innerHeight || document.documentElement.clientHeight;
  var framework_top = (height - 560)/2 + 73;
  
  $('framework').style.marginTop = Math.max(framework_top, 90) + "px";
  document.body.style.backgroundPosition
    = "50% " + (Math.max(framework_top, 73) - 193) + "px";
}

function initialize_home_index_content () {
  window.onresize = center_page_vertically;
  center_page_vertically();
  
  swfobject.embedSWF("swf/main.swf", "swf_main", "862", "404", "9.0.28.0",
    "swf/expressInstall.swf", {}, { wmode : "transparent" });
}

function initialize_how_buttons () {
  var buttons = $$('#how_buttons a');
  
  function reset () {
    buttons.each(function ( a ) {
      a.className = "off";
      $('how_' + a.href.split('#')[1]).style.display = "none";
    });
  }
  
  buttons.each(function ( a ) {
    var div = $('how_' + a.href.split('#')[1]);
    var h2 = div.down('div.h2');
    
    a.onclick = function () {
      reset();
      
      this.className = "on"
      div.style.display = "block";
      
      if ( h2 ) {
        h2.replace("<h2>" + h2.innerHTML + "</h2>");
        h2 = null;
        
        sIFR_replace_how_h2("#" + div.id + " h2");
      }
      
      return false;
    }
  });
}

function clear_initial ( input, initial ) {
  input.onblur = function () {
    if ( this.value == "" ) {
      this.value = initial;
      $(this).addClassName("initial");
    }
  }
  
  if ( input.value == initial ) {
    input.value = "";
    $(input).removeClassName("initial");
  }
}

function download_check_fragment () {
  if ( window.location.hash == "#text_me" ) {
    var phone = $('phone');
    
    if ( phone ) {
      Event.observe(window, 'load', function () {
        phone.select();
        phone.focus();
      });
    }
  }
}

function initialize_download_browser () {
  var a = $('browser_gecko').up('form').down('a.button');
  
  $('browser_gecko').onclick = function () {
    if ( this.checked ) {
      a.onclick = function () { return true };
      a.href = "http://app.pingbyzos.com/ZOSPingClient/Install/FFPlugin.xpi";
    }
    return true;
  }
  
  $('browser_msie').onclick = function () {
    if ( this.checked ) {
      a.onclick = function () { return true };
      a.href = "http://i.zhiing.com/ZOSPingClient/Install/IE/zhiingiesetup.exe";
    }
    return true;
  }
}

function initialize_download_browser_toolbox () {
  var a = $('browser_toolbox_gecko').up('form').down('a.button');
  
  $('browser_toolbox_gecko').onclick = function () {
    if ( this.checked ) {
      a.onclick = function () { show_gecko_toolbox(); return false };
      a.href = "#";
    }
    return true;
  }
  
  $('browser_toolbox_chrome').onclick = function () {
    if ( this.checked ) {
      a.onclick = function () { alert("Missing chrome download"); return false };
      a.href = "#";
    }
    return true;
  }
}

function show_gecko_toolbox () {
  var container = $('ff_toolbox_container');
  var framework = $('framework');
  
  var relayout_page = function () {};
  
  if ( Prototype.Browser.Gecko ) {
    // FireFox 2 doesn't update the page layout without this.
    relayout_page = function () {
      if ( framework.style.borderBottom == "1px solid transparent" ) {
        framework.style.borderBottom = "none";
      } else {
        framework.style.borderBottom = "1px solid transparent";
      }
    }
  }
  
  if ( container.style.display == "none" ) { 
    Effect.BlindDown(container, {
      duration : 0.2,
      afterUpdate : relayout_page
    });
    Effect.Appear(container, { duration : 0.5 });
  }
}

function find_form ( node ) {
  if ( node.nodeName == "FORM" ) {
    return $(node);
  } else {
    return $(node).up('form');
  }
}

function ajax_submit ( form, on_success, on_failure  ) {
  new Ajax.Request(form.action, {
    method : form.method,
    parameters : form.serialize(),
    onSuccess : on_success,
    onFailure : on_failure
  });
}

function button_builder ( node ) {
  var code = $('widget_code'),
    lat_lng_errors = $A([]),
    address_errors = $A([]);
  
  code.innerHTML = "";
  
  $w("lat lng").each(function ( id ) {
    $(id + "_error").innerHTML = "";
    
    if ( $(id).value == "" ) {
      lat_lng_errors.push(id);
    }
  });
  
  $w("address city state zip").each(function ( id ) {
    $(id + "_error").innerHTML = "";
    
    if ( $(id).value == "" ) {
      address_errors.push(id);
    }
  });
  
  if ( lat_lng_errors.length > 0 && address_errors.length > 0 ) {
    address_errors.each(function ( id ) {
      $(id + "_error").innerHTML = "required";
    });
    
    return;
  }
  
  code.innerHTML = "working...";
  
  ajax_submit(find_form(node),
    function ( response ) { /* success */
      code.innerHTML = "";
      code.appendChild(document.createTextNode(response.responseText));
    },
    function ( response ) { /* failure */
      code.innerHTML = "";
      code.appendChild(document.createTextNode(response.responseText));
    });
}

function contact_validate () {
  var valid = true;
  
  $w("email reason").each(
  function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

function specials_validate () {
  var valid = true;
  
  $w("first_name last_name email country provider").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

var where_validate = specials_validate;

function dictionary_validate () {
  var valid = true;
  
  $w("word definition").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  var value = "";
  $A(document.getElementsByName("word_type")).each(function ( e ) {
    if ( e.checked || e.selected ) {
      value = e.value;
      throw $break;
    }
  });
  
  if ( value == "" ) {
    valid = false;
    $("word_type_error").innerHTML = "required";
  } else {
    $("word_type_error").innerHTML = "";
  }
  
  return valid;
}

function text_me_validate () {
  var valid = true;
  
  $w("phone mobile_device").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

function gold_validate () {
  var valid = true;
  
  $w("phone first_name last_name email").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

function browser_notify_change ( select ) {
  var other = $('notify_browser_other');
  
  if ( select.value == "" ) {
    other.removeClassName("off");
    other.focus();
    other.select();
  } else {
    other.addClassName("off");
    other.value = "";
  }
}

function browser_notify_validate () {
  var valid = true;
  
  $A(["notify_email"]).each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  if ( $('notify_browser').value == ""
      && $('notify_browser_other').value == "" ) {
    valid = false;
    $("notify_browser_error").innerHTML = "required";
  } else {
    $("notify_browser_error").innerHTML = "";
  }
  
  return valid;
}

function device_notify_validate () {
  var valid = true;
  
  $A(["device"]).each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  if ( $('phone').value == "" && $('email').value == "" ) {
    alert("You must enter a mobile phone number or an email address.");
    return false;
  } else {
    return valid;
  }
}

function dash_register_validate () {
  var valid = true;
  
  $w("name email phone device_number").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

function press_validate () {
  var valid = true;
  
  $w("name email company").each(function ( id ) {
    if ( $(id).value == "" ) {
      valid = false;
      $(id + "_error").innerHTML = "required";
    } else {
      $(id + "_error").innerHTML = "";
    }
  });
  
  return valid;
}

function zhiing_icon_click ( a ) {
  window.open(a.href + "&url=" + escape(document.location.href),
      "zhiing_icon_window", "width=279,height=493")
    .focus();
  return false;
}