function addLocation(location, id) {
  addComponent(location, id, 'location', 'locations', 5)
}

function addNewLocation(location) {
  if (location != null && $j('#location_list li').length < 5) {
    num = num + 1;
    if ($j('#location_list').html().indexOf(location) == -1) {
      location_html = '<li class="loc' + num +  '"> <a class="delete_image loc" onclick="$j(\'.loc' + num + '\').remove()"> <img src ="/images/ze-delete-tag.png"> </a>' + location + '</li>';
      $j('#location_list').append(location_html);

      name_field = '<input class="loc' + num + '" type="hidden" name="new_locations[loc'+ num + '][name]" value="' + location + '"></input>'
      parent_field = '<input class="loc' + num + '" type="hidden" name="new_locations[loc'+ num + '][parent]" value="' + $j('#parent_id_field').val() + '"></input>'
      type_field = '<input class="loc' + num + '" type="hidden" name="new_locations[loc'+ num + '][type]" value="' + $j('#locations_type').val() + '"></input>'

      $j('#listing_form').append(name_field)
      $j('#listing_form').append(parent_field)
      $j('#listing_form').append(type_field)
    }
  }
}

function set_locations_parent_field(id) {
  $j('#listing_form #parent_id_field').remove();
  parent_field_html = '<input type="hidden" name="parent_location" id="parent_id_field" value="' + id + '"></input>';
  $j('#listing_form').append(parent_field_html);
}

function set_locations_type_field(type) {
  $j('#listing_form #locations_type').remove();
  type_field_html = '<input type="hidden" name="locations_type" id="locations_type" value="' + type + '"></input>';
  $j('#listing_form').append(type_field_html);
}

function submit_new_location() {
  addNewLocation($j("#new_location").val());
  $j("#location_board").hide();
  $j("#location_board .location_checkbox[checked]").each(function(){
    addLocation($j(this).val(), $j(this).next().val());
  })
  return false;
}

function initRegularForm() {
  $j('#switch_to_regular').hide()
  $j('#switch_to_express').show()
  $j('#listing_express').attr('checked', false)
  $j('.extended').show();
}

function initExpressForm() {
  $j('#switch_to_regular').show()
  $j('#switch_to_express').hide()
  $j('#listing_express').attr('checked', true)
  $j('.extended').hide();
}

$j(document).ready(function() {
  $j('#add_image').fancybox({'titleShow': false});
  $j('#add_video').fancybox({'titleShow': false});
  
  $j('#listing_price_negotiable').change(function() {
    if ($j(this).attr('checked')) {
      $j('#listing_price').attr('disabled', true).val('');
      $j('#listing_pay_unit').attr('disabled', true);
    } else {
      $j('#listing_price').attr('disabled', false);
      $j('#listing_pay_unit').attr('disabled', false);
    }
  })

  $j('#listing_purpose_profit').click(function() {
    if ($j(this).attr('checked') && !$j('#listing_express').attr('checked')) {
      $j('#floater').show();
    }
  })

  if (!$j('#listing_purpose_profit').attr('checked') || $j('#listing_express').attr('checked')) {
    $j('#floater').hide();
  }

  if ($j('#listing_express').attr('checked')) {
    initExpressForm()
  }

  if ($j('#listing_price_negotiable').attr('checked')) {
    $j('#listing_price').attr('disabled', true);
    $j('#listing_pay_unit').attr('disabled', true);
  }
  //$j('div.video embed').attr({wmode: 'opaque', width: '100%'});
})
