$(function(){
	function updateImagePreviewState(showDialog) {
		if ($('#imagePreview').val() == '1') {
			$.tooltip.disableBlock();
			if (showDialog) {
				$('#dialog-imagePreviewEnabled').dialog('open');	
			}
		}
		else {
			$.tooltip.enableBlock();
			if (showDialog) {
				$('#dialog-imagePreviewDisabled').dialog('open');	
			}
		}
	}

	$('#searchBarDetailed-imagesPerPage').change(function(){
		if ($(this).val() == '-1') {
			$('#dialog-showAllWarning').dialog('open');
		}
		else {
			document.location.href = '/gallery/view/category/'+galleryCategory+'/images-per-page/'+$(this).val()+'/';
		}
	});

	$('#dialog-showAllWarning').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			"Ok": function(){
				$(this).dialog('close');

				document.location.href = '/gallery/view/category/'+galleryCategory+'/images-per-page/-1/';
			},
			"Cancel": function(){
				$(this).dialog('close');
				
				$('#searchBarDetailed-imagesPerPage').val(galleryImagesPerPage);
			}
		}
	});

	$('#dialog-imagePreviewEnabled').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			"Ok": function(){
				$(this).dialog('close');
			}
		}
	});

	$('#dialog-imagePreviewDisabled').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			"Ok": function(){
				$(this).dialog('close');
			}
		}
	});

	$('.page-gallery-photoButton-downloadMockupLow-noAccess').click(function(){
		$('#dialog-downloadMockup-noAccess').dialog('open');	
	});

	$('#dialog-downloadMockup-noAccess').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			"Register": function(){
				$(this).dialog('close');

				document.location.href = '/user/register/';
			},
			"Login": function(){
				$(this).dialog('close');

				document.location.href = '/user/';
			},
			"Cancel": function(){
				$(this).dialog('close');
			}
		}
	});

	$('.imageEntry').tooltip({
	    delay: 0,
		fade: true,
		top: 3,
		left: 3,
	    showURL: true,
	    bodyHandler: function() {
	        return $('<img/>').attr('src', $(this).attr('tm:previewUrl'));
	    }
	});

	$('#imagePreview').change(function() {
		if ($('#imagePreview').val() == '1') {
			$.cookie('imagePreview', 'enabled', { path: '/' });
		}
		else {
			$.cookie('imagePreview', 'disabled', { path: '/' });
		}

		updateImagePreviewState(true)
	});

	$('.selectPage').change(function(){
		document.location.href = '/gallery/view/category/'+galleryCategory+'/images-per-page/'+$('#searchBarDetailed-imagesPerPage').val()+'/page/'+$(this).val()+'/';
	});


	$('.page-gallery-photoButton-lightbox').click(function(){
		var idArray = $(this).attr('id').split('-');
		$.imageId = idArray[1];

		$('#dialog-lightbox').dialog('open');
		return false;
	});

	if ($.cookie('imagePreview') == 'disabled') {
		$('#imagePreview').val(0);
	}
	else {
		$('#imagePreview').val(1);
	}

	updateImagePreviewState(false);
});
