// Comment out this part if you don't want the slider to run in slideshow mode.
var duration = 6000;
var intervalID = setInterval(selectNext, duration);

/**
 * A function to handle the contact form processes.
 */
$(function() {
    $("#contactform").submit(function() {
        // Get the form data
        var name = $("#name").val();
        var email = $("#email").val();
        var message = $("#message").val();
        var emailRegex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;

        // Validate fields
        if (name == "") {
			$("#form-warning").html("Please enter your name.").css({display: "block", color: "red"});
            $("#name").focus();
            return false;
		} else if (email == "") {
			$("#form-warning").html("Please enter your e-mail.").css({display: "block", color: "red"});
            $("#email").focus();
            return false;
		} else if (message == "") {
            $("#form-warning").html("Please enter your message.").css({display: "block", color: "red"});
            $("#message").focus();
            return false;
        } 

        // Validate the e-mail
        if (!emailRegex.test(email)) {
            $("#form-warning").html("Please enter a valid e-mail address.").css({display: "block", color: "red"});
            return false;
        }

        // Disable the submit button
        $("#submit-button").attr("disabled", "true");

        // Clear the warning
        $("#form-warning").html("");

        // Display the preloader
        $("#form-loader").css({display: "block"});

        // Get the form data
        var data = $(this).serialize();

        // Send a post request
        $.ajax({
            type: "POST",
            url: "send.php",
            data: data,
            success: function(msg) {
                // Hide the preloader and enable the submit button.
                $("#form-loader").css({display: "none"});
                $("#submit-button").attr("disabled", "false");

                // Display an information message
                if (msg == "success") {
                    var result = "Your message has been sent. We will get back to your shortly. Thank you."

                    // Clear the form
                    $("#contactform #name, #email, #url, #message").val("");
                    $("#form-warning").html(result).css({display: "block", color: "#1c6baf"});
                } else {
                    result = msg;
                    $("#form-warning").html(result).css({display: "block", color: "red"});
                }                
            }
        });

        return false;
    });
});


/**
 * This function prepares and initializes the slider.
 */
$(function() {
    // Hide all divs except the first one
    $("#slider-container div").not(":first").fadeTo(0, 0);
    

    // Hover states
    $("#slider-navigation ul li").hover(
        function() {
            if (!$(this).hasClass("selected")) {
                $(this).addClass("hover");
            }
        },
        function() {
            if (!$(this).hasClass("selected")) {
                $(this).removeClass("hover");
            }
        }
        ).bind("click", function(event) {
        event.preventDefault();
        select($(this).index());
    });    

});


/**
 * Selects the div with the given index.
 */
function select(index) {
    // Get the index number of the previous div
    var prevIndex = $("#slider-navigation ul li.selected").removeClass("selected").index();

    // Prepare the navigation
    $("#slider-navigation ul li:nth-child(" + (index + 1) + ")").removeClass("hover").addClass("selected");

    // Here is where the cross fade effect happens
    if (prevIndex != index) {
        $("#slider-container div:nth-child(" + (prevIndex + 1) + ")").addClass("previous").removeClass("current").animate({
            opacity: 0.0
        }, 1000);
        $("#slider-container div:nth-child(" + (index + 1) + ")").addClass("current").removeClass("previous").animate({
            opacity: 1.0
        }, 1000);

        // Sets the interval if it's defined at the top of the page
        if (typeof(intervalID) != "undefined") {
            clearInterval(intervalID);
            intervalID = setInterval(selectNext, duration);
        }
    }

}

/**
 * Selects the next div in the queue. 
 */
function selectNext() {
    // Get the number of the slides
    var items = $("#slider-navigation ul li");

    // Get the current index
    var index = $("#slider-navigation ul li.selected").index();

    if (++index == items.length) {
        index = 0;
    }

    // Select the new div
    select(index);
}
