
$(document).ready(function() {

  $('.focus').focus();

  $('a[href=#goback]').click(function() { history.back(); });

  // validate searchbox
  $('.searchbox').submit(function() {
      if($('#searchbox', this).attr('defaultValue') == $('#searchbox', this).attr('value')) return false;

      return true;
  });
  $('.searchbox-404').submit(function() {
      if($('#searchbox-404', this).attr('defaultValue') == $('#searchbox-404', this).attr('value')) return false;

      return true;
  });

  // faq toggle answers
  $('dt').click(function() { $(this).next().toggleClass('active'); });

// voor top menu
  $('.menu li').hover(
    function() {
      var li = this;
      if ($(li).data('closing')) {
        window.clearTimeout($(li).data('closing-timeout'));
        $(li).removeData('closing');
        return;
      }
      $('> ul', this).show();
      $('> a', this).each(function() { 
        var a = this;
        $(li).addClass('clickable'); 
      });
    },
    function() {
      var li = this;
      $(li).data('closing', true);
      $(li).data('closing-timeout', window.setTimeout(function() {
        $('> ul', li).hide();
        $(li).removeData('closing');
      }, 10));
    }
  );

  // put 4 corner pictures on div.blokje

  $('.blokje').prepend('<img class="tl" src="/style/blokje-tl.png" alt=""/><img class="tr" src="/style/blokje-tr.png" alt=""/><img class="bl" src="/style/blokje-bl.png" alt=""/><img class="br" src="/style/blokje-br.png" alt=""/>');

  // put bottom 2 corner pictures on ul.logged_in

//  $('.blokje-login').prepend('<img class="bl" src="/style/blokje-bl.png" alt=""/><img class="br" src="/style/blokje-br.png" alt=""/>');
//  $('.logged_in').prepend('<img class="bl" src="/style/blokje-bl.png" alt=""/><img class="br" src="/style/blokje-br.png" alt=""/>');

  $('[@name=cities]').change(function() {
    var form = $(this).parents('form');
    $('#searchbox_zip', form).val('');
    $(form).submit();
  });

  $('.inputbox').addClass('default');

  // clear default value on focus
  $('.inputbox').focus(function() {
    $(this).removeClass('default');
    if(($(this).attr('id') == 'searchbox') && ($(this).val() != 'Zoek')) return true;
    if ( $(this).val() == this.defaultValue) $(this).val('');
  });

  // reset default value on focus lose
  $('.inputbox').blur(function() {
    if ( $(this).val() == "") {
      $(this).val(this.defaultValue);
      $(this).addClass('default');
    }
  });

  // check op aantal characters in shop search
  $('form.location').submit(
    function() {
      if($('select', this).val() == "") {
        if($('#searchbox_zip', this).val().length < 4) {
          alert('Voer a.u.b. alle vier de cijfers van de postcode in.');
          return false;
        }
        if(isNaN($('#searchbox_zip', this).val()) == true) {
          alert('Voer a.u.b. alle vier de cijfers van de postcode in.');
          return false;
        }
      }
      return true;
    }
  )

  // snel inloggen mouseover & submit 
  $('#login-form input[name=name]').focus(function() { this.select(); });
  $('#login-form input[name=password]').focus(function() { this.select(); });
  $('#login-form').each(function() {
    var self = this;
    $(self).hover(function() { $('form', self).addClass('active'); 
                               /*$('input[name=name]', self).focus();*/ }, 
                  function() { $('form', self).removeClass('active'); }
                  );
    //$('a[href$=#login-form]', self).click(function() { 
    //  $('form', self).submit(); 
    //  return false; 
    //});
    $('form', self).submit(function() {
      $('p.msg', self).html('<img src="/style/ajax-loader.gif"/>');
      var data = {'stentor_user-login': true, 
                  name: $('input[name=name]', self).val(), 
                  password: $('input[name=password]', self).val(),
                  current_url: $('input[name=current_url]', self).val(),
                  'stentor_user-login': $('input[name=stentor_user-login]', self).val()
                  };
      $.ajax({
        url: '/login',
        type: 'POST',
        data: data,
        dataType: "json",
        error: function() { $('p.msg', self).html(''); },
        success: function(data)
        {
          if (data && data['result']=='success') {
            window.location = data['redirect'];
          }
          if (data && data['result']=='failure') {
            $('p.msg', self).html(data['msg']);
          }
        }
      });
      return false; 
    });
  });

  // toggle favorite
  $('form.togglefavorite').each(function() {
    var self = this;
    var form = self;
    var submitsrc = $(form).children("input").attr("src");
    $(form).submit(function() { return false; });
    $('input', form).click(function() {
      var info = $('#info-basket-favorite');
//      $(form).children("input").after('<img src="/style/ajax-loader.gif"/>');
      $.ajax({
        url: $(form).attr('action'),
        type: 'POST',
        data: {'returntype':'json'},
        dataType: "json",
        error: function() { info.html('??'); },
        success: function(data)
        {
          if (data['result']==1) {
            info.html(data['itemcount']); 

            if(submitsrc.indexOf('delete')==-1) {
              submitsrc = submitsrc.replace("add","delete");
            }
            else {
              submitsrc = submitsrc.replace("delete","add");
            }

            $('img', form).remove();
            $(form).children("input").attr("src", submitsrc);
          }
          else {
            info.html('??'); 
          }
        }
      });
    });
  });

/* makes an href container clickable */
$('.clickable-container').click(function() {
    if(!$('a', this).attr('href')) {
        return;
    }
    if($('a', this).attr('target') == '_blank') {
        window.open($('a', this).attr('href'), "Pontmeyer");
    } else {
        window.location = $('a', this).attr('href');
    }
});

  // zoom image
  $('a.lightbox').lightBox({
    overlayBgColor: '#878889',
    overlayOpacity: 0.6,
    imageLoading: '/style/lightbox-ico-loading.gif',
    imageBlank: '/style/lightbox-blank.gif',
    imageBtnClose: '/style/lightbox-btn-close.gif',
    imageBtnPrev: '/style/lightbox-btn-prev.gif',
    imageBtnNext: '/style/lightbox-btn-next.gif'
  });

  $('#catalogger table:not(.nohover) tr').hover(function() {
      $('td', this).addClass('hover');
  },function() {
      $('td', this).removeClass('hover');
  });

});