(function ($) {
	$(function ($) {
		var minus = 'minus.png';
		var plus = 'plus.png';
		if (msie6) {
			minus = 'minus.gif';
			plus = 'plus.gif';
		}
		$("body").delay(1000, function () {
			$("a.menu-toggle").click(function () {
				$(this).next().slideToggle("fast");
				if ($(this).find('img').attr('rel') == "closed") {
					$(this).find('img').attr('src', "/includes/templates/djbpens09/images/" + minus);
					$(this).find('img').attr('rel', "open");
				} else {
					$(this).find('img').attr('src', "/includes/templates/djbpens09/images/" + plus);
					$(this).find('img').attr('rel', "closed");
				}
			}).next().hide().slideUp();
			var cPathTmp = cPath.concat();
			while (cPathTmp.length > 0) {
				var path = '#menu_' + cPathTmp.join('_');
				if ($(path).length > 0) {
					$(path).trigger('click');
				}
				cPathTmp.pop();
			}
		});
		var stepIndex = 0;
		$('.steps').each(function () {
			var stepGroups = {};
			$(this).find('li[rel^=group]').each(function () {
				var groupName = $(this).attr('rel');
				if (stepGroups[groupName] == undefined) {
					stepGroups[groupName] = [];
					stepGroups[groupName].push($(this));
					var href = $(this).find('a').attr('href');
					stepGroups[groupName].push(href);
				} else {
					var root = stepGroups[groupName][0];
					var rootB = $(root.find('a').attr('href'));
					var href = $(this).find('a').attr('href');
					stepGroups[groupName].push(href);
					var div = $(href);
					rootB.append(div);
					$(this).remove();
				}
			});
			var $i = 0;
			for (var group in stepGroups) {
				for (var j = 1; j < stepGroups[group].length - 1; j++) {
					var href = stepGroups[group][j];
					var match = $(href + ' p.nextstep-container');
					var match2 = $(href + ' p.backstep-container');
					$(match.get(0)).remove();
					$(match2.get(0)).remove();
				}
				$i++;
			}
			var index = 0;
			var lastOpen = null;
			var lastOpenLi = null;
			$(this).find('ul').each(function () {
				$(this).find('li').each(function () {
					$(this).find('a').each(function () {
						var href = $(this).attr('href');
						$(href).attr('step-order', index);
						$(this).find('.step-number[rel!=last]').html('Step ' + (index + 1));
						if (index > 0) {
							$(href).hide();
							$(this).parent().addClass('off');
						} else {
							lastOpen = $(this).attr('href');
							lastOpenLi = $(this);
							$(this).parent().addClass('on');
						};
						$(this).parent().css('z-index', 10 - index);
						var $bc = $(href).find("p.backstep-container:first")
						if ($bc.length > 0 && stepIndex == 0) {
							$bc.remove();
						}
						stepIndex++;
						$(this).click(function () {
							if (lastOpen != href) {
								var sDir = 'left';
								var hDir = 'right';
								if ($(href).attr('step-order') < $(lastOpen).attr('step-order')) {
									sDir = 'left';
									hDir = 'right';
								} else {
									sDir = 'right';
									hDir = 'left';
								};
								if (lastOpen != null) {
									$(lastOpenLi).parent().removeClass('on');
									$(lastOpenLi).parent().addClass('off');
									var self = this;
									$(lastOpen).trigger('hidding');
									if (jQuery.browser.msie) {
										$(lastOpen).hide();
										lastOpen = href;
										lastOpenLi = $(self);
									} else {
										var t = lastOpen;
										lastOpen = href;
										lastOpenLi = $(self);
										$(this).queue(function () {
											$(t).hide('slide', {
												direction: hDir
											}, 200, function () {
												$(self).dequeue();
											});
										});
									}
								};
								$(this).parent().removeClass('off');
								$(this).parent().addClass('on');
								$(href).trigger('showing');
								if (jQuery.browser.msie) {
									$(href).show();
									var fix = $(this).delay(200, function () {
										$(href).css('position', 'relative');
										$(href).css('top', '1');
										$(href).css('top', '0');
									});
									$(this).delay(200, fix);
									$(this).delay(1000, fix);
								} else {
									$(this).queue(function () {
										var self = this;
										$(href).show('slide', {
											direction: sDir
										}, 200, function () {
											$(self).dequeue();
										});
									});
								};
							}
							return false;
						});
					});
					index++;
				});
			});
		});
	});
	
$(function() {
	// fix textarea maxlength
    // Get all textareas that have a "maxlength" property.
    $('textarea[maxlength]').each(function() {

        // Store the jQuery object to be more efficient...
        var $textarea = $(this);

        // Store the maxlength and value of the field.
        var maxlength = $textarea.attr('maxlength');
        var val = $textarea.val();

        // Trim the field if it has content over the maxlength.
        $textarea.val(val.slice(0, maxlength));

        // Bind the trimming behavior to the "keyup" event.
        $textarea.bind('keyup', function() {
            $textarea.val($textarea.val.slice(0, maxlength));
        });

    });
});

})(jQuery);
