//var currentlyViewingGalleryId = 1;
var SSIPath = "/Control/Include/";
var addAlbumURL = location.protocol + "//" + location.host + SSIPath + "add_album.php";
var updatingAlbumURL = location.protocol + "//" + location.host + SSIPath + "save_album.php";
var removeAlbumURL = location.protocol + "//" + location.host + SSIPath + "remove_album.php";
var addingImageURL = location.protocol + "//" + location.host + SSIPath + "add_image.php";
var removeImageURL = location.protocol + "//" + location.host + SSIPath + "remove_image.php";

var addingAlbum = false;
var updatingAlbum = false;
var removingAlbum = false;
var addingImage = false;
var removingImage = false;

var selectedImageId = "";
var needsUpdate = false;

function showAlbum(albumId, galleryId)
{


	debug("first = album_" + lastShownAlbum + "_" + currentlyViewingGalleryId);
	debug("second = album_" + albumId + "_" + galleryId);

	document.getElementById("album_" + lastShownAlbum + "_" + currentlyViewingGalleryId).style.display = "none";
	document.getElementById("album_" + albumId + "_" + galleryId).style.display = "block";
	
	currentlyViewingGalleryId = galleryId;
	lastShownAlbum = albumId;

}

function selectImage(aImage)
{

	// Go through the album and reset all borders
	var alist = document.getElementById("list_" + lastShownAlbum + "_" + currentlyViewingGalleryId);
	//debug(alist.childNodes.length);
	
	cleanWhitespace(alist);
	for(var il = 0; il < alist.childNodes.length; il++)
	{
	
		//debug(il + " " + alist.childNodes[il].nodeType);
		if(alist.childNodes[il].nodeType == 1)
		{
		
			//debug(alist.childNodes[il].firstChild.tagName);
			alist.childNodes[il].firstChild.style.border = "1px Solid #666";
			//alist.childNodes[il].firstChild.style.marginRight = "1em";
			//alist.childNodes[il].innerHTML
			
		
		}
		//var aItem = alist.childNodes[il];
		//debug(alist.childNodes[il].tagName);
		//aItem.firstChild.style.border = "1px Solid #666";
		
	
	}

	aImage.style.border = "1px Dashed Red";
	
	//if(selectedImageId == "")
	//{
		selectedImageId = aImage.id;
	//}
	//aImage.style.borderRight = "2px Solid Red";
	//aImage.style.borderLeft = "2px Solid Red";

}

function addNewAlbum(id)
{
	addingAlbum = true;
	var inpt = document.getElementById("input_" + id);
	var g_name = id.replace("new_album_for_", "");
	var a_name = inpt.value
	debug(g_name + " " + inpt.value);
	// Get the gallery name from the id string
	loadXMLDocPost(addAlbumURL, "g_name=" + g_name + "&name=" + a_name);

}

function handleNewAlbum(resp)
{

	var idSplit = resp.split(":");
	// Create a new album block and add the album name to the sidebar
	// Sidebar 
	var albumList = document.getElementById("album_list_" + idSplit[0]);
	var newLI = document.createElement("LI");
	newLI.innerHTML = "<a id='album_link_id_" + idSplit[2] + "' href='#' onclick='showAlbum(" + idSplit[2] + ", " + idSplit[1] + ")' class='album_link'>" + idSplit[3] + "</a>";
	
	albumList.appendChild(newLI);
	// Album Block
	var mainDiv = document.getElementById("gallery");
	var albumDiv = document.createElement("DIV");
	var albumTitle = document.createElement("h2");
	var albumActions = document.createElement("h2");
	var brEl = document.createElement("br");
	
	var albumImageList = document.createElement("ul");
	albumImageList.style.listStyleType = "none";

	albumDiv.style.display = "none";
	albumDiv.id = "album_" + idSplit[2] + "_" + idSplit[1];
	
	//albumImageList.setAttribute("style", "float:left");
	albumImageList.className = "album";
	albumImageList.style.display = "inline";
	albumImageList.style.margin = "0";
	albumImageList.style.padding = "0";
	albumImageList.id = "list_" + idSplit[2] + "_" + idSplit[1];
	
	albumTitle.innerHTML = "<div style='float:left'>" + idSplit[0] + " >";
	albumTitle.innerHTML += "<input autocomplete='off' onkeyup='updateAlbumLink(this)' style='font-weight: Bold; font-size: 1em; padding-left: .2em' type='text' id='input_album_name" + idSplit[2] + "' value='" + idSplit[3] + "' />";
	albumTitle.innerHTML += "</div>";
/*
	albumActions.innerHTML = "<div style='float: left'>";
	albumActions.innerHTML += "<a style='padding-right: 2em' href='#' class='action_link' onclick='removeImage()'>";
	albumActions.innerHTML += "<img style='vertical-align: middle;' border='0' src='/Control/Images/Remove-Selected-Image.jpg' /></a>";
	albumActions.innerHTML += "<a style='padding-right: 2em' href='#' class='action_link' onclick='openFileDialog()'>";
	albumActions.innerHTML += "<img style='vertical-align: middle;' border='0' src='/Control/Images/Add-Image.jpg' /></a>";
	albumActions.innerHTML += "</div><div style='float: right; padding-right: 3em;'>";
	albumActions.innerHTML += "<a href='#' class='action_link' style='padding-right: 1em;' onclick='removeAlbum()'><img style='vertical-align: middle;' border='0' src='/Control/Images/Remove-Album.jpg' /></a>";
	albumActions.innerHTML += "<a href='#' class='action_link' onclick='saveAlbum()'>";
	albumActions.innerHTML += "<img style='vertical-align: middle;' border='0' src='/Control/Images/Update-Album.jpg' /></a></div><div style='clear: both'></div>";
*/
	albumActions.innerHTML += "<div style='float: left'>";
	albumActions.innerHTML += "    <img onclick='removeImage()' style='cursor: hand; cursor: pointer;padding-right: .7em;' border='0' src='/Control/Images/Remove-Selected-Image.jpg' />";
	albumActions.innerHTML += "    <img onclick='openFileDialog()' style='cursor: hand; cursor: pointer;padding-right: 2em;' border='0' src='/Control/Images/Add-Image.jpg' />";
	albumActions.innerHTML += "    <img onclick='removeAlbum()' style='cursor: hand; cursor: pointer;padding-right: .7em;' border='0' src='/Control/Images/Remove-Album.jpg' />";
	albumActions.innerHTML += "    <img onclick='saveAlbum()' style='cursor: hand; cursor: pointer;' border='0' src='/Control/Images/Update-Album.jpg' />";
	albumActions.innerHTML += "</div>";
	albumActions.innerHTML += "<div style='clear: both'>";
	albumActions.innerHTML += "</div>";
	cleanWhitespace(albumActions);

	mainDiv.appendChild(albumDiv);
	albumDiv.appendChild(albumTitle);
	//albumDiv.appendChild(brEl);
	albumDiv.appendChild(albumActions);
	albumDiv.appendChild(albumImageList);
	//cleanWhitespace(albumDiv);
	debug(albumDiv.innerHTML);
	showAlbum(idSplit[2], idSplit[1]);	
	
	debug("resp: " + resp);
	hideInput(resp);

}

function showInput(el)
{

	var containerDiv = document.getElementById(el);
	if(containerDiv.style.display == "block")
	{
	
		document.getElementById(el).style.display = "none";
		
	}
	else
	{
	
		document.getElementById(el).style.display = "block";
	
	}
	

}

function hideInput(id)
{
	var idSplit = id.split(":");
	var galleryName = idSplit[0];
	
	id = "new_album_for_" + galleryName;
	var inpt = document.getElementById("input_" + id);

	inpt.value = "New Album Name";
	document.getElementById(id).style.display = "none";

}

function openFileDialog()
{

	
	var dialog = document.getElementById("add_image");
	dialog.style.display = "block";

}

function addNewImage(uploadFailed, uploadError, filename)
{

	debug("addNewImage: " + uploadFailed + " " + uploadError +  " " + filename);
	
	if(uploadFailed)
	{
	
		debug(uploadFailed);
		//displayUploadFeedback();
		if(filename != "")
		{
		
			alert("An error occurred while adding your image to the database:\n" + uploadError);
		
		}
	
	}
	else if(filename && filename != '')
	{
	
		addingImage = true;
		loadXMLDocPost(addingImageURL, "imageName=" + filename + "&albumId=" + lastShownAlbum + "&galleryId=" + currentlyViewingGalleryId);
	
	}
	
	//alert(uploadSuccessful + " " + filename);

}

function handleAddNewImage(resp)
{

	// Close File Dialog
	closeFileDialog();
	
	// Add the image to the album display
	respSplit = resp.split(":");
	
	var displayOrder = respSplit[0];
	var filename = respSplit[1];
	
	var imageList = document.getElementById("list_" + lastShownAlbum + "_" + currentlyViewingGalleryId);
	var liEl = document.createElement("li");
	var newImage = document.createElement("img");
	
	newImage.setAttribute("style", "position: relative; border: 1px Solid Grey; display: inline;");
	newImage.id = "image_" + respSplit[2];
	newImage.src = "/Images/Photos/" + respSplit[3].replace(" ", "%20") + "/" + lastShownAlbum + "/Thumbnails/med/" + filename;
	newImage.onclick = function() {
	
		selectImage(this);	
	
	};
	
	liEl.setAttribute("style", "float: left;margin-right: 10px; margin-bottom: 10px; position: relative; display: inline");
	liEl.appendChild(newImage);
	imageList.appendChild(liEl);

	dragsort.makeListSortable(document.getElementById("list_" + lastShownAlbum + "_" + currentlyViewingGalleryId));

}

function displayUploadFeedback()
{

	

}

function removeAlbum()
{

	removingAlbum = true;
	loadXMLDocPost(removeAlbumURL, "id=" + lastShownAlbum);

}

function handleRemoveAlbum(resp)
{

	// Remove the currently displayed album element
		
	
	// Adjust the last displayed album or reload page!
	location.reload();

}

function removeImage()
{

	debug(selectedImageId);
	removingImage = true;
	loadXMLDocPost(removeImageURL, "id=" + selectedImageId.replace("image_", ""));

}

function handleRemoveImage(resp)
{

	// Remove the currently displayed album element
		
	
	// remove image and list item
	var alist = document.getElementById("list_" + lastShownAlbum + "_" + currentlyViewingGalleryId);
	//debug(alist.childNodes.length);
	
	//cleanWhitespace(alist);
	for(var il = 0; il < alist.childNodes.length; il++)
	{
	
		//debug(il + " " + alist.childNodes[il].nodeType);
		if(alist.childNodes[il].firstChild.id == selectedImageId)
		{
			debug(alist.childNodes[il].firstChild.id + " " + selectedImageId);
			alist.removeChild(alist.childNodes[il]);
			//alist.childNodes[il].innerHTML
			
		
		}
		//var aItem = alist.childNodes[il];
		//debug(alist.childNodes[il].tagName);
		//aItem.firstChild.style.border = "1px Solid #666";
		
	
	}
	selectedImageId = "";
	
	

}

function saveAlbum()
{

	updatingAlbum = true;
	albumId = document.getElementById("input_album_name" + lastShownAlbum).id.replace("input_album_name", "");
	albumName = document.getElementById("input_album_name" + lastShownAlbum).value;
	
	
	// Get images and there new order
	var ids = "";
	var order ="";
	
	var alist = document.getElementById("list_" + lastShownAlbum + "_" + currentlyViewingGalleryId);
	//debug(alist.childNodes.length);
	
	cleanWhitespace(alist);
	for(var il = 0; il < alist.childNodes.length; il++)
	{
	
		//debug(il + " " + alist.childNodes[il].nodeType);
		if(alist.childNodes[il].tagName == "LI")
		{
		
			//debug(alist.childNodes[il].firstChild.tagName);
			//alist.childNodes[il].firstChild.style.border = "1px Solid #666";
			//alist.childNodes[il].firstChild.style.marginRight = "1em";
			//alist.childNodes[il].innerHTML
			order += il + ":";
			ids += alist.childNodes[il].firstChild.id.replace("image_", "") + ":";
			
		
		}
		//var aItem = alist.childNodes[il];
		//debug(alist.childNodes[il].tagName);
		//aItem.firstChild.style.border = "1px Solid #666";
		
	
	}
	
	loadXMLDocPost(updatingAlbumURL, "name=" + albumName + "&id=" + albumId + "&img_ids=" + ids + "&img_order=" + order);
	

}

function handleSaveAlbum(resp)
{

	// Remove the currently displayed album element
		
	
	// Adjust the last displayed album or reload page!

}

function closeFileDialog()
{

	var fileDlg = document.getElementById("add_image");
	fileDlg.style.display = "none";
	//alert("window closed");

}

function updateAlbumLink(inputEl)
{
	
	var albumId = inputEl.id.replace("input_album_name", "");
	var albumLink = document.getElementById("album_link_id_" + albumId);
	albumLink.innerHTML = inputEl.value;
	
}

function rollGalleryNumber(num, state, img)
{
	
	if(state == "on")
	{
	
		var filename = img.src;
		filename = filename.replace(/Default/, "Hover");
		img.src = filename;
		debug(filename);
	
	}
	else
	{
		
		var filename = img.src;
		filename = filename.replace(/Hover/, "Default");
		img.src = filename;
		debug(filename);
		
	}	
	
}

function fadingText(textNode)
{

		
	
}

function changeMainPhoto(replacement)
{

	var newPhoto = new Image();
	newPhoto.src = replacement;
	newPhoto.style.visibility = "hidden";
	newPhoto.onLoad = function()
	{
		
		//document.getElementById("full_display").style.width = this.offsetWidth;
		
	};
	var mainPhoto = document.getElementById("full_display");
	mainPhoto.innerHTML = "";
	//alert(newPhoto.offestWidth);
	mainPhoto.style.background = "url(" + replacement + ")";
	mainPhoto.style.backgroundPosition = "Center Center";
	mainPhoto.style.backgroundRepeat = "no-repeat";
	
	mainPhoto.appendChild(newPhoto);
	//mainPhoto.style.width = newPhoto.offestWidth;
	
}
