$( document ).ready( function () {
    $('#map').children('a').click(function(event){
        event.preventDefault();

        var parent = $(this).closest('div'),
        child = $('#mapbox', parent);

        if (child.is(':hidden')) {
          	$('#mapbox').show("slide", { direction: "up" }, 1000, function(){
          	    $("#textMap").text("Click to close the map");
          	});
      	
        	initialize();
        } else {
          $('#mapbox').hide("slide", { direction: "up" }, 1000, function(){
              $("#textMap").text("Click to find us on a map");
          }); 
        }
    });
    
    $('#map a.closemap').click(function(event){
        event.preventDefault();

        var parent = $(this).closest('div'),
        child = $('#mapbox', parent);
        
        $('#mapbox').hide("slide", { direction: "up" }, 1000, function(){
              $("#textMap").text("Click to find us on a map");
          });
    });
    
    $("a[rel]").overlay({ 
 
        expose: '#f1f1f1',
 
        onBeforeLoad: function() { 
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap"); 
            // load the page specified in the trigger 
            wrap.load(this.getTrigger().attr("href")); 
        },
        
        onLoad: function() {
            validate();
        }
 
    });
    
    $('.slideshowcontact').cycle({ 
        fx:     'fade', 
        speed:  3000,  
        timeout:  2500
    });
    
    // select the thumbnails and make them trigger our overlay
    $("#gallery_promo a").overlay({

    	// each trigger uses the same overlay with the id "gallery"
    	target: '#gallery',

    	// optional exposing effect
    	expose: '#f1f1f1'

    // let the gallery plugin do its magic!
    }).gallery({
        autohide: false,
    	// the plugin accepts its own set of configuration options
    	speed: 800
    });
});


    
    function validate() {
        $("#contactForm").validate({
            rules: {
                name: "required",
                enquiry: "required",
                company: "required",
                telephone: {
                    required: true,
                    telephone: true
                },
                email_address: {
                    required: true,
                    email: true
                }

            },
            messages: {
                name: "Please enter your name",
                enquiry: "Please enter your enquiry",
                company: "Please enter your company name",
                telephone: {
                    required: "Please enter your phone number",
                    telephone: "Please enter a valid phone number"
                },
                email_address: {
                    required: "Please enter your email address",
                    email: "Please enter a valid email address"
                }
            }
        });
    }