$(document).ready(function(){
	
	$('a.new_window').attr('target', '_blank');
	
	$("div#content table").each(function(){
		$(this).find("tr:even").addClass("alt");
	});
	
	// competitions & results
	
	$("div.competition_results a").click(function(){
		$("div.wide_box").removeClass("wide_box");
		$(this).parents("div.competition_results:first").addClass("wide_box");
		return false;
	});
	
	// Newsletter
	
	if( $("form#form_newsletter input#field_email").val() == "" ){
		$("form#form_newsletter input#field_email").addClass("plzenteremail").val("");
		$("form#form_newsletter input#field_email").val("Enter email address");
	}
	
	$("form#form_newsletter input#field_email").focus(function(){
		if( $(this).hasClass("plzenteremail") ){
			$(this).removeClass("plzenteremail");
			$(this).val("");
		}
	});
	
	$("form#form_newsletter input#field_email").blur(function(){
		if( $(this).val() == "" ){
			$(this).addClass("plzenteremail");
			$(this).val("Enter email address");
		}
	});
	
	$("form#form_newsletter").submit(function(){
		if( $(this).find("input#field_email").hasClass("plzenteremail") ){
			alert("Please enter your email address, then click \"Subscribe\"");
		}else{
			$(this).find("fieldset").hide();
			$(this).find("p#subscribing").show();
			
			$.ajax({
				url: URL_http+"/newsletter/",
				data: {
					email: $("form#form_newsletter input#field_email").val()
				},
				type: "POST",
				success: function( data, textStatus ){
					$("form#form_newsletter p#subscribing").hide();
					
					if( data == "0" ){
						$("form#form_newsletter p#subscribe_done").show();
					}else if( data == "1" ){
						$("form#form_newsletter p#subscribe_invalid").show();
						$("a#subscribe_back").click(function(){
							$("form#form_newsletter p#subscribe_invalid").hide();
							$("form#form_newsletter fieldset").show();
						});
					}else{
						$("form#form_newsletter p#subscribe_error").show();
					}
				}
			});
		}
		return false;
	});
	
	// Course Layout
	
	$("div#course_layout a").fancybox({
	
	});
	
	$("div#course_layout a").hover(function(){
		$(this).find("span").show();
	},function(){
		$(this).find("span").hide();
	});
	
	$("div#course_layout a span").hover(function(){
		$(this).hide();
	},function(){});
	
	// Manufacturers
	
	$("ul#manufacturers li a > span").each(function(){
		$(this).css( "background-image", "url(" + $(this).children("span").children("img").attr("src") + ")" ).css("background-position", "bottom left");
	});
	
	$("ul#manufacturers a").hover(function(){
		$(this).children("span").children("span").stop().fadeTo(200, 0.0);
	},function(){
		$(this).children("span").children("span").stop().fadeTo(200, 1.0);
	});
	
	$("ul#manufacturers a").click(function(){
		alert("Full product ranges will be listed soon");
		return false;
	});
	
	// Special Offers
	
	setInterval( function(){
		$("span.offer_text:visible").fadeOut( 200, function(){
			if( !$(this).next().fadeIn( 200 ).length ){
				$("span.offer_text:first").fadeIn( 200 );
			}
		});
	}, 5000 );
	
	/*
	// Home photo
	
	function home_img_toggle(){
		if( $("img#home_img_2").is(":visible") ){
			$("img#home_img_2").fadeOut( 550 );
		}else{
			$("img#home_img_2").fadeIn( 550 );
		}
		setTimeout( home_img_toggle, 4000 );
	}
	setTimeout( home_img_toggle, 3450 );
	*/
	
});