//******************************************************************************
// PHMS (Pardee Homes Map Search)                                   LJG Partners
// -------------------------------------------------------------------- 6/2/2009
// Provides the functions necessary to serve region-based content in the map
// adspace and communicate between the flash pieces.
//******************************************************************************

// Object containing the two flash pieces.
var phms_swfs = {
  map: null,
  ad:  null
};

// Acceptable values for inter-application communication.
var phms_values = {
  california: {
    sd: "san_diego",
    oc: "orange_county",
    la: "la_ventura",
    ie: "inland_empire"
  },
  nevada: {
    lv: "las_vegas",
    cs: "coyote_springs"
  }
};

var current_region = false;
var phms_live_chat_code = "ca-nv";

//******************************************************************************
// phms_setSwf
// Used to identify the id/name of the map/ad swfs and set the keys within
// the phms_swfs{} object.
//******************************************************************************
function phms_setSwf(key, val) { 
  if (typeof(phms_swfs[key]) != "undefined") { 
    if (phms_swfs[key] == null) { 
      phms_swfs[key] = val; 
    }
  }
  if (current_region) { phms_changeLocation(current_region); }
} // end phms_setSwf

//******************************************************************************
// phms_init
// Converts the stored key values in phms_swfs{} to usable document objects.
//******************************************************************************
function phms_init(region) {
  if (typeof region != "undefined") { current_region = region; }
  var is_ms = (navigator.appName.indexOf("Microsoft") != -1);
  for (var i in phms_swfs) {
    if (phms_swfs[i] == null) { continue; }
    if (is_ms) { phms_swfs[i] = window[phms_swfs[i]];   }
    else       { phms_swfs[i] = document[phms_swfs[i]]; }
  }
  if (typeof region != "undefined") {
    phms_changeLocation(region);
  }
} // end phms_init

//******************************************************************************
// phms_changeLocation
// Primary mechanism for communication between flash applications and the
// Quick Search drop-down menu. Converts the passed string value into a region
// information object, then informs all applications of the change.
//******************************************************************************
function phms_changeLocation(str) {
  current_region = str;
  var info = phms_valueConversion(str);

  var out = "phmsAPI received " + str + ". Converted to:\n" +
            "  State:     " + info.state + "\n" +
            "  Region:    " + info.region + "\n" +
            "  Code:      " + info.code + "\n" +
            "  Match_Key: " + info.match_key;
  //alert(out);

  // If we matched based on "code", then the changeLocation
  // directive came from the search map itself.
  if (info.match_key == "code") {

    // Update the drop-down menu.
    phms_updateDropDown(info.region);
  }

  // If we matched based on "region" or "state", then the 
  // directive was sent by the drop-down box.
  else {

    // Update the search map. It requires the short version
    // to match it's internal data structures.
    if (phms_swfs.map != null) {
      phms_swfs.map.phms_changeLocation(info.code);
    }
  }

  // Update the ad space
  if (phms_swfs.ad != null) { if (typeof(phms_swfs.ad.phms_changeLocation) != "undefined") {
    var pass_var = (info.region != "default") ? info.region : info.state;
    phms_swfs.ad.phms_changeLocation(String(pass_var));
  }}

  // Update the live chat button
  try {
  if (info.code != phms_live_chat_code) {
    var receiver = document.getElementById('phms_live_chat');
    var hidden   = document.getElementById('phms_hidden_div');
    var newbtn   = document.getElementById('phms_live_chat_region_' + info.code);
    var current  = document.getElementById('phms_live_chat_region_' + phms_live_chat_code);

    if (current) { current.parentNode.removeChild(current); hidden.appendChild(current); }
    newbtn.parentNode.removeChild(newbtn);
    receiver.appendChild(newbtn);
    phms_live_chat_code = info.code;
  }
  } catch(e) {}

} // end phms_changeLocation

//******************************************************************************
// phms_valueConversion
// Searches the phms_values{} object for the correct information object based
// on the passed key. The key can be the "code", state, or region. All of these
// values are sent back in an object, along with a string version of the key
// which was used to make the match. If no values are matched, the default
// information object is returned.
//******************************************************************************
function phms_valueConversion(val_code) {
  var match_key   = "code";
  var match_state = "default";
  var match_found = false;

  // Make sure case sensitivity doesn't screw us up.
  val_code = val_code.toLowerCase();

  for (var state in phms_values) {

    // If the value code is a state selected by the drop-down,
    // send the default information but flag it as a state
    // so that the map is updated accordingly. If the val_code
    // is equal to "", then the Location option was selected.
    if (val_code == state || val_code == "") { 
      if (val_code != "") { match_state = state; }
      match_key = "state"; break; 
    }

    // The value code is not the state, so check the
    // regions and codes.
    for (var code in phms_values[state]) {
      var region = phms_values[state][code];
      var conditions = {
        code:   (val_code == code),
        region: (val_code.substring(0, region.length) == region)
      }

      for (var key in conditions) {
        if (conditions[key]) {
          match_key   = key;
          match_found = true;
          break;
        }
      }

      // If the region/code matched, return the match.
      if (match_found) {
        return { state: state, region: phms_values[state][code], code: code, match_key: match_key };
      }
    }
  }

  // Return the default. This info can include the state, if there 
  // was a state match from the location drop down box.
  return { state: match_state, region: "default", code: "ca-nv", match_key: match_key };

} // end phms_valueConversion


function phms_updateDropDown(region_code) {

  // Correct the "default" and "las_vegas" region codes.
  switch (region_code) {
    case "las_vegas": region_code = "las_vegas_northern_valley"; break;
    case "default":   region_code = ""; break;
  }

  // Update the select box value.
  document.getElementById("city_region").value = region_code;

  // This line of code references the "global.js" function.
  // It should be replaced when the framework is updated.
  getPrices(false);
}
