var whereYouveBeen = function(){
  
  var hstry            = {};
  hstry.rootClassName  = SiteVars.modules.whereYouveBeen.rootClassName;
  hstry.siteHistory    = new Array();
  hstry.headingTxt     = (SiteVars.modules.whereYouveBeen.headingTxt!='') ? SiteVars.modules.whereYouveBeen.headingTxt : 'Recently Viewed:' ;
  hstry.homepageTxt    = SiteVars.modules.whereYouveBeen.homepageTxt;
  hstry.landingpageTxt = SiteVars.modules.whereYouveBeen.landingpageTxt;
  hstry.isHomepage     = SiteVars.modules.whereYouveBeen.isHomepage;
  hstry.isLandingpage  = SiteVars.modules.whereYouveBeen.isLandingpage;
  hstry.skip           = SiteVars.modules.whereYouveBeen.skip;
  hstry.thisTitle      = (SiteVars.modules.whereYouveBeen.pageTitle != '') ? SiteVars.modules.whereYouveBeen.pageTitle : escape(document.title);
  
  var thisEntry = '{"title":"' + escape(hstry.thisTitle) +'","url":"' + document.URL +'"}';
  var objEntry = false;
  try {
    objEntry = modules.sanitizeResponse(thisEntry, {});
  }
  catch(e){}
  hstry.currentLink = objEntry;
  
  
  hstry.get = function(){
    hstry.strHistory = Cookie.get('siteHistory');
    if(hstry.strHistory){
      hstry.siteHistory = hstry.strHistory.split('|');
    }
  }
  
  hstry.set = function(){
    
    /* page title are now set up across the board - no need for seperate behavior based on location or template */
    /* if(hstry.isHomepage == 'true'){
      hstry.thisTitle = hstry.homepageTxt;
    }
    else if(hstry.isLandingpage == 'true'){
      hstry.thisTitle = SiteVars.islandName + ' ' +hstry.landingpageTxt;
    }
    else{
      hstry.thisTitle = (SiteVars.modules.whereYouveBeen.pageTitle != '') ? SiteVars.modules.whereYouveBeen.pageTitle : escape(document.title); //(document.title != '') ? escape(document.title) : SiteVars.modules.whereYouveBeen.pageTitle ;
    } */
    
    if(hstry.siteHistory[hstry.siteHistory.length-1] == thisEntry){ // likely that the refresh url button was hit
      hstry.siteHistory.pop();
    }
    if(hstry.siteHistory.length == 5){
      hstry.siteHistory.shift();
    }
    
    var newHistory = ''
    $A(hstry.siteHistory).each(function(link){
        newHistory += link + '|'
    });
    newHistory += (objEntry) ? thisEntry : '';
    Cookie.set('siteHistory', newHistory, 0, '/'); //, location.hostname
    
    if($$('.'+hstry.rootClassName)[0]) {
      hstry.load();
    }
    
  }
  
  hstry.load = function(){
    hstry.loadFlag = true;
    if($$('.'+hstry.rootClassName)[0]) {
      hstry.elem = $$('.'+hstry.rootClassName)[0];
      Element.insert(hstry.elem, '<h3>' + hstry.headingTxt + '</h3>');
      $A(hstry.siteHistory).each(function(link){
          var objLink = modules.sanitizeResponse(link, {})
          
          if(objLink.title == '' ){
            objLink.title = 'Title not set properly';
          }
          
          var newLink = '<a href="' + objLink.url + '" >' + unescape(objLink.title) + '</a>';
          Element.insert(hstry.elem, newLink);
      });
      
      var finalLink = '<span class="last">' + unescape(hstry.currentLink.title) + '</span>';
      Element.insert(hstry.elem, finalLink);
      
    }
  }
  
  
  if('loadFlag' in hstry){
  }
  else if(hstry.skip == true){
  }
  else if(objEntry){
    hstry.get();
    hstry.set();
  }
}

var backToPrevious = function(){
  if($$('.backtoprevious')[0]){
    
    var prevBtn = $$('.backtoprevious')[0];
    
    Event.observe(prevBtn, 'click', function(e){
        //console.dir(history);
        history.back();
        Event.stop(e);
    });
    
  }
  
}

modules.loadActions.prototype.whereYouveBeen = function(){
  if($$('.backtoprevious')[0]){
    var bButton = new backToPrevious();
  }
  var hist = new whereYouveBeen();
}

