(function( $ ){
	// plugin declarations
	$.fn.collapsableMenu = function( options ) 
	{  
		// default options
		var settings = 
		{
		  'expandImage'         : '/PublishingImages/expand.gif',
		  'collapseImage' 		: '/PublishingImages/collapse.gif',
		  'dashImage' 			: '/PublishingImages/dash.gif' 
		};
		
		// always override defaults with any new values passed in
		if ( options ) 
		{ 
			$.extend( settings, options );
		}	
		var $this = $(this);
		RecursiveCollapsableMenu($this, 1, settings);
		
		// return the object itself to keep inline with jquerys cascading model
		return $this;
	};
  

	$.fn.collapsableDivs = function( options ) 
	{
		// default options
		var settings = 
		{
		  'expandImage'         : '/PublishingImages/expand.gif',
		  'collapseImage' 		: '/PublishingImages/collapse.gif',
		  'backgroundRepeat'	: '',
		  'backgroundColor'		: '',
		  'backgroundPosition'	: '',
		  'cursor'				: ''  
		  
		};
		
		// always override defaults with any new values passed in
		if ( options ) 
		{ 
			$.extend( settings, options );
		}	
		var $this = $(this);
		
		CollapsableDivs($this, settings);
		
		// return the object itself to keep inline with jquerys cascading model		
		return $this;

	};
	
	// private functions
	function RecursiveCollapsableMenu(control,iteration, settings)
	{
		var iterate = false;
		$(control).find('li.lvl' + iteration).each(function()
		{	
			var haschildren = $(this).find('ul.lvl' + (iteration + 1));
			if(haschildren  && haschildren.length  > 0)
			{
				// if class contains selected, we are at this site so expand the current node
				var imageName = settings.expandImage;
				if($(this).attr('class').indexOf('selected') > -1)
				{
					imageName = settings.collapseImage;
					$(this).children('ul').css('display','block');
					
				}
				
				$(this).prepend("<div class=\"imageholder\"><img src='" + imageName + "' /></div>");
				$(this).children('.imageholder').children('img').css('cursor','pointer');			
				$(this).children('.imageholder').children('img').click(function()
				{
					$(haschildren).slideToggle('slow');
					var name = $(this).attr('src');
					if(name.indexOf(settings.expandImage) > -1)
					{
						$(this).attr('src', settings.collapseImage);
					}
					else			
					{
						$(this).attr('src', settings.expandImage);
					}				
				});
				iterate = true;
			}
			else
			{
				// if item is "selected" make sure parents up the tree are displayed
				if($(this).attr('class').indexOf('selected') > -1)
				{ 		
					if($(this).parent().length > 0 && $(this).parent().attr('class').indexOf('menu') > -1)
					{
						$(this).parent().css('display','block');
						$(this).parent().parent().children('.imageholder').children('img').attr('src', settings.collapseImage);
					}
				}		
				
				$(this).prepend("<div class=\"imageholder\"><img src='" + settings.dashImage + "' /></div>");			
			}		
		});
		if(iterate)
		{			
			RecursiveCollapsableMenu(control, iteration + 1, settings);
		}	
	};
  
	// allows div to be collapsable by a click
	function CollapsableDivs(control, settings)
	{
		$(control).each(function()
		{
			var parent = $(this);	
			var title = parent.attr('title');	
			var className = $(this).attr('class');
			
			$(this).wrap("<div></div>");
			$(this).before("<div class='collapsebar'>" + title + "&nbsp;</div>");					
			
			SetStyle($(this), settings);
						
			if(className.indexOf('collapsed') > - 1)
			{
				$(this).css('display','none');
				$(this).parent().children('.collapsebar').css('background-image',"url('" + settings.expandImage + "')");
			}

			if(title)
			{
				title = removeSpaces(title);
				var status = 'open';
				if($.cookie)
				{
					status = $.cookie(title);
				}
							
				if(status)
				{
					if(status == 'closed')
					{
						$(this).parent().children('.collapsebar').css('background-image',"url('" + settings.expandImage + "')");
						$(this).css('display','none');
					}
				}
			}
			
			$(this).parent().children('.collapsebar').click(function()
			{
				var image = $(this).parent().children('.collapsebar').css('background-image');
				if(image.indexOf(settings.collapseImage) > -1)
				{
					$(this).parent().children('.collapsebar').css('background-image',"url('" + settings.expandImage + "')");
					if(title)
					{
						SetCookie(title, 'closed');
					}
				}
				else
				{
					$(this).parent().children('.collapsebar').css('background-image',"url('" + settings.collapseImage + "')");
					if(title)
					{
						SetCookie(title, 'open');
					}
				}
				$(parent).slideToggle('slow');
			});
		});
	};
	
	//removes space from the passed in string
	function removeSpaces(string) 
	{
	 return string.split(' ').join('');
	};
	
	// sets the cookie so that collapsed div remains closed on page load depending on user preferences
	function SetCookie(title, status)
	{
		if($.cookie)
		{
			var expires = 999 * 1000 * 60 * 60 * 24;
			var today = new Date();
			var expires_date = new Date( today.getTime() + (expires) );
			$.cookie(title, status, {expires:expires_date.toGMTString(),path:'/'});
		}
	};
	
	// sets the style of the control based on the settings provided
	function SetStyle(control, settings)
	{
		if(settings.backgroundRepeat)
		{
			control.parent().children('.collapsebar').css('background-repeat', settings.backgroundRepeat);
		}
		if(settings.backgroundColor)
		{
			control.parent().children('.collapsebar').css('background-color', settings.backgroundColor);
		}
		if(settings.backgroundImage)
		{
			control.parent().children('.collapsebar').css('background-image', 'url(' + settings.backgroundImage + ')');
		}
		if(settings.backgroundPosition)
		{
			control.parent().children('.collapsebar').css('background-position', settings.backgroundPosition);
		}
		if(settings.cursor)
		{
			control.parent().children('.collapsebar').css('cursor', settings.cursor);
		}
		
	};

})( jQuery );
