var hasClicked = true;
var photoToMove;
var redirect;

/* By Maurice Svay */
function getHTTPObject(callback)
{
	var xmlhttp = false;

	/* Compilation conditionnelle d'IE */
	/*@cc_on
	@if (@_jscript_version >= 5)
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}

	@else
	xmlhttp = false;
	@end @*/

	/* on essaie de créer l'objet si ce n'est pas déjà fait */
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}

	if (xmlhttp)
	{
		if ( callback )
			xmlhttp.onreadystatechange=
				function()
				{
					if (xmlhttp.readyState == 4)
						eval(callback+'(xmlhttp)');
				};
	}
	return xmlhttp;
}

function dragObserver()
{
	dragObserver.prototype.onStart = function(eventName, draggable, event)
	{
		hasClicked = false;
	}
}

function getId(element)
{
	var id = element.id;
	
	return id.substring(id.indexOf('-')+1);
}

function sendRequest(url, params, callback)
{
	var xmlhttp = getHTTPObject(callback); 
	xmlhttp.open("POST", url, true); 
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send(params);
}

function parseDropables(node)
{
	//var children = document.evaluate('//ul[@id=\'albumOverviewItems\']/li[@class=\'album\']', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	var children = document.getElementsByTagName('li');
	
	for ( var i=0 ; i < children.length ; i++ )
	{
		var classname;
		
		if ( children[i].className )
			classname = children[i].className;
		else
			classname = children[i].getAttribute('class');
		
		if ( classname && classname.indexOf('album')!=-1 && classname.indexOf('current')==-1 )
		{
			var id = children[i].id ? children[i].id : children[i].getAttribute('id');
			
			Droppables.add(children[i].getAttribute('id') ,
			{
				hoverclass:'dragOverAlbum',
				onDrop:processMove
			} );
		}
	}
}

function parseDraggables(node)
{
	//var children = document.evaluate('//ul[@id=\'albumOverviewItems\']/li[@class=\'album\']', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	var children = document.getElementsByTagName('li');
	
	for ( var i=0 ; i < children.length ; i++ )
	{
		var classname;
		
		if ( children[i].className )
			classname = children[i].className;
		else
			classname = children[i].getAttribute('class');
			
		if ( classname && classname.indexOf('draggablePhoto')!=-1 )
		{
			var id = children[i].id ? children[i].id : children[i].getAttribute('id');
			new Draggable(id, {revert:true} );
		}
	}
}

function processMove(draggable, dropable, event)
{
	var photo = getId(draggable);
	var album = getId(dropable);
	
	if ( album==0 )
		showDynamicForm('createAlbumForm', 'photo='+ photo);
	
	else
		movePhoto(photo,album);

	draggable.style.display = 'none';
}

function updateAlbumOverview(userId, albumId)
{
	sendRequest('/Scripts/AlbumController.php', 'ac=albumOverview&albumId='+ albumId +'&userId='+ userId, 'updateAlbumOverviewResponse');
}

function movePhoto(photoId, albumId)
{
	sendRequest('/Scripts/AlbumController.php', 'ac=movePhoto&album='+ albumId +'&photo='+ photoId, 'movePhotoResponse');
}

function movePhotoResponse(xmlhttp)
{
	updateAlbumOverview(currentUserId, currentAlbumId);
}

function updateAlbumOverviewResponse(xmlhttp)
{
	var element = document.getElementById('albumOverview');
	
	if (element)
	{
		element.innerHTML = xmlhttp.responseText;
		parseDropables(document);
	}
}

function checkAlbumRename(album, name)
{
	var element = document.getElementById(name+'Text');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=checkAlbumName&album='+ album +'&name='+ element.value, 'checkAlbumRenameResponse');
}

function checkAlbumRenameResponse(xmlhttp)
{
	var element = document.getElementById('renameHint');
	
	if ( element )
		element.innerHTML = xmlhttp.responseText;
}

function checkRenamePhoto(photo, name)
{
	var element = document.getElementById(name+'Text');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=checkPhotoName&photo='+ photo +'&name='+ element.value, 'checkRenamePhotoResponse');
}

function checkRenamePhotoResponse(xmlhttp)
{
	var element = document.getElementById('renamePhotoHint');
	
	if ( element )
		element.innerHTML = xmlhttp.responseText;
}

function renameAlbum(album, name)
{
	var element = document.getElementById(name+'Text');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=renameAlbum&album='+ album +'&name='+ element.value, 'renameAlbumResponse');
}

function renameAlbumResponse(xmlhttp)
{
	if ( xmlhttp.responseText.indexOf('location:')!=0 )
		checkNameResponse(xmlhttp);
		
	else if ( currentAlbumId )
		window.location = xmlhttp.responseText.substr(9);
		
	else
	{
		refreshAlbumView();
		hideForm();
	}
}

function renamePhoto(photo, name)
{
	var element = document.getElementById(name+'Text');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=renamePhoto&photo='+ photo +'&name='+ element.value, 'renamePhotoResponse');
}

function renamePhotoResponse(xmlhttp)
{
	if ( xmlhttp.responseText.indexOf('location:')!=0 )
		checkRenamePhotoResponse(xmlhttp);
		
	else if ( currentPhotoId )
		window.location = xmlhttp.responseText.substr(9);
		
	else
	{
		refreshPhotoView();
		hideForm();
	}
}

function mustClick()
{
	var tmp = hasClicked;
	hasClicked = true;
	
	return tmp;
}

function checkCreate(value)
{
	sendRequest('/Scripts/AlbumController.php', 'ac=checkAlbumName&name='+ value, 'checkCreateResponse');
}

function checkCreateResponse(xmlhttp)
{
	var element = document.getElementById('createHint');
	
	if ( element )
		element.innerHTML = xmlhttp.responseText;
}

function createAlbum(photo)
{
	var element = document.getElementById('newAlbumName');
	
	if ( element )
	{
		redirect = photo ? 0 : 1;
	
		if ( photo )
			sendRequest('/Scripts/AlbumController.php', 'ac=createAlbum&name='+ element.value +'&photo='+ photo, 'createAlbumResponse');
			
		else
			sendRequest('/Scripts/AlbumController.php', 'ac=createAlbum&name='+ element.value, 'createAlbumResponse');
	}
}

function createAlbumResponse(xmlhttp)
{

	if ( xmlhttp.responseText.indexOf('location:')!=0 )
		checkCreateResponse(xmlhttp);
		
	else
	{
		if ( ! currentAlbumId )
			refreshAlbumView(currentUserId);
		
		else if ( redirect==1 )
			window.location = xmlhttp.responseText.substr(9);
			
		else
			updateAlbumOverview(currentUserId, currentAlbumId);
			
		hideForm();
	}
}

function refreshAlbumView()
{
	sendRequest('/Scripts/AlbumController.php', 'ac=showAlbumView&user='+ currentUserId, 'refreshAlbumViewResponse');
}

function refreshAlbumViewResponse(xmlhttp)
{
	setElementHtml('albumList', xmlhttp.responseText);
}

function refreshPhotoView()
{
	sendRequest('/Scripts/AlbumController.php', 'ac=showPhotoView&album='+ currentAlbumId, 'refreshPhotoViewResponse');
}

function refreshPhotoViewResponse(xmlhttp)
{
	setElementHtml('photosThumbs', xmlhttp.responseText);
	parseDraggables(document);
}

function showForm(name)
{
	var arrayPageSize = getPageSize();
	
	var objOverlay = document.getElementById('overlay');
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	showElement(name);
}

function showRenameAlbumForm(album)
{
	var sendParams = 'ac=showRenameAlbumForm&album='+ album;
	sendRequest('/Scripts/AlbumController.php', sendParams, 'showDynamicFormResponse');
}

function showRenamePhotoForm(photo)
{
	var sendParams = 'ac=showRenamePhotoForm&photo='+ photo;
	sendRequest('/Scripts/AlbumController.php', sendParams, 'showDynamicFormResponse');
}

function showDynamicForm(name, params)
{
	var sendParams = 'ac='+ name + (params ? '&'+ params : '');
	sendRequest('/Scripts/AlbumController.php', sendParams, 'showDynamicFormResponse');
}

function showDynamicFormResponse(xmlhttp)
{
	setElementHtml('formBlock', xmlhttp.responseText);
	showForm('formBlock');
}

function switchDescription()
{
	showElement('descriptionForm');
	hideElement('albumDescriptionBlock');
}

function switchPhotoDescription()
{
	showElement('descriptionForm');
	hideElement('photoDescriptionBlock');
}

function cancelDescription()
{
	hideElement('descriptionForm');
	showElement('albumDescriptionBlock');
}

function cancelPhotoDescription()
{
	hideElement('descriptionForm');
	showElement('photoDescriptionBlock');
}

function saveDescription()
{
	var element = document.getElementById('description');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=setAlbumDescription&album='+ currentAlbumId +'&description='+ element.value, 'saveDescriptionResponse');
}

function saveDescriptionResponse(xmlhttp)
{
	hideElement('descriptionForm');
	showElement('albumDescriptionBlock');
	
	setElementHtml('albumDescription', xmlhttp.responseText);
}

function savePhotoDescription()
{
	var element = document.getElementById('description');
	
	if ( element )
		sendRequest('/Scripts/AlbumController.php', 'ac=setPhotoDescription&photo='+ currentPhotoId +'&description='+ element.value, 'savePhotoDescriptionResponse');
}

function savePhotoDescriptionResponse(xmlhttp)
{
	hideElement('descriptionForm');
	showElement('photoDescriptionBlock');
	
	setElementHtml('photoDescription', xmlhttp.responseText);
}

function hideForm()
{
	hideElement('overlay');
	hideElement('formBlock');
}

function setElementHtml(elementId, html)
{
	var element = document.getElementById(elementId);
	
	if ( element )
		element.innerHTML = html;
		
	return element;
}

function showElement(name)
{
	var element = document.getElementById(name);
	
	if ( element )
		element.style.display = 'block';
}

function hideElement(name)
{
	var element = document.getElementById(name);
	
	if ( element )
		element.style.display = 'none';
}

var clickObserver = new dragObserver();
Draggables.addObserver(clickObserver);
