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

	var myScrollbars = new Scrollbar($('prodTabs_content_wrap'), {
	    mode:'vertical', // vertical by default
	    buttons: true // true by default


	});
	
	

	if ($('prodTabs')) {

		$$('ul#prodTabs li a').each(function(item) {

			//alert(item.get('href'));
			//item.set('href', 'javascript:;');

			item.addEvent('click', function() {
				tabMe(item, 'tab');
				return false;
			});
		});




		$('prodEnquire').addEvent('click', function() {
			tabMe($('prodEnquire'), 'enqLink');
			return false;
		});

		if ($('productListing')) {
			// we're in the product listing tab at the start.. so we need to execute the enquiry stuff
			addEnquiryLinks();
		}


	}

	function tabMe(item, lType) {

		// get the original HREF
		var link = item.get('href');

		// wipe .htm off the end...
		var arrLink = link.split('.htm')

		// add -js.htm on the end..
		var url = arrLink[0] + '-js.htm';



		// start the loader thinggu here?
		showLoad('prodTabs_content');




		// send the request.
		var htmlReq = new Request.HTML({
			onSuccess: function(responseTree, responseElements, responseHTML) {

				// Turn off any selected tabs.
				$$('ul#prodTabs li a').each(function(item) {

					// Select a tab if we have it...
					if (item.getParent('li').hasClass('selected')) {
						item.getParent('li').removeClass('selected');
					}
				});

				if (lType == 'tab') {
					item.getParent('li').set('class', 'selected');
				} else if (lType == 'enqLink') {
					$('tabProductListing').set('class', 'selected');
				}


				$('prodTabs_content').empty();


				$('prodTabs_content').set('html', responseHTML);


				myScrollbars.refresh();

				// If we're on a product listing page.. we need to do a tiny tiny bit more dont we?


				var split_url = url.split('/');



				if (split_url[3] == 'product-listing-js.htm') {
					addEnquiryLinks();
				}
			},
			onFailure: function() {
				// the request failed.. lets just forward them on so they are under the
				// impression that nothing was wrong!!
				window.location.href = link;

			}
		}).get(url);


	}

	function addEnquiryLinks() {
		$$('table#productListing td.enquire a').each(function(item) {
				item.addEvent('click', function() {
					doEnquiry(item);

					return false;
				});
		});
	}

	var hasSendEvent=false;
	function doEnquiry(el) {
		if ($('enqDiv')) {

			var popDiv = $('enqDiv');


			// this bit TRICKS ie into working? (I dont know how.. or why)
			try {
				var popFrm = $('enqFrm');
			} catch(err) {
				// do nothing - i like errors.
			}



			var popFrm = $('enqFrm');


			$('popCloseE').addEvent('click', function() {
				hasSendEvent=false;
				hideOverlay(popDiv);
				return false;
			});

			if ($('msgDivE')) {
				$('msgDivE').parentNode.removeChild($('msgDivE'));

				$$('form#enqFrm input').each(function(item) {
					if (item.getPrevious('label')) {
						item.getPrevious('label').setStyle('color', '');
					}
				});
			}


			// Figure out which row we'r in..
			var row = el.getParent('tr');
			var tds = row.getChildren('td');

			$('catalogue_number').set('value', tds[0].get('html'));
			$('instrument').set('value', tds[1].get('html'));

			//$('p_catno').set('html', tds[0].get('html'));
			//$('e_catno').set('value', tds[0].get('html'));


			//$('p_descr').set('html', tds[1].get('html'));
			//$('e_descr').set('value', tds[1].get('html'));


			//$('p_size').set('html', tds[2].get('html'));
			//$('e_size').set('value', tds[2].get('html'));



			showOverlay(popDiv);





			$('btnEnq').addEvent('click', function(e) {
				if(hasSendEvent==true) {
					return false;
				} else {

					e = new Event(e);
					var passVal = 1;
					$$('form#enqFrm input').each(function(item) {

						if (item.id == 'firstname' || item.id == 'surname' || item.id == 'email' || item.id == 'organisation' || item.id == 'department' || item.id == 'position') {
							if (item.value.length <= 2) {
								item.getParent('div').getPrevious('label').setStyle('color', 'red');
								passVal = 0;
							} else {
								item.getParent('div').getPrevious('label').setStyle('color', '');
							}
						}

					});


					if($('title')) {
						var title = $('title');
						if (title.get('type') != 'hidden') {
							if (title.value == -1) {
								title.getPrevious('label').setStyle('color', 'red');
								passVal = 0;
							} else {
								title.getPrevious('label').setStyle('color', '');
							}
						}
					}


					if (passVal == 1) {
						var url		= '/process/support-enquiry-js';
						//var myForm 	= $('enqFrm');
						hasSendEvent=true;

						var req = new Request({
							url: url,
							method:'get',
							onSuccess: function(responseTree) {
								if (responseTree == 1) {
									// this is good news.. it means the email actually sent..
									showMsg('Your enquiry has been sent successfully', $('enqFrm'), 'top', 'done');
									var elP = new Element('p', {
										'href': 'javascript:;',
										'id':'pConfirmMsg'
									});
									elP.setStyles({
										'display':'block',
										'color':'#FFFFFF',
										'background-color':'#00A77E',
										'padding':'5px',
										'margin-top':'10px',
										'border-bottom':'2px solid #333333'
									});

									elP.set('text', 'Thank you, your enquiry has been sent. ');
									
									var elA = new Element('a', {
										'href': 'javascript:;'
									});
									elA.setStyles({
										'text-decoration':'underline',
										'color':'#FFFFFF',
										'cursor':'pointer',
										'font-weight':'normal'
									});
									elA.set('text', 'Click here to close the form.');
									
									elA.addEvent('click', function() {
										hasSendEvent=false;
										hideOverlay(popDiv);
										return false;
									});
									elA.inject(elP, 'bottom');
									elP.inject($('btnEnq'), 'after');


									// clear down some of the values to stop spam sends.
									$('stf_fname').value = '';
									$('stf_femail').value = '';

								} else {
									// failed
									showMsg('There was a problem with one or more fields. Please check that all fields contain valid data and that email addresses have been entered in the correct format.', $('enqFrm'), 'top', 'err');
								}
							},
							onFailure: function() {
								// the request failed.. lets tell them..
								showMsg('There was an error whilst trying to process your request. Please try again and if the problem persists contact the site administrator for assistance.', $('enqFrm'), 'top', 'err');

							}
						}).send($('enqFrm').toQueryString());




					} else {
						showMsg('There was a problem with one or more fields. Please check that all fields contain valid data and that email addresses have been entered in the correct format.', $('enqFrm'), 'top', 'err');
					}

					e.stop();
					return false;
				}
			});
		}
	}



	function showMsg(msg, targetDiv, injectTo, classStr) {

		if ($('msgDivE')) {
			$('msgDivE').parentNode.removeChild($('msgDivE'));
		}

		var errDiv = new Element('div', {
			'html':msg,
			'id':'msgDivE',
			'class':classStr
		});

		errDiv.inject(targetDiv, injectTo);

	}


	function showLoad(wrapEl) {

		// moo it..
		wrapEl = $(wrapEl);

		var wrapDim = wrapEl.getSize();
		var lDiv = new Element('div', {
		    'class': 'loading',
		    'styles': {
			'display': 'block',
			'position': 'absolute',
			'height': wrapDim.y,
			'width': wrapDim.x,
			'top': '0px'
		    },
		    'id': 'loadingDiv'
		});



		lDiv.inject(wrapEl, 'inside');


	}


	function hideLoad(wrapEl) {
		$('loadingDiv').remove()
	}



});