/*
 * lib_photo_upload.js	:	Photo functions (Upload, Delete, etc.)
 *
*/

function photo_edit(photoid)
{
	var btn_edit = getElement('omchat_photo_edit'+photoid);
	var btn_edit_cancel_container = getElement('omchat_photo_edit_cancel_container'+photoid);
	var photo_info_basic_container = getElement('omchat_photo_info_basic_container'+photoid);
	var photo_info_container = getElement('omchat_photo_info_container'+photoid);
	var title_edit_em = getElement('omchat_title_edit'+photoid);
	var description_edit_em = getElement('omchat_description_edit'+photoid);

	// Edit
	if( btn_edit_cancel_container.style.display == 'none' )
	{
		photo_info_basic_container.style.display = 'none';
		photo_info_container.style.display = 'none';
		getElement('omchat_photo_edit_container'+photoid).style.display = 'block';
		getElement('omchat_photo_info_basic_edit_container'+photoid).style.display = 'block';
		title_edit_em.focus();

		btn_edit.innerHTML = 'Save Changes';
		btn_edit_cancel_container.style.display = 'inline';
	}
	// Save Changes
	else
	{
		btn_edit.innerHTML = 'Saving...';
		btn_edit_cancel_container.style.display = 'none';

		var tags_str = getElement('omchat_tags_edit'+photoid).value;

		// asynchronously save changes
		xmlhttp_request('/CMS/php/xml_photo_edit.php', 'photoid='+photoid+'&title='+encodeURIComponent(title_edit_em.value)+'&description='+encodeURIComponent(description_edit_em.value)+'&tags_str='+encodeURIComponent(tags_str), 'POST', photo_edit_processxml, xmlhttp_error, true, true);
	}
}

// process resulting xml from `/xml_photo_edit.php` above
function photo_edit_processxml(xml)
{
	/*
	 * XML output format
	 *
	 *	<?xml version="1.0" ?>
	 *	<items>
     *		<item>
     *			<photoid></photoid>
     *			<title></title>
     *			<description></description>
     *			<tags_html></tags_html>
	 *		</item>
	 *	</items>
	*/

	// get the items in the xml document
	var items = xml.documentElement.getElementsByTagName('item');

	var photoid = getNodeValue(items.item(0), 'photoid');
	var title = getNodeValue(items.item(0), 'title');
	var description = getNodeValue(items.item(0), 'description');
	var tags_html = getNodeValue(items.item(0), 'tags_html');

	var btn_edit = getElement('omchat_photo_edit'+photoid);
	var btn_edit_cancel_container = getElement('omchat_photo_edit_cancel_container'+photoid);
	var description_em_em = getElement('omchat_description_em'+photoid);
	var photo_info_basic_container = getElement('omchat_photo_info_basic_container'+photoid);
	var photo_info_container = getElement('omchat_photo_info_container'+photoid);
	var photo_edit_container = getElement('omchat_photo_edit_container'+photoid);
	var title_em = getElement('omchat_title'+photoid);
	var description_em = getElement('omchat_description'+photoid);

	photo_info_basic_container.style.display = 'block';
	photo_info_container.style.display = 'block';
	photo_edit_container.style.display = 'none';
	btn_edit_cancel_container.style.display = 'none';

	title_em.firstChild.innerHTML = title;
	description_em_em.innerHTML = description;

	document.getElementById('omchat_tags'+photoid).innerHTML = tags_html;

	if( document.getElementById('omchat_photo_tag_container'+photoid) != null )
		btn_edit.innerHTML = 'Tag Photo';
	else
		btn_edit.innerHTML = 'Edit Photo';
}

function photo_edit_cancel(photoid, bFullEdit)
{
	var btn_edit = getElement('omchat_photo_edit'+photoid);
	var btn_edit_cancel_container = getElement('omchat_photo_edit_cancel_container'+photoid);
	var photo_info_basic_container = getElement('omchat_photo_info_basic_container'+photoid);
	var photo_info_container = getElement('omchat_photo_info_container'+photoid);
	var photo_edit_container = getElement('omchat_photo_edit_container'+photoid);

	// Discard changes
	btn_edit_cancel_container.style.display = 'none';
	photo_info_basic_container.style.display = 'block';
	photo_info_container.style.display = 'block';
	photo_edit_container.style.display = 'none';

	if( bFullEdit )
	{
		btn_edit.innerHTML = 'Edit Photo';
	}
	else
	{
		btn_edit.innerHTML = 'Tag Photo';
	}
}

function photo_tag(photoid)
{
	var btn_edit = getElement('omchat_photo_edit'+photoid);
	var btn_edit_cancel_container = getElement('omchat_photo_edit_cancel_container'+photoid);
	var photo_info_basic_container = getElement('omchat_photo_info_basic_container'+photoid);
	var photo_info_container = getElement('omchat_photo_info_container'+photoid);
	var photo_edit_container = getElement('omchat_photo_edit_container'+photoid);
	var tags_edit_em = getElement('omchat_tags_edit'+photoid);
	var title_edit_em = getElement('omchat_title_edit'+photoid);
	var description_edit_em = getElement('omchat_description_edit'+photoid);

	// Edit
	if( btn_edit_cancel_container.style.display == 'none' )
	{
		photo_info_basic_container.style.display = 'none';
		photo_info_container.style.display = 'none';
		//photo_edit_container.style.display = 'block';
		btn_edit_cancel_container.style.display = 'inline';
		photo_edit_container.style.display = 'block';
		tags_edit_em.focus();

		btn_edit.innerHTML = 'Save Changes';
		btn_edit_cancel_container.style.display = 'inline';
	}
	// Save Changes
	else
	{
		btn_edit.innerHTML = 'Saving...';
		btn_edit_cancel_container.style.display = 'none';

		var tags_str = tags_edit_em.value;

		// asynchronously save changes
		xmlhttp_request('/CMS/php/xml_photo_edit.php', 'photoid='+photoid+'&title='+encodeURIComponent(title_edit_em.value)+'&description='+encodeURIComponent(description_edit_em.value)+'&tags_str='+encodeURIComponent(tags_str), 'POST', photo_edit_processxml, xmlhttp_error, true, true);
	}
}

function photo_delete(photoid)
{
	if( confirm('Delete this photo?') )
	{
		var container = getElement('omchat_photo_delete_container'+photoid);
		container.innerHTML = '<p class="info" style="display:inline;">Deleting...</p>';

		xmlhttp_request('/CMS/php/xml_photo_delete.php', 'photoid='+photoid, 'POST', photo_delete_processxml, xmlhttp_error, true, true);
	}
}

function photo_delete_processxml(xml)
{
	/*
	 * XML output format
	 *
	 *	<?xml version="1.0" ?>
	 *	<items>
     *		<item>
     *			<error></error>
     *			<msg></msg>
     *			<photoid></photoid>
	 *		</item>
	 *	</items>
	*/

	// get the items in the xml document
	var items = xml.documentElement.getElementsByTagName('item');
	
	var error = getNodeValue(items.item(0), 'error');
	var msg = getNodeValue(items.item(0), 'msg');
	var photoid = getNodeValue(items.item(0), 'photoid');

	var container = getElement('omchat_photo_delete_container'+photoid);
	container.innerHTML = '<input type="button" class="omchat_button_simple2" value="'+msg+'" disabled="disabled" />';

	var container_all = getElement('omchat_photo_container'+photoid);
	container_all.style.display = 'none';
}

// Set user's profile picture to photo with given id
function photo_set_profile_picture(photoid)
{
	var button = getElement('omchat_photo_set_profile_picture_btn'+photoid);
	button.innerHTML = 'Setting Profile Picture...';
	button.disabled = true;

	xmlhttp_request('/CMS/php/xml_photo_set_profile_picture.php', 'photoid='+photoid, 'POST', photo_set_profile_picture_processxml, xmlhttp_error, true, true);
}

function photo_set_profile_picture_processxml(xml)
{
	/*
	 * XML output format
	 *
	 *	<?xml version="1.0" ?>
	 *	<items>
     *		<item>
     *			<photoid></photoid>
	 *		</item>
	 *	</items>
	*/

	// get the items in the xml document
	var items = xml.documentElement.getElementsByTagName('item');
	
	var photoid = getNodeValue(items.item(0), 'photoid');

	var button = getElement('omchat_photo_set_profile_picture_btn'+photoid);
	button.innerHTML = 'Profile Picture Set';
}

function photo_upload_form_submit()
{
	var progress = getElement('omchat_photo_upload_progress');

	var file = getElement('omchat_photo_upload_file').value;

	var bValidated = false;
	if( file.length == 0 )
	{
		progress.innerHTML = 'Please browse for your photo.';
	}
	else if( !getElement('omchat_photo_upload_rights_1').checked )
	{
		progress.innerHTML = 'Please make sure you have rights to this photo.';
	}
	else
	{
		bValidated = true;
	}
	
	if( bValidated )
	{
		progress.innerHTML = 'Processing...';

		getElement('omchat_photo_upload_form').submit();
	}

	progress.style.display = 'inline';

	return bValidated;
}