$(window).load(function()
{
    $.ajaxSetup(
    {
        cache: false
    });

    $(window).resize(function()
    {
        $("#Wallpaper").css({"width" : "", "height" : ""});

        SetupLayout();
    });

    SetupLayout();

    // Get page name
    var sCurrentURL = location.href;
    var nStartIndex = sCurrentURL.lastIndexOf("/");
    var nEndIndex = sCurrentURL.lastIndexOf(".");
    var sCurrentPage = sCurrentURL.substring(nStartIndex + 1, nEndIndex);

    switch (sCurrentPage)
    {
        case "Accommodation_AlmondRoom":
            var oManageGallery = new ManageGallery(4);
            break;

        case "Accommodation_Luxury":
            var oManageGallery = new ManageGallery(3);
            break;

        case "Accommodation_RoseRoom":
            var oManageGallery = new ManageGallery(5);
            break;

        case "Accommodation_SuperiorLuxury":
            var oManageGallery = new ManageGallery(11);
            break;

        case "Facilities_Gallery":
            var oManageGallery = new ManageGallery(10);
            break;
    }

    if ($("#ContactFormSubmitButton").length)
    {
        $("#ContactFormSubmitButton").click(function()
        {
            SubmitContactForm();
        });
    }

});

function SubmitContactForm()
{
    // Init
    var errorMessage = "";

    // Validate
    var name = $("#Name").val();
    var telephoneNumber = $("#TelephoneNumber").val();
    var emailAddress = $("#EmailAddress").val();
    var country = $("#Country").val();
    var dateInDay = $("#DateInDay").val();
    var dateInMonth = $("#DateInMonth").val();
    var dateInYear = $("#DateInYear").val();
    var dateOutDay = $("#DateOutDay").val();
    var dateOutMonth = $("#DateOutMonth").val();
    var dateOutYear = $("#DateOutYear").val();
    var message = $("#Message").val();
    var whereDidYouHearAboutUs = $("#WhereDidYouHearAboutUs").val();

    if (name == "")
        errorMessage += "<li>Please enter your name</li>";

    if (telephoneNumber == "")
        errorMessage += "<li>Please enter a contact number</li>";

    var emailAddressRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    if (!emailAddressRegex.test(emailAddress))
        errorMessage += "<li>Please enter a valid email address</li>";

    if (country == "Choose One")
        errorMessage += "<li>Please select your country</li>";

    if (dateInDay == "Day" || dateInMonth == "Month" || dateInYear == "Year")
        errorMessage += "<li>Please specify your check-in date</li>";

    if (dateOutDay == "Day" || dateOutMonth == "Month" || dateOutYear == "Year")
        errorMessage += "<li>Please specify your check-out date</li>";

    if (errorMessage == "")
    {
        // Send
        var formData = "name=" + name + "&telephoneNumber=" + telephoneNumber + "&emailAddress=" + emailAddress + "&country=" + country + "&dateInDay=" + dateInDay + "&dateInMonth=" + dateInMonth + "&dateInYear=" + dateInYear + "&dateOutDay=" + dateOutDay + "&dateOutMonth=" + dateOutMonth + "&dateOutYear=" + dateOutYear + "&message=" + message + "&whereDidYouHearAboutUs=" + whereDidYouHearAboutUs;

        $.ajax(
        {
            type: "POST", url: "Bin/Contact.php", data: formData, success: function()
            {
                $("#ContactForm").html("Thank you. We will be in touch shortly.");
            }
        });
    }
    else
    {
        $("#ErrorMessage").html("<span style=\"color: #cc0000;\"><strong>Please correct the following errors:</strong><ul>" + errorMessage + "</ul></span>")
    }
}

var nCurrentImage = 1;
var nImageCountGlobal = 0;
var oChangeInterval = null;
var nChangeInterval = 5 * 1000;

function ManageGallery(nImageCount)
{
    // Initialise
    nImageCountGlobal = nImageCount;
    oChangeInterval = setInterval(NextGalleryImage, nChangeInterval);

    $("#Slide" + nCurrentImage).fadeTo("slow", 1.0);
    $("#Dot" + nCurrentImage).css({"background-position":"bottom"});

    // Actions
    $("#BtnForward").click(function ()
    {
        NextGalleryImage();
    });

    $("#BtnBack").click(function ()
    {
        PreviousGalleryImage();
    });
}

function NextGalleryImage()
{
    clearInterval(oChangeInterval);
    oChangeInterval = setInterval(NextGalleryImage, nChangeInterval);

    // Fade out current image
    $("#Slide" + nCurrentImage).fadeTo("slow", 0);
    $("#Dot" + nCurrentImage).css({"background-position":"top"});

    nCurrentImage++;

    if (nCurrentImage > nImageCountGlobal)
        nCurrentImage = 1;

    // Fade in new image
    $("#Slide" + nCurrentImage).fadeTo("slow", 1.0);
    $("#Dot" + nCurrentImage).css({"background-position":"bottom"});
}

function PreviousGalleryImage()
{
    clearInterval(oChangeInterval);
    oChangeInterval = setInterval(NextGalleryImage, nChangeInterval);

    // Fade out current image
    $("#Slide" + nCurrentImage).fadeTo("slow", 0);
    $("#Dot" + nCurrentImage).css({"background-position":"top"});

    nCurrentImage--;

    if (nCurrentImage < 1)
        nCurrentImage = nImageCountGlobal;

    // Fade in new image
    $("#Slide" + nCurrentImage).fadeTo("slow", 1.0);
    $("#Dot" + nCurrentImage).css({"background-position":"bottom"});
}

function SetupLayout()
{
    var nViewWidth = $(window).width();
    var nViewHeight = $(window).height();
    var nPageHeight = $(document).height();

    // Set menu height to match
    $("#Menu").height(nPageHeight);

    // Resize according to width
    $("#Wallpaper").css({"width" : nViewWidth, "height" : "auto"});

    // If resultant height is too short, scale by height instead
    if ($("#Wallpaper").height() < nViewHeight)
        $("#Wallpaper").css({"width" : "auto", "height" : nViewHeight});
}
