function mark_for_destroy(element) {
	jQuery(element).closest('li.image').hide();
	jQuery(element).closest('li.image').find('.delete').val(1);
}

function edit_photo_tags(el) {
  var field = jQuery("input[name^='product[photos_attributes]'][name$='[tag_list]']", jQuery(el).parents('li'));
  var userInput = prompt("Tag list (e.g. \"color=red, view=top\")", field.val());
  if (userInput != null) { field.val(userInput); }
}

// TODO this is quite convulated, look into prettyfying it later on
function increment_last_photo_attributes_index(ele) {
  if (previous_ele = jQuery('input[type=file]', ele.parents('.newimage').prev('.newimage'))[0]) {
      index = parseInt(previous_ele.name.match(/\[photos_attributes\]\[(\d+)\]/)[1]) + 1;
      ele = ele[0];
      ele.name = ele.name.replace(/\[photos_attributes\]\[(\d+)\]/, '[photos_attributes]['+index+']');
      ele.id = ele.id.replace(/\_photos_attributes_\d+/, '_photos_attributes_'+index);
  }
}

function initMenu() {
	$('#menu ul').hide();
	//$('#menu ul:first').show();
	$('#menu li a').click(function() {
		var checkElement = $(this).next();
		
		if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			//return false;
		}
		
		if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('#menu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			//return false;
		}
	});
}

jQuery.validator.setDefaults({
  meta: "validate",
  onfocusout: false,
  onkeyup: false,
  onclick: false
});

jQuery.validator.addMethod("tagListFormat", function(value, element) {
  return this.optional(element) || /^(\w+=\w+(,\s*\w+=\w+)*)?$/.test(value);
}, "is invalid");