var js_server_path = location.pathname.replace('.asp','');
var js_pathparts = js_server_path.split("/");

var root_path = '';
if (location.host != "rl-us-beta-realbranding.info"
    && location.host != "www.redlobster.com" && location.host != "www.redlobster.com" && location.host != "demo.redlobster.com"
    && location.host != "www.redlobster.ca" && location.host != "www.redlobster.ca" && location.host != "demo.redlobster.ca"){
  root_path = "//demo.redlobster.com";
}

if (location.search == '?force_root_path') root_path = "//"+location.host;
if (root_path != '' && location.search == '?check_root_path') alert(root_path);



// BEGIN OMNITURE FUNCTIONS

function get_omni_account_number(){
  var acct = "";
  if (location.host.indexOf("redlobster.com") != -1){
    acct = "drredlobsterdev";
  }
  else if (location.host.indexOf("redlobster.ca") != -1){
    acct = "drredlobstercanadadev";
  }
  // Override acct number if site is staging, demo or beta (even if it is at rl domain)
  if (acct == "" || location.host.indexOf("stage") != -1 || location.host.indexOf("demo") != -1 || location.host.indexOf("beta") != -1){
    // Omniture Account Number (test)
    acct = "drredlobsterdev";
  }
  if (location.search == '?check_omni_acct') alert(acct);
  return acct;
}

function get_omni_content_cat(){
  var cat = "";
  // The info below needs to be updated per Red Lobster
  if (location.host.indexOf('gift.redlobster') > -1 || location.host.indexOf('store.redlobster') > -1 || location.host.indexOf('giftcardcompany') > -1 || location.host.indexOf('webfodder') > -1 || location.host.indexOf('65.121.22.155') > -1){
    cat = "Marketplace";
  }
  else if (location.host == 'locator.redlboster.com' || location.host == 'locator.redlobster.ca' || location.host.indexOf('where2getit') > -1){
    cat = "Locator";
  }
  else if (location.host.indexOf("bluehornet.com") > 0 || location.host.indexOf("overboard") > 0){
    if (location.href.indexOf('invite') != -1){
      cat = "Invite Your Friends";
    }
    else{
      cat = "Club";
    }
  }
  else{
    var content_categories = new Object;
    content_categories['menus'] = "Our Menus";
    content_categories['passion'] = "Passion For The Sea";
    content_categories['kitchen'] = "Chefs Kitchen";
    content_categories['health'] = "Seafood and Health";
    content_categories['marketplace'] = "Marketplace";
    content_categories['club'] = "Club";
    content_categories['press'] = "Press Room";
    content_categories['employment'] = "Employment";
    content_categories['contact_us'] = "Contact Us";
    content_categories['sitemap'] = "Site Map";
    content_categories['search'] = "Search";
    content_categories['invite'] = "Invite Your Friends";
    content_categories['locator'] = "Locator";

    var top_level_directory;
    top_level_directory = js_pathparts[1];
    if (content_categories[top_level_directory]){
      cat = content_categories[top_level_directory];
    }
    else if (js_pathparts[1].indexOf('Default') != -1 || js_pathparts[1].indexOf('default') != -1){
      cat = "Home Page";
    }
    else if (js_pathparts[2] == ""){
      cat = "Root";
    }
    else{
      cat = "Other";
    }
  }
  if (location.search == '?check_omni_cat') alert(cat);
  return cat;
}

// END OMNITURE FUNCTIONS



// BEGIN BREAK OUT OF FRAMES

function bustFrames(){
  if (window != top){
    top.location.href=location.href
  }
}

// END BREAK OUT OF FRAMES



// BEGIN FIELD-CLEARING FUNCTION

function clearField(field){
  if (field.value == field.defaultValue) {
    field.value = "";
  }
}

// END FIELD-CLEARING FUNCTION



// BEGIN DHTML FUNCTIONS

// initRollovers - Find all images that have the class "roll", and set events for each (avoids needing to add event handling to image tags)
// - loops through all image tags on the page, and skips any that have no 'roll' class

function initRollovers(){
  // Set rollstate variable (this is typically either 'on' or 'over')
  var roll_state = "on";
  // Loop through all im if they contain the class roll
  var imgs=document.getElementsByTagName('img');
  for(i=0;i<imgs.length;i++){
    img_obj = imgs[i];
    if(/roll/.test(img_obj.className)){

      // add the function roll to the parent Element (i.e a href) of the image
      img_obj.parentNode.onmouseover=function(){rollOver(this, 'on');};
      img_obj.parentNode.onmouseout=function(){rollOver(this, '');};
      img_obj.parentNode.onfocus=function(){rollOver(this, 'on');};
      img_obj.parentNode.onblur=function(){rollOver(this, '');};
      // Preload images (on and over variations)
      img_width = img_obj.width;
      img_height = img_obj.height;
      img_name = img_obj.id;
      img_ext = img_obj.src.substring(img_obj.src.lastIndexOf('.'), img_obj.src.length);

      // Preload 'over' images (if applicable)
      //img_name_over = img_name.replace("_button", "_over");
      //eval (img_name_over + " = new Image(" + img_width + "," + img_height + ")" );
      //eval (img_name_over + ".src = \"" + img_obj.src.replace(img_ext, "_over" + img_ext) + "\"");

      // Preload 'on' images (if applicable)
      img_name_on = img_name.replace("_button", "_on");
      eval (img_name_on + " = new Image(" + img_width + "," + img_height + ")" );
      eval (img_name_on + ".src = \"" + root_path + img_obj.src.replace(img_ext, "_on" + img_ext) + "\"");
    }
  }
}

// initSmartNav - Highlights all navigation for current page (at each level)
// - loops through each part of path (parts = directory or filename)

function initSmartNav(){
  pathparts = js_pathparts;
  // Specify filename suffix for highlighted nav
  var hl_suffix = "_on";
  // Set up customizations
  var virtual_path = '';
  if (location.pathname.indexOf("/blog/") == 0){
    virtual_path = '/kitchen/chef_blog/';
  }
  if ((location.host.indexOf("bluehornet.com") > -1 || location.host.indexOf("overboard") > -1) && location.pathname.indexOf("invite") == -1){
    virtual_path = '/club/';
    if (location.pathname.indexOf("survey.htm") > -1){
      virtual_path = '/club/join/';
    }
    if (location.pathname.indexOf("change.htm") > -1){
      virtual_path = '/club/member_options/';
    }
    if (location.pathname.indexOf("clupdate.htm") > -1){
      virtual_path = '/club/member_options/update_profile/';
    }
    if (location.pathname.indexOf("forgot_pass") > -1){
      virtual_path = '/club/member_options/forgot_password/';
    }
    if (location.pathname.indexOf("unsubscribe") > -1){
      virtual_path = '/club/member_options/unsubscribe/';
    }
    if (location.pathname.indexOf("raf") > -1){
      virtual_path = '/club/member_options/refer_a_friend/';
    }
  }
  if (location.host.indexOf('gift.redlobster') > -1 || location.host.indexOf('store.redlobster') > -1 || location.host.indexOf('giftcardcompany') > -1 || location.host.indexOf('webfodder') > -1 || location.host.indexOf('65.121.22.155') > -1){
    virtual_path = '/marketplace/';
    if (location.pathname.indexOf("gift_card") > -1){
      virtual_path = '/marketplace/';
    }
    if (location.host.indexOf("webfodder") > -1){
      virtual_path = '/marketplace/live_lobster/';
    }
    if (location.pathname.indexOf("freshfish") > -1){
      virtual_path = '/marketplace/fresh_seafood/';
    }
    if (location.pathname.indexOf("SeafoodGifts") > -1){
      virtual_path = '/marketplace/seafood_gift/';
    }
  }
  if (virtual_path != ''){
    // override path on which highlighting is based.
    pathparts = virtual_path.split("/");
  }
  
  // Loop through path parts to find navigation matches
  for(i=0;i<pathparts.length;i++){
    pathpart = pathparts[i];
    // If pathpart is not empty and if element exists with pathpart and '_button' as it's ID
    if (pathpart.length > 0 && document.getElementById(pathpart+'_button')){
      navbutton = document.getElementById(pathpart+'_button');
      // If navbutton doesn't already include hl_suffix in the filename add it (new src = everything before the period, then hl_suffix, then everything after)
      if (navbutton.src.indexOf(hl_suffix) == -1){
        img_ext = navbutton.src.substring(navbutton.src.lastIndexOf('.'), navbutton.src.length);
        navbutton.src= navbutton.src.replace(img_ext, hl_suffix+img_ext);
      }
      disable_rollover_effects(navbutton);
    }
  }
}

function disable_rollover_effects(butt){
  // Disable rollover effects for highlighted nav
  butt.parentNode.onmouseover=null;
  butt.parentNode.onmouseout=null;
  butt.parentNode.onfocus=null;
  butt.parentNode.onblur=null;
}

function rollOver(obj, rollstate){
  var i,isnode,src,ftype,newsrc,nownode;
  // loop through all childNodes (i.e. img within a)
  for (i=0;i<obj.childNodes.length;i++){
    nownode=obj.childNodes[i];
    // if the node is an element and an IMG set the variable and exit the loop
    if(nownode.nodeType==1 && /img/i.test(nownode.nodeName)){
      isnode=i;
      break;
    }
  }
  // check src and do the rollover
  img_src = obj.childNodes[isnode].src;
  ftype = img_src.substring(img_src.lastIndexOf('.'), img_src.length);
  if(/_on/.test(img_src)){
    currstate = 'on';
  }
  else if(/_over/.test(img_src)){
    currstate = 'over';
  }
  else{
    currstate = '';
  }
  newsrc = '';
  if (currstate != rollstate){
    newsrc = img_src.replace(ftype, '_'+rollstate+ftype);
  }
  else{
    return;
  }
  if (rollstate.length == 0){
    newsrc = img_src.replace('_'+currstate, '');
  }
  if (newsrc.length != 0){
    obj.childNodes[isnode].src=newsrc;
  }
}

// END DHTML FUNCTIONS



// WINDOW FUNCTIONS
function loadPopup(url, name, width, height, chrome, scrollbars, resizable, centered) {
  // For chrome, scrollbars, resizable, centered use 1 or 0
  if (centered == 1){
    var winleft = Math.round((screen.width-width)/2);
    var wintop = Math.round((screen.height-height)/2);
    if (chrome == 1){
      wintop = wintop - 80;
    }
    else{
      wintop = wintop - 30;
    }
    if (wintop < 0) wintop = 0;
  }
  var features = '';
  features = features + 'width=' + width;
  features = features + ',height=' + height
  if (chrome == 1){
    features = features + ',directories=1,location=1,menubar=1,status=1,toolbar=1';
  }
  else{
    features = features + ',directories=0,location=0,menubar=0,status=0,toolbar=0';
  }
  features = features + ',resizable=' + (resizable - 0);
  features = features + ',scrollbars=' + (scrollbars - 0);
  if (winleft > 0 && wintop > 0){
    features = features + ',left=' + (winleft - 0);
    features = features + ',top=' + (wintop - 0);
  }
  //alert("Features: " + screen.width + "/" + screen.height + " -- " +features);
  popup = window.open (url, name, features);
  popup.focus();
  return void(0);
}

// To close a window use... window.close();
// To print use... window.print();
// To redirect use... window.location = "http://www.google.com/";


// Begin Show and Hide Layers

function showLayer (layer_id) {
  document.getElementById(layer_id).style.display='block';
}
function hideLayer (layer_id) {
  document.getElementById(layer_id).style.display='none';
}

// End Show and Hide layers