(function($) { 
	jQuery(document).ready(function() {
		
		// backwards compatibility CSS3
		$('li:last-child').addClass('last-child');
		
		// Old site links, open new window
		$("a[href*='iconicadventures.co.nz']").attr('target', '_blank');
		
		
		var resetField = function(field) {
			if (field.value == '') {
				field.value = $(field).parents('.field').find('label').text();
			}
		}
		var clearField = function(field) {
			if (field.value == $(field).parents('.field').find('label').text()) {
				field.value = '';
			}
		}
		$('#header input.text').focus(function() { clearField(this); });
		$('#header input.text').blur(function() { resetField(this); });
		$('#header input.text').each(function() { resetField(this); });
		
		$('#newsletter input').attr('autocomplete', 'off');
		$('#newsletter #Email input').focus(function() {
			$('#newsletter').css('backgroundPosition', 'left bottom');
			$('#newsletter').animate({
				height: '78px'
			}, function() {
				$(this).css('backgroundPosition', 'left top');
			});
		});
		$('#newsletter form').submit(function(e) {
			if (!e.eventPhase) return true;
			
			var valid = true;
			$(this).find('input.text').each(function() {
				if ($(this).val() == '' || $(this).val() == $(this).parents('.field').find('label').text()) {
					valid = false;
				}
			});
			if (!valid) {
				alert('Please fill in all of the fields.');
				return false;
			}
			$(this).submit();
		});
		$('html').click(function(event) {
			var $target = $(event.target);
			if ($target.parents('#newsletter').size() == 0 && !$target.is('#newsletter') && $('#newsletter').height() > 21) {
				$('#newsletter').css('backgroundPosition', 'left bottom');
				$('#newsletter').animate({
					height: '21px'
				}, function() {
					$('#newsletter').css('backgroundPosition', '0 -57px');
				});
			}
		});
		/*
		$('.EventsComponent>ul>li').entwine({
			onmouseenter: function() {
				this.find('.overlay').animate({left: 12}, {duration: 'normal', queue: false});
				this.css({backgroundColor: '#586F7D', cursor: 'pointer'});
			},
			onmouseleave: function() {
				this.find('.overlay').animate({left: '243px'}, {duration: 'normal', queue: false});
				this.css({backgroundColor: '#7996A6', cursor: 'default'});
			},
			onclick: function() {
				document.location = this.find('h3 a').attr('href');
			}
		});
		*/
		$('.events>ul>li').entwine({
			MinHeight: 28,
			MaxHeight: null,
			onmatch: function() {
				this.setMaxHeight($(this).find('.content').height());
				$(this).find('.content').css({
					height: this.getMinHeight(),
					bottom: '29px'
				});
			},
			onmouseenter: function() {
				this.find('.overlay').stop().fadeOut();
				this.find('.content').animate({height: this.getMaxHeight()}, {duration: 'normal', queue: false});
			},
			onmouseleave: function() {
				this.find('.overlay').stop().fadeIn();
				this.find('.content').animate({height: this.getMinHeight()}, {duration: 'normal', queue: false});
			},
			onclick: function() {
				document.location = this.find('h3 a').attr('href');
			}
		});
		
		$('.events').each(function() {
			var ul = $(this).children('ul');
			var move = 784;
			var max = parseInt(ul.css('left'));
			var min = (Math.ceil(ul.children('li').size() / 4) * move * -1) + max + move;
			
			$(this).find('.controls a').click(function() {
				var pos = parseInt(ul.css('left'));
				if ($(this).hasClass('next')) {
					pos = pos - move;
				} else {
					pos = pos + move;
				}
				if (pos <= max && pos >= min) {
					ul.animate({left: pos}, {duration: 'fast', queue: false});
				}
				return false;
			});
		});
		
		// This selector won't parse in entwine, so adding class to use
		$('.AccordionComponent>ul>li:has(ul)').addClass('hasUL');
		$('.AccordionComponent>ul>li.hasUL').entwine({
			DefaultHeight: 37,
			
			expand: function() {
				$('.AccordionComponent>ul>li.hasUL').collapse();
				this.addClass('open');
				this.animate({height: this.getFullHeight() + this.getDefaultHeight()}, {queue: false})
			},
			collapse: function() {
				this.removeClass('open');
				this.animate({height: 37}, {queue: false})
			},
			onmouseenter: function(){
				this.expand();
			},
			onmouseleave: function(){
			},
			getFullHeight: function() {
				return parseInt(this.find('ul').height());
			}
		});
		
		$('.PageGalleryComponent a').each(function() {
			$(this).click(function() {
				$(this).parents('.PageGalleryComponent').find('.current').attr('src', $(this).attr('href'));
				return false;
			})
		});

		
		// preload other images and setup prev/next buttons once main image has uploaded
		$(window).load(function() {
			$('.PageGalleryComponent li a').each(function() {
				$.preloadImage($(this).attr('href'));
			});
			var images = $('.PageGalleryComponent li a').map(function() {
				return $(this).attr('href');
			});
			if (images.length > 1) {
				images.each(function(i, val) {
					if (val != $('.PageGalleryComponent img.current').attr('src'))
						$('.PageGalleryComponent p').append($('<img src="' + val + '" />'));
				});
				$('.PageGalleryComponent').append($('<a href="#next" class="next">next</a><a href="#prev" class="prev">prev</a>'));
			}
		});
		
		$('.PageGalleryComponent .next, .PageGalleryComponent .prev').live('click', function() {
			var direction = $(this).attr('class');
			var current = $(this).parent('.PageGalleryComponent').find('.current');
			
			if (direction == 'next')
				var upcoming = current.next().size() ? current.next() : $(this).parent('.PageGalleryComponent').find('p img:first');
			else
				var upcoming = current.prev().size() ? current.prev() : $(this).parent('.PageGalleryComponent').find('p img:last');

			upcoming.addClass('upcoming');
			current.fadeOut('slow', function() {
				$(this).removeClass('current').show();
				upcoming.addClass('current').removeClass('upcoming');
			});
			return false;
		});
		
		// Photo gallery
		$('.PhotoGalleryComponent a').attr('rel', 'lightbox_vlb');
		var $VisualLightBoxParams$ = {autoPlay:false,borderSize:39,enableSlideshow:true,startZoom:true};
		(function($){$.fn.visualLightbox=function(options){var C=null,badObjects=["select","object","embed"],l=null,B=[],refTags=["a","area"],k=null,c=null,m=50,I,showTimer;if(!document.getElementsByTagName){return;}options=$.extend({animate:true,autoPlay:true,borderSize:39,containerID:document,enableSlideshow:true,googleAnalytics:false,imageDataLocation:"south",closeLocation:"",initImage:"",loop:true,overlayDuration:0.2,overlayOpacity:0.7,prefix:"",relAttribute:"lightbox",resizeSpeed:7,Ae:false,slideTime:4,strings:{closeLink:"",loadingMsg:"loading",nextLink:"",prevLink:"",startSlideshow:"",stopSlideshow:"",numDisplayPrefix:"",numDisplaySeparator:"/"},featBrowser:true,breathingSize:20,startZoom:false,floating:true},options);if(options.animate){var overlayDuration=Math.max(options.overlayDuration,0);options.resizeSpeed=Math.max(Math.min(options.resizeSpeed,10),1);var resizeDuration=(11-options.resizeSpeed)*0.15;}else{var overlayDuration=0;var resizeDuration=0;}var enableSlideshow=options.enableSlideshow;options.overlayOpacity=Math.max(Math.min(options.overlayOpacity,1),0);var playSlides=options.autoPlay;var container=$(options.containerID);var relAttribute=options.relAttribute;AW();var objBody=container.length&&container.get(0)!=document?container.get(0):document.getElementsByTagName("body").item(0);var firstChild=objBody.childNodes[0];var n=document.createElement("div");n.setAttribute("id",getID("overlay"));n.style.display="none";if(!firstChild){objBody.appendChild(n);}else{$(firstChild).before(n);}$(n).click(end);var W=document.createElement("div");W.setAttribute("id",getID("lightbox"));W.style.display="none";if(!firstChild){objBody.appendChild(W);}else{$(firstChild).before(W);}$(W).click(end);var U=document.createElement("div");U.setAttribute("id",getID("imageDataContainer"));U.className=getID("clearfix");var p=document.createElement("div");p.setAttribute("id",getID("imageData"));U.appendChild(p);var g=document.createElement("div");g.setAttribute("id",getID("imageDetails"));p.appendChild(g);var AP=document.createElement("span");AP.setAttribute("id",getID("caption"));g.appendChild(AP);var AK=document.createElement("span");AK.setAttribute("id",getID("numberDisplay"));g.appendChild(AK);var S=document.createElement("span");S.setAttribute("id",getID("detailsNav"));g.appendChild(S);var T=document.createElement("a");T.setAttribute("id",getID("prevLinkDetails"));T.setAttribute("href","javascript:void(0);");T.innerHTML=options.strings.prevLink;S.appendChild(T);$(T).click(showPrev);var q=document.createElement("a");q.setAttribute("id",getID("slideShowControl"));q.setAttribute("href","javascript:void(0);");S.appendChild(q);$(q).click(AH);var f=document.createElement("a");f.setAttribute("id",getID("nextLinkDetails"));f.setAttribute("href","javascript:void(0);");f.innerHTML=options.strings.nextLink;S.appendChild(f);$(f).click(showNext);var P=document.createElement("table");P.setAttribute("id",getID("outerImageContainer"));P.cellSpacing=0;W.appendChild(P);var AG=P.insertRow(-1);var AY=AG.insertCell(-1);AY.className="tl";var Ac=AG.insertCell(-1);Ac.className="tc";var AI=AG.insertCell(-1);AI.className="tr";var AF=P.insertRow(-1);var Af=AF.insertCell(-1);Af.className="ml";var b=AF.insertCell(-1);b.setAttribute("id",getID("lightboxFrameBody"));b.innerHTML="&nbsp;";var Ag=AF.insertCell(-1);Ag.className="mr";var AD=P.insertRow(-1);var AZ=AD.insertCell(-1);AZ.className="bl";var Ab=AD.insertCell(-1);Ab.className="bc";var Ad=AD.insertCell(-1);Ad.className="br";if(options.imageDataLocation=="north"){b.appendChild(U);}var h=document.createElement("div");h.setAttribute("id",getID("imageContainer"));b.appendChild(h);var AJ=document.createElement("img");AJ.setAttribute("id",getID("lightboxImage"));h.appendChild(AJ);var AB=document.createElement("div");AB.setAttribute("id",getID("hoverNav"));h.appendChild(AB);var AA=document.createElement("a");AA.setAttribute("id",getID("prevLinkImg"));AA.setAttribute("href","javascript:void(0);");AB.appendChild(AA);$(AA).click(showPrev);var u=document.createElement("a");u.setAttribute("id",getID("nextLinkImg"));u.setAttribute("href","javascript:void(0);");AB.appendChild(u);$(u).click(showNext);var AE=document.createElement("div");AE.setAttribute("id",getID("loading"));h.appendChild(AE);var a=document.createElement("a");a.setAttribute("id",getID("loadingLink"));a.setAttribute("href","javascript:void(0);");a.innerHTML=options.strings.loadingMsg;AE.appendChild(a);$(a).click(end);if(options.imageDataLocation!="north"){b.appendChild(U);}var _=document.createElement("div");_.setAttribute("id",getID("close"));if(options.closeLocation=="top"){AI.appendChild(_);}else{p.appendChild(_);}var Z=document.createElement("a");Z.setAttribute("id",getID("closeLink"));Z.setAttribute("href","javascript:void(0);");Z.innerHTML=options.strings.closeLink;_.appendChild(Z);$(Z).click(end);if(options.initImage!=""){start("#"+options.initImage);}function AW(){$(refTags.join(","),container).each(function(){var rel=String(this.getAttribute("rel"));if(this.getAttribute("href")&&rel.toLowerCase().match(relAttribute)){this.onclick=function(){start(this);return false;};}});}var start=this.start=function(G){Aa();G=$(G);var pageSize=w();$$("overlay").css({height:pageSize.pageHeight+"px"});$$("imageDataContainer").hide();$$("lightboxImage").hide().attr({src:""});if(options.startZoom){$$("imageContainer").css({width:G.width()+"px",height:G.height()+"px"});if(!document.all){$$("outerImageContainer").css({opacity:0.1});}$$("lightbox").css({left:G.offset().left-options.borderSize+"px",top:G.offset().top-options.borderSize+"px",width:G.width()+options.borderSize*2+"px",height:"auto"});}else{$$("overlay").css({opacity:0}).show().fadeTo(overlayDuration*1000,options.overlayOpacity);$$("lightbox").css({left:0,width:"100%"});}$$("lightbox").show();B=[];l=null;var rel=G.attr("rel");if(rel==relAttribute){B.push({link:G.attr("href"),title:G.attr("title")});c=0;}else{$(G.attr("tagName"),container).each(function(){if(this.getAttribute("href")&&this.getAttribute("rel")==rel){B.push({link:this.getAttribute("href"),title:this.getAttribute("title")});if(this==G.get(0)){c=B.length-1;}}});l=rel.substring(relAttribute.length+1,rel.length-1);}if(options.featBrowser){$(window).resize(v);}if(options.floating){$(window).scroll(v);}$(window).resize(adjustOverlay);$(window).scroll(adjustOverlay);changeImage(c);};function changeImage(imageNum){C=imageNum;disableKeyboardNav();V();showLoading();if(!options.startZoom){$$("lightboxImage").hide();}$$("hoverNav").hide();$$("imageDataContainer").hide();$$("numberDisplay").hide();$$("detailsNav").hide();I=new Image;I.onload=function(){B[C].link=I.src;B[C].width=I.width;B[C].height=I.height;AC(false);};if(options.startZoom&&!$$("lightboxImage").attr("src")){B[C].width=320;B[C].height=240;AC(false,true);}I.src=B[C].link;if(options.googleAnalytics){urchinTracker(B[C].link);}}function AC(recall,J){var imgWidth=B[C].width;var imgHeight=B[C].height;var L=w();var r=imgWidth/imgHeight;if(options.featBrowser){var AX=L.AL/L.s;if(r>AX){var t=L.AL-options.borderSize*2-options.breathingSize*2;var z=Math.round(t/r);}else{var z=L.s-options.borderSize*2-options.breathingSize*2-m;var t=Math.round(z*r);}if(imgWidth>t||imgHeight>z){imgWidth=t;imgHeight=z;}}var K=AM().y+(w().s-(imgHeight+m+options.borderSize*2))/2;var Q=$$("imageContainer");if(recall==true){Q.css({height:imgHeight+"px",width:imgWidth+"px"});if(options.floating){moveEffect($$("lightbox"),K);}else{$$("lightbox").css({top:K+"px"});}}else{var F=$$("lightboxImage");Q.stop(true,false);F.stop(true,false);var H;if(options.startZoom&&F.attr("src")){H=F;H.attr({id:getID("lightboxImage2")});}else{F.remove();}if(!J){F=$(I);F.hide();F.attr({id:getID("lightboxImage")});Q.append(F);}with(Q){var D=r/(width()/height());}if(!J){if(options.startZoom){if(H){$$("lightboxImage2").stop(true,true);}var o=H?120:100;if(H){with(H){css({width:1>D?"auto":width()/parent().width()*100+"%",height:1>D?height()/parent().height()*100+"%":"auto",left:0,top:0});}}F.css({opacity:0,display:"block",position:"absolute",width:1>D?o+"%":"auto",height:1>D?"auto":o+"%",left:(100-o*(1>D?1:D))/2+"%",top:(100-o*(1>D?1/D:1))/2+"%"});}if(options.startZoom){hideLoading();}}AV(K,imgWidth,imgHeight,D,J);}$$("imageDataContainer").css({width:imgWidth+"px"});}function AV(K,imgWidth,imgHeight,D,J){var Q=$$("imageContainer");var F=$$("lightboxImage");var lightbox=$$("lightbox");if(!J){var H=$$("lightboxImage2");}if(options.startZoom){F.fadeTo(resizeDuration*1000,1);if(!document.all){$$("outerImageContainer").fadeTo(resizeDuration*1000,1);}}moveEffect(lightbox,K);if(options.startZoom&&!J){H.animate($.extend({opacity:0},D<1?{height:"120%",top:"-10%",left:(100-120/D)/2+"%"}:{width:"120%",left:"-10%",top:(100-D*120)/2+"%"}),{queue:false,duration:resizeDuration*1000,complete:function(){$(this).remove();}});F.animate($.extend({left:0,top:0},D<1?{width:"100%"}:{height:"100%"}),{queue:false,duration:resizeDuration*1000});}Q.animate({width:imgWidth+"px",height:imgHeight+"px"},{queue:false,duration:resizeDuration*1000,complete:!J?function(){showImage();}:null});}function moveEffect(lightbox,K){lightbox.stop(true,false);lightbox.animate({width:"100%",left:0,top:K},{queue:false,duration:resizeDuration*1000});}function showLoading(){clearTimeout(showTimer);var loading=$$("loading");loading.show();loading.css({visibility:"hidden"});showTimer=setTimeout(function(){$$("loading").css({visibility:"visible"});},300);}function hideLoading(){clearTimeout(showTimer);$$("loading").hide();}function showImage(){hideLoading();if(options.startZoom){$$("overlay:hidden").css({opacity:0}).show().fadeTo(overlayDuration*1000,options.overlayOpacity);updateDetails();}else{$$("lightboxImage").css({opacity:0}).show().fadeTo(500,1,function(){updateDetails();});}AS();}function updateDetails(){$$("caption").show();$$("caption").text(B[C].title||"");if(B.length>1){var num_display=options.strings.numDisplayPrefix+" "+eval(C+1)+" "+options.strings.numDisplaySeparator+" "+B.length;if(options.Ae&&l!=""){num_display+=" "+options.strings.numDisplaySeparator+" "+l;}$$("numberDisplay").text(num_display).show();if(!enableSlideshow){$$("slideShowControl").hide();}$$("detailsNav").show();}$$("imageDataContainer").animate({height:"show",opacity:"show"},650,null,function(){updateNav();});}function updateNav(){var d=1/B.length;m=m*(1-d)+$$("imageDataContainer").height()*d;if(B.length>1){$$("hoverNav").show();if(enableSlideshow){if(playSlides){AN();}else{AO();}}}AR();}function AN(){if($$("lightbox:hidden").length){return;}V();playSlides=true;k=setTimeout(function(){showNext();},options.slideTime*1000);$$("slideShowControl").text(options.strings.stopSlideshow);$$("slideShowControl").addClass("started");}function AO(){playSlides=false;V();$$("slideShowControl").text(options.strings.startSlideshow);$$("slideShowControl").removeClass("started");}function AH(){if(playSlides){AO();}else{AN();}}function V(){if(k){k=clearTimeout(k);}}function showNext(){if(B.length>1){V();if(!options.loop&&(C==B.length-1&&c==0||C+1==c)){end();return;}if(C==B.length-1){O(0);}else{O(C+1);}}}function O(imageNum){$$("imageDataContainer").animate({height:"hide",opacity:"hide"},650,null,function(){changeImage(imageNum);});}function showPrev(){if(B.length>1){if(C==0){O(B.length-1);}else{O(C-1);}}}function showFirst(){if(B.length>1){O(0);}}function showLast(){if(B.length>1){O(B.length-1);}}function AR(){document.onkeydown=keyboardAction;}function disableKeyboardNav(){document.onkeydown="";}function keyboardAction(e){if(e==null){keycode=event.keyCode;}else{keycode=e.which;}key=String.fromCharCode(keycode).toLowerCase();if(key=="x"||key=="o"||key=="c"){end();}else if(key=="p"||key=="%"){showPrev();}else if(key=="n"||key=="'"){showNext();}else if(key=="f"){showFirst();}else if(key=="l"){showLast();}else if(key=="s"){if(B.length>0&&options.enableSlideshow){AH();}}}function AS(){var AT=B.length-1==C?0:C+1;(new Image).src=B[AT].link;var AQ=C==0?B.length-1:C-1;(new Image).src=B[AQ].link;}function end(Event){if(Event){var id=$(Event.target).attr("id");if(getID("closeLink")!=id&&getID("lightbox")!=id&&getID("overlay")!=id){return;}}$$("imageContainer").stop(true,false);$$("lightboxImage").stop(true,false);I.onload=null;disableKeyboardNav();V();$$("lightbox").hide();if(options.overlayOpacity){$$("overlay").fadeOut(overlayDuration*1000);}else{$$("overlay").hide();}$(window).unbind("resize",v);$(window).unbind("scroll",v);$(window).unbind("resize",adjustOverlay);$(window).unbind("scroll",adjustOverlay);AU();}function v(){AC(true);}function adjustOverlay(){var L=w();$$("overlay").css({left:AM().x+"px",top:0,width:"100%",height:Math.max(L.s,L.pageHeight)+"px"});}function AU(){var els;var tags=badObjects;for(var i=0;i<tags.length;i++){els=document.getElementsByTagName(tags[i]);for(var j=0;j<els.length;j++){$(els[j]).css({visibility:"visible"});}}}function Aa(){var tags=badObjects;for(var i=0;i<tags.length;i++){$(tags[i]).css({visibility:"hidden"});}}function AM(){var x,y;if(self.pageYOffset){x=self.pageXOffset;y=self.pageYOffset;}else if(document.documentElement&&document.documentElement.scrollTop){x=document.documentElement.scrollLeft;y=document.documentElement.scrollTop;}else if(document.body){x=document.body.scrollLeft;y=document.body.scrollTop;}return{x:x,y:y};}function w(){var scrollX,scrollY,N,M,pageX,pageY;if(window.innerHeight&&window.scrollMaxY){scrollX=document.body.scrollWidth;scrollY=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){scrollX=document.body.scrollWidth;scrollY=document.body.scrollHeight;}else{scrollX=document.body.offsetWidth;scrollY=document.body.offsetHeight;}if(self.innerHeight){N=self.innerWidth;M=self.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){N=document.documentElement.clientWidth;M=document.documentElement.clientHeight;}else if(document.body){N=document.body.clientWidth;M=document.body.clientHeight;}pageY=scrollY<M?M:scrollY;pageX=scrollX<N?N:scrollX;return{pageWidth:pageX,pageHeight:pageY,AL:N,s:M};}function getID(id){return options.prefix+id;}function $$(name){return $("#"+getID(name));}return this;};})(jQuery);jQuery().visualLightbox($VisualLightBoxParams$);
		

		
		// preload other images and setup prev/next buttons once main image has uploaded
		$('.PageGalleryComponent .current').load(function() {
			$('.PageGalleryComponent li a').each(function() {
				$.preloadImage($(this).attr('href'));
			});
			var images = $('.PageGalleryComponent li a').map(function() {
				return $(this).attr('href');
			});
			if (images.length > 1) {
				images.each(function(i, val) {
					if (val != $('.PageGalleryComponent img.current').attr('src'))
						$('.PageGalleryComponent p').append($('<img src="' + val + '" />'));
				});
				$('.PageGalleryComponent').append($('<a href="#next" class="next">next</a><a href="#prev" class="prev">prev</a>'));
			}
		});
		
		$('.PageGalleryComponent .next, .PageGalleryComponent .prev').live('click', function() {
			var direction = $(this).attr('class');
			var current = $(this).parent('.PageGalleryComponent').find('.current');
			
			if (direction == 'next')
				var upcoming = current.next().size() ? current.next() : $(this).parent('.PageGalleryComponent').find('p img:first');
			else
				var upcoming = current.prev().size() ? current.prev() : $(this).parent('.PageGalleryComponent').find('p img:last');

			upcoming.addClass('upcoming');
			current.fadeOut('slow', function() {
				$(this).removeClass('current').show();
				upcoming.addClass('current').removeClass('upcoming');
			});
			return false;
		});
		
		// pop ups

		$("a.popup, a[target=_blank]").not('[href^="http://"]').fancybox({
			'width'				: 800,
			'height'			: '75%',
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
		
		
		//
		// Members
		//
		$('#RegisterForm').attr('autocomplete', 'off');
		// This is no good for 3 field dates
		//$('.date input').datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
		
		// Silverstripe credit card tab indexing is broken, works fine when unset
		$('.creditcard input').attr('tabindex', '');

	});
})(jQuery);


jQuery.preloadImage = function() {
	return jQuery("<img>").attr("src", arguments[0]);
}