
var recently_uploaded_image_id = 0;

function image_insert_panel() {
	load_garden_albums();
}

function load_garden_albums() {
	$(".image_insert_panel").html('<h2>Loading...</h2>');				

	$.post("/plantstew/gears/gear_stew_get_garden_albums_posting.php", function(data){
		$(".image_insert_panel").html(data);
		$("a[@name='upload_new_photo']").click(upload_new_photo);			
		$("a[@name='load_garden_album_images']").click(load_garden_album_images);
		$("a[@name='image_insert_panel']").click(image_insert_panel);
		$("a[@name='close_image_insert_panel']").click(close_image_insert_panel);
	  });
}

function load_garden_album_images() {
	$(".image_insert_panel").html('<h2>Loading...</h2>');

	var info_arr = $(this).attr("rel").split("_");

	$.ajax({
		type: "POST",cache: false,url: "/plantstew/gears/gear_stew_get_garden_images_posting.php?page=" + info_arr[0],
		data: "album_id=" + info_arr[1],
		success: function(data){
			$(".image_insert_panel").html(data);
			$("a[@name='image_to_insert']").click(insert_image);
			$("a[@name='image_insert_panel']").click(image_insert_panel);
			$("a[@name='close_image_insert_panel']").click(close_image_insert_panel);
			$("a[@name='load_garden_album_images']").click(load_garden_album_images);
		}
	});
	//return false;
}

function upload_new_photo() {	
	$.post("/plantstew/gears/gear_stew_upload_garden_photo_posting.php", function(data){
		$(".image_insert_panel").html(data);
		$("#panel1").hide();
		$(".upload_image_button").click(ajaxFileUpload);		
		$("a[@name='image_insert_panel']").click(image_insert_panel);
		$("a[@name='close_image_insert_panel']").click(close_image_insert_panel);			
	  });		
}

function close_image_insert_panel() {
	$(".image_insert_panel").html('');
	return false;
}

function insert_image() {
	var info_arr = $(this).attr("rel").split("_");

	$(".feedback" + info_arr[0]).html('Inserting...');

	$.post("/plantstew/gears/gear_stew_get_garden_image_insert_code.php?image_id=" + info_arr[0], function(data){
		$("textarea[@name='message']").val($("textarea[@name='message']").val() + '\n\n' + data + '\n');
		$(".feedback" + info_arr[0]).html('<b style="background:yellow;font-size:15px;">Image inserted</b>');
	  });		

	return false;
}

function ajaxFileUpload() {			

	$("#panel2").hide();
	$("#panel1").show();

	/*
	prepareing ajax file upload
	url: the url of script file handling the uploaded files
	fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
	dataType: it support json, xml
	secureuri:use secure protocol
	success: call back function when the ajax complete
	error: callback function when the ajax failed
	*/

	$.ajaxFileUpload ({
			url:'/plantstew/gears/gear_stew_upload_garden_photo.php?album_id=' + $("select[@name='album_id']").val(), 
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined') {
					if(data.error != '') {
						$("#panel1").hide();
						$("#panel2").show();
						alert(data.error);							
					}else {
					
						recently_uploaded_image_id = data.image_id;
						
						$.post("/plantstew/gears/gear_stew_enter_photo_description.php?image_id=" + recently_uploaded_image_id, function(data){
							$(".image_insert_panel").html(data);
							$(".save_description_button").click(save_description);	
						  });						
					
					}
				}
			},

			error: function (data, status, e) {
				alert(e);
			}
		}
	)

	return false;
}	

function save_description() {
	
	$.ajax({
		type: "POST",cache: false,url: "/plantstew/gears/gear_stew_display_uploaded_photo.php?image_id=" + recently_uploaded_image_id,
		data: $(".description_form").serialize(),
		success: function(data){
			$(".image_insert_panel").html(data);
			$("a[@name='image_to_insert']").click(insert_image);
			$("a[@name='image_insert_panel']").click(image_insert_panel);
			$("a[@name='close_image_insert_panel']").click(close_image_insert_panel);
			$("a[@name='load_garden_album_images']").click(load_garden_album_images);
			$("a[@name='upload_new_photo']").click(upload_new_photo);	
		}
	});
	
	return false;
}