/**
 * Catalog Plugin written for Klett und Balmer Verlag Zug
 *
 * Plugin to handle the left side catalog navigation
 * structure (expanding|contracting) and highlighting.
 *
 * @author michael.kuck@h2g.ch
 * @version v1.00 13-01-2011
 */
(function($) {
	// set global options default vars
	var o = false;
	// define the catalog navigation plugin
	$.fn.catalog = function(options) {
		// build main options before initial procedure
		var opts = $.extend({}, $.fn.catalog.defaults, options);
		// initalize catalog plugin
		return this.each(function() {
			var $this = $(this);
			// build elemetn specific options
			o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			// enabling click on top level navigation nodes (slide toggle)
			$(this).find("ul li").not("ul li ul li").each(function() {
				var li = $(this);	$(li).children("a").bind('click', function() {
					if (o.topref) {
						// sliding up open slide contents
						$($this).find(".open").not("ul li ul li .open").each(function() {
							$(this).slideUp("fast", function() {
								$(this).removeClass('open').addClass('closed');
							});
						});
						// sliding down current tree node
						$(this).next(".closed").slideDown("fast", function() {
							$(this).removeClass('closed').addClass('open');
						}); return false;
					} else { return true; }
				});
			});
			// iterating each toggle element
			$(this).find(o.toggle).each(function() {
				// adding hover animation effects
				$(this).hover(function() {
					$(this).animate({
						color: o.color['on']
					}, o.speed, function() {});
					var src = $(this).find('img').attr('src');
					$(this).find('img').attr({
						'src': src.replace(/_off/g, "_on")
					});
				}, function() {
					$(this).animate({
						color: o.color['off']
					}, o.speed, function() {});
					var src = $(this).find('img').attr('src');
					$(this).find('img').attr({
						'src': src.replace(/_on/g, "_off")
					});
				});
				// adding toggle click effects
				$(this).bind('click', function() {
					var ul = $(this).parent().parent().find('ul');
					var src = $(this).find('img').attr('src');
					var txt = $(this).html();
					// expanding additional list entries
					if ($(ul).attr('class') == 'closed') {
						$(this).html(txt.replace(/einblenden/, "ausblenden"));
						$(this).find('img').attr({'src': src.replace(/_down/, "_up")});
						$(ul).slideDown("fast", function() {
							$(ul).removeClass('closed').addClass('open');
						});
					} else if ($(ul).attr('class') == 'open') {
						$(this).html(txt.replace(/ausblenden/, "einblenden"));
						$(this).find('img').attr({'src': src.replace(/_up/, "_down")});
						$(ul).slideUp("fast", function() {
							$(ul).removeClass('open').addClass('closed');
						});
					}
				});
			});
		});
	};
	function debug($msg) {
		if (window.console && window.console.log)
			window.console.log('debug: '+$msg);
	}
	$.fn.catalog.defaults = {
		topref: true,
		toggle: '.toggle',
		speed: 150,
		color: {
			'on': '#ff0000',
			'off': '#646464'
		}
	}
})(jQuery);
