var speed = 150, valid = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

(function($) {

    $( document ).ready( function() {
		//show the latest vintages
		
		if ($(".vintage").length > 0) {
			$(".vintage").hide();
			if ($("#vintage_2011").length > 0) {
			$("#vintage_2011").show(500);
			} else if ($("#vintage_2010").length > 0) {
			$("#vintage_2010").show(500);
			}
		}
		
		var regions = $('#regions');
		if (regions) {
			//alert('regions');
			$(".region").hide();
			$("#auckland_northland").show(500);
			$("#ak_nl").addClass("selected");
			$(".international_quick").hide();
		}
		
		//hide and show vintage function:
		$("#vintage_selector a").click(
			function(){
				$("#vintage_selector a").removeClass("selected");
				$(this).addClass("selected");
				$(".vintage").hide();
				if ( this.id == "2011_select" ) {
					$("#vintage_2011").show(500);
				} 
				if ( this.id == "2010_select" ) {
					$("#vintage_2010").show(500);
				}
				if ( this.id == "2009_select" ) {
					$("#vintage_2009").show(500);
				}
				if ( this.id == "2008_select" ) {
					$("#vintage_2008").show(500);
				}
			});//vintage selector function
			
		$("#region_selector a").click(
			function(){
				$("#region_selector a").removeClass("selected");
				$(this).addClass("selected");
				$(".region").hide();
				if ( this.id == "ak_nl" ) {
					$("#auckland_northland").show(500);
					if ("#international_list") {
					$("#international_list").hide();
					$(".international_quick").hide();
					}
				} 
				if ( this.id == "c_l_ni" ) {
					$("#central_north").show(500);
					if ("#international_list") {$("#international_list").hide();$(".international_quick").hide();}
				}
				if ( this.id == "u_si" ) {
					$("#upper_south").show(500);
					if ("#international_list") {$("#international_list").hide();$(".international_quick").hide();}
				}
				if ( this.id == "c_l_si" ) {
					$("#central_south").show(500);
					if ("#international_list") {$("#international_list").hide();$(".international_quick").hide();}
				}
				if ( this.id == "int" ) {
					$(".international_quick").show(500);
					$("#international_list").show(500);
				}
		});//region selector function
		
        $( '#newsletter input' ).each( function() {
            if ( $( this ).hasClass( 'text' ) ) {
                if ( this.value == '' ) $( this ).val( 'your email' );
                $( this ).focus( function() { if ( this.value == 'your email' ) $( this ).val( '' ); } );
                $( this ).blur( function() { if ( this.value == '' ) $( this ).val( 'your email' ); } );
            } else {
                $( this ).click( function() {
                    $( '#newsletter #form, #newsletter #invalid, #newsletter #success, #newsletter #failure' ).slideUp( speed );
                    e = $( '#email' ).val();
                    if ( e != 'your email' && e != '' && valid.test( e ) ) {
                        $( '#newsletter #loading' ).slideDown( speed );
                        $.post(
                            '/ajax.php',
                            { action: 'newsletter', e: e },
                            function( data ) {
                                if ( data.status == true ) $( '#newsletter #loading' ).slideUp( speed, function() { $( '#newsletter #success' ).slideDown( speed ); } );
                                else {
                                    if ( data.reason == 'invalid' ) $( '#newsletter #loading' ).slideUp( speed, function() { $( '#newsletter #invalid' ).slideDown( speed ); } );
                                    else $( '#newsletter #loading' ).slideUp( speed, function() { $( '#newsletter #failure' ).slideDown( speed ); } );
                                }
                            },
                            'json'
                        );
                    } else $( '#newsletter #invalid' ).slideDown( speed );
                } );
            }
        } );

        $( '#newsletter #intro a' ).click( function() {
            $( '#newsletter #invalid, #newsletter #success, #newsletter #failure' ).slideUp( speed, function() {
                $( '#newsletter #form' ).slideDown( speed );
            } );
        } );

        $( '#compform input.button' ).click( function() {
            $( '#compform, #compinvalid, #compsuccess, #compfailure' ).slideUp( speed );
            e = $( '#compemail' ).val();
            n = $( '#compname' ).val();
            p = $( '#compph' ).val();
            if ( e != '' && valid.test( e ) && n != '' && p != '' ) {
                $( '#comploading' ).slideDown( speed );
                $.post(
                    '/ajax.php',
                    { action: 'competition', e: e, n: n, p: p },
                    function( data ) {
                        if ( data.status == true ) $( '#comploading' ).slideUp( speed, function() { $( '#compsuccess' ).slideDown( speed ); } );
                        else {
                            if ( data.reason == 'invalid' ) $( '#comploading' ).slideUp( speed, function() { $( '#compinvalid' ).slideDown( speed ); } );
                            else $( '#comploading' ).slideUp( speed, function() { $( '#compfailure' ).slideDown( speed ); } );
                        }
                    },
                    'json'
                );
            } else $( '#compinvalid' ).slideDown( speed ).delay( 3000 ).slideUp( speed, function() { $( '#compform' ).slideDown( speed ); } );
        } );

    } );

} )( jQuery );

