function addOverlay( big, image ){
	if( big ){
		if( image ){
			$('body').prepend('<div id="overlay_bg"></div><div id="overlay_big_image"></div>');
			
		 	$('#overlay_big_image').css('opacity', '0');
		 	$('#overlay_big_image').animate({
		 		'opacity' : '1'
		 	}, 1000);
	 	}else{
	 		$('body').prepend('<div id="overlay_bg"></div><div id="overlay_big"></div>');
			
		 	$('#overlay_big').css('opacity', '0');
		 	$('#overlay_big').animate({
		 		'opacity' : '1'
		 	}, 1000);
	 	}
	 	
	 	$('#overlay_bg').css('opacity', '0');
	 	$('#overlay_bg').animate({
	 		'opacity' : '0.7'
	 	}, 1000);
	}else{
		$('body').prepend('<div id="overlay_bg"></div><div id="overlay"></div>');
	 	
	 	$('#overlay').css('opacity', '0');
	 	$('#overlay').animate({
	 		'opacity' : '1'
	 	}, 1000);
	 	
	 	$('#overlay_bg').css('opacity', '0');
	 	$('#overlay_bg').animate({
	 		'opacity' : '0.7'
	 	}, 1000);
	}
}

function removeOverlay( ){
	if( $('#overlay_bg').length > 0 ){
		$('#overlay_bg').animate({
			'opacity' : '0'
		},500,function(){
			$('#overlay_bg').remove();
		});
	}
	
	if( $('#overlay').length > 0 ){
		$('#overlay').animate({
			'opacity' : '0'
		},500,function(){
			$('#overlay').remove();
		});
	}
	
	if( $('#overlay_big').length > 0 ){
		$('#overlay_big').animate({
			'opacity' : '0'
		},500,function(){
			$('#overlay_big').remove();
		});
	}
	
	if( $('#overlay_big_image').length > 0 ){
		$('#overlay_big_image').animate({
			'opacity' : '0'
		},500,function(){
			$('#overlay_big_image').remove();
		});
	}
}

jQuery(function(){
	/*
	 $("input.picture").filestyle({ 
	     image: '/mahout_cms/plugins/pictureevent/images/uploadbutton.png',
	     imageheight : 23,
	     imagewidth : 116,
	     width : 360
	 });
	 */
	 $("input.picture").customFileInput();	
	 $('.datei').find('br').remove();
	 
	 $('.foto_stimmen').click(function(){
	 	addOverlay();
	 	
	 	var id = $(this).attr('id').replace('foto_stimme_', '');
	 	
		$.ajax({
			type: "POST",
			url: "/mahout_cms/plugins/pictureevent/ajax.php",
			data: "mode=rate&key=" + id,
			async: false,
			success: function(msg){
				$('#overlay').html( '<a href="javascript:;" id="close">Schließen</a>' + msg );
			}
		});
	 });
	 
	 $('.foto_weiterempfehlen').click(function(){
	 	addOverlay( true );
	 	
		var key = $(this).attr('id').replace('foto_weiterempf_','');
	 	$.ajax({
			type: "POST",
			url: "/mahout_cms/plugins/pictureevent/ajax.php",
			data: "mode=weiterempfehlen&key=" + key,
			async: false,
			success: function(msg){
				$('#overlay_big').html( '<a href="javascript:;" id="close">Schließen</a>' + msg );
			}
		});
	 });
	 
	 $('.big_image').click(function(){
	 	addOverlay( true, true );
	 	
		var key = $(this).attr('id').replace('bigimage_','');
	 	$.ajax({
			type: "POST",
			url: "/mahout_cms/plugins/pictureevent/ajax.php",
			data: "mode=bigimage&key=" + key,
			async: false,
			success: function(msg){
				$('#overlay_big_image').html( '<a href="javascript:;" id="close">Schließen</a>' + msg );
			}
		});
	 });
	 
	 $('#close, #close_big_image, #overlay_bg').live('click',function(){
	 	removeOverlay();
	 });
	 
	 
	 $('#ajax_form').live('submit',function(){
	 	var antwort = $(this).children('input.frage').val();
	 	var frage = $(this).children('label.frage').attr('id').replace('frage_','');
	 	
		var id = $('#gallery_ajax_rate .image').attr('id').replace('image_', '');
	 	
	 	$.ajax({
			type: "POST",
			url: "/mahout_cms/plugins/pictureevent/ajax.php",
			data: "mode=rateNow&key=" + id + "&frage=" + frage + "&antwort=" + antwort,
			async: false,
			success: function(msg){
				if( msg != "" ){
					$('#overlay').html( '<a href="javascript:;" id="close">Schließen</a>' + msg );
					$('#foto_stimme_' + id).before('<a class="foto_abgestimmt">Für dieses Foto haben<br />Sie bereits gestimmt</a>');
					$('#foto_stimme_' + id).remove();
				}else{
					alert("Leider stimmt die Antwort auf diese Frage nicht. Versuchen Sie es einfach erneut.");
				}
			}
		});
		
	 	return false;
	});
	 
	$('#ajax_form_empfehlen').live('submit',function(){
		var name = $(this).find('input.name').val();
		var email = $(this).find('input.email').val();
		var empf_name = $(this).find('input.empf_name').val();
		var empf_email = $(this).find('input.empf_email').val();
		var text = $(this).find('.text').val();
		
		var id = $('#gallery_ajax_weiterempf .image').attr('id').replace('image_', '');
		
		$.ajax({
			type: "POST",
			url: "/mahout_cms/plugins/pictureevent/ajax.php",
			data: "mode=empfNow&key=" + id + "&name=" + name + "&email=" + email + "&empf_name=" + empf_name + "&empf_email=" + empf_email + "&text=" + text,
			async: false,
			success: function(msg){
				if( msg != "" ){
					$('#overlay_big').before('<div id="overlay"></div>').remove();
					$('#overlay').html( '<a href="javascript:;" id="close">Schließen</a>' + msg );
				}else{
					alert("Bitte füllen Sie alle Felder aus.");
				}
			}
		});
		
	 	return false;
 	});
})
