/**
 * fbml_loader()
 *
 * Loads fbml elements asyncronously for an upload item and for a given page
 *
 * PAGE PARAMETER
 * Pass either a page name, or the keyword 'generic' to this parameter to load widgets associated with a particular page
 * (if nothing is passed, 'generic' will be assumed)
 *
 * LOADING WIDGETS
 * Pass the name of a widget to the third parameter, to load it
 * Alternatively, if a null value or the keyword 'all' is passed, then all the widgets associated with a specific page will be passed back as a javascript object to the function
 *
 * CALLBACK
 * This is the callback parameter for the function and thus must be passed another function, manipulating the data retrieved.
 * Returned data is in the format { 'tool' : 'like_button', 'fbml' : '<fb:ml>...</fb:ml>' } ...
 * ...and so manipulating this data will often take this format:
 *
 * function(data) {
 *		$each(data, function(i, item){
 *			if(item.tool == 'like_button')
 *			$('.like_button_holder).html(item.fbml)
 *		});
 * });
 *
 * @param integer uploadid
 * @param string page
 * @param string widget
 * @return object
 *
 */
function fbml_loader(uploadid, page, widget, callback) {

	var url = base_url()+'ajax/fbml_loader/'+uploadid;

		if ((page !== '') && (widget !== ''))
			url = url + '/' + page + '/' + widget;
		else if ((page !== '') && (widget == ''))
			url = url + '/' + page;
		else if ((page == '') && (widget !== ''))
			url = url + '/generic/' + widget;

		$.ajax({
			type: 'GET',
			dataType: 'json',
			url: url,
			success: callback
		});

}


/**
 * comments_load_stagger()
 *
 * A Very Short History
 * - - - - - - - - - - - - -
 * 1. RENDERING ON DOCUMENT.READY
 *		Comments used to be loaded simultaneously with the like button, i.e. on document.ready but this caused strange behaviour in the like button, where it didn't remember any 'likes'
 *
 * 2. RENDERING ONCLICK OF BUTTON
 *		Comments box was then loaded as and when a user required it, through the press of a button in the DOM,
 *		but this, whilst saving space, did not give enough prominence to the comments box and thus didn't prompt the user to use it.
 *
 * 3. STAGGERED RENDERING
 *		The button idea was scrapped, but the problems of the alternative document.ready method came back, along with the problem of the comments box sometimes not rendering properly
 *
 *	If we stagger the times at which these elements (like button and comments) are loaded then all problems seem to be absent.
 *
 */

 function comments_load_stagger(vid_id, page, timeout) {

	if(timeout == null)
		timeout = 500;

	window.setTimeout(function () {
	fbml_loader(vid_id, page, 'comments_box', function(data){

		$.each(data, function(i, item) {
			$('.comments_box_holder').html(item.fbml);
		});

		FB.XFBML.parse();

	});
	}, timeout);
 }