(function($) {
	$.fn.blowfish = function() {
		// hide original ul dom tree
		$(this).hide();
	  
	    // create container from top-level li tags
	    var top = $(this).children('li');
	    var container = $('<div/>').addClass('bfcontainer').attr('id', 'cv' + Math.floor(Math.random()*10e10)).insertAfter(this);
	    var topdiv = $('<div class="top"></div>').appendTo(container);
	    
	    // check if IE and set fixed width for first column
	    if($.browser.msie) {
	    	$('.top').width('200px');
	    }
	   
	    $.each(top, function(i, item) {
	    	var topitem = $(':eq(0)', item).clone().data('sub', $(item).children('ul')).appendTo(topdiv);
	    
	    	if($(topitem).data('sub').length) {
	    		$(topitem).addClass('hasChildMenu');
	    		
	    		if($.browser.safari) {
	    			$(topitem).css({'margin-right' : '15px'});
	    		}
	    	}
	    });
	    
	    // event handlers
	    $('a', container).live('click', function() {
	    	var container = $(this).parents('.bfcontainer');
	    	
	    	// click handler
	    	var level = $('div', container).index($(this).parents('div'));
	    	
	    	// remove blocks to the right in the tree, and 'deactivate' other links within the same level
	    	$('div:gt('+level+')', container).remove();
	    	$('div:eq('+level+') a', container).removeClass('active').removeClass('inpath');
	    	$('.active', container).addClass('inpath');
	      	$(this).addClass('active');
	      
	      	if($(this).data('sub').children('li').length) {
	      		// add submenu if container has children
	      		submenu(container, this);
	      	}
	      	else {
	      		// load ajax portfolio
	      		$('#pictures').load($(this).attr('href'), function() {
	      			$(this).cycle({ 
						fx: 'fade', 
						speed: 'slow', 
						timeout: 0, 
						next: '#next2', 
						prev: '#prev2' 
					});	
	
	      		});
	      	}
	      	
	      	return false;
		});
	};

	// create submenus
	function submenu(container, item) {
		var leftPos = 0;
		
		$.each($(container).children('div'), function(i, mydiv) {
			leftPos += $(mydiv).width();
		});
		
		var submenu = $('<div/>').css({'top' : 0, 'left' : leftPos}).appendTo(container).fadeIn();
	
		// check if IE and set fixed width for submenu column
		if($.browser.msie) {
			$(submenu).width('200px');
		}
		
		var subitems = $(item).data('sub').children('li');
	
		$.each(subitems, function(i, subitem) {
			var subsubitem = $(':eq(0)', subitem).clone().data('sub', $(subitem).children('ul')).appendTo(submenu);
	
			if($(subsubitem).data('sub').length) {
				$(subsubitem).addClass('hasChildMenu');
				
				if($.browser.safari) {
					$(subsubitem).css({'margin-right' : '15px' });
				}
			}
		});
	}  
})(jQuery);