$.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('title');
				}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;
}

$.widget('ui.changeGallery', {

	_create: function() {
		var obj = this;
		var parent = this.element;
		this.thumbs = parent.find('.thumb-container .thumbs');
		var itemWidth = 680;
		this.count = parent.find('.six-small-gallery').length;
		this.galleries = parent.find('.small-gallery');

		this.galleries.find('.overlay a').click(function(e){
			var gallery = $(this).closest('.small-gallery');
			obj.change(gallery);
			if(e.originalEvent!=undefined){
				$('html, body').animate({
					scrollTop: $(".gallery-container").offset().top
				}, 2000);
			}
		});

		parent.find('.small-gallery:first .overlay a').click();

		parent.find('.next-previous .previous').click(function(){
			obj.scrollLeft(parent.find('.small-galleries-infinite'));
		});
		parent.find('.next-previous .next').click(function(){
			obj.scrollRight(parent.find('.small-galleries-infinite'),itemWidth);
		});
		parent.find('.small-pagination').children('.pager').click(function(){
			if(!$(this).hasClass('active')){
				listItem = $(this);
				var indx = parent.find('.small-pagination').children('.pager').index(listItem);
				
				obj.pageScroll(parent.find('.small-galleries-infinite'),indx);
			}
		});

	},

	change: function(gallery){
		var files = gallery.find('.files .file');
		var title = gallery.find('.gallery-title strong').html();
		var file_length = files.length;
		var thumb_clone = $('.large-gallery .thumb-container .thumbs .clonable').html();
		var thumb_html = '';
		var circle_count = Math.ceil( file_length / 6 );
		var circle_clone = $('.large-gallery .thumb-container .large-pagination .clonable').html();
		var circle_html = '';
		var i = 0;
		var max_width = 110 * file_length;

		files.each(function(){
			var path = $(this).find('.detail-tiny').html();
			var description = $(this).find('.detail-description').html();
			var large = $(this).find('.detail-large').html();
			var original = $(this).find('.detail-original').html();
			thumb_html += $.snippet(thumb_clone, {
				path: path,
				description: description,
				large: large,
				original: original
			});
		});

		for(i=0;i < circle_count;i++){
			circle_html += circle_clone;
		}

		$('.gallery-container h1.title').html(title);
		//Cufon.set('fontSize', '24px').replace('.gallery-container h1.title');
		$('.large-gallery .thumb-container .thumbs').children('.thumb').remove();
		$('.large-gallery .thumb-container .thumbs').append(thumb_html);
		$('.large-gallery .thumb-container .large-pagination').children('.pager').remove();
		$('.large-gallery .thumb-container .large-pagination .clonable').after(circle_html);
		$('.large-gallery').gallery();
		$('.large-gallery .thumb-container .thumbs').children('.thumb:first').click();
		$('.large-gallery .thumb-container .large-pagination').children('.pager:first').addClass('active');

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

	},
	
	scrollLeft: function(el){
		var mleft = parseInt(el.css('margin-left'));
		if(mleft < 0 && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mleft += 680;
			el.animate({'margin-left': mleft+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			listItem = $('.small-pagination').children('.pager.active');
			var active_index = $('.small-pagination').children(' .pager').index(listItem);
			active_index-=1;
			$('.small-pagination').children('.pager.active').removeClass('active');
			$('.small-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-1)*width*-1;
		
		if(mleft > max && !el.hasClass('scrolling')){
			el.addClass('scrolling');
			mleft -= 680;
			el.animate({'margin-left': mleft+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			listItem = $('.small-pagination').children('.pager.active');
			var active_index = $('.small-pagination').children(' .pager').index(listItem);
			active_index+=1;
			$('.small-pagination').children('.pager.active').removeClass('active');
			$('.small-pagination').children('.pager:eq('+active_index+')').addClass('active');
		}
	},
	
	pageScroll: function(el,indx){
		var margin = indx*680*-1;
		
		if(!el.hasClass('scrolling')){
			el.addClass('scrolling');
			el.animate({'margin-left': margin+'px'},1000,function(){
				el.removeClass('scrolling');
			});
			$('.small-pagination').children('.pager.active').removeClass('active');
			$('.small-pagination').children('.pager:eq('+indx+')').addClass('active');
		}
	}

});







$.widget('ui.frontColumns', {
	options: {
	},
	_create: function() {
		var obj = this;
		var parent = this.element;
		
		parent.find('.block-small').each(function(){
			var pos = $(this).position();
			
			$(this).css({ bottom: '0px', left: pos.left+'px' });
		});
		
		parent.find('.block-small').css({ position: 'absolute' });
		var links = parent.find('.block-small .links');
		links.hide();
		
		parent.find('.block-small').mouseenter(function(){
			$(this).find('.links').stop(true,false).slideDown();
		});
		parent.find('.block-small').mouseleave(function(){
			$(this).find('.links').stop(true,true).slideUp(function(){
				$(this).css({ display: 'none', height: '' });
			});
		});
	}
	
});

$.widget('ui.imgRotate', {
	options: {
	},
	_create: function() {
		var obj = this;
		var parent = this.element;
		
		var images = parent.find('.rotating-image');
		var counter = 0;
		$(images).hide();
		$(images[0]).show();
		setInterval(function() {
			$(images[counter]).fadeOut('slow');
			 counter++;
			 if (counter == images.length) {
				 counter = 0;
			 }
			$(images[counter]).fadeIn(1000);
		}, 5000);
	}
	
});

$.widget('ui.helperValue', {
	_create: function() {
		var obj = this;
		var parent = this.element;
		
		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.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.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');
					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;
			
			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;
			
			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')){
					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;
			}
			
		}
		
	});

$().ready(function() {
	
	$( "#datepicker" ).datepicker({
			showOn: "both",
			buttonImage: "/sites/all/themes/surfacademy/images/calendar.jpg",
			buttonImageOnly: true
		});

	$('.lesson-list .dropdown').children('li').click(function(){
		if( !$(this).hasClass('active') ){
			var subitem = $(this).find('.subitem');
			var me = $(this);
			console.log(subitem.html().length);
			if( subitem.html().length > 22 ){
				$('.lesson-list .dropdown').children('.active').find('.subitem').stop(true,true).slideUp(function(){
					subitem.stop(true,true).slideDown();
					$('.lesson-list .dropdown').children('.active').children('.active').removeClass('active');
					$('.lesson-list .dropdown').children('.active').removeClass('active');
					subitem.parent().addClass('active');
					subitem.prev('a').addClass('active');
				});
			}
		}
	});
	
	$('.validate-form').validation({ valtype: 'inline' });
	
	$('.helpervalue').helperValue();
	$('.block-container').frontColumns();
	$('.content .upper-block .banner').imgRotate();

	$('.enquiry-form .form-container form').ajaxform({address:'/php/sendemails.php', reqclass:'validated', change: function(data){
		if(data == 'success'){
			$('.enquiry-form .form-container').fadeOut(function(){
				$('.enquiry-form .thankyou').fadeIn();
			});
		}
	}});

	$('.content .bottom-block .news-n-offers .from-container form').ajaxform({address:'/php/sendemails.php', reqclass:'validated', change: function(data){
		if(data == 'success'){
			alert('Thank you for signing up!');
		}
	}});

	$(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');
								});

							}
						}
					}
				});
			}
		}
	});
	
	$('.large-gallery').gallery();
	$('.fullscreen-gallery').gallery();

	$('.large-gallery .full-screen').click(function(){
		$('.large-gallery .desc-block').css({ height:'0px' });

		listItem = $('.large-gallery .thumbs').children('.thumb.active');
		var active_index = $('.large-gallery .thumbs').children('.thumb').index(listItem);

		$('.fullscreen-gallery .thumbs').children('.thumb:eq('+active_index+')').click();
		$('.fullscreen-gallery').css({ opacity: '0', display: 'block' });
		$('.fullscreen-gallery').fadeTo(500, 1, function(){
			$('.wrapper').css({ height:'0px', overflow:'hidden', width:'0px' });
			$('.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');
	});
	$('.fullscreen-gallery .close').click(function(){
		$('.wrapper').css({ height:'auto', overflow:'visible', width:'966px' });
		$('.fullscreen-gallery').fadeTo(500, 0, function(){
			$('.fullscreen-gallery').hide();
		});
	});

	$('.small-galleries').changeGallery();
	
	$('.content .upper-block .banner .block-container .last').click(function(){
		window.location = '/surf-instructor-training';
	});
	
});

