/**
 * Javascript functions for cityfarming. Uses jquery.
 */

function redirect(url) {
  window.location = url;
}

function init_basket_handlers() {
  $('a.modify-basket-add').click(modify_basket_handler);
  $('a.modify-basket-minus.notLastBox').click(modify_basket_handler);
  $('a.modify-basket-minus.lastBox').click(function () {
    var url = this.href;
    if (confirm("Hinweis: Sie entfernen die letzte Box. Durch das Entfernen werden auch alle Extras entfernt. Möchten Sie fortfahren?")) {
      reload_basket(url);
    }
    return false;
  });

  $('a.add-extra').click(add_extra_handler);
}

function reload_basket(url, after_loading_fn) {
  $('#basket_container').load(url, {}, function() {
    init_basket_handlers();
    bindThickBoxEvents();
    if ($.isFunction(after_loading_fn)) {
        after_loading_fn();
    }
  });
}

function modify_basket_handler() {
  reload_basket(this.href, function() {});
  return false;
}

function add_extra_handler() {
  var button = this;
  $('#basket_container').load(this.href, {}, function() {
    init_basket_handlers();
    bindThickBoxEvents();
    $(button).html("Extra hinzugefügt");
    $(button).animate({delay: 1}, 1000, "linear", function() {
      $(this).html("In den Warenkorb");
    });
  });
  return false;
}

function removeThickBoxEvents() {
  $('.thickbox').each(function(i) {
    $(this).unbind('click');
  });
}

function bindThickBoxEvents() {
  removeThickBoxEvents();
  tb_init('a.thickbox, area.thickbox, input.thickbox');
}

function changeSelectedDate(date_param, id, date_text) {
  $('h3#date-formatted').html("Liefertag: " + date_text);
  $('input[name=d]').attr("value", date_param);
  // deselect old date
  $('td.selected').removeClass('selected');
  $('td.day#' + id).addClass('selected');
}

function initDeliveryForm() {
  $('td.day.selectable').hover(function() {
    $(this).addClass('active');
  }, function() {
    $(this).removeClass('active');
  });
}

function submitDateForm() {
 $('form#date-form').submit();
}

function init_checkout_form() {
  // initial display of payment and invoice address: hidden

  $('#invoice-address-differs').each(function() {
    $('div#bill_address').toggle(this.checked);
  });

  $('#debit').each(function() {
    $('div#lastschrift_fields').toggle(this.checked);
  });

  $('input#invoice-address-differs').click(function() {
    $('div#bill_address').toggle(this.checked);
  });

  $('input#by-invoice').click(function() {
    $('div#lastschrift_fields').hide();
  });

  $('input#debit').click(function() {
    $('div#lastschrift_fields').show();
  });
}

function init_background() {
    swfobject.switchOffAutoHideShow();
    var flashvars = {};
    var params = {wmode:'opaque'};
    swfobject.embedSWF('/pub/flash/background.swf', 'flashcontent', '100%', '100%', '9', 'expressInstall.swf', flashvars, params);
}

function init_handlers() {
    init_background();
    $('a.cancel-datepicker').click(function() {
        tb_remove();
    });

    init_basket_handlers();
    init_checkout_form();
}

$(document).ready(init_handlers);
