$(document).ready(function(){
	
	var show_stars_details = false;
	var load_stars_details = true;
	
	// initially load comments
	var image_id = $('#comments').attr('class').substring(6);
	
	// add new comment
	$('a#comment_add').click(function(event) {
		event.preventDefault();
		
		$("#dialog_comment_add").dialog({
			bgiframe: true,
			resizable: false,
			height:300,
			width: 600,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Kommentar hinzufügen': function() {
					$("#dialog_comment_add").append('<img src="/img/spinner_bw.gif" alt="bitte warten" class="spinner" />');
					//var image_id = $('a#comment_add').attr('rel').substring(6);
					var url = '/galerie/kommentar-speichern';
					var data = { image_id: image_id, comment: $('#comment_new').val() }
					$('div#comments').load(url, data, function(){
						$("#dialog_comment_add #comment_new").val('');
						$("#dialog_comment_add").dialog('close');
						$("#dialog_comment_add").dialog('destroy');
						$("#dialog_comment_add .spinner").remove();
					});
				},
				'Abbrechen': function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
				}
			}
		});
	});
	
	// vote
	$('#stars').change(function() {
		var stars = $(this).val();
		
		// save stars
		$.post("/galerie/sterne-speichern", 
				{ image_id: image_id,
				  stars: stars },
				function(data){
					  if(data.result == 1)
					  {
						load_stars_details = true;
						var stars_width = 120/5 * data.stars_avg;
						// stars avg
						var stars_avg = data.stars_avg.toString();
						$('#stars_avg').html(number_format(stars_avg, 2, ',', '.'));
						$('#stars_graph').css('width', stars_width + 'px');
						
						// stars count
						if(data.stars_count > 1)
							var voices = "Stimmen";
						else
							var voices = "Stimme";
						$('a#show_stars_details').html(data.stars_count +' '+voices);
						
						if(show_stars_details)
						{
							getStarDetails(image_id);
						}
						
						// show info
						$('#stars_comment').html('Vielen Dank für Ihre Bewertung!');
					  } else {
						  alert(data.message);
					  }
				  }, 
				  "json"
			);
		
	});
	
	$('#show_stars_details').click(function(event) {
		event.preventDefault();
		
		// show details
		if(!show_stars_details)
		{
			getStarDetails(image_id);
			show_stars_details = true;
			
		// hide details
		} else {
			$('#stars_details').html('&nbsp;');
			show_stars_details = false;
		}
	});
	
	$('.comment_del').live('click', function(event) {
		event.preventDefault();
		var comment_id = $(this).attr('name').substring(12);
		console.log(comment_id);
		//var image_id = $('img#image').attr('rel');
		
		// show dialog
		$("#dialog_comment_del").dialog({
			bgiframe: true,
			resizable: false,
			height: 150,
			width: 350,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Kommentar löschen': function() {
					$("#dialog_comment_del").append('<img src="/img/spinner_bw.gif" alt="bitte warten" class="spinner" />');
					//var image_id = $('img.galleryimage').attr('rel');
					var url = '/galerie/kommentar-loeschen';
					var data = { image_id: image_id, comment_id: comment_id }
					$('div#comments').load(url, data, function(){
						$("#dialog_comment_del").dialog('close');
						$("#dialog_comment_del").dialog('destroy');
						$("#dialog_comment_del .spinner").remove();
					});
				},
				'Abbrechen': function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
				}
			}
		});
	});
	
	$('.comment_edit').live('click', function(event) {
		event.preventDefault();
		var comment_id = $(this).attr('name').substring(13);
		var content = $('#comment_'+comment_id+' div.comment_content').html();		
		//var image_id = $('img.galleryimage').attr('id');
		
		// get old comment
		var url = '/galerie/kommentar-lesen';
		var data = { comment_id: comment_id };
		$.post(url, data, function(data){
				if(data.result == 1)
				{
					$('#dialog_comment_edit #comment_edit').val(data.comment);
				} else {
					$('#dialog_comment_edit #comment_edit').val('(Beim lesen der Daten ist ein Fehler aufgetreten.)');
				}
			},
			"json"
		);
		
		// show dialog
		$("#dialog_comment_edit").dialog({
			bgiframe: true,
			resizable: false,
			height:300,
			width: 600,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Kommentar speichern': function() {
					$("#dialog_comment_edit").append('<img src="/img/spinner_bw.gif" alt="bitte warten" class="spinner" />');
					var url = '/galerie/kommentar-aendern';
					var data = { image_id: image_id, comment_id: comment_id, comment: $('#dialog_comment_edit #comment_edit').val() }
					$('div#comments').load(url, data, function(){
						$("#dialog_comment_edit #comment_edit").val('');
						$("#dialog_comment_edit").dialog('close');
						$("#dialog_comment_edit").dialog('destroy');
						$("#dialog_comment_edit .spinner").remove();
					});
				},
				'Abbrechen': function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
				}
			}
		});
	});
	
	$('#image_del').live('click', function(event) {
		event.preventDefault();
		//var image_id = $('img.galleryimage').attr('id');
		
		// show dialog
		$("#dialog_image_del").dialog({
			bgiframe: true,
			resizable: false,
			height: 150,
			width: 300,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Foto löschen': function() {
					$("#dialog_image_del").append('<img src="/img/spinner_bw.gif" alt="bitte warten" class="spinner" />');
					var url = '/galerie/foto-loeschen';
					var data = { image_id: image_id };
					$.post(url, data, function(response) {
							if(response.result == 1)
							{
								if(course_id == -1)
									path = '/mein-bereich/meine-fotos/geloescht';
								else
									path =  '/galerie/'+course_id+'/1/geloescht';
								window.location = path;
							} else {
								alert(response.error);
							}
							$("#dialog_image_del .spinner").remove();
						},
						'json'
					);
				},
				'Abbrechen': function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
				}
			}
		});
	});
	
	$('#image_edit').live('click', function(event) {
		event.preventDefault();	
		//var image_id = $('img.galleryimage').attr('id');
		
		// get old comment
		var url = '/galerie/fotodaten-lesen';
		var data = { image_id: image_id };
		$.post(url, data, function(data){
				if(data.result == 1)
				{
					showImageEditDialog(data.title, data.description);
				} else {
					alert('(Beim lesen der Bilddaten ist ein Fehler aufgetreten.)');
				}
			},
			"json"
		);
	});
	
	function showImageEditDialog(title, description)
	{
		// set vars
		$('#title_edit').val(title);
		$('#description_edit').val(description);
		
		// show dialog
		$("#dialog_image_edit").dialog({
			bgiframe: true,
			resizable: false,
			height:300,
			width: 600,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Änderungen speichern': function() {
					$("#dialog_image_edit").append('<img src="/img/spinner_bw.gif" alt="bitte warten" class="spinner" />');
					var url = '/galerie/fotodaten-aendern';
					var data = { image_id: image_id, title: $('#title_edit').val(), description: $('#description_edit').val() };
					// edit data
					$.post(url, data, 
						function(data)
						{
							if(data.result == 1)
							{
								$('#title').html(data.title);
								$('#description').html(data.description);
								if(data.description == '')
									$('#description_container').hide();
								else
									$('#description_container').show();
								$("#dialog_image_edit .spinner").remove();
							} else {
								alert(data.error);
							}
						},
						'json'
					);
					$(this).dialog('close');
					$(this).dialog('destroy');
				},
				'Abbrechen': function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
					$("#dialog_image_del .spinner").remove();
				}
			}
		});
	}
	
	$('#toggle_comments').click(function(event){
		event.preventDefault();
		if($(this).attr('class') == 'comments_off')
		{
			$('#comments_container').show();
			$('#toggle_comments').html('ausblenden');
			$(this).attr('class', 'comments_on');
		} else {
			$('#comments_container').hide();
			$('#toggle_comments').html('einblenden');
			$(this).attr('class', 'comments_off');
		}
	});
});

function getStarDetails(image_id)
{
	var url = '/galerie/sternvergaben-lesen';
	var data = { image_id: image_id };
	$.post(url, data, function(data){
			if(data.result == 1)
			{
				$('#stars_details').html(data.html);
			} else {
				$('#stars_details').html('Beim lesen der Daten ist ein Fehler aufgetreten.');
			}
		},
		"json"
	);
}
