/*@cc_on
	@if (@_jscript_version >= 5.5) 
@*/
// This hides from IE versions < 5.5 (which can't handle the HTML_AJAX framework)
var Scratchpad = {
	selected_ids: null,
	callback: {
		addRemoveProduct: function(result) {
	
			if (typeof Scratchpad.products_list == 'undefined' || Scratchpad.products_list == null) {
				Scratchpad.createProductsList('scratchpad_content');
			} else {
				while (typeof Scratchpad.products_list.firstChild != 'undefined' && Scratchpad.products_list.firstChild != null) {
					Scratchpad.products_list.removeChild(Scratchpad.products_list.firstChild);
				}
			}

			Scratchpad.selected_ids = new Array();

			result.each(function(res) {
				if (typeof res['variant_id'] == 'undefined' || typeof res['product_name'] == 'undefined')
					return;

				Scratchpad.selected_ids[res['variant_id']] = true;

				var new_list_item = document.createElement('li');
				var new_link = document.createElement('a');

				new_link.setAttribute('href', '/catalogue/details.html?variant_id=' + escape(res['variant_id']));
				new_link.appendChild(document.createTextNode(res['product_name']));
				new_list_item.appendChild(new_link);

				Scratchpad.products_list.appendChild(new_list_item);
			});
	
			Scratchpad.updatePageCheckBoxes();
			Scratchpad.productContainer.show();
			Scratchpad.display();
		},
		addRemoveService: function(result) {
			if (typeof Scratchpad.contracts_list == 'undefined' || Scratchpad.contracts_list == null) {
				Scratchpad.createContractsList('scratchpad_content');
			} else {
				while (typeof Scratchpad.contracts_list.firstChild != 'undefined' && Scratchpad.contracts_list.firstChild != null) {
					Scratchpad.contracts_list.removeChild(Scratchpad.contracts_list.firstChild);
				}
			}

			for (var i in result) {
				if (typeof result[i]['contract_name'] == 'undefined' || typeof result[i]['contract_suppliers'] == 'undefined')
					continue;

				var new_list = document.createElement('dl');
				var new_list_title = document.createElement('dt');
				new_list_title.appendChild(document.createTextNode(result[i]['contract_name']));
				new_list.appendChild(new_list_title);
				new_list.className = 'scratchpad-list';

				for (var j in result[i]['contract_suppliers']) {
					if (typeof result[i]['contract_suppliers'][j]['supplier_id'] == 'undefined' || typeof result[i]['contract_suppliers'][j]['supplier_name'] == 'undefined')
						continue;

					var new_list_item = document.createElement('dd');
					var new_link = document.createElement('a');
					new_link.setAttribute('href', '/catalogue/suppliers.html?redirect=1&contract_id=' + escape(result[i]['contract_id']) + '&supplier_id=' + escape(result[i]['contract_suppliers'][j]['supplier_id']));
					new_link.appendChild(document.createTextNode(result[i]['contract_suppliers'][j]['supplier_name']));
					
					new_list_item.appendChild(new_link);
					new_list.appendChild(new_list_item);
				}

				Scratchpad.contracts_list.appendChild(new_list);

				var new_comparison_p = document.createElement('p');
				new_comparison_p.className = 'comparison_table';
				var new_comparison_link = document.createElement('a');
				var new_comparison_span = document.createElement('span');
				new_comparison_span.className = 'hide_2';
				new_comparison_link._contract_id = result[i]['contract_id'];
				new_comparison_link.href = '/catalogue/do_service_compare.html?contract_id=' + escape(new_comparison_link._contract_id);
				
				new_comparison_link.appendChild(document.createTextNode('View comparison table '));
				new_comparison_span.appendChild(document.createTextNode('for the ' + result[i]['contract_name'] + ' framework agreement'));
				
				new_comparison_link.appendChild(new_comparison_span);
				new_comparison_p.appendChild(new_comparison_link);		
				
				Scratchpad.contractContainer.show();
				Scratchpad.contracts_list.appendChild(new_comparison_p);
			}

			Scratchpad.display();
		}
	},
	
	updatePageCheckBoxes: function(){
		Scratchpad.checkboxes.each(function(check) {
			var id = check.id.substring(2);
			
			if (Scratchpad.selected_ids == null) {
				check.checked = false;
			} else {
				
				if (check.checked && !Scratchpad.selected_ids[id]) {
					check.checked = false;
				} else if (!check.checked && Scratchpad.selected_ids[id]) {
					check.checked = true;
				}
			}
		});
	},
	server: null,
	panel: null,

	products_h3: null,
	products_list: null,
	products_a: null,

	contracts_h3: null,
	contracts_list: null,

	checkboxes: new Array(),
	addLink: null,

	init: function() {
		this.server = new scratchpad_server(this.callback);

		addLink = $('add_service_to_scratchpad');
		if (addLink != null) {
			var serviceId = addLink.href.match(/add_service=(\d+-\d+)/);

			if (serviceId && serviceId[1]) {
				Event.observe(addLink, 'click', function(evt) {
					if (this.innerHTML.match(/^Add/)) {
						this.innerHTML = 'Remove this supplier from my Scratch Pad';
					} else {
						this.innerHTML = 'Add this supplier to my Scratch Pad';
					}
					Scratchpad.server.addRemoveService(serviceId[1]);
					Event.stop(evt);
				});
				addLink.href = '#';
			}
		} else {
			addLink = $('add_product_to_scratchpad');
			if (addLink != null) {
				var productId = addLink.href.match(/(add|remove)_product=(\d+)/);

				if (productId && productId[2]) {
					Event.observe(addLink, 'click', function(evt) {
						if (this.innerHTML.match(/^Add/)) {
							this.innerHTML = 'Remove this product from my Scratch Pad';
						} else {
							this.innerHTML = 'Add this product to my Scratch Pad';
						}
						Scratchpad.server.addRemoveProduct(productId[2]);
						Event.stop(evt);
					});
					addLink.href = '#';
				}
			} else {
				// hide the scratchpad submit buttons and related text
				var scratchpadsubmit1 = $('scratchpad_submit1');
				var scratchpadsubmit2 = $('scratchpad_submit2');
				var scratchpadExtraText = $('scratchpad_extra_text');
				if (typeof scratchpadsubmit1 == 'undefined' && typeof scratchpadsubmit2 == 'undefined') {
					return;
				}

				if (typeof scratchpadsubmit1 != 'undefined' && scratchpadsubmit1 != null) {
					scratchpadsubmit1.parentNode.removeChild(scratchpadsubmit1);
				}
				if (typeof scratchpadsubmit2 != 'undefined' && scratchpadsubmit2 != null) {
					scratchpadsubmit2.parentNode.removeChild(scratchpadsubmit2);
				}
				if (typeof scratchpadExtraText != 'undefined' && scratchpadExtraText != null) {
					scratchpadExtraText.parentNode.removeChild(scratchpadExtraText);
				}
				


			}
			this.initCheckBoxes();
		}

		// either get a reference to the scratch pad or create one
		Scratchpad.panel = $('scratchpad');

		if (typeof Scratchpad.panel == 'undefined' || Scratchpad.panel == null) {
			//builds the whole dbx-box with scratchpad inside
			Scratchpad.panel = document.createElement('div');
			Scratchpad.panel.className = 'nav_block';
			Scratchpad.panel.id = 'scratchpad';
			
			Scratchpad.container = document.createElement('div');
			Scratchpad.container.className = 'wrapper scratch-panel';

			var h2 = document.createElement('h2');
			h2.className = 'alt open';

			h2.appendChild(document.createTextNode('Scratch Pad'));
						
			h2.style.zoom = '1';
			Scratchpad.container.appendChild(h2);
			
			var content = document.createElement('div');
			//content.className = 'dbx-content';
			content.id = 'scratchpad_content';

			this.createProductsList(content);
			this.createContractsList(content);
			
			var buttonList = document.createElement('ul');
			buttonList.className = 'right-nav';
			
			if ($('slis_username') == null) {
				var link_li = document.createElement('li');
				var link_a2 = document.createElement('a');

				link_li.appendChild(link_a2);
				link_a2.href = '/catalogue/scratchpad_save.html';
				link_a2.className = 'link_button';
				link_a2.appendChild(document.createTextNode('Save Scratch Pad'));
				buttonList.appendChild(link_li);
			}

			var link_li1 = document.createElement('li');
			var link_a = document.createElement('a');
			link_a.href = '/catalogue/scratchpad.html';
			link_a.appendChild(document.createTextNode('Scratch Pad details'));
			
			link_li1.appendChild(link_a);
			buttonList.appendChild(link_li1);
		
			var link_clear_container = document.createElement('li');
			var link_clear = document.createElement('a');

			link_clear.className = 'link_button';
			link_clear_container.appendChild(link_clear);
			link_clear.href = '/catalogue/scratchpad_clear.html';
			link_clear.appendChild(document.createTextNode('Clear Scratch Pad'));
			buttonList.appendChild(link_clear_container);
			content.appendChild(buttonList);
			Scratchpad.container.appendChild(content);
			Scratchpad.panel.appendChild(Scratchpad.container);
			
		} else {
			Scratchpad.products_h3 = $('scratchpad-products-h3');
			Scratchpad.products_list = $('scratchpad-products-list');
			Scratchpad.products_a = $('products_compare_link');
			Scratchpad.contracts_h3 = $('scratchpad-contracts-h3');
			Scratchpad.contracts_list = $('scratchpad-contracts-lists');
			Scratchpad.productContainer = $('product_container');
			Scratchpad.contractContainer = $('contract_container');
		}

		Scratchpad.server.addRemoveProduct('update');
		Scratchpad.server.addRemoveService('update');
	},
	initCheckBoxes: function(){
		
		// add onclick handlers to the 'mark for scratchpad' checkboxes
		var checks = [
			$$('input.mark_for_scratchpad_product[type=checkbox][id]'),
			$$('input.mark_for_scratchpad_contract[type=checkbox][id]')
		];

		checks[0].each(function(check) {
			Event.observe(check, 'click', function() {
				TP.highlight.highlightProducts();
				Scratchpad.server.addRemoveProduct(this.id.substring(2));
				
			}.bind(check));
		});

		checks[1].each(function(check) {
			Event.observe(check, 'click', function() {				
				TP.highlight.highlightContracts();
				Scratchpad.server.addRemoveService(this.id.substring(2));
			});
		});

		this.checkboxes = checks.flatten();
	},

	createAddedText: function(checkbox) {
		if (checkbox.checked) {
			if (checkbox._addedText) {
				checkbox._addedText.className = 'scratch-note';
			} else {
				var aHref = document.createElement("a");
				aHref.href = "#scratchpad";
				aHref.title = "This product has been added to the Scratch Pad";

				aHref.appendChild(document.createTextNode(' ADDED'));

				if (checkbox.parentNode.tagName.toLowerCase() == 'p') {
					checkbox._addedText = document.createElement('strong');
					checkbox._addedText.appendChild(aHref);
					checkbox.parentNode.appendChild(checkbox._addedText);
				} else {
					checkbox._addedText = document.createElement('dd');
					checkbox._addedText.className = 'scratch-note';
					checkbox._addedText.appendChild(aHref);

					if (checkbox.parentNode.nextSibling) {
						checkbox.parentNode.parentNode.insertBefore(checkbox._addedText, checkbox.parentNode.nextSibling);
					} else {
						checkbox.parentNode.parentNode.appendChild(checkbox._addedText);
					}
				}
				checkbox._addedText.className = 'scratch-note';
			}
		} else if (checkbox._addedText) {
			checkbox._addedText.className = 'hide';
		}
	},
	display: function() {
		var displayPad = false;

		if (typeof Scratchpad.products_list != 'undefined' && Scratchpad.products_list != null && Scratchpad.products_list.childNodes.length > 0) {
			Scratchpad.products_h3.show();
			Scratchpad.products_list.show();
			Scratchpad.products_a.show();
			displayPad = true;
		} 
		if (typeof Scratchpad.contracts_list != 'undefined' && Scratchpad.contracts_list != null && Scratchpad.contracts_list.childNodes.length > 0) {
			Scratchpad.contracts_h3.show();
			Scratchpad.contracts_list.show()
			displayPad = true;
		} 

		var rightMenu = $('rightmenu').down();
		var flashContent = $('flashcontent');

		if (displayPad) {
			// Somehow, despite not being added to the document, scratchpad_panel gets
			// a valid parentNode from somewhere in IE6
			//
			// Nice of Microsoft to provide us with Conditional Compilation so we can
			// work around the myriad of bugs in their browser...
			/*@cc_on
				@if (@_jscript_version >= 5.6) 
					if (typeof Scratchpad.panel.parentNode.parentNode != 'undefined' && Scratchpad.panel.parentNode.parentNode != null) {
						return;
					}
				@else @*/
					if (typeof Scratchpad.panel.parentNode != 'undefined' && Scratchpad.panel.parentNode != null) {
						return;
					}
				/*@end
			@*/
					
			TP.RightmenuInstance.activateMenu();

			rightMenu.insertBefore(Scratchpad.panel, rightMenu.firstChild);
		} else {
			if (typeof Scratchpad.panel.parentNode == 'undefined' || Scratchpad.panel.parentNode == null) {
				return;
			}

			/*@cc_on
				@if (@_jscript_version >= 5.6) 
				if (Scratchpad.panel.parentNode.parentNode == null) {
					return;
				}
				@end
			@*/

			TP.RightmenuInstance.deactivateMenu();
			rightMenu.removeChild(Scratchpad.panel); 
		}
	},
	createProductsList: function(appendTo) {
		appendTo = $(appendTo);
		
		Scratchpad.productContainer = new Element('div');
		Scratchpad.productContainer.className = 'alt_block';
		Scratchpad.productContainer.id = 'product_container';
		Scratchpad.productContainer.hide();
		
		Scratchpad.products_h3 = $(document.createElement('h3'));
		Scratchpad.products_h3.className = 'scratchpad';
		Scratchpad.products_h3.hide();
		Scratchpad.products_h3.appendChild(document.createTextNode('Products'));

		Scratchpad.products_list = $(document.createElement('ul'));
		Scratchpad.products_list.className = 'scratchpad-list';
		Scratchpad.products_list.hide();

		Scratchpad.products_a = $(document.createElement('p'));
		Scratchpad.products_a.className = 'comparison_table';
		Scratchpad.products_a.innerHTML = '<a href="/catalogue/do_compare.html" title="View comparison table">View comparison table</a>'
		Scratchpad.products_a.hide();

		Scratchpad.productContainer.appendChild(Scratchpad.products_h3);
		Scratchpad.productContainer.appendChild(Scratchpad.products_list);
		Scratchpad.productContainer.appendChild(Scratchpad.products_a);

		if (typeof Scratchpad.contracts_h3 == 'undefined') {
			appendTo.appendChild(Scratchpad.productContainer);
		} else {
			appendTo.insert({
				top: Scratchpad.productContainer
			});
		}
	},
	createContractsList: function(appendTo) {
		appendTo = $(appendTo);
		
		Scratchpad.contractContainer = new Element('div');
		Scratchpad.contractContainer.className = 'scratchpad-container';
		Scratchpad.contractContainer.id = "contract_container"
		Scratchpad.contractContainer.hide();

		Scratchpad.contracts_h3 = $(document.createElement('h3'));
		Scratchpad.contracts_h3.className = 'scratchpad';
		Scratchpad.contracts_h3.hide();
		Scratchpad.contracts_h3.appendChild(document.createTextNode('Framework Agreements'));

		Scratchpad.contracts_list = $(document.createElement('div'));
		Scratchpad.contracts_list.hide();
			
		Scratchpad.contractContainer.appendChild(Scratchpad.contracts_h3);
		Scratchpad.contractContainer.appendChild(Scratchpad.contracts_list);

		appendTo.appendChild(Scratchpad.contractContainer);
	}
};

document.observe("dom:loaded", function() { Scratchpad.init(); });
/*@end
@*/
