function photoGalleryInit() {
	if($(".photo-gallery").length > 0) {
		$('#gallery-thumbnails').jcarousel({
    		scroll: 5
		});
	
		$(".photo-gallery .module-content .gallery-preview").append("<img src='' />");
		
		$("#gallery-thumbnails li a").click(function() {
			photoGalleryPreview(this);
			return false;
		});
		
		$(".photo-gallery .module-content .gallery-preview").hover(function() {
			if ($(this).children(".gallery-caption p").children("p").size() > 0) {
				$(this).children(".gallery-caption").stop();
				$(this).children(".gallery-caption").slideDown(function() {
					$(this).css("height", "auto");
				});
			}
			}, function() {
			if ($(this).children(".gallery-caption p").children("p").size() > 0) {
				$(this).children(".gallery-caption").stop();
				$(this).children(".gallery-caption").slideUp(function() {
					$(this).css("height", "auto");
				});
			}
		});
		
		//fixes FF highlighting bug when clicking near the navigation arrows 
		$(".photo-gallery .module-content #gallery-thumbnails").css("-moz-user-select", "none");
		
		//Kicks off the Preview by displaying the first image
		photoGalleryPreview($("#gallery-thumbnails li:first a"));
	}
}

function photoGalleryPreview(objThumbnailLink) {
	var currentImageSrc = $(".photo-gallery .module-content .gallery-preview img").attr("src");
	
	if(currentImageSrc == undefined)
		currentImageSrc = "";
		
	if (currentImageSrc.search($(objThumbnailLink).attr("href")) == -1) {
		$(".photo-gallery .module-content .gallery-preview .gallery-caption p").remove();
		if($(objThumbnailLink).children("img").attr("alt") != undefined && $(objThumbnailLink).children("img").attr("alt") != "") {		
			$(".photo-gallery .module-content .gallery-preview .gallery-caption").html("<p>" + $(objThumbnailLink).children("img").attr("alt") + "</p>");
		}
		$("#gallery-thumbnails li a").removeClass("active");
		$(objThumbnailLink).addClass("active");
		$(".photo-gallery .module-content .gallery-preview img").hide();
		$(".photo-gallery .module-content .gallery-loader").show();
		
		var galleryPreviewImage = new Image();
		
		galleryPreviewImage.onload=function() {	
	
			//calculates the center of the screen for the Funkfe Foto
			var imageLeftMargin = galleryPreviewImage.width / 2;
			var imageTopMargin = galleryPreviewImage.height / 2;
	
			$(".photo-gallery .module-content .gallery-preview img").css("marginLeft", "-" + imageLeftMargin + "px");
			$(".photo-gallery .module-content .gallery-preview img").css("marginTop", "-" + imageTopMargin + "px");					
			$(".photo-gallery .module-content .gallery-preview img").attr("src", galleryPreviewImage.src);													
			$(".photo-gallery .module-content .gallery-loader").hide();
			$(".photo-gallery .module-content .gallery-preview img").fadeIn(300);	
		}
		
		galleryPreviewImage.src = $(objThumbnailLink).attr("href");
	}
}

function formInit() {
	if($.validator != undefined && $("#comment_form").size() > 0) {
		$("#comment_form").validate({
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},
				comment: "required",
				captcha: "required"
			},
			messages: {
				name: "Please enter your name",
				email: "Please enter a valid email address",
				comment: "Don't forget your comment!",
				captcha: "We need to make sure you're human."
			}
		});
	}
	
	if($.validator != undefined && $("#competition_form").size() > 0) {
		$("#competition_form .module-content").prepend("<p class='error'>Oops! You haven&acute;t filled in all the required fields marked with an asterisk *</p>");
		$("#competition_form").validate({
			rules: {
				first_name: "required",
				last_name: "required",
				email: {
					required: true,
					email: true
				},
				phone: {
					minLength: 8,
					required: true,
					digits: true
				},
				postcode: {
					required: true,
					minLength: 4,
					digits: true
				},
				message: "required",
				terms: "required"
			},
			messages: {
				first_name: "Please enter your first name",
				last_name: "Please enter your last name",
				email: "Please enter a valid email address",
				phone: "Please enter your phone number (Numbers only)",
				postcode: "Please enter your postcode",
				message: "Please enter your enquiry",
				terms: "You must accept the terms and conditions before submitting"
			},
			errorContainer: ".competition_form .error"
		});
	}
	
	jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
		return (value != param);
	});
	
	if($.validator != undefined && $("#subscription").size() > 0) {
		$("#subscription").validate({
			wrapper: "p",
			errorLabelContainer: "#subscription-message-box",
			rules: {
				name: {
					required: true,
					notEqualTo: "name"
				},
				email: {
					required: true,
					email: true
				},
				postcode: {
					required: false,
					minLength: 4,
					digits: true
				}
			},
			messages: {
				name: "Please enter your name",
				email: "Please enter a valid email address",
				postcode: "Please enter a valid postcode"
			}
		});
	}
	
	$("#subscription .email").focus(function () {
		if(this.value == 'email address'){
			this.value = "";
		}
	});
	
	$("#subscription .email").blur(function () {
		if(this.value == ""){
			this.value = 'email address';
		}
	});
	
	$("#subscription .name").focus(function () {
		if(this.value == 'name'){
			this.value = "";
		}
	});
	
	$("#subscription .name").blur(function () {
		if(this.value == ""){
			this.value = 'name';
		}
	});
	
	$("#subscription .postcode").focus(function () {
		if(this.value == 'postcode'){
			this.value = "";
		}
	});
	
	$("#subscription .postcode").blur(function () {
		if(this.value == ""){
			this.value = 'postcode';
		}
	});
}

function bannerInit() {
	var fixedBanners = new Array();
	$('.banner-fixed').each(function () {
		var fixedBannerPos = $(this).position();
		fixedBanners.push({"banner" : $(this), "positionTop" : fixedBannerPos.top});

		if (isBannerPastView(fixedBannerPos.top)) {
  			$(this).height($(this).height());
  			$(this).addClass('banner-is-fixed');
  			if ($.browser.msie && parseInt(jQuery.browser.version) == 6) {
  				$('.module-content', $(this)).css('top', $(window).scrollTop());
  			}
  		}
	});
	
	$(window).scroll(function(){
		$(fixedBanners).each(function (index) {
  			if (isBannerPastView($(this)[0].positionTop)) {
  				$($(this)[0].banner).height($($(this)[0].banner).height());
  				$($(this)[0].banner).addClass('banner-is-fixed');
  				if ($.browser.msie && parseInt(jQuery.browser.version) == 6) {
  					$('.module-content', $(this)[0].banner).css('top', $(window).scrollTop());
  				}
  			} else {
  				$($(this)[0].banner).height('auto');
  				$($(this)[0].banner).removeClass('banner-is-fixed');
  			}
 		});
	});  
}

function isBannerPastView(bannerPos) {
	if ($(window).scrollTop() < bannerPos)
  		return false;
  	return true;
} 

$(document).ready(function () {
	photoGalleryInit();
	formInit();
	bannerInit();
	$("a[rel=external]").attr("target", "_blank");
});