$()

function improveList(inplace_url, positions_url)
{
  $('a.delete').click(function(){
    $(this).html('deleting...');
    var item = this.getAttribute('rel');
    $.get(this.getAttribute('href'), { nocache: Math.random() }, function(fromServer){
      $('#'+item).remove();
    });
    return false;
  })

  $('span.inplace').editable(inplace_url, {
      indicator   : 'saving...',
      submit      : 'Save',
      cancel      : 'Cancel',
      tooltip     : 'Click to edit...',
      style       : 'display: inline',
      placeholder : 'Wrong content',
      width       : '120px',
      cssclass    : 'inplace-form'
  });

	$('span.inplace').hover(function() {
    $(this).addClass('highlight')
  }, function(){ $(this).removeClass('highlight') });

	$("#sortable").sortable({
	  stop: function(event, ui) { 
      $.get(positions_url, { 'positions[]': $(this).sortable('toArray')}, function(data){
        // hide indicator
        // update content slot or skip it (already sorted)
      });
	  }
	});
	$("#sortable").disableSelection();
}

$.fn.submitForm = function() {
  $(this).submit(function(e){
	var id = $(this).attr('id')
    $.ajax({
	    type:'POST',
	    dataType:'html',
	    url:this.getAttribute('action'),
	    data:$(this).serialize(),
	    success:function(data, textStatus){
		  $('.'+id+' td').html(data);
		  $('.'+id).removeClass('busy');
	     },
	    beforeSend: function(XMLHttpRequest) {
		  $('.'+id).addClass('busy');
	    }
    });
  e.preventDefault();
  });
};

$.fn.submitDefaultForm = function(container) {
  $(this).submit(function(e){
    $.ajax({
      type:'POST',
      dataType:'html',
      url:this.getAttribute('action'),
      data:$(this).serialize(),
      success:function(data, textStatus){
      $('#'+container).html(data).removeClass('busy');
       },
      beforeSend: function(XMLHttpRequest) {
      $('#'+container).addClass('busy');
      }
    });
  e.preventDefault();
  });
};

$.fn.displayForm = function(container) {
  $(this).click(function(e){
	$('.add-category').remove();
	var rel = $(this).attr('rel');
    var tpl = $('#'+ container).html();
    tpl 	= tpl.replace(/Q/g, rel); 
	$('#category_' + rel).after('<tr class="add-category form-'+rel+'"><td colspan="3" >'+tpl+'</td></tr>');
	$('#form-' + rel).removeClass('busy');
	$('#form' + rel+' :input').select();
	e.preventDefault();
  });
};

$.fn.cancelForm = function(container) {
  $(this).click(function(e){
	var rel = $(this).attr('rel');
	$('tr.' + rel).remove();
	e.preventDefault();
  });
};

$.fn.deleteItem = function() {
  $(this).click(function(e){
    var container = $(this).parent();
    $.ajax({
        type:'POST',
        dataType:'html',
        url:this.getAttribute('href'),
        success:function(data, textStatus){
          container.remove();
         },
        beforeSend: function(XMLHttpRequest) {
          container.addClass('busy');
        }
      });
    e.preventDefault();
  });
};

$.fn.submitUploadify = function (params)
{
  if (params) {
	$(this).uploadifySettings("scriptData", params);
  }
  $(this).uploadifyUpload();
};

$(function(){

  $('table.list tr.stripped').hover(function() {
  	$('table.list tr').removeClass('highlight');
    $(this).addClass('highlight');
  });

  $('table.list tbody tr.stripped:odd').addClass('odd');
  $('table.list tbody tr.stripped:even').addClass('even');
  $('table.fieldset tbody tr:odd').addClass('odd');
  $('table.fieldset tbody tr:even').addClass('even');
});


$.fn.removeOption = function() {
  $(this).unbind('click');
  
  $(this).click(function(e) {
    $(this).parent().remove();
    e.preventDefault();
  });
};

$.fn.addOption = function(template_object, container_object) {
  $(this).unbind('click');
  
  $(this).click(function(e) {
    var nb  = template_object.attr('class');
    var tpl = template_object.html();
    var counter = $(this).attr('rel');
    for (i=0; i < counter; i++) 
    {
      temp = tpl.replace(/Q/g,  ++nb); 
      container_object.append(temp);
    }
    template_object.attr('class', nb);
    e.preventDefault();
  });
};


$.fn.formatPrice = function() {
  $(this).change(function(e){
	  $(this).val($(this).val().replace(',', '.')); 
	  e.preventDefault();
  });
};


function switchItem(from, to) 
{
  $('#' + from).hide(); 
  $('#' + to).show(); 
}
