// Tworzenie obiektu adtaily, żeby wszystkie nasze funkcje do niego przypisywać. Taki namespace dla ubogich.
if(typeof adtaily == "undefined"){
	adtaily = new Object();
};

adtaily.set_price_settings_form_state = function() {
  var fields = "#ui-spinner-widget_daily_price, #widget_currency, #widget_promotion_length_in_days, #widget_promotion_price, #widget_promotion_submit";
  if ($("#widget_optimize_price_automatically").is(":checked")) {
    $(fields).addClass('disabled').attr('readonly', 'readonly');
    $(".delete_widget_promotion").each(function(i,e){
       $(this).fadeOut(400, function() {});
    });
  } else {
    $(fields).removeClass('disabled').attr('readonly', '');
    $(".delete_widget_promotion").each(function(i,e){
      $(this).fadeIn(400, function() {});
    });
  }
}

adtaily.add_price_slider = function(){
  $("#slider").slider({
    value: $("#widget_daily_price").val(),
    min: 0.07,
    max: 700,
    step: 0.07,
    animate: true,
    slide: function(event, ui) {
      $("#widget_daily_price").val(ui.value);
      $("#show_price em").html(ui.value);
    }
  });
  $("#widget_daily_price").val($("#slider").slider("value"));
  $("#show_price em").html($("#slider").slider("value"));
};

adtaily.price_promotions_magic = function(){
  $(".delete.delete_widget_promotion").live('click',function(e) {   
    parent_id = $(this).parents("tr").attr("id");
    if(parent_id != "unsaved"){
      $('#widget_promotions_fields').append('<input name="delete_promotion[]" type="hidden" value="' +$(this).attr("id")+ '" />');
    }else{
      promotion_key= $(this).attr("id");
      $.each($(".new_promotion"), function(index) { 
        if($(this).attr("key") == promotion_key){
          $(this).remove();
        }
      });
    }   
    $(this).parents("tr").remove();
    return false;
  });

  $("#advanced-settings-toggler").click(function() {
    if ($('#advanced-settings').css('display') == 'block') {
      $("#advanced-settings-toggler").text("Show advanced price settings");
    } else {
      $("#advanced-settings-toggler").text("Hide advanced price settings");
    }    
    $('#advanced-settings').toggle('slow');
  });

  $("#advanced-settings a.add, #add-promotion-form a.cancel").click(function() {
    $("#add-promotion-form").toggle();
    $("#advanced-settings a.add").toggle();
  });  
  
}
