//<![CDATA[
$(document).ready(function(){	
	$('.nojava').removeClass('nojava');
	/* Fancybox */
	/* ensures fancybox only uses anchor title tag as caption */
	function titleOnly(){return this.title;}	
	$("a.imgzoom").fancybox({ 'overlayColor': '#000', 'overlayOpacity': 0.9, 'titlePosition': 'inside', 'title': titleOnly });
	$("a.mediazoom").fancybox({ 'overlayColor': '#000', 'overlayOpacity': 0.9, 'titleShow': false });
	/* reveal teacher biography */
	$(".teacher ul.plain li div span.button").toggle(function() {
		$(this).prev().stop(true, true).slideToggle("fast");
		$(this).text("Hide biography");
	}, function() {
			$(this).prev().stop(true, true).slideToggle("fast");
			$(this).text("Read biography");
	});
	/* move form labels inside input */
		 // set all input.text default value according to alt/title attribute
			$("input[type='text']").each(function(){
		          $(this).val($(this).attr("alt"));
			});
			$("input[type='password']").each(function(){
		          $(this).val($(this).attr("alt"));
			}); 
			$("textarea").each(function(){
		          $(this).val($(this).attr("title"));
			});
	     // clear input.text on focus, if still in default
		        $("input[type='text']").focus(function() {
		          if($(this).val()==$(this).attr("alt")) {
		            $(this).val("");
		          }
		        });
				$("input[type='password']").focus(function() {
		          if($(this).val()==$(this).attr("alt")) {
		            $(this).val("");
		          }
		        });
		        $("textarea").focus(function() {
		          if($(this).val()==$(this).attr("title")) {
		            $(this).val("");
		          }
		        });
	      // if field is empty afterward, add text again
	 	       $("input[type='text']").blur(function() { 
		          if($(this).val()=="") {
		            $(this).val($(this).attr("alt"));
		          }
		        });
	 	       $("input[type='password']").blur(function() { 
		          if($(this).val()=="") {
		            $(this).val($(this).attr("alt"));
		          }
		        });
		       $("textarea").blur(function() { 
		          if($(this).val()=="") {
		            $(this).val($(this).attr("title"));
		          }
		        });
	/* Validate forms */
	// contact	
	$("form#contact").validate({
		rules: {
				},
		messages: {
				fullname: "Please enter your full name",
				email: "Please enter a valid email address",
				website: "Please enter your website address",
				address1: "Please enter your address",
				city: "Please enter your city",
				state: "Please enter your county",
				postcode: "Please enter your postcode",
				country: "Please enter your country",
				phone1: "Please enter your telephone number",
				fax: "Please enter your fax number",
				message: "Please enter your message",
				question: "Please enter your question",
				captcha: "Please enter the above word"
				}
	});	
	// mailing list	
	$("form#mailinglist").validate({
		rules: {
			email: {required: true, email:true}
		},
		messages: {
			cmname: {required:"Please enter your name"},
			email: {required:"Please enter your email address"}
		}
	});	

		$("form#mailinglist_extended").validate({
			rules: {
				firstname: {required: true},
				email_ext: {required: true, email:true}
			},
			messages: {
				firstname: {required:"Please enter your name"},
				email_ext: {required:"Please enter your email address"}
			}
		});


		$("form#unsub").validate({
			rules: {
				email_unsub: {required: true, email:true}
			},
			messages: {
				email_unsub: {required:"Please enter your email address"}
			}
		});
	
	// search
	$.validator.addMethod("keywords_reqstring", function(value, element) {
	    return (value != "Search");},"Please enter a keyword");
	
	$("form#global_search").validate({
		rules: {
			keywords: {required: true, minlength: 3, keywords_reqstring: "#keywords"}
		},
		messages: {
			keywords: {required:"Please enter a keyword", minlength:"Minimum 3 characters"}
		}
	});	
	$("form#try_again").validate({
		rules: {
			keywords: {required: true, minlength: 3, keywords_reqstring: "#keywords"}
		},
		messages: {
			keywords: {required:"Please enter a keyword", minlength:"Minimum 3 characters"}
		}
	});	
	// press area
	$.validator.addMethod("password_reqstring", function(value, element) {
	    return (value != "Password");},"Please enter your password");
	
	$("form#sign_in").validate({
		rules: {
			username: {required: true},
			password: {required: true, password_reqstring: "#password"}
		},
		messages: {
			username: {required:"Please enter your username"},
			password: {required:"Please enter your password"}
		}
	});	
	
		$("form#register").validate({
			rules: {
				username: {required: true, email:true},
				password: {required: true},
				password_confirm: {required: true},
				captcha: {required: true}
			},
			messages: {
				username: {required:"Please enter your email address"},
				password_confirm: {required:"Please confirm your password"},
				password: {required:"Please enter your password"},
				captcha: {required:"Please enter the word you see in the image above"}
			}
		});
	
	
		$("form#forgot").validate({
			rules: {
				email: {required: true, email:true}
			},
			messages: {
				email: {required:"Please enter your email address"}
			}
		});

	/* track outgoing links in Google Analytics */
	$("a[rel*='external']").click(function(){
		pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
	});
	
	/* dropdown */
	$("#nav li").mouseenter(function() {
		$("ul", this).stop(true, true).slideToggle("fast");
	}).mouseleave(function() {
		$("ul", this).stop(true, true).hide();
	});		
	/* Easy slider */
	$("#slider").easySlider({
		numeric: true,
		numericId: 'slidercontrols'
	});
	/*random footer image*/
	var footerImages = new Array("http://greenwichdance.org.uk/site_assets/images/generic/footer_bg.jpg","http://greenwichdance.org.uk/site_assets/images/generic/footer_bg2.jpg","http://greenwichdance.org.uk/site_assets/images/generic/footer_bg3.jpg","http://greenwichdance.org.uk/site_assets/images/generic/footer_bg4.jpg")
	var randomImage=Math.floor(Math.random() * footerImages.length);
	var bgURL=footerImages[randomImage];
	$("#footer div.content").css("background-image", "url('" + bgURL + "')");

		/* small slideshow */
		$('div#slider li').mouseenter(function(){
			$("span.title", this).stop(true, true).slideToggle("fast");
		}).mouseleave(function() {
			$("span.title", this).stop(true, true).slideToggle("fast");
		});
		
		/* homepage background slideshow */
		// button revealing image
		$(".show_button.left").click(function(){
			$("#content").toggle();
			$("#footer").toggle();
			$("#controls").toggle();
			$(".show_button.show").toggle();
			$(".show_button.left.hide").toggle();
			$(".show_button.top.hide").show();
		});
		$(".show_button.top").click(function(){
			$("#content").toggle();
			$("#footer").toggle();
			$("#controls").toggle();
			$(".show_button.show").toggle();
			$(".show_button.left.hide").toggle();
			$(".show_button.top.hide").hide();
		});
	
		jQuery.each(jQuery.browser, function(i, val) {
		  if(i=="mozilla" && jQuery.browser.version<"1.9") {
				$(".show_button.left").hide();
				$('div#bg').addClass('nojava');
		}else{
			// Options for SuperBGImage
			$.fn.superbgimage.options = {
				id: 'superbgimage', // id for the container
				z_index: -1, // z-index for the container
				inlineMode: 0, // 0-resize to browser size, 1-do not resize to browser-size
				showimage: 1, // number of first image to display
				vertical_center: 1, // 0-align top, 1-center vertical
				transition: 1, // 0-none, 1-fade, 2-slide down, 3-slide left, 4-slide top, 5-slide right, 6-blind horizontal, 7-blind vertical, 90-slide right/left, 91-slide top/down
				transitionout: 0, // 0-no transition for previous image, 1-transition for previous image
				randomtransition: 0, // 0-none, 1-use random transition (0-7)
				showtitle: 0, // 0-none, 1-show title
				slideshow: 0, // 0-none, 1-autostart slideshow
				slide_interval: 5000, // interval for the slideshow
				randomimage: 0, // 0-none, 1-random image
				speed: 500, // animation speed
				preload: 1 // 0-none, 1-preload images
			};	
			// initialize SuperBGImage
			$('#controls').superbgimage();
		}
		});			
});
//]]>
/* Google Analytics */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1646179-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();