$(document).ready(function() {
	// validate form
	$("form[name=form]").validate({
		rules: {
			myth_created_by_name: {
				required: true
			},
			myth_created_by_email: {
				required: true,
				email: true
			},
			myth_name: {
				required: true
			}
		}
	});
	//
	var loding = '<img src="../images/loading.gif" alt="loading" style="vertical-align:middle;" />';
	var arr = new Array();
	$("#dialog").dialog({modal: true, resizable: false, width: 320}).dialog('close');
	$('.uploadFile').click(function() {
		var element = $(this).attr('rel');
		$("#dialog").dialog('open');
		$("form[name=uploadFile]").find('input[type=submit]').attr('disabled', 'disabled');
		$("form[name=uploadFile]").find('input[type=file]').bind('change', function() {
			if($(this).val() != '') {
				$("form[name=uploadFile]").find('input[type=submit]').removeAttr('disabled');
			} else
				return false;
		});
		$("form[name=uploadFile]").find('input[type=file]').val('');
		$("form[name=uploadFile]").find('input[type=file]').show().next().remove();
		$("form[name=uploadFile]").ajaxForm({
			beforeSubmit: function(data, form) {
				$form = form;
				$form.find('input[name=type]').val(element);
				$form.find('input[type=submit]').hide().after(loding);
			},
			resetForm: true,
			success: function(data) {
				//alert(data);
				$form.find('input[type=submit]').show().next().remove();
				if(data != 0) {
					data = data.replace(/&amp;/g, '&');
					if(element == 'image') {
						$('.imageUpload').css({backgroundImage: 'url('+data+')'});
						$('#imageUploadInput').val(data.split("#")[1]);
					} else if(element == 'attachment') {
						arr.push(data.split("#")[1]);
						$('ul.attachments').append('<li><a href="#remove" rel="'+(arr.length-1)+'"><img src="../images/remove_attachment.gif" alt="remove" border="0" /></a><div><img src="'+data+'" /></div>' + data.split("#")[1] + '<span class=clip></span></li>');
						$('#attachmentsInput').val(arr.join('|'));
					}
					$("#dialog").dialog('close');
				} else
					alert(data);

			}
		});
	});
	$('ul.attachments li a').live('click', function() {
		var obj = $(this).parent().parent().children().index($(this).parent());
		$.ajax({
			type: 'POST',
			url: this.action,
			data: 'del_file='+arr[obj]
		});
		arr.splice((obj),1);
		$('#attachmentsInput').val(arr.join(','));
		$(this).parent('li').fadeOut(function() { $(this).remove() });
	});
	display_form_opts();
	$('select[name=myth_is_fictional]').bind('change', function() {
		display_form_opts();
	});
	$('#xmas_competition').bind('click', function() {
		xmasComp();
	});
	$('.tooltip').each(function() {
		$(this).tooltip({
			track: true,
			delay: 0,
			top: -90,
			left: 0
		});
	});
});

function display_form_opts() {
	if($('select[name=myth_is_fictional]').val() == 1)
		$('.this_is_date, .this_is_origins').addClass('disabled').find('input, textarea').attr('disabled', 'disabled');
	else
		$('.this_is_date, .this_is_origins').removeClass('disabled').find('input, textarea').removeAttr('disabled');
}

function xmasComp() {
	if($('#xmas_competition').is(':checked'))
		$('#xmas_competition_block').fadeIn().find('input[name=myth_created_by_email2]').addClass('required');
	else
		$('#xmas_competition_block').hide().find('input[name=myth_created_by_email2]').removeClass('required');
}
