/*
 */
var cv = {
  open: null,
  field: null,
  field_name: null
};

function show_login() {
  $('#account').attr('action', '/cv/login');
  $('#register').hide();
  $('#login').show();  
}

function show_form(dt) {
  if (cv.open) {
    hide_form(cv.open);
  }
  cv.open = dt;
  dt.addClass('active').next('dd').addClass('visible');
}
 
function hide_form(dt) {
  dt.removeClass('active').next('dd').removeClass('visible');
  cv.open = null;
}

function hide_slot_forms() {
  $('dd a.open').removeClass('active')
  $('ul.slots div.slot-form').remove();
}

/**
 * UNUSED
 *
function slots_sorted(e, ui) {
  var ids = $(ui.item).parents('ul.slots').sortable('toArray');

  var a = $(ui.item).find('a');
  var section = a.attr('cte:section');
  var id = a.attr('cte:id');
  
  $.post('/cv/sort_slots', {
    section: section,
    ids: ids
  });

  show_slot_form(ui.item, section, id);
}
 *
 */

function show_slot_form(a) {
  //function show_slot_form(item, section, id) {
  hide_slot_forms();

  a.addClass('active');
  
  var parent = a.parents('li');
  var id = $(parent).attr('cte:id');
  var section = a.attr('cte:section');

  var form_id = '#cte' + id;
  $.get('/cv/show_slot/' + section + '/' + id, {}, function(data, textStatus) {
    $(data).appendTo(parent).slideDown(300);
    activate_forms();
  }, 'html');
}

/*
 * Only one slot sub-form is open
 */
function activate_slots() {
  $('dd a.open').click(function(e) {
    // if we don't do this, the click is executed twice:
    e.stopImmediatePropagation(); 
    
    show_slot_form($(this));
    return false;
  });
  
  /*
   * Link to add a slot
   */
  $('#forms a.add').click(function(e) {
    // if we don't do this, the click is executed twice:
    e.stopImmediatePropagation(); 
    
    var url = $(this).attr('href');
    var slots = $(this).parents('dd').find('ul.slots');
    $.get(url, {}, function(data, textStatus) {
      //D alert("add callback: " +  data);
      $('li.last', slots).removeClass('last');
      $(data).appendTo(slots).addClass('last');
      activate_slots();
      show_slot_form($('a.open:last', slots));
    }, 'html');
    return false;
  });
  
  /*
   * List management
   */
  $('#forms a.up, #forms a.down, #forms a.del').click(function(e) {
    // if we don't do this, the click is executed many times:
    e.stopImmediatePropagation(); 
    
    var confirmed = true;
    if ($(this).hasClass('confirm')) {
      confirmed = confirm('Wirklich ' + $(this).attr('title') + '?');
    }
    if (confirmed) {
      var harp = $(this).parents('dd.visible');
      harp.load($(this).attr('href'), {}, activate_slots);
    }
    return false;
  });
}

function activate_forms() {
  $('form.save input,form.save textarea').focus(function() {
    if ($(this).attr('cte:default') == $(this).val()) {
      $(this).val('');
    }
  });

  $('form.save input,form.save textarea').blur(function() {
    if ('' == $(this).val()) {
      $(this).val($(this).attr('cte:default'));
      save(this, '');
    }
    else {
      save(this);
    }
  });

  $('form.save select').change(function() {
    save(this);
  });
}

function saved() {
  // $('#info').text(field[field_name]);
  if (cv.field_name == 'url') {
    location.href = 'http://' + cv.field[cv.field_name] + '.cv8.me/';
  }
  if (cv.field_name == 'styling') {
    location.reload();
  }
}

function save(el, value) {
  cv.field = new Object;
  cv.field_name = $(el).attr('name');
  if (typeof(value) == 'undefined') {
    cv.field[cv.field_name] = $(el).val();
  }
  else {
    cv.field[cv.field_name] = value;
  }
  
  var form = $(el).parents('form');
  var type = form.find('input[name=type]').val();
  var id = form.find('input[name=id]').val();
  
  var url = '/' + type + '/save';

  var selector = 'span.cte-' + type;
  if (typeof(id) != 'undefined') {
    cv.field['id'] = id;
    selector += '-' + cv.field['id'];
  }
  selector += '-' + cv.field_name;
  
  var display = $(selector);
    //alert("SEL " + selector + ': ' + display.length);

  if (display.length > 0) {
    display.load(url, cv.field, saved);
  }
  else {
    $.post(url, cv.field, saved);
  }  
}

function upd_input(id, value) {
  $(id).val(value).css({ backgroundImage: 'url(/img/spinner.gif)' });
  $('#f_foto').load(function() {

    location.reload(true);
   /**
    * XXX later, when we offer image manipulation
    *
    $(id).val(value).css({ backgroundImage: 'none' });
    $(this).animate({
      opacity: '1.0',
      width: '400px',
      height: '400px',
      left: '-340px'
    }, 200);
    *
    */
  });
}

$(document).ready(function() {
  $('#remind').click(function() {
    $(this).parents('form')
      .attr('action', $(this).attr('href'))
      .submit();
    return false;
  });

  /*
   * Only one section is open
   */
  var i = 0;
  $('#forms dt').each(function() {
    $(this).attr('id', 'm' + i++);
  });
  
  $('#forms dt.form').click(function() {
    if (cv.open && cv.open.attr('id') == $(this).attr('id')) {
      hide_form($(this));
    }
    else {
      show_form($(this));
    }
  });
  
  activate_forms();

  $('form.save input[class=file]').each(function() {
    var name = $(this).attr('name');
    $(this).change(function() {
      // show filename in overlay
      $(this).prev('div.overlay').children().val($(this).val());
      var form = $(this).parents('form');
      form.attr('target', 'f_' + name);
      form.submit();
    });
  });
  
  $('.editable').click(function() {
    hide_form($('#forms dt'));
    var form = $(this).attr('cte:form');
    show_form($('#forms dt.' + form));

    var id = $(this).attr('cte:id');
    if (id) {
      var item = $('#cte-slot-' + id);
      hide_slot_forms();
      var a = item.find('a.open').addClass('active');
      show_slot_form(a /*item, form, id*/);
    }
  });
  
  /*
   * show registration form
   */
  $('#show_register').click(function() {
    $('#account').attr('action', '/cv/register');
    $('#login').hide();
    $('#register').show();
    return false;
  });

  /*
   * show login form
   */
  $('#show_login').click(function() {
    show_login();
    return false;
  });

  activate_slots();
  
  /*
   * Make slots sortable
   *
  $('ul.slots').sortable({
    items: 'li',
    cursor: 'move',
    start: hide_slot_forms,
    stop: slots_sorted
  });
   *
   */
   
  if (location.hash == '#account') {
    show_form($('#m0'));
    show_login();
  }
});

