var caption_editor;
var photographer_editor;
var chosen_img = '';

window.onload = function() 
{
	if (document.getElementById('trashcan')) {
		// Trash functionality
		Droppables.add
		(
			'trashcan', 
			{
				accept: ['gallery_img', 'chosen_img'], 
				onDrop: function(element) {
					input_box = confirm("Are you sure you want to delete this image?");
					if (input_box==true) {
						hideImgInfo();
						$('trashcan').innerHTML = '<img src="../shared/images/indicator.gif" />';
						new Ajax.Request
						(
							'../modules/Gallery/func.php', 
							{ 
								onSuccess: function(r) {
									Element.hide(element);
									$('trashcan').innerHTML = '';
									if (element.src == $('default_photo').src) {
										$('edit_artist_default_photo').innerHTML = '<span class="inplaceeditor-empty">Drag your default photo here.</span>';
									}
								},
								onFailure: function(r) {
									alert('Failure: ' + r.responseText);
								}, 
								method: 'post', 
								parameters: 'func=delete_img&id=' + escape($F('id')) + '&val=' + escape(element.src),  
								asynchronous:true
							}
						);
					}
					else {
						Element.show(element);
						element._opacity = 1;
					}
				}, 
				hoverclass:'trashcan-active'
			}
		);
		hideImgInfo();
		caption_editor = new Ajax.InPlaceEditor('img_caption', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=caption&id=' + data.info[0].id + '&val=' + escape(value);}});
		photographer_editor = new Ajax.InPlaceEditor('img_photographer', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=photographer&id=' + data.info[0].id + '&val=' + escape(value);}});
		order_editor = new Ajax.InPlaceEditor('img_order', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=img_order&id=' + data.info[0].id + '&val=' + escape(value);}});
		
		//Event.observe('imgs_data', "click", hideImgInfo, false);
		
		runScript();
	}
}

function makeDraggable(img_id)
{
	new Draggable(img_id, {revert:true});
}

function makeClickable(img_id)
{
	Event.observe
	(
	 	img_id, 
		"click", 
		function(e) {
			$(img_id).className = 'chosen_img';
			hideImgInfo();
			chosen_img = img_id;
			$('image_info').innerHTML = '<strong>Caption:</strong> <div id="img_caption"></div><br /><strong>Photographer:</strong> <div id="img_photographer"></div><br /><strong>Image Order:</strong> <div id="img_order"></div>';
			$('image_info_loading').innerHTML = '<img src="../shared/images/indicator.gif" />';
			new Ajax.Request
				(
					'../modules/Gallery/func.php', 
					{  
						onSuccess: function(r) {
							$('image_info_loading').innerHTML = '';
							var data = eval('(' + r.responseText + ')');
							caption_editor.dispose();
							photographer_editor.dispose();
							caption_editor = new Ajax.InPlaceEditor('img_caption', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=caption&id=' + data.info[0].id + '&val=' + escape(value);}});
							photographer_editor = new Ajax.InPlaceEditor('img_photographer', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=photographer&id=' + data.info[0].id + '&val=' + escape(value);}});
							order_editor = new Ajax.InPlaceEditor('img_order', '../modules/Gallery/func.php', {onFailure: function(r) {inPlaceError(r)}, callback: function(form, value) {return 'func=updateImgData&field=img_order&id=' + data.info[0].id + '&val=' + escape(value);}});
							
							Effect.Appear('image_info');
							if (data.info[0].caption != '') {
								$('img_caption').innerHTML = data.info[0].caption;
							}
							if (data.info[0].photographer != '') {
								$('img_photographer').innerHTML = data.info[0].photographer;
							}
							if (data.info[0].order != '') {
								$('img_order').innerHTML = data.info[0].order;
							}
						},
						onFailure: function(r) {
							alert('Failure: ' + r.responseText);
						}, 
						method: 'post', 
						parameters: 'func=img_info&id=' + escape($F('id')) + '&val=' + escape($(img_id).src),  
						asynchronous:true
					}
				);	
		}, 
		false
	);	
}

function hideImgInfo()
{
	$('image_info').style.display = 'none';
	if (chosen_img != '') {
		$(chosen_img).className = 'gallery_img';
	}
}

// JavaScript Document
function checkGalleryForm()
{
	if (document.getElementById('title').value == '') {
		alert('Please type in the gallery title.');
		return false;
	} 
	return true;
}

// Swap main images in gallery
function switchImage(id, gallery_id)
{
	$('caption').innerHTML = 'Loading...';
	new Ajax.Request
	(
		'bunnyfarm/modules/Gallery/func.php', 
		{ 
			onSuccess: function(r) {
				var data = eval('(' + r.responseText + ')');
				$('full_image').style.height = data.info[0].height + 'px';
				$('full_image').style.backgroundImage = "url('"  + data.info[0].url + "')";
				$('caption').innerHTML = data.info[0].caption;
				var last = 'thumb_' + $('last_selected').innerHTML;
				$(last).className = '';
				$('last_selected').innerHTML = id;
				var current = 'thumb_' + id;
				$(current).className = 'selected';
			},
			onFailure: function(r) {
				alert('Failure: ' + r.responseText);
			}, 
			method: 'post', 
			parameters: 'func=switch_image&id=' + escape(id) + '&gallery_id=' + escape(gallery_id),  
			asynchronous:true
		}
	);
}