var NAV_KEYS = {'ecards'      : 'ec',
                'printables'  : 'cnp',
                'cnp'         : 'cnp',
                'downloads'   : 'dl',
                'invitations' : 'inv'}

function navtype_toString(navtype_object){
  var navtype_string = '';
  for (var value in navtype_object) {
      navtype_string += ('"' + value + '"' +  ':' + '"' + navtype_object[value] + '"' + ',');
  }
  dojo.debug('navtype_toString - navtype_string ' + navtype_string);
  return '{' + navtype_string.slice(0,-1) + '}'
}

function setNavTypeCookie(key, type){
  dojo.debug('*********  BEGIN  NAVTYPE ***************');
  dojo.debug('setNavTypeCookie');
  // set the navtype cookie logic goes here
  navtype_key = 'navtype';

  // get navtype from magic cookie
  var navtype_cookie = MagicCookie.getCookieValue(navtype_key);
  dojo.debug('MagicCookie: ' + navtype_cookie);

  var ary_navtype = new Object();
  // Create a navtype object (associative array)
  if (navtype_cookie){
     dojo.debug('Cookie exists: not null');
     //var ary_navtype = eval(navtype_cookie);
     eval("var ary_navtype =" +  navtype_cookie + ';');
     dojo.debug(ary_navtype);
  }
  else {
     dojo.debug('Cookie does not exist: null');
  }

  // Assign navtype
  ary_navtype[key] = type;

  // Serialize the object for storage in the MagicCookie
  var navtype_serialized = navtype_toString(ary_navtype);
  dojo.debug('navtype_serialized: ' + navtype_serialized);

  // Write the session cookie
  MagicCookie.setCookieValue(navtype_key, navtype_serialized);

  //dojo.debug(key + ':' + type);
  dojo.debug('*********  END NAVTYPE ***************');
}

function getKey(){
  // Get the key for the navtype to be storedretreived based on the path.
  // - default navkey is ecards
  // - This function should be made more robust
  var path = window.location.pathname.split('/')[1];
  return NAV_KEYS[path];
}

function getNavTypeFromCookie(){
  // @return string for navtype for current usage
  dojo.debug('*********  BEGIN  NAVTYPE ***************');
  dojo.debug('getNavTypeCookie');
  navtype_key = 'navtype';
  var navtype_cookie = MagicCookie.getCookieValue(navtype_key);

  // if navtype cookie exists, return the navtype for this "Tab" (ecards, downloads, etc)
  // - if there is not a navtype cookie, return the default navtype of other
  dojo.debug('navtype_cookie ' + navtype_cookie);
  if (navtype_cookie){
     try {
        dojo.debug('Cookie exists: not null');
        eval("var ary_navtype =" +  navtype_cookie + ';');
        dojo.debug(ary_navtype);
        dojo.debug(ary_navtype[getKey()].toLowerCase());
        return ary_navtype[getKey()].toLowerCase();
     }
     catch (err){
        return 'other'
     }
  }
  else{
     dojo.debug('Cookie does not exist: null');
     return 'other';
  }
}

function removeNavTypeFromCookie(self){
   //  If cookie exists, remove navtype(ec,dl,etc) from cookie value, write the new cookie value
   navtype_key = 'navtype';
   var navtype_cookie = MagicCookie.getCookieValue(navtype_key);
   dojo.debug('removeNavTypeFromCookie');
   dojo.debug(navtype_cookie);

   // if navtype cookie exists,
   //  - attempt to delete the navtype for this "Tab" (ecards, downloads, etc)
   // - set the cookie
   // if there is not a navtype cookie, do nothing
   if (navtype_cookie){
      eval("var ary_navtype =" +  navtype_cookie + ';');
      key = getKey();
      dojo.debug('key '+ key);
      if (eval('delete ary_navtype.' + key + ';')){
         // Serialize the object for storage in the MagicCookie
         var navtype_serialized = navtype_toString(ary_navtype);
         dojo.debug('navtype_serialized: ' + navtype_serialized);

         // Write the session cookie
         MagicCookie.setCookieValue(navtype_key, navtype_serialized);
      }
      dojo.debug(ary_navtype);
   }
}
