var DirectionsDisplay;
var DirectionsService;
var KitchenAddress;
var KitchenLocation;
var Map;
var MapInitialized;
var Slides;

/* Pre-DOM pre-load? Causes delay in MSIE 8.. */
/* Slides = new Image();
Slides.src = "../images/Image-019.png"; */

$(document).ready(function() {

    var Sections = $("#Sections").tabs({
        cookie: { expires: 0 }
    });
    
    $("#HolidayMenuAnchor").bind("click", function() {
        Sections.tabs("select", "Holiday");
    });

    Slides = new Image();
    Slides.src = "../images/Image-019.png";

    $("#TroyerSlides").slides({
        bigTarget: true,
        container: "slides",
        generateNextPrev: false,
        generatePagination: false,
        hoverPause: false,
        preload: true,
        play: 4000
    });

    $("#Menu").tabs();

    KitchenAddress = $("#Address").html();

    MapInitialized = false;

    if (Sections.tabs("option", "selected") == 3) InitializeMap();

    $("#MapDirButton").bind("click", GetDirections);

    Sections.bind("tabsshow", function(event, ui) {
        if (ui.panel.id == "Map") {
            if (!MapInitialized) InitializeMap();
        }
        return false;
    });

    $("#ContactForm input,textarea").bind("focus", function(event) {
        event.target.value = (event.target.value == event.target.defaultValue) ? "" : event.target.value;
    });
    
    $("#ContactForm input,textarea").bind("blur", function(event) {
        event.target.value = (event.target.value.length == 0) ? event.target.defaultValue : event.target.value;
    });
    
    $("#ContactForm").bind("submit", function(event) {
        var emailRegex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
        var phoneRegex = /^[2-9]{1}[0-8]{1}[0-9]{1}\-[2-9]{1}[0-9]{2}\-[0-9]{4}$/;
        var submit = true;
        
        $("#ContactForm input,textarea,select").each(function(index) {
            if (this.value == this.defaultValue) {
                alert("Please enter a value...");
                this.focus();
                submit = false;
            }
            else if (this.name == "Phone" &&
                     !this.value.match(phoneRegex)) {
                alert("Please enter a valid phone number, area code first, in the format: ###-###-####. Thank you.");
                this.focus();
                submit = false;
            }
            else if (this.name == "Email" &&
                     !this.value.match(emailRegex)) {
                alert("Please enter a valid email. Thank you.");
                this.focus();
                submit = false;
            }
            else if (this.name == "Verification" &&
                     this.value.length == 0) {
                alert("Please select a verification answer. Thank you.");
                this.focus();
                submit = false;
            }
            return submit;
        });
        return submit;
    });
});

function DirectionsCallback(Result, Status) {

    if (Status == google.maps.DirectionsStatus.OK) {

        DirectionsDisplay.setDirections(Result);
        DirectionsDisplay.setPanel(document.getElementById("MapSteps"));
    }
    else if (Status == google.maps.DirectionsStatus.NOT_FOUND) {
        alert("The originating address could not be found/geocoded. Please try again.");
    }
    else {
        alert("Unable to calculate route. Please try again.");
    }

}

function GetDirections() {

    var From = $("#MapDirInput").attr("value");

    var DirRequest = {
        destination: KitchenLocation,
        origin: From,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    }

    DirectionsService.route(DirRequest, DirectionsCallback);

}

function InitializeMap() {

    KitchenLocation = new google.maps.LatLng(36.1179317, -95.9402466);

    var MapOptions = {
        center: KitchenLocation,
        draggable: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
        zoom: 15
    }

    var MarkerOptions = {
        clickable: false,
        draggable: false,
        position: KitchenLocation,
        title: "The Amish Kitchen",
        visible: true
    }

    Map = new google.maps.Map(document.getElementById("MapCanvas"), MapOptions);

    var Marker = new google.maps.Marker(MarkerOptions);
    Marker.setMap(Map);

    DirectionsDisplay = new google.maps.DirectionsRenderer();
    DirectionsDisplay.setMap(Map);

    DirectionsService = new google.maps.DirectionsService();

    MapInitialized = true;

}
