
	//	FUNCTIONS

	function quote_init() {

		$('td.add_quote').each(function() {
			var productID = $(this).find('a').attr('rel')
			$(this).html('<input type="checkbox" class="add_to_quote" id="' + productID + '" />')
		})



		$('input.add_to_quote').live('click', function(event) {
			var productID = $(event.target).attr("id")

			var tableCell = $(event.target).parent()

			$(event.target).remove()

			tableCell.append('<img src="/img/icons/quote-spinner.gif" width="16" height="16" id="quote_spinner" />')

			$.ajax({
				type: 'POST',
				url: '/incs/ajax/add_quote.php',
				data: 'DATAid='+productID,
				success: function() {
					$('#quote_spinner').remove()
					tableCell
						.html('<p class="in_quote">In Quote <a href="#" class="remove_product" rel="' + productID + '" title="'+text_remove_full+'">'+text_remove+'</a></p>')
						.addClass('in_quote')
						.attr('id','td-' + productID)

					$('#quote_holder').empty().load('/incs/ajax/small_quote.php')
				}
			})
		})



		$('a.remove_product').live('click', function(event) {
			var tableCell = $(event.target).parent().parent()
			var productID = $(event.target).attr("rel")

			tableCell
				.removeClass('in_quote')
				.addClass('add_quote')
				.html('<img src="/img/icons/quote-spinner.gif" width="16" height="16" id="quote_spinner" />')

			// remove product from cart
			$.ajax({
				type: 'POST',
				url: '/incs/ajax/remove_quote.php',
				data: 'DATAid=' + productID,
				success: function() {
					$('#quote_spinner').remove()
					tableCell
						.html('<input type="checkbox" class="add_to_quote" id="' + productID + '" />')
							.parent()
							.removeClass('tr_in_cart')

					$('#quote_holder').empty().load('/incs/ajax/small_quote.php')
				}
			})

			return false;
		})



		$('a.remove_quote_full').live('click', function(event) {
			var listItem = $(event.target).parent()
			var productID = $(event.target).attr('rel')

			// remove product from cart
			$.ajax({
				type: 'POST',
				url: '/incs/ajax/remove_quote.php',
				data: 'DATAid=' + productID,
				success: function() {
					$('#quote_holder')
						.empty()
						.load(
							'/incs/ajax/small_quote.php',
							// updates the table as well
							function() {
								$('#td-' + productID)
									.removeClass('in_quote')
									.addClass('add_quote')
									.html('<input type="checkbox" class="add_to_quote" id="' + productID + '" />')
										.parent()
										.removeClass('tr_in_cart')
							}
						)
				}
			})

			return false;
		})



		$('a.ql_add').click( function() {
			var pid = $(this).attr('rel')
			var rData;

			$.ajax({
				type: 'POST',
				url: '/incs/ajax/new_product.php',
				data: 'PRODUCTid=' + pid,
				dataType: 'html',
				// should not use async, but struggled with variable scope & callback
				async: false,
				success: function(msg) { rData = msg; }
			})

			$(this).closest('table').append(rData)

			return false;
		})



		$('select.select_product_option').live('change', function(event) {
			var did = $(this).val()

			if (did != '') {
				var rData;

				$.ajax({
					type: 'POST',
					url: '/incs/ajax/complete_row.php',
					data: 'DATAid=' + did,
					dataType: 'html',
					// should not use async
					async: false,
					success: function(msg) { rData = msg; }
				})

				$(this).parent().parent().parent().html(rData)
			}
		})



		$('a.remove_row').live('click', function(event) {

			// Clear table cell, and add spinner
			var theTD = $(event.target).parent()
			var theTR = theTD.parent()

			theTD.html('<img src="/img/icons/quote-spinner.gif" width="16" height="16" id="quote_spinner" />')
			var productID = $(event.target).attr('rel')

			// not yet set as product
			if (productID != 0) {
				// remove
				$.ajax({
					type: 'POST',
					url: '/incs/ajax/remove_quote.php',
					data: 'DATAid=' + productID,
					success: function() {
						$('#quote_spinner').remove()

						theTD.parents('tr').find('span').slideUp('fast', function() {
							theTD.parents('tr').remove()

							$('ol#quote_list tbody tr').removeClass('even')
							$('ol#quote_list tbody tr:even').addClass('even')

							if (!$('ol#quote_list tbody tr')[0]) window.location = '/quote-request/';
						})
					}
				})
			} else {
				$('#quote_spinner').remove()

				theTD.parents('tr').find('span').slideUp('fast', function() {
					theTD.parents('tr').remove()

					$('ol#quote_list tbody tr').removeClass('even')
					$('ol#quote_list tbody tr:even').addClass('even')

					if (!$('ol#quote_list tbody tr')[0]) window.location = '/quote-request/';
				})
			}

			return false;
		})
	}



	//	PLUGINS

	;(function($) {
		$.fn.warnOnExit = function() {
			return this.click(function() {
				link = $(this)

				rel = link.attr('rel')
				link.removeAttr('rel')

				target_url = link.attr('href')

				$('object').fadeOut(250)

				triggerColorbox(target_url)

				link.attr(rel)

				return false;
			})
		}

		function triggerColorbox(target_url) {
			link.colorbox({
				height:  250,
				href:    '/incs/ajax/warn_on_exit.php?target_url=' + target_url,
				opacity: .5,
				open:    true,
				rel:     false,
				width:   400
			})
		}
	})(jQuery);


	;(function($) {
		$.fn.elandSearchForm = function() {
			search_input = $(this)
			default_val  = search_input.siblings('legend').text()

			if (!$('.search_form input').val()) search_input.val(default_val)

			search_input
				.focus(function() {
					if ($(this).val() == default_val) $(this).val('')
				})
				.blur(function() {
					if (!$(this).val()) $(this).val(default_val)
				})

			return this;
		}
	})(jQuery);


	/*	ColorBox v1.3.0 - a full featured, light-weight, customizable lightbox based on jQuery 1.3 */
	;(function(A){var p="colorbox",m="hover",w=true,R=false,V,o=A.browser.msie&&A.browser.version<7?w:R,x="cbox_open",L="cbox_load",s="cbox_complete",K="cbox_cleanup",l="cbox_closed",O="resize.cbox_resize",I="resize.cboxie6 scroll.cboxie6",F,T,U,d,y,i,b,E,c,P,C,f,q,h,k,M,j,H,r,W,g,e,a,v,N,n,z,Q,u,G,B={transition:"elastic",speed:350,width:R,height:R,initialWidth:"400",initialHeight:"400",maxWidth:R,maxHeight:R,scalePhotos:w,scrollbars:R,inline:R,html:R,iframe:R,photo:R,href:R,title:R,rel:R,opacity:0.9,preloading:w,current:"image {current} of {total}",previous:"previous",next:"next",close:"close",open:R,overlayClose:w,slideshow:R,slideshowAuto:w,slideshowSpeed:2500,slideshowStart:"start slideshow",slideshowStop:"stop slideshow"};function J(X){if(X.keyCode===37){X.preventDefault();H.click()}else{if(X.keyCode===39){X.preventDefault();j.click()}}}function D(X,Y){Y=Y==="x"?document.documentElement.clientWidth:document.documentElement.clientHeight;return(typeof X==="string")?(X.match(/%/)?(Y/100)*parseInt(X,10):parseInt(X,10)):X}function t(X){return Q.photo||X.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i)}function S(){for(var X in Q){if(typeof(Q[X])==="function"){Q[X]=Q[X].call(n)}}}V=A.fn.colorbox=function(Y,X){if(this.length){this.each(function(){var Z=A(this).data(p)?A.extend({},A(this).data(p),Y):A.extend({},B,Y);A(this).data(p,Z).addClass("cboxelement")})}else{A(this).data(p,A.extend({},B,Y))}A(this).unbind("click.colorbox").bind("click.colorbox",function(aa){n=this;Q=A(n).data(p);S();A().bind("keydown.cbox_close",function(ab){if(ab.keyCode===27){ab.preventDefault();V.close()}});if(Q.overlayClose){F.css({cursor:"pointer"}).one("click",V.close)}n.blur();G=X||R;var Z=Q.rel||n.rel;if(Z&&Z!=="nofollow"){c=A(".cboxelement").filter(function(){var ab=A(this).data(p).rel||this.rel;return(ab===Z)});z=c.index(n);if(z<0){c=c.add(n);z=c.length-1}}else{c=A(n);z=0}if(!u){u=w;A.event.trigger(x);r.html(Q.close);F.css({opacity:Q.opacity}).show();V.position(D(Q.initialWidth,"x"),D(Q.initialHeight,"y"),0);if(o){P.bind(I,function(){F.css({width:P.width(),height:P.height(),top:P.scrollTop(),left:P.scrollLeft()})}).trigger(I)}}V.slideshow();V.load();aa.preventDefault()});if(Y&&Y.open){A(this).triggerHandler("click.colorbox")}return this};V.init=function(){function X(Y){return A('<div id="cbox'+Y+'"/>')}P=A(window);T=A('<div id="colorbox"/>');F=X("Overlay").hide();U=X("Wrapper");d=X("Content").append(C=X("LoadedContent").css({width:0,height:0}),f=X("LoadingOverlay"),q=X("LoadingGraphic"),h=X("Title"),k=X("Current"),M=X("Slideshow"),j=X("Next"),H=X("Previous"),r=X("Close"));U.append(A("<div/>").append(X("TopLeft"),y=X("TopCenter"),X("TopRight")),A("<div/>").append(i=X("MiddleLeft"),d,b=X("MiddleRight")),A("<div/>").append(X("BottomLeft"),E=X("BottomCenter"),X("BottomRight"))).children().children().css({"float":"left"});A("body").prepend(F,T.append(U));if(o){F.css("position","absolute")}d.children().addClass(m).mouseover(function(){A(this).addClass(m)}).mouseout(function(){A(this).removeClass(m)}).hide();W=y.height()+E.height()+d.outerHeight(w)-d.height();g=i.width()+b.width()+d.outerWidth(w)-d.width();e=C.outerHeight(w);a=C.outerWidth(w);T.css({"padding-bottom":W,"padding-right":g}).hide();j.click(V.next);H.click(V.prev);r.click(V.close);d.children().removeClass(m)};V.position=function(aa,Z,Y,ab){var ac=document.documentElement.clientHeight,ae=ac/2-Z/2,ad=document.documentElement.clientWidth/2-aa/2,X;if(Z>ac){ae-=(Z-ac)}if(ae<0){ae=0}if(ad<0){ad=0}ae+=P.scrollTop();ad+=P.scrollLeft();aa=aa-g;Z=Z-W;X=(T.width()===aa&&T.height()===Z)?0:Y;U[0].style.width=U[0].style.height="9999px";function af(ag){y[0].style.width=E[0].style.width=d[0].style.width=ag.style.width;q[0].style.height=f[0].style.height=d[0].style.height=i[0].style.height=b[0].style.height=ag.style.height}T.dequeue().animate({height:Z,width:aa,top:ae,left:ad},{duration:X,complete:function(){af(this);U[0].style.width=(aa+g)+"px";U[0].style.height=(Z+W)+"px";if(ab){ab()}},step:function(){af(this)}})};V.resize=function(ac){if(!u){return}var Y,ai,ad,ab,Z,af,ak,X,aa=Q.transition==="none"?0:Q.speed;P.unbind(O);if(!ac){var aj=setTimeout(function(){ai=C.children().outerHeight(w);C[0].style.height=ai+"px";V.position(C.width()+a+g,ai+e+W,aa)},1)}else{C.remove();C=A(ac);C.load(function(){alert()});function ag(){Y=Q.width?v:v&&v<C.width()?v:C.width();return Y}function ae(){ai=Q.height?N:N&&N<C.height()?N:C.height();return ai}if(Q.scrollbars){C.css({overflow:"auto"})}C.hide().appendTo("body").attr({id:"cboxLoadedContent"}).css({width:ag()}).css({height:ae()}).prependTo(d);if(o){A("select:not(#colorbox select)").filter(function(){return A(this).css("visibility")!=="hidden"}).css({visibility:"hidden"}).one(K,function(){A(this).css({visibility:"inherit"})})}X=A("#cboxPhoto")[0];if(X&&Q.height){ad=(ai-parseInt(X.style.height,10))/2;X.style.marginTop=(ad>0?ad:0)+"px"}function ah(am){var al=Y+a+g,an=ai+e+W;A().unbind("keydown",J);V.position(al,an,am,function(){if(!u){return}if(A.browser.msie){if(X){C.fadeIn(100)}T[0].style.removeAttribute("filter")}d.children().show();A("#cboxIframeTemp").after("<iframe id='cboxIframe' name='iframe_"+new Date().getTime()+"' frameborder=0 src='"+(Q.href||n.href)+"' />").remove();f.hide();q.hide();M.hide();if(c.length>1){k.html(Q.current.replace(/\{current\}/,z+1).replace(/\{total\}/,c.length));j.html(Q.next);H.html(Q.previous);A().bind("keydown",J);if(Q.slideshow){M.show()}}else{k.hide();j.hide();H.hide()}h.html(Q.title||n.title);A.event.trigger(s);if(G){G.call(n)}if(Q.transition==="fade"){T.fadeTo(aa,1,function(){if(A.browser.msie){T[0].style.removeAttribute("filter")}})}P.bind(O,function(){V.position(al,an,0)})})}if((Q.transition==="fade"&&T.fadeTo(aa,0,function(){ah(0)}))||ah(aa)){}if(Q.preloading&&c.length>1){ab=z>0?c[z-1]:c[c.length-1];af=z<c.length-1?c[z+1]:c[0];ak=A(af).data(p).href||af.href;Z=A(ab).data(p).href||ab.href;if(t(ak)){A("<img />").attr("src",ak)}if(t(Z)){A("<img />").attr("src",Z)}}}};V.load=function(){var X,ab,Y,aa,Z=V.resize;n=c[z];Q=A(n).data(p);S();A.event.trigger(L);X=Q.height?D(Q.height,"y")-e-W:R;ab=Q.width?D(Q.width,"x")-a-g:R;Y=Q.href||n.href;f.show();q.show();r.show();if(Q.maxHeight){N=Q.maxHeight?D(Q.maxHeight,"y")-e-W:R;X=X&&X<N?X:N}if(Q.maxWidth){v=Q.maxWidth?D(Q.maxWidth,"x")-a-g:R;ab=ab&&ab<v?ab:v}N=X;v=ab;if(Q.inline){A('<div id="cboxInlineTemp" />').hide().insertBefore(A(Y)[0]).bind(L+" "+K,function(){C.children().insertBefore(this);A(this).remove()});Z(A(Y).wrapAll("<div/>").parent())}else{if(Q.iframe){Z(A("<div><div id='cboxIframeTemp' /></div>"))}else{if(Q.html){Z(A("<div/>").html(Q.html))}else{if(t(Y)){aa=new Image();aa.onload=function(){aa.onload=null;if((N||v)&&Q.scalePhotos){var ae=this.width,ac=this.height,ag=0,af=this,ad=function(){ac+=ac*ag;ae+=ae*ag;af.height=ac;af.width=ae};if(v&&ae>v){ag=(v-ae)/ae;ad()}if(N&&ac>N){ag=(N-ac)/ac;ad()}}Z(A("<div />").css({width:this.width,height:this.height}).append(A(this).css({width:this.width,height:this.height,display:"block",margin:"auto",border:0}).attr("id","cboxPhoto")));if(c.length>1){A(this).css({cursor:"pointer"}).click(V.next)}if(A.browser.msie&&A.browser.version==7){this.style.msInterpolationMode="bicubic"}};aa.src=Y}else{A("<div />").load(Y,function(ac,ad){if(ad==="success"){Z(A(this))}else{Z(A("<p>Request unsuccessful.</p>"))}})}}}}};V.next=function(){z=z<c.length-1?z+1:0;V.load()};V.prev=function(){z=z>0?z-1:c.length-1;V.load()};V.slideshow=function(){var Y,X,Z="cboxSlideshow_";M.bind(K,function(){clearTimeout(X);M.unbind(s+" "+L+" click")});function aa(){M.text(Q.slideshowStop).bind(s,function(){X=setTimeout(V.next,Q.slideshowSpeed)}).bind(L,function(){clearTimeout(X)}).one("click",function(){Y();A(this).removeClass(m)});T.removeClass(Z+"off").addClass(Z+"on")}Y=function(){clearTimeout(X);M.text(Q.slideshowStart).unbind(s+" "+L).one("click",function(){aa();X=setTimeout(V.next,Q.slideshowSpeed);A(this).removeClass(m)});T.removeClass(Z+"on").addClass(Z+"off")};if(Q.slideshow&&c.length>1){if(Q.slideshowAuto){aa()}else{Y()}}};V.close=function(){A.event.trigger(K);u=R;A().unbind("keydown",J).unbind("keydown.cbox_close");P.unbind(O+" "+I);F.css({cursor:"auto"}).fadeOut("fast");T.stop(w,R).removeClass().fadeOut("fast",function(){C.remove();T.css({opacity:1});d.children().hide();A.event.trigger(l)})};V.element=function(){return n};V.settings=B;A(V.init)}(jQuery));
	$().bind('cbox_closed', function(){ if ($('object')[0]) $('object').fadeIn() })


	$(function() {

		$('body').addClass('js')

		$('.search_form input').elandSearchForm()

		$('.home_box')
			.css({ cursor: 'pointer' })
			.click(function() { window.location = $(this).find('a:first').attr('href') })

		$('a[rel=tooltip]').tooltip({ delay: 0, showURL: false, track: true })

		$('.show_tooltips td').tooltip({ delay: 0, track: true })

		$('table.product_specs tbody tr:even').addClass('even')

		$('<img />')
			.attr('src', '/img/fullsize/petra_2.jpg')
			.load(function() {
				$('.staff_shot').hover(
					function() { $(this).attr('src', '/img/fullsize/petra_2.jpg') },
					function() { $(this).attr('src', '/img/fullsize/petra_1.jpg') }
				)
			})

		if ($('.errors')[0]) {
			$('html').animate({ scrollTop : $('.errors').offset().top }, 500)

			$.each([$('.errors'), $('.error')], function() {
				$(this)
					.css('background-color', '#ffdcdc')
					.animate({ backgroundColor : '#fff' }, 1000)
			})
		}

		$('input.ql_metres')
			.focus(function() { if ($(this).val() == 0) $(this).val('') })
			.blur(function() { if ($(this).val() == '') $(this).val('0') })

		quote_init()

		if ($('#quote_holder')[0]) {
			$("a[href*='eland.co.uk']").warnOnExit()
			$("a.colorbox_close").live('click', function() {
				$.fn.colorbox.close()
				return false;
			})
		}
	})
