var printCart = {
  update: function(){
    new Ajax.Updater('savings_cart', '/cart/show.js', { method: 'get', parameters: { cart: cart_state.toJSON()}});
  },

  saveVoucher: function(vid, bigfeature_elt){
    cart_state.set(vid, 1);
    if (bigfeature_elt){
      voucher_set_saved(bigfeature_elt, true, false);
    }else{
      printCart.applyState();
    }
    printCart.update();
  },

  removeVoucher: function(vid, bigfeature_elt){
    cart_state.unset(vid);
    if (bigfeature_elt != undefined){
      voucher_set_saved(bigfeature_elt, false, false);
    }else{
      printCart.applyState();
    }
    printCart.update();
  },

  applyState: function(){
    $$('.voucher').each( function(v) {
      vid = parseInt(v.getAttribute('title'));
      if (cart_state.get(vid)){
        // set this voucher to saved
        voucher_set_saved(v, true, true);
      }else{
        // set this voucher to unsaved
        voucher_set_saved(v, false, true);
      }
    });
  }
};

function voucher_set_saved(vid_elt, state, silent){
  if (state == true){
    vid_elt.removeClassName('unsaved');
    vid_elt.addClassName('saved');
    // online vouchers dont have vtools_delete or save
    if (vid_elt.down('.vtools_delete') != undefined){
      vid_elt.down('.vtools_delete').show();
      vid_elt.down('.vtools_save').hide();
    }
    if((savedhelp = vid_elt.down('.vsaved_help')) && (silent == false)) {
      savedhelp.show();
      setTimeout(savedhelp.hide.bind(savedhelp), 4000);
    }
  }else{
    vid_elt.removeClassName('saved');
    vid_elt.addClassName('unsaved');
    // online vouchers dont have vtools_delete or save
    if (vid_elt.down('.vtools_delete') != undefined){
      vid_elt.down('.vtools_delete').hide();
      vid_elt.down('.vtools_save').show();
    }
  }
}

var timeouts_h = new Hash(); // an array indexing timeouts by the vid

function voucherEventHandler(e){
//   alert(e.type);
  voucher_elt = e.element();
  // get the voucher id
  if (voucher_elt.hasClassName('voucher') || (voucher_elt = voucher_elt.up('.voucher'))) {
    vid = parseInt(voucher_elt.getAttribute('title'));

    // find out what happened
    elt = e.element();
    switch(e.type){
      case 'click':
        if (elt.hasClassName('vtools_save')){
          printCart.saveVoucher(vid, voucher_elt);
          e.stop();
        }else if (elt.hasClassName('vtools_delete')){
          printCart.removeVoucher(vid, voucher_elt);
          e.stop();
        }
        break;
      case 'mouseover':
        t = timeouts_h.get(vid);
        if (clearTimeout(t)){ timeouts_h.unset(vid);}
        voucher_elt.addClassName('hovered');
        voucher_elt.down('.vtools').visualEffect('Appear',{duration:0.25});
        break;
      case 'mouseout':
        if (voucher_elt.hasClassName('hovered')){
          // set a timer to fade this vtools out after a waiting period
          timeouts_h.set(vid, setTimeout('fadeout('+vid+')', 400));
        }
        break;
    }
  }else{
    return false;
  }
}

function fadeout(vid){
  // if this function is called.. then the timeout was not cleared.. and its safe to unhover the voucher
  // locate the voucher.....
  bf_a = $$('.bigfeatures');
  for(i = 0; i < bf_a.length; i++){
    // see if this bigfeatures element has the property title with the vid we're looking for
    if (parseInt(bf_a[i].getAttribute('title')) == vid){
      bf_a[i].removeClassName('hovered');
      vtools = bf_a[i].down('.vtools');
      vtools.visualEffect('Fade',{duration:0.25});
      timeouts_h.unset(vid);
    }
  }
}

function zoom_save(vid){
  printCart.saveVoucher(vid, $('zoomview'));
  zoomhelpbubble.showHelp('zoomhelpbubble_saved');
}

function zoom_unsave(vid){
  printCart.removeVoucher(vid, $('zoomview'));
}

function switch_tree(showdiv) {
  trees = ['rp_refine_reg', 'rp_refine_cat'];
  trees.each(function(e){$(e).hide()});
  $(showdiv).show();
}

function bookmarksite(title, url){
  if (document.all)
    window.external.AddFavorite(url, title);
  else if (window.sidebar)
    window.sidebar.addPanel(title, url, "")
}

function reset_search(){
  if ($('h1_searchfield').value == '')
    $('h1_searchfield').value = 'Search';
}

function clear_search(){
  if ($('h1_searchfield').value == 'Search')
    $('h1_searchfield').value = '';
}

// wait till the dom is loaded then start some stuff
//Event.observe(window, 'load', function() {
document.observe('dom:loaded', function() {
  if ($('voucher_content')) {
    $('voucher_content').observe('click', voucherEventHandler);
    $('voucher_content').observe('mouseout', voucherEventHandler);
    $('voucher_content').observe('mouseover', voucherEventHandler);
  }
  $('h1_searchfield').observe('blur', reset_search);
  $('h1_searchfield').observe('focus', clear_search);
  // set state? i think thats broken now
  printCart.applyState();
  
});