/*
jQuery plugin for NetMaverick pagelet div's.

The XSL template using this code should have the following HTML tag in it. 

<div id="poll" pagelet="pagelet_survey" onload="ksSetupVoting('poll', 'pagelet_survey');"></div>

The <div/> is the container that will house the pagelet (any other container element should also work).
The innerHTML of the <div/> will be filled using an AJAX call to ws_display.asp.

Required attributes:
	@id:  code will use this to identify the <div/> to append the resulting pagelet HTML.
	@pagelet:  the name of the page in NetMaverick that will generate the pagelet HTML.  This may be optional if calling the update method of the plugin (see provided usage examples).
	
Optional attributes:
	@onload:  when the AJAX call is complete, whatever is in the onload will be called.

Example usage:
	$('#my_div').ksPagelet();		// Applies to a single div (assumes div has pagelet attribute)
	$('div[pagelet]').ksPagelet();	// Applies to 1 or more divs (assumes each div has pagelet attribute)
	$('#my_div').ksPagelet('update', 'the_pagelet_name');	// div with no pagelet attribute.
*/
(function($) {
	var methods = {
		init : function(options) {
			//var defaults = {
			//	'pagelet' : this.attr('pagelet'),
			//	'onload' : this.attr('onload')
			//};
			
			// Return the elements for jQuery chainability
			return this.each(function(index) {
				//if (options) { 
				//	$.extend(defaults, options);
				//}
				ksGetPage($(this));
			});
		},
		update : function(pagelet, onload) {
			ksGetPage(this, pagelet, onload);
		}
	};
	$.fn.ksPagelet = function(method) {
		if (methods[method]) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if (typeof method === 'object' || ! method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' +  method + ' does not exist on jQuery.ksPagelet');
		}
	};
	// Private function only available inside this plugin.
	function ksGetPage(el, pagelet, onload) {
		var error = false;
		
		container = el.attr('id');
		pagelet = (pagelet == null) ? el.attr('pagelet') : pagelet;
		onload = (onload == null) ? el.attr('onload') : onload;
		
		$.ajax({
			url: 'ws_display.asp?filter='+pagelet,
			type: 'GET',
			dataType: 'html',
			async: false,
			error: function() {
				error = true;
				$('#'+container).html('<span style="display:inline-block;color:#ff0000;font-style:italic;border:1px dotted #ff0000;padding:4px;">ERROR: Communication timeout for pagelet "' + pagelet + '" on container "' + container + '". Please try again later!</span>');
			},
			success: function(msg){
				$('#'+container).html(msg);
			},
			complete: function() {
				if (onload != null && onload != '' && !error) {
					try {
						eval(onload);
					}
					catch(e) {
						$('#'+container).html($('#'+container).html() + '<span style="display:inline-block;color:#ff0000;font-style:italic;border:1px dotted #ff0000;padding:4px;">ERROR: Execution of "' + onload + '" failed on container "' + container + '" with pagelet "' + pagelet + '"!</span>');
					}
				}
			}
		});	
	};
})(jQuery);
/* This is the non-plugin version
$(document).ready(function() {
	$('div[pagelet]').each(function(index) {
		ksGetPagelet($(this).attr('id'), $(this).attr('pagelet'), $(this).attr('onload'));
	});
});
function ksGetPagelet(pageletContainer, pagelet, onload) {
	$.ajax({
		url: 'ws_display.asp?filter='+pagelet,
		type: 'GET',
		dataType: 'html',
		async: false,
		error: function() {
			$('#'+pageletContainer).html('<span style="display:inline-block;color:#ff0000;font-style:italic;border:1px dotted #ff0000;padding:4px;">Error: Communication timeout for pagelet "' + pagelet + '". Please try again later!</span>');
		},
		success: function(msg){
			$('#'+pageletContainer).html(msg);
		},
		complete: function() {
			if (onload != null) {
				eval(onload);
			}
		}
	});	
}
*/
