window.addEvent('domready', function() {

	// Convert Manual Cat List to Tree
	if ($('manual-nodes')) {
		var list = $$('#manual-nodes a');
		list.each(function(element) {

			element.addEvent('click', function(){
				var node = element.getProperty('name').replace(/NODE-/, '');
				if (node != 0) {
					var url = '/ajax/manuallist/' + node;
					$('manual-list').setHTML('Loading Manuals....');
					$('manual-disp').empty();
					new Ajax(url, {
						method: 'get',
						update: $('manual-list'),
						onCancel: function() {
							$('manual-list').setHTML('Request Cancelled');
						},
						onComplete: function() {
							var manualprods = $$('#manual-list ul li');
							manualprods.each(function(el) {
								el.addEvent('click', function(e) {
									var mnls = $$('#manual-list ul li');
									manualprods.each(function(mnel) {
										mnel.removeClass('manual_selected');
									});
									this.addClass('manual_selected');
									e = new Event(e).stop();
									var url = '/ajax/manualpdf/' + this.getProperty('id').replace(/PDF-/, '');
									new Ajax(url, {
										method: 'get',
										update: $('manual-disp'),
										onCancel: function() {
											$('manual-disp').setHTML('Request Cancelled');
										}
									}).request();
								});
							});
						}
					}).request();	
				}
			});

		});
	}

	// Convert Brochure Cat List to Tree
	if ($('brochure-nodes')) {
		var list = $$('#brochure-nodes a');
		list.each(function(element) {

			element.addEvent('click', function(){
				var node = element.getProperty('name').replace(/NODE-/, '');
				if (node != 0) {
					var url = '/ajax/brochurelist/' + node;
					$('brochure-list').setHTML('Loading Brochures....');
					$('brochure-disp').empty();
					new Ajax(url, {
						method: 'get',
						update: $('brochure-list'),
						onCancel: function() {
							$('brochure-list').setHTML('Request Cancelled');
						},
						onComplete: function() {
							var brochureprods = $$('#brochure-list ul li');
							brochureprods.each(function(el) {
								el.addEvent('click', function(e) {
									var mnls = $$('#brochure-list ul li');
									brochureprods.each(function(mnel) {
										mnel.removeClass('brochure_selected');
									});
									this.addClass('brochure_selected');
									e = new Event(e).stop();
									var url = '/ajax/brochurepdf/' + this.getProperty('id').replace(/PDF-/, '');
									new Ajax(url, {
										method: 'get',
										update: $('brochure-disp'),
										onCancel: function() {
											$('brochure-disp').setHTML('Request Cancelled');
										}
									}).request();
								});
							});
						}
					}).request();	
				}
			});

		});
	}

	// Convert QandA Cat List to Tree
	if ($('qanda-nodes')) {
		var list = $$('#qanda-nodes a');
		list.each(function(element) {

			element.addEvent('click', function(){
				var node = element.getProperty('name').replace(/NODE-/, '');
				if (node != 0) {
					var url = '/ajax/qandalist/' + node;
					$('qanda-list').setHTML('Loading Questions and Answers....');
//					$('qanda-disp').empty();
					new Ajax(url, {
						method: 'get',
						update: $('qanda-list'),
						onCancel: function() {
							$('qanda-list').setHTML('Request Cancelled');
						}
/*
						,
						onComplete: function() {
							var brochureprods = $$('#qanda-list ul li');
							brochureprods.each(function(el) {
								el.addEvent('click', function(e) {
									var mnls = $$('#qanda-list ul li');
									brochureprods.each(function(mnel) {
										mnel.removeClass('qanda_selected');
									});
									this.addClass('qanda_selected');
									e = new Event(e).stop();
									var url = '/ajax/brochurepdf/' + this.getProperty('id').replace(/PDF-/, '');
									new Ajax(url, {
										method: 'get',
										update: $('qanda-disp'),
										onCancel: function() {
											$('qanda-disp').setHTML('Request Cancelled');
										}
									}).request();
								});
							});
						}
*/
					}).request();	
				}
			});

		});
	}

});