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 != "stage.redlobster.com" && location.host != "demo.redlobster.com"
    && location.host != "www.redlobster.ca" && location.host != "stage.redlobster.ca" && location.host != "demo.redlobster.ca"){
  root_path = "//www.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 = "none";
  // Override acct number if site is staging, demo or beta (even if it is at rl domain)
  if (location.host.indexOf("stage") != -1 || location.host.indexOf("demo") != -1 || location.host.indexOf("beta") != -1){
    // Omniture Account Number (test)
	if (location.host.indexOf("redlobster.ca") != -1)
	{
		acct = "drredlobstercanadadev";
	}
	else
	{
		acct = "drredlobsterdev";	
	}
  }  
  else if (location.host.indexOf("redlobster.com") != -1)
  {
	acct = "drredlobster";	
  }
  else if (location.host.indexOf("redlobster.ca") != -1)
  {
	acct = "drredlobstercanada";
  }
  
  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('webfodder') != -1 || location.host.indexOf('65.121.22.155') != -1){
    cat = "Marketplace";
  }
  else if (location.host.indexOf('locator_VE') != -1 || location.host == 'locator.redlobster.com' || location.host == 'locator.redlobster.ca' || location.host.indexOf('where2getit') != -1){
    cat = "Locator";
  }
  else if (location.host == 'menus.redlobster.com'){
    cat = "Menus";
  }

  else if (location.host.indexOf("bluehornet.com") != -1 || location.host == 'freshcatch.redlobster.com' || location.host.indexOf('email.redlobster.com')){
    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";
    content_categories['blog'] = "Chefs Blog";

    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 || js_pathparts[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.pathname.indexOf("/position/") == 0){
    virtual_path = '/employment/management/';
  }
  
  if ((location.host.indexOf("bluehornet.com") > -1 || location.host == "freshcatch.redlobster.com") && location.pathname.indexOf("invite") == -1){
    virtual_path = '/club/';
    if (location.pathname.indexOf("survey") > -1){
      virtual_path = '/club/join/';
    }
    if (location.pathname.indexOf("change") > -1){
      virtual_path = '/club/member_options/update_profile/';
    }
    if (location.pathname.indexOf("clupdate") > -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('giftcertificatecompany') > -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.pathname.indexOf("LiveLobster") > -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];
    pathpart = pathpart.replace("_cn","");
    // 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);
}

function loadRecipe(id){
  loadPopup("/kitchen/recipes/recipe.asp?recipe="+id,'recipePopup', 800, 600, 0, 1, 0, 1);
}

// 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


// Begin basic functions for reading/writing cookies

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(cookiename) {
   var cookiestring=""+document.cookie;
   var index1=cookiestring.indexOf(cookiename);
   if (index1==-1 || cookiename=="") return ""; 
   var index2=cookiestring.indexOf(';',index1);
   if (index2==-1) index2=cookiestring.length;
   var cookie_value = unescape(cookiestring.substring(index1+cookiename.length+1,index2));
   cookie_value = cookie_value.replace(/\+/g, " ")
   return cookie_value
}

// End basic functions for reading/writing cookies


// Begin Third Party Locator Functions

function LocatorCheckCookie(){
   var RestaurantNum = getCookie("RestaurantNum");
   if (RestaurantNum == null || RestaurantNum == ""){
      // Make "My Red Lobster" layer invisible
      document.getElementById("locator_1").style.display='none';
   }
}

function LocatorAddress(){
   var str='';
   str+='      <div style="text-align: center; font-size: 10px; line-height: 12px;">\n';
   str+='      <b>' + getCookie("Address1")+ '</b><br />\n';
   str+='      ' + getCookie("City") + ', ' + getCookie("StateCode") + '&nbsp;' + getCookie("ZipCode") + '<br />\n';
   str+='      <b>Phone:  ' + getCookie("PhoneNum") + '</b><br />\n';
   str+='      Fax:  ' + getCookie("FaxNum") + '\n';
   str+='      </div>\n';
   str+='      <div style="margin: 7px 0px 0px 0px; text-align: center; font-size: 10px; line-height: 12px;">\n';
   str+='      Sun - Thurs: ' + getCookie("STOpen") + ' - ' + getCookie("STClose") + '<br />';
   str+='      Fri - Sat: ' + getCookie("FSOpen") + ' - ' + getCookie("FSClose");
   str+='      </div>\n';
   str+='      <div style="padding-top: 8px;"><a href="http://www.redlobster.com/locater_VE/search.asp?W2GI_QFIELD=BusinessID&W2GI_QVALUE=' + getCookie("RestaurantNum") + '&domain=' + document.domain + '&client=redlobster2&template=moreinfo"><img src="//www.redlobster.com/images/locator/directions_btn.gif" alt="Map and Directions" width="197" height="22" border="0" /></a></div>\n';
   document.write(str);
}

function LocatorMyRL(){
   var str='';
   str+='<b>';
   str+='<span style="font-size: 9px; color: #00244d;">My Red Lobster: ';
   str+='&#160;<a style="font-size: 9px;color: #3776b5; text-decoration: none;"  href="javascript:void(0);" onclick="showLayer(\'locator_2\');hideLayer(\'locator_1\');">' + getCookie("City") + '</a>'
   str+='</b>';
   str+='</span>';
   document.write(str);
}

// End Third Party Locator Functions
