$.snippet = function(template, data) { 
	var i;
   
   for (i in data) {
		template = template.split('%7B' + i + '%7D').join(data[i]);
   	template = template.split('{' + i + '}').join(data[i]);
   }

   return template;
}

jQuery.fn.center = function (top_extra) {
	var popup = this;
	if ($.browser.msie){
		popup.css('left','0px');
		popup.css('top','0px');
	}
	
		popup.find('.lightbox-container').css("margin-left", "0");
		popup.find('.lightbox-container').css("margin-top", "0");
		popup.css({ position: "fixed", visibility: "hidden", display: "block" });

		var top = ( ( $(window).height() - popup.find('.lightbox-container').outerHeight() ) / 2 ) + top_extra;
		popup.find('.lightbox-container').css("left", ( $('body').width() - popup.find('.lightbox-container').outerWidth() ) / 2 + "px");
		popup.find('.lightbox-container').css("top", top + "px");

		var header_width = parseInt( popup.find('.lightbox-header-container').width() ) / 2;
		popup.find('.lightbox-header-container').css({
			left: '50%',
			marginLeft: '-'+header_width+'px'
		});
		
		popup.css({ position: "", visibility: "", display: "" });
	
    return this;
}

$.widget('ui.contentLightbox', {

	_init: function() {
		var obj = this;
		var parent = this.element;
		var infoId = this.element.attr('rel');
		var infoElement = $('.lightbox-dyno-content'+infoId);

		obj.show(infoElement);

		$('#lightbox-white .close, #lightbox-white .overlay').unbind().bind('click',function(){
			obj.hide();
		});

	},

	show: function(el){
		var obj = this;
		if($.browser.msie){
			$('#lightbox .lightbox-container .close').hide();
			$('#lightbox').children('.close').remove();
		}
		$('#lightbox-white').hide();
		var content = el.html();

		$('#lightbox-white .content-container').html(content);
		$('#lightbox-white').css({ position: "fixed", visibility: "hidden", display: "block" });


		$('#lightbox-white').center();
		$('#lightbox-white').show();
		$(window).trigger('scroll');

		$('#lightbox-white .lightbox-container').css('opacity', 0);
		$('#lightbox-white .overlay').css('opacity', 0);
		$('#lightbox-white .lightbox-container').animate({ opacity: '1' }, 500);
		$('#lightbox-white .overlay').animate({ opacity: '0.65' }, 500);
	},

	hide: function(){
		var obj = this;
		$('#lightbox-white .lightbox-container').animate({ opacity: '0' }, 500);
		$('#lightbox-white .overlay').animate({ opacity: '0' }, 500, function(){
			$('#lightbox-white').hide();
			$('#lightbox-white .content-container').css({ width: 'auto', height: 'auto' });
			$('#lightbox-white .lightbox-container').css({ width: 'auto', height: 'auto' });
			obj.destroy();
		});
	},

	destroy: function() {
       $.Widget.prototype.destroy.apply(this, arguments); // default destroy
        // now do other stuff particular to this widget
   }
	
});

$.widget('ui.membersTooltip', {

	_init: function() {
		var obj = this;
		var parent = this.element;
		var container = this.options.container;
		
		parent.find('.photo-frame').live('click', function() {
			$('.author-info-clone .more a').attr('href', $('.author-info-clone .more a').attr('rel') );
			$('.author-info-clone .photo img').attr('src', $('.author-info-clone .photo img').attr('title') );
			
			var offset = $(this).closest(container).position();
			
			var height = $('.author-info').height();
			var width = $('.author-info').width();
			$('.author-info').html( $('.author-info-clone').html() );
			
			var minWidth = 1000;
			var left = offset.left - width;
			
			if($('body').width() < minWidth){
				left = 0;
			}else if($('body').width() > minWidth && left < 0 && $(this).closest(container).offset().left < width){
				var leftVal = -((parseInt($('body').width())-1000)/2);
				if(leftVal>left){
					left = leftVal;
				}
			}
			
			var top = offset.top - height;
			
			var picheight = $(this).closest('.photo').height();
			var picwidth = $(this).closest('.photo').width();

			
			$(this).parent().next('.author-info-hidden').find('li').each(function() {
				var replacement = $(this).attr('class').split('-');
				var val = $('.author-info').html();
				var regex = new RegExp( "\-\-" + replacement[1] + "\-\-", "g" );
				var matches = val.match(regex);
				var occurrences = ( matches ) ? matches.length : 0;
				
				for( var i = 0; i < occurrences; i++ ) {
					$('.author-info').html( $('.author-info').html().replace( "\-\-" + replacement[1] + "\-\-", $(this).html() ) );
				}
			});
		
			// $('.author-info').css('top', offset.top-height+picheight + "px");
			$('.author-info').css('top', top + "px");
			// $('.author-info').css('left', offset.left-width+picwidth + "px");
			$('.author-info').css('left', left + "px");
			$('.author-info').fadeIn('slow');
		});

	}
	
});

jQuery.fn.accordeon = function () {
	var parent = this;
	parent.find('.block:visible').hide();
	parent.find('.handle').click(function(){
		var block = $(this).attr('rel');
		if(parent.find('#'+block).is(':hidden')){
			parent.find('.block:visible').stop(true,true).slideToggle(600);
			parent.find('.block:visible').prev('.handle').toggleClass('active');
		}
		parent.find('#'+block).stop(true,true).slideToggle(575);
		$(this).toggleClass('active');
	});
    return this;
}

$.widget('ui.videoList', {

	_create: function() {
		var obj = this;
		var parent = this.element;
		var itemHeight = 67;
		
		parent.find('.video-thumb').live('click',function(){
			if( !$(this).hasClass('active') ){
				$('.video-thumb.active').removeClass('active');
				$(this).addClass('active');
				$('.videos-ltbx .lightbox-content h2.title').html( $(this).find('.name').html() );
				$('.lightbox-left-side .lightbox-content .video-box').html($(this).find('.embed-code').html());
				var index = parseInt( $('#load-videos-list .views-row').index( $(this).closest('.views-row') ) ) + 1;
				var margin = ( Math.ceil( index/6 ) - 1 )*(-402);
				parent.find('ul.items').css({ marginTop: margin+'px' });
			}
		});
		
		parent.find('.newer a').click(function(){
			obj.scrollUp(parent.find('ul.items'),itemHeight);

			return false;
		});
		parent.find('.older a').click(function(){
			obj.scrollDown(parent.find('ul.items'),itemHeight);

			return false;
		});
	},
	
	scrollUp: function(el,height){
		var parent = this.element;
		var count = parent.find('li.item').length;
		var mtop = parseInt(el.css('margin-top'));
		var max = (count-6)*height*-1;
		
		if(mtop < 0 && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mtop += 402;
			el.animate({marginTop: mtop+'px'},600,'linear',function(){
				el.removeClass('scrolling');
				mtop = parseInt(el.css('margin-top'));
				if(mtop == 0){ parent.find('.newer a').addClass('disabled'); }
				parent.find('.older a').removeClass('disabled');
			});
		}
	},
	
	scrollDown: function(el,height){
		var parent = this.element;
		var count = parent.find('li.item').length;
		var mtop = parseInt(el.css('margin-top'));
		var max = (count-6)*height*-1;
		
		if(mtop > max && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mtop -= 402;
			el.animate({marginTop: mtop+'px'},600,'linear',function(){
				el.removeClass('scrolling');
				mtop = parseInt(el.css('margin-top'));
				$('.newer a').removeClass('disabled');
				if(max - mtop >= 0){ $('.older a').addClass('disabled'); }
			});
		}
	}
	
});

$.widget('ui.validation', {
	options: {
		validate: '',
		valtype: 'outline' //outline, inline
	},
	_create: function() {
		var obj = this;
		var parent = this.element;
		var items = parent.find('.validate');
		var validated = 0;
		var validate = this.options.validate;
		var valtype = this.options.valtype;
		
		items.blur(function(){
			validated = 0;
			validated = obj.validate($(this));
		});
		parent.submit(function(){
			validated = 0;
			items.each(function(){
				if($(this).is(':visible')){
					validated += obj.validate($(this));
				}
			});
			if(validated != 0){
				parent.removeClass('validated');
				return false;
			}else{
				parent.addClass('validated');
				if( parent.hasClass('booking-form') && parent.find('input[name="terms"]').val() != '1' && parent.find('.hidden').length == 0 ){
					parent.removeClass('validated');
					return false;
				}
				return true;
			}
		});
		
	},
	
	_init: function(){
		var obj = this;
		var validate = this.options.validate;
		var items = this.element.find('.validate');
		var validated = 0;
		var valtype = this.options.valtype;
		var parent = this.element;
		
		if(validate == 'validate'){
			validated = 0;
			items.each(function(){
				validated += obj.validate($(this));
			});
			if(validated != 0){
				this.element.removeClass('validated');
			}else{
				this.element.addClass('validated');
			}
		}
	},
	
	validate: function(el){
		var content = el.val();
		var emailregex = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
		var creditregex = /^[0-9\-\ ]+$/;
		var cvvregex = /^([0-9]{3,4})$/;
		var passregex = /^([A-Za-z0-9]+)$/;
		var passlength = /^([A-Za-z0-9]{6,25})$/;
		var errors = 0;
		var error_msg = '';
		var valtype = this.options.valtype;
		var parent = this.element;
		
		if(el.hasClass('vldemail')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(el.val() != el.attr('title') || el.attr('title') == ''){
					if(content.search(emailregex) == -1){
						errors+=1;
						error_msg += '<li class="entry">A valid email is required</li>';
					}
				}
			}
		}
		if(el.hasClass('vldcredit')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(creditregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid credit card number is required</li>';
				}
			}
		}
		if(el.hasClass('vldcvv')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(cvvregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid CSV code is required</li>';
				}
			}
		}
		if(el.hasClass('vldpass')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(passregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid Password is required</li>';
				}
			}
		}
		if(el.hasClass('vldpasslength')){
			if(el.hasClass('vldrequired') && el.val().length > 0){
				if(content.search(passlength) == -1){
					errors+=1;
					error_msg += '<li class="entry">A minimum of 6 characters long</li>';
				}
			}
		}
		if(el.hasClass('vldrequired')){
			if( (content.length == 0 || el.val() == el.attr('title')) && !el.parent().hasClass('hidden') ){
				errors+=1;
				error_msg += '<li class="entry">This field is required</li>';
			}
		}
		
		el.next('.validation-msg').remove();
		if(errors > 0){
			el.addClass('error');
			
			if(valtype == 'outline'){
				el.after('<div class="validation-msg"><ul class="list">'+ error_msg +'</ul><div class="validation-bottom"></div></div>');
				el.next('.validation-msg').show();
				el.next('.validation-msg').mouseleave(function(){
					if(!$.browser.msie){
						$(this).fadeOut(function(){
							$(this).remove();
						});
					}else{
						$(this).remove();
					}
				});
			}
		
			return 1;
		}else{
			el.removeClass('error');
			return 0;
		}
		
	}
	
});

$.widget('ui.ajaxform', {
	options: {
		reqclass: '',
		type: 'POST'
	},
	
	_create: function() {
		var obj = this;
		var parent = this.element;
		var reqclass = this.options.reqclass;
		
		parent.submit(function(){
			if(reqclass.length > 0){
				if(parent.hasClass(reqclass)){
					obj.sendout();
				}
			}else{
				obj.sendout();
			}
			return false;
		});
	},
	
	sendout: function(){
		var formdata = this.element.serialize();
		var callback = this.options.change;
		var type = this.options.type;
		var address = this.options.address;
		
		$.ajax({
			type: type,
			url: address,
			data: formdata,
			success: function(data) {
				if ($.isFunction(callback)) callback(data); 
			}
		});
	}
	
});

$.widget("ui.helperValue", {
	_create: function() {
		var obj = this;
		var parent = this.element;
		var form = parent.closest('form');

		if(parent.val() == ''){
			parent.val(parent.attr('title'));
		}

		parent.focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		});

		parent.blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
			}
		});
	}
});

$.widget('ui.gallery', {
	_create: function(){
		var obj = this;
		var parent = this.element;
		this.mouseTimer = null;
		this.thumbsVisible = true;
		this.thumbs = parent.find('.thumb-container .thumbs');

		var itemWidth = 107;
		this.count = parent.find('.thumbs').children('.thumb').length;
		
		parent.find('.active-image img').live('click',function(){
			if(!obj.thumbs.hasClass('scrolling')){
				var ind = obj.thumbs.children('.thumb').index(obj.thumbs.children('.thumb.active')) + 1;
				if(ind < obj.count){
					if((!$('.fullscreen-gallery').is(':visible') && ind % 6 == 0) || ($('.fullscreen-gallery').is(':visible') && ind % 17 == 0)){
						var mleft = parseInt(obj.thumbs.css('margin-left'))-642;
						
						if(!obj.thumbs.hasClass('scrolling')){
							obj.thumbs.addClass('scrolling');
							obj.thumbs.animate({'margin-left': mleft+'px'},1000,function(){
								obj.thumbs.removeClass('scrolling');
							});
							listItem = parent.find('.large-pagination').children('.pager.active');
							var active_index = parent.find('.large-pagination').children(' .pager').index(listItem);
							active_index+=1;
							parent.find('.large-pagination').children('.pager.active').removeClass('active');
							parent.find('.large-pagination').children('.pager:eq('+active_index+')').addClass('active');
						}
					}
					var thumb = parent.find('.thumb-container .thumbs').children('.thumb:eq('+ind+')');
					var thumb_margin = parseInt(parent.find('.thumb-container .thumbs').css('margin-left'))*-1;
					if( ((ind+1)*110)-thumb_margin > $(document).width()){
						parent.find('.thumb-infinite .next-side').click();
					}
					thumb.click();
				}else{
					parent.find('.thumb-infinite .thumbs').animate({'margin-left': '0px'},1000);
					parent.find('.thumb-container .thumbs').children('.thumb:first').click();
					parent.find('.large-pagination').children('.pager:first').click();
				}
			}
		});

		parent.find('.active-image').mouseenter(function(){
			if($(this).find('.description').html().length > 0 && !$(this).hasClass('mouseover')){
				$(this).addClass('mouseover');
				var desc = $(this).find('.desc-block');

				desc.css({ visibility:'hidden', height:'auto', display:'block' });
				var height = desc.height();
				var width = desc.width() / 2;
				if($('.fullscreen-gallery').is(':visible')){
					desc.css({ visibility:'visible', height:'auto', display:'block' });
					desc.css({ marginLeft: '-'+width+'px' });
					desc.css({ marginTop: '-'+height+'px' });
					desc.stop(true,false).animate({ marginTop:'0px' }, 1000);
				}else{
					desc.css({ visibility:'visible', height:'0px', display:'block' });
					desc.stop(true,false).animate({ height:height }, 500);
				}
			}
			var fullscreen = $(this).find('.full-screen');
			fullscreen.stop(true,false).animate({ opacity:'0.6' }, 500);
		});
		parent.find('.active-image').mouseleave(function(){
			var desc = $(this).find('.desc-block');
			var height = $(this).find('.desc-block').height();

			if($('.fullscreen-gallery').is(':visible')){
				desc.stop(true,false).animate({ height:'0px', marginTop:'0px' }, 700);
			}else{
				desc.stop(true,false).animate({ height:'0px' }, 500);
			}
			$(this).removeClass('mouseover');
			var fullscreen = $(this).find('.full-screen');
			fullscreen.stop(true,false).animate({ opacity:'0' }, 500);
		});
		parent.find('.active-image').mousemove(function(){
			if (obj.mouseTimer) {
		    	window.clearTimeout(obj.mouseTimer);
		    }
			if (!obj.thumbsVisible) {
	        	obj.showThumbs();
	    	}
		    obj.mouseTimer = window.setTimeout(function(){ obj.hideThumbs(); }, 2500);
		});
		parent.find('.thumb-container').mouseenter(function(){
			window.clearTimeout(obj.mouseTimer);
			if (!obj.thumbsVisible) {
	        	obj.showThumbs();
	    	}
		});

	},

	_init: function() {
		var obj = this;
		var parent = this.element;
		this.thumbs = parent.find('.thumb-container .thumbs');

		var itemWidth = 107;
		this.count = parent.find('.thumbs').children('.thumb').length;

		parent.find('.thumb-container .totals .total').html(this.count);
		

		parent.find('.thumbs .thumb .overlay a').mouseenter(function(){
			if(!$(this).hasClass('active')){
				if(!$.browser.msie){
					$(this).stop(true, false).fadeTo('normal', 0);
				}
			}
		});
		parent.find('.thumbs .thumb .overlay a').mouseleave(function(){
			if(!$(this).hasClass('active')){
				if(!$.browser.msie){
					$(this).stop(true, false).fadeTo('normal', 1);
				}
			}
		});
		parent.find('.large-pagination img.arrow-left').click(function(){
			obj.scrollLeft(parent.find('.thumb-container .thumbs'));
		});
		parent.find('.large-pagination img.arrow-right').click(function(){
			obj.scrollRight(parent.find('.thumb-container .thumbs'),itemWidth);
		});
		parent.find('.large-pagination').children('.pager').click(function(){
			if(!$(this).hasClass('active')){
				listItem = $(this);
				var indx = parent.find('.large-pagination').children('.pager').index(listItem);
				
				obj.circleScroll(parent.find('.thumb-container .thumbs'),indx);
			}
		});
		parent.find('.thumbs .thumb').click(function(){
			var link = $(this).find('.overlay a');
			if(!link.hasClass('active')){
				parent.find('.thumbs .thumb a.active').removeClass('active');
				parent.find('.thumbs .thumb.active').removeClass('active');
				link.addClass('active');
				if(!$.browser.msie){
					parent.find('.thumbs .thumb .overlay a').stop(true,false).fadeTo('normal', 1);
				}
				$(this).addClass('active');
				if($('.fullscreen-gallery').is(':visible') || !parent.is(':visible')){
					var image = $(this).find('img').attr('rel');
				}else{
					var image = $(this).find('img').attr('rel');
				}
				var description = $(this).find('img').attr('alt');
				var ind = obj.thumbs.children('.thumb').index($(this)) + 1;
				obj.changeImage(image, ind, description);
			}
		});


	},
	
	scrollLeft: function(el){
		var parent = this.element;
		var mleft = parseInt(el.css('margin-left'));
		if(mleft < 0 && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mleft += 642;
			el.animate({'margin-left': mleft+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			listItem = parent.find('.large-pagination').children('.pager.active');
			var active_index = parent.find('.large-pagination').children(' .pager').index(listItem);
			active_index-=1;
			parent.find('.large-pagination').children('.pager.active').removeClass('active');
			parent.find('.large-pagination').children('.pager:eq('+active_index+')').addClass('active');
		}
	},
	
	scrollRight: function(el,width){
		var parent = this.element;
		var mleft = parseInt(el.css('margin-left'));
		var max = (this.count-6)*width*-1;
		
		if(mleft > max && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mleft -= 642;
			el.animate({'margin-left': mleft+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			listItem = parent.find('.large-pagination').children('.pager.active');
			var active_index = parent.find('.large-pagination').children(' .pager').index(listItem);
			active_index+=1;
			parent.find('.large-pagination').children('.pager.active').removeClass('active');
			parent.find('.large-pagination').children('.pager:eq('+active_index+')').addClass('active');
		}
	},
	
	circleScroll: function(el,indx){
		var margin = indx*642*-1;
		var parent = this.element;
		
		if(!el.hasClass('scrolling')){
			el.addClass('scrolling');
			el.animate({'margin-left': margin+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			parent.find('.large-pagination').children('.pager.active').removeClass('active');
			parent.find('.large-pagination').children('.pager:eq('+indx+')').addClass('active');
		}
	},

	changeImage: function(image, ind, description){
		var parent = this.element;

		parent.find('.active-image .image-holder img').remove();
		parent.find('.active-image .preloader').show();
		parent.find('.active-image .image-holder').prepend('<img src="'+image+'" />');
		parent.find('.active-image .image-holder img').load(function(){
			var image = $(this);
			if($('.fullscreen-gallery').is(':visible')){
				var width = $(this).width() + 1;
				var margin_left = width / 2;
				$('.fullscreen-gallery .active-image .desc-block').css({ width:width, marginLeft: '-'+margin_left+'px' });
			}
			image.hide();
			parent.find('.active-image .preloader').fadeOut(function(){
				image.fadeIn();
			});
		});
		parent.find('.active-image .description').html(description);
		parent.find('.thumb-container .totals .current').html(ind);
	},

	hideThumbs: function(){
		this.mouseTimer = null;
        this.thumbsVisible = false;
        var parent = this.element;

		if($('.fullscreen-gallery').is(':visible')){
			var thumb_height = parent.find('.thumb-container').height();
			parent.find('.thumb-container').stop(true,false).animate({ marginTop:'0px' }, 1000);
		}
	},

	showThumbs: function(){
		this.mouseTimer = null;
        this.thumbsVisible = true;
        var parent = this.element;

		if($('.fullscreen-gallery').is(':visible')){
			var thumb_height = parent.find('.thumb-container').height();
			parent.find('.thumb-container').stop(true,false).animate({ marginTop:'-97px' }, 1000);
		}
	}

});

$.snippet = function(template, data) { 
	var i;
   
   for (i in data) {
		template = template.split('%7B' + i + '%7D').join(data[i]);
   	template = template.split('{' + i + '}').join(data[i]);
   }

   return template;
}

function initialize() {
	if ( !$.browser.msie || ( $.browser.msie && $.browser.version.substr(0,1)>6 ) ){
		var latlng = new google.maps.LatLng(51.463112,-0.210031);
		var myOptions = {
		  zoom: 15,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.SATELLITE
		};
		var map = new google.maps.Map(document.getElementById("map_canvas"),
			myOptions);

		latlng = new google.maps.LatLng(51.463112,-0.210031);
		var marker = new google.maps.Marker({
			position: latlng,
			map: map
		});

	} else {
		$('.map-container').hide();
	}
}

/*******
	***	Anchor Slider by Cedric Dugas   ***
	*** Http://www.position-absolute.com ***
	
	Never have an anchor jumping your content, slide it.

	Don't forget to put an id to your anchor !
	You can use and modify this script for any project you want, but please leave this comment as credit.
	
*****/
jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function() {
		var caller = this
		$(caller).click( function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

$().ready(function() {

	$(".anchor-scroll").anchorAnimate();

	$('.instructors-container').accordion({
		collapsible: true,
		active: false,
		clearStyle: true
	});

	if( $('#map_canvas').length > 0 ){
		initialize();
	}

	$('.full-gallery-button').click(function(){
		var image_info_lists = $(this).closest('.media-container').find('.gallery-hidden').find('ul');
		var image_length = image_info_lists.length;
		var thumb_clone = $('.fullscreen-gallery .thumb-container .thumbs .clonable').html();
		var thumb_html = '';
		var max_width = 110 * image_length;


		image_info_lists.each(function(){
			var path = $(this).find('li[rel="image-thumb-path"]').html();
			var description = $(this).find('li[rel="image-alt"]').html();
			var large = $(this).find('li[rel="image-path"]').html();
			var original = $(this).find('li[rel="image-path"]').html();

			thumb_html += $.snippet(thumb_clone, {
				path: path,
				description: description,
				large: large,
				original: original
			});
		});

		$('.fullscreen-gallery .thumb-container .thumbs').children('.thumb').remove();
		$('.fullscreen-gallery .thumb-container .thumbs').append(thumb_html); //FIX
		$('.fullscreen-gallery .thumb-container .large-pagination').children('.pager').remove();
		//$('.fullscreen-gallery .thumb-container .large-pagination .clonable').after(circle_html); //FIX
		$('.fullscreen-gallery').gallery();
		$('.fullscreen-gallery .thumb-container .thumbs').css({ width: max_width+'px' }); //FIX
		$('.fullscreen-gallery .thumb-container .thumbs').children('.thumb:first').click();
		$('.fullscreen-gallery .thumb-container .large-pagination').children('.pager:first').addClass('active');

		$('.fullscreen-gallery .thumbs').children('.thumb:first').click();
		$('.fullscreen-gallery').fadeTo(500, 1, function(){
			$('.wrapper').css({ height:'0px', overflow:'hidden', width:'0px', display:'none' });
			$('.fullscreen-gallery .active-img img').hide();
			$('.fullscreen-gallery .active-image .preloader').fadeOut(function(){
				$('.fullscreen-gallery .active-img img').fadeIn();
			});
		});
		$('.fullscreen-gallery .desc-block').css({ width: $('.fullscreen-gallery .active-image img').width()+1, marginTop: '0px' }).hide();
		$(window).trigger('resize');

	});

	$(window).resize(function(){
		if($('.fullscreen-gallery').is(':visible')){
			$('.fullscreen-gallery .thumb-container .thumb-infinite .next-side').remove();
			$('.fullscreen-gallery .thumb-container .thumb-infinite .prev-side').remove();
			var thumb_width = $('.fullscreen-gallery .thumb-container .thumbs').width();
			var doc_width = $(document).width();
			if(thumb_width > doc_width){
				var diff = thumb_width - doc_width;
				$('.fullscreen-gallery .thumb-container .thumb-infinite').append('<a class="next-side" href="javascript:void(0);">&#187;</a>');
				$('.fullscreen-gallery .thumb-container .thumb-infinite').append('<a class="prev-side" href="javascript:void(0);">&#171;</a>');
				$('.fullscreen-gallery .thumb-container .next-side').click(function(){
					var orig_margin = parseInt($('.fullscreen-gallery .thumb-container .thumbs').css('margin-left')) * -1;
					var count = $('.fullscreen-gallery .thumb-container .thumbs').children('.thumb').length;
					var el = $('.fullscreen-gallery .thumb-container .thumbs');

					if((doc_width + orig_margin) / 110 < count){
						if(!el.hasClass('scrolling')){
							el.addClass('scrolling');
							el.animate({
								marginLeft: '-='+doc_width+'px'
							}, 1000, function(){
								el.removeClass('scrolling');
							});
						}
					}
				});
				$('.fullscreen-gallery .thumb-container .prev-side').click(function(){
					var orig_margin = parseInt($('.fullscreen-gallery .thumb-container .thumbs').css('margin-left'));
					var el = $('.fullscreen-gallery .thumb-container .thumbs');

					if(orig_margin != 0){
						if(!el.hasClass('scrolling')){
							el.addClass('scrolling');

							if( orig_margin < doc_width ){
								
								el.animate({
									marginLeft: '0px'
								}, 1000, function(){
									el.removeClass('scrolling');
								});

							}else{

								el.animate({
									marginLeft: '+='+doc_width+'px'
								}, 1000, function(){
									el.removeClass('scrolling');
								});

							}
						}
					}
				});
			}
		}
	});
	$('.fullscreen-gallery').gallery();
	$('.fullscreen-gallery .close').click(function(){
		$('.wrapper').css({ height:'auto', overflow:'visible', width:'1008px', display:'block' });
		$('.fullscreen-gallery').fadeTo(500, 0, function(){
			$('.fullscreen-gallery').hide();
		});
	});

	$('.agreement-ltbx .i-accept-btn').click(function(){
		$('.lightbox.agreement-ltbx').hide().closest('.lightbox-overlay').hide();
		$('.booking-left .form form input[name="terms"]').val('1');
		$('.booking-left .form form').submit();
	});

	$('.booking-left .form form').validation();
	$('.validate-form').validation({ valtype:'inline' });

	$('input.step-hidden').click(function(){

		$('.booking-left .form form').validation({ validate: 'validate' });

		if( $('.booking-left .form form ').hasClass('validated') ){
			$('.booking-left .form form .hidden').removeClass('hidden');

			$(this).unbind('click');
		}
		return false;
	});
	
	
	$('select.coursesselect option').not(':first').attr('disabled',true);
	$('select.coursesselect option').not(':first').css('display','none');

	$('select.resortsselect').change(function(){
		var rid = $(this).val();

		$('select.coursesselect option').attr('disabled',true);
		$('select.coursesselect option').css('display','none');
		$('select.coursesselect option').each(function(){
			var nids = $(this).attr('title').split(',');
			var el = $(this);

			for( var i=0; i<nids.length; i+=1 ){
				if( nids[i] == rid || nids[i] == '' ){
					el.attr('disabled', false);
					el.css('display', 'block');
				}
			}
		});
	});

	$('form .radios').children('div').click(function(){
		$(this).parent().children('.active').removeClass('active');
		$(this).parent().find('input[type="radio"]').removeAttr('checked');
		$(this).addClass('active');
		$(this).find('input[type="radio"]').attr('checked', 'checked');

		return false;
	});

	$('.color-overlay .close, .video-overlay').click(function(){
		$('.color-overlay .video-box').css({ visibility: 'hidden' }).html('');
		$('.color-overlay').fadeOut(function(){
			$('.color-overlay .video-box').css({ visibility: 'visible' });
		});
	});

	$('form').find('.inputfield, .textarea').each(function(){
		if( !$(this).hasClass('helpervalue') ){
			$(this).val($(this).attr('title'));
		}
	});
	$('form').find('.inputfield, .textarea').focus(function(){
		if( !$(this).hasClass('helpervalue') ){
			if($(this).attr('title') == $(this).val()){
				$(this).val('');
			}
		}
	}).blur(function(){
		if( !$(this).hasClass('helpervalue') ){
			if($(this).val().length < 1){
				$(this).val($(this).attr('title'));
			}
		}
	});

	$('.lightbox .lightbox-right-side ').videoList();

	$('.resort-questions .list').accordeon();

	$('.content-lightbox-button').live('click',function(){
		$(this).contentLightbox();
		return false;
	});

	$('.video-lightbox-button').live('click',function(){
		var nid = $(this).attr('rel');

		$('.color-overlay .lightbox-container').hide();
		$('.color-overlay .loading').show();
		$('.color-overlay').show();
		
		$('.videos-list').load('/video-list #load-videos-list', function() {
			$('.color-overlay .loading').hide();
			$('.color-overlay .lightbox-container').show();
			$('.color-overlay, .color-overlay .videos-ltbx').show();
			$('.color-overlay').center(-50);
			$('.color-overlay, .color-overlay .videos-ltbx').show();
			$('#load-videos-list .item .video-thumb a[rel="'+nid+'"]').closest('.video-thumb').click();
		});
	
		return false;
	});

	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";

	if( $('.upcoming-courses-container').length > 0 ){
		$('.upcoming-courses-container .upcoming-courses .heading').html( $('.course-filter-name').html() );
	}

	$('.upcoming-courses-container .categories a').click(function(){
		if( !$(this).hasClass('active') ){
			$('.upcoming-courses-container .date-list .month').remove();
			$('.upcoming-courses-container .categories a').removeClass('active');
			$(this).addClass('active');
			var rel = $(this).attr('rel').split(',');
			var action = rel[0];
			var field = rel[1];
			var content = rel[2];

			
		
			$('.upcoming-courses-container .date-list .lesson .'+field).each(function(){
				if( $(this).text() != content ){
					$(this).closest('.lesson').hide();
				}else{
					$(this).closest('.lesson').show();
				}
			});
		
			var months = new Array();
				
			if( action == 'filter' ){
				$('.upcoming-courses-container .date-list .lesson:visible').each(function(){				
					var d = new Date( parseInt( $(this).find('.start-date').text() ) * 1000 );
					var index = d.getFullYear() + '_' + d.getMonth();
					if( months[ index ] != true ){
						months[ index ] = true;
						$(this).before('<li class="month"><div class="header">'+month[d.getMonth()]+' '+d.getFullYear()+'</div></li>');
					}
				});
			}
		}
		
		return false;
	});

	$('.upcoming-courses-container .categories2 a').click(function(){
		if( !$(this).hasClass('active') ){
			var rel = $(this).attr('rel').split(',');

			$('.upcoming-courses-container .date-list .month').remove();
			$('.upcoming-courses-container .categories2 a').removeClass('active');
			$(this).addClass('active');
			var action = rel[0];
			var field = rel[1];
			
			var groups = new Array();

			$('.upcoming-courses-container .date-list .'+field).each(function(){

				if(field == 'start-date'){
					var d = new Date( $(this).html() * 1000 );
					var content = month[d.getMonth()]+' '+d.getFullYear();
					
				}else{
					var content = $(this).html();
				}
				if(field == 'price'){
					content = content.replace('£','');
					content = '&pound;'+content;
				}

				var element = $(this).closest('.lesson');
				var elementHTML = element.clone().wrap('<div></div>').parent().html();


				if( typeof groups[content] == "undefined" ){
					groups[content] = new Array();
				}
				groups[content].push( elementHTML );

				element.remove();
			});

			if(field == 'resort' || field == 'price'){
				
			}

			for(var i in groups){
				$('.upcoming-courses-container .date-list ul').append('<li class="month"><div class="header">'+i+'</div></li>');
				for(var j in groups[i]){
					$('.upcoming-courses-container .date-list ul').append(groups[i][j]);
				}
			}

		}
		
		return false;
	});
	$('.upcoming-courses-container .categories a:first').click();
	$('.upcoming-courses-container .categories2 a:first').click();
	
	$('.contact-form').find('.inputfield').each(function(){
		$(this).val($(this).attr('title'));
	});
	$('.contact-form').find('.inputfield').click(function(){
		if($(this).attr('title') == $(this).val()){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val().length < 1){
			$(this).val($(this).attr('title'));
		}
	});
	
	
	$('.blog-content .authors').membersTooltip({ container: '.author-box' });
	$('.crew .author-box').membersTooltip({ container: '.member' });
	/*$('.authors .photo-frame').live('click', function() {
		$('.author-info-clone .more a').attr('href', $('.author-info-clone .more a').attr('rel') );
		$('.author-info-clone .photo img').attr('src', $('.author-info-clone .photo img').attr('title') );
		
		var offset = $(this).closest('.member').position();

		var height = $('.author-info').height();
		var width = $('.author-info').width();
		$('.author-info').html( $('.author-info-clone').html() );

		var left = offset.left - width;
		var top = offset.top - height;
		
		var picheight = $(this).closest('.photo').height();
		var picwidth = $(this).closest('.photo').width();
		
		
		$(this).parent().next('.author-info-hidden').find('li').each(function() {
			var replacement = $(this).attr('class').split('-');
			var val = $('.author-info').html();
			var regex = new RegExp( "\-\-" + replacement[1] + "\-\-", "g" );
			var matches = val.match(regex);
			var occurrences = ( matches ) ? matches.length : 0;
			
			for( var i = 0; i < occurrences; i++ ) {
				$('.author-info').html( $('.author-info').html().replace( "\-\-" + replacement[1] + "\-\-", $(this).html() ) );
			}
		});
	
		// $('.author-info').css('top', offset.top-height+picheight + "px");
		$('.author-info').css('top', top + "px");
		// $('.author-info').css('left', offset.left-width+picwidth + "px");
		$('.author-info').css('left', left + "px");
		$('.author-info').fadeIn('slow');
	});*/
	
	$('.author-info .close-button').live('click', function() {
		$(this).parent().fadeOut('slow');
	});
	
	$('#tab-container .tab a').click(function() {		
		if( !$(this).hasClass('ui-tabs-selected') ) {
			var id = '#'+$(this).attr('href').replace('#','');
			$('.tab-container .tab-content:visible').stop(true,true).fadeOut('fast', function(){	
				$(id).stop(true,true).stop(true,true).fadeIn('fast');
			});
			$('#tab-container .tab a.ui-tabs-selected').removeClass('ui-tabs-selected');
			
			$(this).addClass('ui-tabs-selected');
		}
		
		return false;
	});
	
	$('.booking-left .form form').submit(function(){
		if( $(this).hasClass('validated') && $(this).find('input[name="terms"]').val() == '1' ){
			$(this).find('input[type="text"], textarea').each(function(){
				if( $(this).val() == $(this).attr('title') ){
					$(this).val('');
				}
			});
		}else{
			if( $(this).hasClass('validated') || $(this).find('.error').length == 0 ){
				$('.lightbox.agreement-ltbx').closest('.lightbox-overlay').find('.terms').html( $('.termsbox').html() );
				$('.lightbox.agreement-ltbx').show().closest('.lightbox-overlay').show();
				$('.lightbox-overlay').center(-50);
				$('.lightbox.agreement-ltbx').show().closest('.lightbox-overlay').show();
			}
		}
	});

	$('.agreement-ltbx .bottom-buttons .decline-lnk, .lightbox-overlay .video-overlay, .agreement-ltbx .lightbox-content-container .close a').click(function(){
		$(this).closest('.lightbox-overlay').hide();

		return false;
	});

	$('.video-container .watch-big').click(function(){
		var embedcode  = $(this).next('.embed-code').html();

		$(this).hide();
		$(this).prev('img').replaceWith(embedcode);

		return false;
	});

	$('.email-signup-left-side .form form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
		if(data == 'success'){
			document.location = '/submission-successful';
		}
	}});

	$('.free-borochure-container .free-borochure').hover(function(){
		$(this).css({ cursor: 'pointer' });
	});
	$('.free-borochure-container .free-borochure').click(function(){
		$(this).find('a.pink').click();
	});

	$('.lightbox-dyno-content-button').click(function(){
		var lbox = $(this).attr('rel');
		var lbox_html = $('.lightbox.'+lbox).clone().wrap('<div></div>').parent().html();

		$('.generic-overlay .lightbox').replaceWith( lbox_html );
		$('.generic-overlay').center(-50);
		$('.generic-overlay').stop(true,true).fadeIn();

		return false;
	});

	$('.generic-overlay .video-overlay, .generic-overlay .close a').live('click', function(){
		$(this).closest('.generic-overlay').stop(true,true).fadeOut();
	});

	$('.lets-talk-ltbx .send-us-a-message-btn').live('click',function(){
		$(this).closest('.generic-overlay').stop(true,true).fadeOut();

		var lbox_html = $('.lightbox.send-message-ltbx').clone().wrap('<div></div>').parent().html();

		$('.generic-overlay .lightbox').replaceWith( lbox_html );
		$('.generic-overlay').center(-50);
		$('.generic-overlay').stop(true,true).fadeIn();

		$('.generic-overlay form.validate').validation();
		$('.generic-overlay .helpervalue').helperValue();

		$('form.contact-form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
			if(data == 'success'){
				document.location = '/submission-successful';
			}
		}});

		return false;
	});

	$('.course-controls .button-container .send-a-message-btn').click(function(){

		var lbox_html = $('.lightbox.send-message-ltbx').clone().wrap('<div></div>').parent().html();

		$('.generic-overlay .lightbox').replaceWith( lbox_html );
		$('.generic-overlay').center(-50);
		$('.generic-overlay').stop(true,true).fadeIn();

		$('.generic-overlay form.validate').validation();
		$('.generic-overlay .helpervalue').helperValue();

		$('form.contact-form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
			if(data == 'success'){
				document.location = '/submission-successful';
			}
		}});

		return false;
	});

	$('.send-us-a-message-btn').click(function(){

		var lbox_html = $('.lightbox.send-message-ltbx').clone().wrap('<div></div>').parent().html();

		$('.generic-overlay .lightbox').replaceWith( lbox_html );
		$('.generic-overlay').center(-50);
		$('.generic-overlay').stop(true,true).fadeIn();

		$('.generic-overlay form.validate').validation();
		$('.generic-overlay .helpervalue').helperValue();

		$('form.contact-form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
			if(data == 'success'){
				document.location = '/submission-successful';
			}
		}});

		return false;
	});

	$('form.validate').validation();
	$('.helpervalue').helperValue();

	$('.contact-form form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
		if(data == 'success'){
			document.location = '/submission-successful';
		}
	}});
	$('.lp-right-side form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
		if(data == 'success'){
			document.location = '/submission-successful';
		}
	}});

	$('.dates-n-prices.camps-table .surf-adventures-row').remove();
	$('.dates-n-prices.camps-table .surf-instructor-training-row').remove();
	$('.dates-n-prices.adventure-table .surf-camps-row').remove();
	$('.dates-n-prices.adventure-table .surf-instructor-training-row').remove();
	
	$('.dates-n-prices.instructor-table .surf-camps-row').remove();
	$('.dates-n-prices.instructor-table .surf-adventures-row').remove();
	
	$('.dates-n-prices.instructor-table').show();
	$('.dates-n-prices.adventure-table').show();
	$('.dates-n-prices.camps-table').show();

	$('table.sortable').each(function() {
		var $table = $(this);

		$('th', $table).each(function(column) {

			if ($(this).is('.sort-alpha')) {
				$(this).addClass('clickable').hover(function() {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
				}).click(function() {
					var rows = $table.find('tbody > tr').get();

					rows.sort(function(a, b) {
						var keyA = $(a).children('td').eq(column).text().toUpperCase();
						var keyB = $(b).children('td').eq(column).text().toUpperCase();
						if (keyA < keyB) return -1;
						if (keyA > keyB) return 1;

						return 0;
					});
					$.each(rows, function(index, row) {
						$table.children('tbody').append(row);
					});

					$('table.sortable tbody tr:odd').removeClass('odd').removeClass('even').addClass('odd');
					$('table.sortable tbody tr:even').removeClass('odd').removeClass('even').addClass('even');
				});
			}

			if ($(this).is('.sort-time')) {
				$(this).addClass('clickable').hover(function() {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
				}).click(function() {
					var rows = $table.find('tbody > tr').get();

					rows.sort(function(a, b) {
						var keyA = $(a).children('td').eq(column).find('.timestamp').text().toUpperCase();
						var keyB = $(b).children('td').eq(column).find('.timestamp').text().toUpperCase();
						if (keyA < keyB) return -1;
						if (keyA > keyB) return 1;

						return 0;
					});
					$.each(rows, function(index, row) {
						$table.children('tbody').append(row);
					});

					$('table.sortable tbody tr:odd').removeClass('odd').removeClass('even').addClass('odd');
					$('table.sortable tbody tr:even').removeClass('odd').removeClass('even').addClass('even');
				});
			}

		});
	});
	$('.lightbox-container').find('a.contextual-links-trigger').live('click',function(){
		$(this).next('ul').stop(true,true).slideToggle();
	});

	$('.content .dates-banner .navigation li a').click(function(){
		if( !$(this).hasClass('active') ){
			$('.content .dates-banner .navigation li .active').removeClass('active');
			$(this).addClass('active');
			$('table.sortable th.'+ ( $(this).attr('rel') ) ).click();
		}
	});

	$('.content .dates-banner .navigation li a[rel="course"]').click();
	
	$('table.what-to-expect').hide();
	$('.what-to-expect-banner .navigation li a').click(function(){
		$('table.what-to-expect').hide();
		$('table.what-to-expect.'+ $(this).attr('rel') ).show();
		
		location.hash = '#' + $(this).attr('rel');
		
		return false;
	});
	
	if( $('.what-to-expect-banner .navigation li a').length > 0 ){
		var hash = location.hash;
		hash = hash.replace('#','');
		if( hash.length > 0 ){
			$('.what-to-expect-banner .navigation li a[rel="'+hash+'"]').click();
		}else{
			$('.what-to-expect-banner .navigation li a:first').click();
		}
	}
	
	
	$('a.skypejslink').each(function(){
		$(this).attr('href', $(this).attr('rel') );
	});
	
	$(document).keyup(function(e){
		if( e.keyCode == 27 ){
			if( $('.fullscreen-gallery').is(':visible') && $('.fullscreen-gallery').css('opacity') == 1 ){
				$('.fullscreen-gallery .close').click();
			}
		}
	});

	$('.country-info .list .item .info-header').click(function(){
		if( $(this).next('.wrap-box').length > 0 ){
			$(this).next('.wrap-box').stop(true,true).slideToggle(300, 'linear');
		}
	});

	$('.world-map .close a').click(function(){
		$(this).closest('.details').prev('.pointer').removeClass('active');
		$(this).closest('.details').fadeOut();

		return false;
	});

	$('.resorts .world-map .pointer').click(function(){
		if( !$(this).hasClass('active') ){
			$('.resorts .world-map .pointer.active').removeClass('active');
			$(this).addClass('active');
			$('.world-map .point .details:visible').hide();
			$(this).next('.details').stop(true,true).fadeIn(200);

			if( $(window).scrollTop() > $(this).next('.details').offset().top ){
				$('html,body').animate({
					scrollTop: $(this).next('.details').offset().top
				}, 400);
			}

			return false;
		}
	});

	$('.foundation').click(function(){
		//window.location = "http://www.ttridefoundation.com/";
		window.open('http://www.ttridefoundation.com/'); return false;
	});

	/* LATEST NEWS */
	var news = $('.media-bar .media li');
	var len = $(news).size();
	var counter = 0;
	if(len > 1){
		$(news).hide();
		$(news[0]).show();
		setInterval(function() {
			$(news[counter]).hide( 'slide', {direction: 'up'} );
			counter++;
			if (counter == news.length) {
				counter = 0;
			}
			$(news[counter]).show( 'slide', {direction: 'down'} );
		}, 4000);
	} 
	
	$('.social-bar .message a').click(function(){

		var lbox_html = $('.lightbox.send-message-ltbx').clone().wrap('<div></div>').parent().html();

		$('.generic-overlay .lightbox').replaceWith( lbox_html );
		$('.generic-overlay').center(-50);
		$('.generic-overlay').stop(true,true).fadeIn();

		$('.generic-overlay form.validate').validation();
		$('.generic-overlay .helpervalue').helperValue();

		$('form.contact-form').ajaxform({address:'/forms/sendemails.php', reqclass:'validated', change: function(data){
			if(data == 'success'){
				document.location = '/submission-successful';
			}
		}});

		return false;
	});
	
	$('.social-bar .close').click(function(){
		setCookie('social-bar', '0', 1);
		$('.social-bar').animate({
			bottom: '-65px'
		}, 500, 'linear', function(){
			$(this).hide();
		});
		return false;
	});
	
	if( getCookie('social-bar') != 0 ){
		$('.social-bar').show();
	}

});


function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}
