// Widgets 
(function($) {
		  
	// jQuery plugin definition.
	$.fn.widget = function(options, callback) {
		
		// Set default parameters.
		defaults = {
			Open: true,
			Toggle: true,
			Close: false,
			Hide: false,
			Padding: true,
			Selectable: false,
			Share:  { toggle: false, url:'', script:'' },
			Delete: { toggle: false, url: '', script: '' },
			Icon: { toggle: false, img: ''} // doesn't work yet
		}
			
		// Extend our default options with those provided.
		options = $.extend(defaults, options);
		
		// Traverse all nodes
		this.each(function() {
		
			// Assign nodes and attibutes to jQuery objects.
			var $t = $(this);
			var $id = $t.attr('id');
			var title = $t.attr('title');
			var content = $t.html();
			
			// If Toggle is true, show the toggle arrow and execute toggle functions.
			if(options.Toggle == true) {
				Toggle = '<a href="#" class="toggle-arrow" title="Toggle"></a>';
				
				// Open or close a dropBox when you click its H2.		  
				$('#' + $id + ' h2').live("click", function() {
					$(this).next().slideToggle(200);
					$(this).parent().children('a.toggle-arrow').toggleClass('open');
					return false;
				});
				
				// Open or close a dropBox when you click its toggle arrow.
				$('#' + $id + ' a.toggle-arrow').live("click", function() {
					$(this).parent().children('div.content').slideToggle(200);
					$(this).toggleClass('open');	
					return false;
				});
			} else {
				Toggle = '';	
			}
			
			// If Hide is true, hide the widget.
			if(options.Hide == true) {
					$('#' + $id).hide();
			}
			
			// If Close is true, show a close button instead of a toggle arrow and allow the widget to be closed.
			if(options.Close == true) {
				Close = '<a href="#" class="close" title="Close">Close</a>';
				Toggle = '';
				$('#' + $id + ' a.close').live("click",function() {
					$('#' + $id).hide();
					return false;
				});
			} else {
				Close = '';	
			}
			
			// If Selectable is true, put a checkbox beside the widget's title.
			if(options.Selectable == true){
				Select = '<input type="checkbox" class="select" />';	
			} else {
				Select = '';	
			}
			
			// If Share is true, put a "share this" link on the right side of the widget title bar.
			if(options.Share.toggle == true){
				Share = '<a href="'+options.Share.url+'" class="share" onclick="'+options.Share.script+';return false;'+'">Share This</a>';
			} else {
				Share = '';	
			}
			
			// If Share is true, put a "delete this" link on the right side of the widget title bar	.
			if(options.Delete.toggle == true){
				Delete = '<a href="'+options.Delete.url+'" class="delete" onclick="'+options.Delete.script+';return false;'+'">Delete This</a>';	
			} else {
				Delete = '';	
			}
			
			
			// Create the widget
			$('#' + $id).html('<div class="widget" id="'+$id+'">'+Select+Close+Toggle+Delete+Share+'<h2>'+title+'</h2><div class="content">'+content+'</div></div>');
			
			if(options.Icon.toggle == true){
				$('#' + $id + ' h2').addClass('icon');
				$('#' + $id + ' h2').css('background-image', 'url('+ options.Icon.img +')');	
			}
			
			// If Padding is false, remove the padding on the content div.
			if(options.Padding == false){
				$('#' + $id + ' div.content').addClass("nopad"); 
			}

			// If Open is false, hide the content div and put the toggle arrow in the closed position.
			if(options.Open == false) {
				$('#' + $id + ' div.content').hide();
				$('#' + $id + ' h2').children('a.toggle-arrow').toggleClass('open');
			}
			
			$('div.dropBox > h3').live("click", function(){
				$(this).next().slideToggle(200);
				$(this).toggleClass('open');
				$(this).children('a.toggle-arrow').toggleClass('open');
				return false;			
			});
		
		});
	
		// Allow jQuery chaining.
		return this;
	};
})(jQuery);