$(function () {
	$('#bilde_input').uploadify({
		uploader: '/share/js/jquery/plugins/uploadify/uploadify.swf',
		script: '/form2.php?form=fotokonkurranse10',
		cancelImg: '/share/img/deny_16x16.png',
		auto: true,
		multi: true,
		fileDesc: 'Bildefiler (.jpg, .jpeg)',
		fileExt: '*.jpg;*.jpeg',
		buttonImg: '/share/img/b.gif',
		sizeLimit: 5 * 1024 * 1024,
		wmode: 'transparent',
		width: $('#bilde_browse').width() + 5,
		height: $('#bilde_browse').height() + 5,
		queueID: 'bilde_inputQueue',
		onComplete: function (e, qid, file, response, data) {
			$('#bilde_input' + qid + ' .cancel').html('<img src="/share/img/check_16x16.png" border="0" />')
			$('#bilde_input_container' + qid).append('<input type="hidden" name="bilde[' + qid + '][path]" value="' + response + '" />');
			return false;
		},
		onAllComplete: function (e, data) {
			$('#fotokonkurranse10 input[type=submit]').val('Send inn bilde(r)').removeAttr('disabled');
		},
		onSelect: function (e, qid, file) {
			var photos = $('.uploadifyQueueItem').length;
			
			if (photos > 2) {
				return false;
			} else if (photos > 1) {
				$('#bilde_browse, #bilde_input').hide();
				$('#bilde_inputUploader').css('top', '-1000px');
				$('#bilde_inputQueue').after('<p><b class="error">Du kan laste opp maks tre bilder.</b></p>');
			}
		},
		onSelectOnce: function (e, data) {
			$('.uploadifyQueueItem:not(.contained)').each(function () {
				var qid = $(this).attr('id'),
					qid = qid.substr(qid.length - 6);
					
				$(this).addClass('contained')
					.wrap('<div class="container" id="bilde_input_container' + qid + '" />')
					.before('<label>Bilde</label>');
				
				$('#bilde_input_container' + qid).after(
					'<div class="container bilde_input_container" id="bilde_beskrivelse_container' + qid + '">' +
						'<label for="bilde_beskrivelse' + qid + '">Beskrivelse</label>' +
						'<textarea class="large" id="bilde_beskrivelse' + qid + '" name="bilde[' + qid + '][beskrivelse]"></textarea>' +
						'<p class="no_label">Beskriv sted, motiv og andre relevante opplysninger om bildet.</p>' +
					'</div>'
				);
			});
		},
		onCancel: function (e, qid, file, data) {
			$('#bilde_input_container' + qid + ', #bilde_beskrivelse_container' + qid).fadeOut().remove();
		},
		onOpen: function () {
			$('#fotokonkurranse10 input[type=submit]').val('Vennligst vent mens bildene lastes opp ...').attr('disabled', 'disabled');
		}
	});
	
	$('#fotokonkurranse10').submit(function () {
		var ok = true;
	
		$('textarea, #kontaktinformasjon input', $(this)).each(function () {
			
			if ($(this).val() === '') {
				ok = false;
				$(this).prev().addClass('error');
			} else {
				$(this).prev().removeClass('error');
			}
		});
		
		if (!$('.uploadifyQueueItem').length) {
			if (!$('#noimageserror').length) {
				$('#bilde_bilde_container').prepend('<p id="noimageserror"><b class="error">Du må laste opp minst ett bilde.</b></p>');
			}
			ok = false;
		} else {
			if ($('#noimageserror').length) {
				$('#noimageserror').remove();
			}
		}
		
		if (!ok) {
			alert('For å delta i konkurransen må du fylle ut alle feltene i skjemaet.\nDe som mangler er merket med rødt.');
			return false;
		}
	});
});
