var uploaded = false;
$(document).ready(function() {

/*
	if($('.kuenstler_navi').length) {
		$('.kuenstler_navi a').click(function(){
			$('.kuenstler_navi li').removeClass('active');
			$(this).parent('li').addClass('active');
		});
	}
*/
	
	if($('#kontakt').length) {
		/* Spamschutz Kontaktformular */
			$('#kontakt').append('<input type="hidden" name="no_test" value="yes"/>');
			$('#kontakt').append('<input type="hidden" name="js" value="1"/>');
		/* ENDE Spamschutz */
		$('#kontakt').FormValidate({
			phpFile:"",
			ajax:false,
			validCheck:true
		});
		$('input[name=\'files\']').val('');
		$.ajax({
			url: 'generator_prefix.php',
			cache: false,
			success: function(html){
				$('input[name=\'prefix\']').val(html);
				
				//'scriptData':		{'prefix': $('input[name=\'prefix\']').val()},
				$('#bilder').uploadify({ 
					'height':			25,
					'width':			106,			
					'uploader':			'lib/js/uploadify.swf', 
					'script':			'lib/js/uploadify.php', 
					'folder':			'uploads/', 
					'buttonImg':		'lib/js/browse.jpg',
					'cancelImg':		'lib/js/cancel.png',
					'multi':			true,
					'onError':			onError,
					'onComplete':		afterUpload,
					'onAllComplete':	afterAllUpload,
					'sizeLimit':		1500000,
					'fileExt':			'*.png;*.jpg;*.jpeg;*.gif;*.bmp;*.tif',
					'fileDesc':			'Bilddateien der Formate PNG, JPG, JPEG, GIF, BMP oder TIF',
					'scriptData':		{'prefix': html},
					'rollover':			true
				});
			}
		});
		

		$('#kontakt').submit(function(){
			if(!uploaded && $('.uploadifyQueue .uploadifyQueueItem').length) {
				$('#bilder').uploadifyUpload();
				return false;
			} else {
				return true;
			}
		});	
	}
});

function onError(event,queueID,fileObj,errorObj) {
	//alert(errorObj.info);
	uploaded = false;
}

function afterUpload(event,queueID,fileObj,response,data) {
	//alert(response);
	
	var splitted = response.split('_');
	if(splitted[0]=='1') {
		var f_name = fileObj.name;
		if(splitted[1]) {
			f_name = fileObj.name.replace(fileObj.type,'')+'_'+splitted[1]+fileObj.type;
		}
		$('input[name=\'files\']').val($('input[name=\'files\']').val()+';'+f_name);
		$('ul#attached_pictures').append('<li>'+f_name+'</li>');
		$('ul#attached_pictures').prev('label').show();
	}
	//if($('.uploadifyQueue .uploadifyQueueItem').length==1) {
	//	submitForm();
	//}
}

function afterAllUpload(event,data) {
	submitForm();
}

function submitForm() {
	uploaded = true;
	$('#kontakt').trigger('submit');
}
