/**
 * Page module: Virtual Post-its
 *
 * This module allows you to send virtual post its to other users.
 * Requires some modification in the index.php file of the template and frontend login enabled.
 *
 * This file includes the Javascript code of the Postits module
 * 
 * LICENSE: GNU General Public License 3.0
 * 
 * @author		Christian Sommer (doc)
 * @copyright	(c) 2006-2009
 * @license		http://www.gnu.org/licenses/gpl.html
 * @version		0.30
 * @platform	Website Baker 2.7
 *
*/

// Postits - global settings
var Postits = { 
	'Timer' : '',			// internal Postits timer variable
	'Interval' : 60000,		// check every xx milliseconds for new Postits (5000 ms := 5 sec)
	'ActivateTimer' : 1,	// 1.. activate timer (set to other value for debugging)
	'ShowMax' : 5			// maximum number of Postits shown at once
};

// function is executed if DOM is ready
$(document).ready(function() 
{
	// if ready, check for news Postits
	checkForPostits();

	// only enable timer if not in debug mode
	if (Postits.ActivateTimer == 1) {
		Postits.Timer = window.setInterval('checkForPostits()', Postits.Interval);
	}
});

// function to check if Postits needs to be displayed
function checkForPostits()
{
	// create Ajax call to check if Postits are available
	$.ajax({
		type: 'POST',
		url: WB_URL + '/modules/postits/get_postits.php',
		data: 'action=check_postits&show=' + Postits.ShowMax,
		dataType: 'json',
		
		success: function(result){
			if (result && result.Id.length > 0) {
				addPostits(result);
			}
		}
	});
}

// function to add dragable Postit to the document tree from JSON object
function addPostits(result)
{
	if (!(result && result.Id.length > 0)) return;
	
	// loop over JSON object and add Postits to document tree
	postits = result.Id.length;
	
	for (i = 0; i < postits; i++) {
		// only add Postit if not already added in previous loop
		if ($('#postit_' + i).length > 0) continue;
		
		// create a div container for each postit
		$('<div id="postit_' + i + '" class="postits"></div>')
		.append('<a href="#"></a>')
		.append('<h1>Postit ' + (i + 1) + '</h1>')
		.append('<p>' + result.Id[i].message + '</p>')
		.append('<p class="sendby">' + result.Id[i].sender + '</p>')
		.appendTo('body')
		.hide().fadeIn('slow')
		.draggable({'containment' : 'html'});

		// assign click function to remove Postit from the DOM
		$('#postit_' + i + ' a').click(function()
		{
			// get postit_id from div id and pass over to remove function
			var id = $(this).parents('div.postits').attr('id');
			id = id.substring(7);
			removePostits(id);
		});
	}
}

// function to remove a Postit from the document tree
function removePostits(id)
{
	// create Ajax call to delete Postit from database
	$.ajax({
		type: 'POST',
		url: WB_URL + '/modules/postits/delete_postits.php',
		data: 'action=delete&postit_id=' + id + '&show=' + Postits.ShowMax,
		dataType: 'json',
		
		success: function(result){
			if (result && result.status == 'ok') {
				$('#postit_' + id).fadeOut('def'), function()
				{
					$(this).remove();
				}
			} else {
				alert('Unable to delete Postit. Refresh browser (F5) and try again.');
			}
		}
	});
}
