﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

function InitializePage() {
    SetHeaderImages();
    PreLoadImage('Images/Home_over.jpg');
    PreLoadImage('Images/MeetTheTeam_over.jpg');
    PreLoadImage('Images/MeetTheClients_over.jpg');
    PreLoadImage('Images/FarmAndRanch_over.jpg');
    PreLoadImage('Images/WhereWeGo_over.jpg');
    PreLoadImage('Images/PetsInNeed_over.jpg');
    PreLoadImage('Images/ContactUs_over.jpg');
}

function SetHeaderImages() {
    $.ajax({
        type: "GET",
        url: "headerFiles.xml",
        dataType: "xml",
        success: function(xml) {
            var files = new Array();
            var fileCount = 0;
            var fileIndex = 0;

            $(xml).find('FileName').each(function() {
                fileCount += 1;
                var fileName = $(this).text();
                files[fileIndex] = fileName;
                fileIndex = fileCount;
            })

            var image1Index = Math.floor(Math.random() * (fileCount + 1));
            $("#headerImage1").attr("src", files[image1Index]);

            do {
                var image2Index = Math.floor(Math.random() * (fileCount + 1));
            }
            while (image2Index == image1Index);
            $("#headerImage2").attr("src", files[image2Index]);

            do {
                var image3Index = Math.floor(Math.random() * (fileCount + 1));
            }
            while ((image3Index == image1Index) || (image3Index == image2Index));
            $("#headerImage3").attr("src", files[image3Index]);
        }
    })
}

function BuildClientList() {
    $.ajax({
        type: "GET",
        url: "clients.xml",
        dataType: "xml",
        success: function(xml) {

            $(xml).find('Image').each(function() {
                clientCount += 1;
                var clientName = $(this).attr("name");
                var thumbUrl = $(this).attr("thumbnail");
                var largeImageUrl = $(this).attr("large");

                clientNames[clientIndex] = clientName;
                clientThumbnails[clientIndex] = thumbUrl;
                clientLargeImages[clientIndex] = largeImageUrl;

                clientIndex = clientCount;
            })
            if ($('#clientTable').length > 0) {
                BuildClientTable();
            }
            if ($('#imgPolaroid').length > 0) {
                GetClientIdFromQueryString();
            }
        }
    })
}

function BuildClientTable() {
    $('#clientTable').empty();
    for (clientIndex = firstClientOnPage; clientIndex < firstClientOnPage + clientsPerPage; clientIndex += clientsPerRow) {
        tableRow = '<tr>';
        for (cellIndex = 0; cellIndex < clientsPerRow; cellIndex++) {
            if (clientIndex <= clientCount - 1) {
                tableRow += '<td class="clientTableCell"><img alt="Client Thumbnail" src="' +
                 clientThumbnails[clientIndex + cellIndex] +
                 '" onclick="OpenClientPolaroid(' + (clientIndex + cellIndex) + ');"/><br/><span onclick="OpenClientPolaroid(' +
                 (clientIndex + cellIndex) + ');">' +
                 clientNames[clientIndex + cellIndex] + '</span></td>';
            }
        }
        tableRow += '</tr>';
        $('#clientTable').append(tableRow);
    }

    SetClientButtonsState();
}

function PreviousClientTable() {
    clientPageIndex = clientPageIndex - 1;
    firstClientOnPage = clientPageIndex * clientsPerPage;
    BuildClientTable();
}

function NextClientTable() {
    clientPageIndex = clientPageIndex + 1;
    firstClientOnPage = clientPageIndex * clientsPerPage;
    BuildClientTable();
}

function PreLoadImage(imageName) {
    newImage = new Image();
    newImage.src = imageName;
}

function SwitchImages(id, newImage) {
    $(id).attr('src', newImage);
}

function SetClientButtonsState() {
    if (firstClientOnPage < clientCount - clientsPerPage)
        $("#btnNextClient").css("display", "block");
    else
        $("#btnNextClient").css("display", "none");

    if (clientIndex > clientsPerPage)
        $("#btnPreviousClient").css("display", "block");
    else
        $("#btnPreviousClient").css("display", "none");
}

function OpenClientPolaroid(targetClientId) {
    window.open('clientPolaroid.html?clientId=' + targetClientId, 'ClientWindow', 'width=350,height=450,resizable=0');
}

function GetClientIdFromQueryString() {
    var currentUrl = window.location.href;
    var queryString = currentUrl.split("?")[1];
    if (queryString != null) {
        clientIndex = parseInt(queryString.split("=")[1]);
        SetClientPolaroid();
    }
}

function SetClientPolaroid() {
    $("#imgPolaroid").attr("src", clientLargeImages[clientIndex]);
    EnablePolaroidButtons();
}

function NextPolaroidImage() {
    clientIndex = clientIndex + 1;
    SetClientPolaroid();
}

function PreviousPolaroidImage() {
    clientIndex = clientIndex - 1;
    SetClientPolaroid();
}

function EnablePolaroidButtons() {
    if (clientIndex < clientCount )
        $("#btnNextPolaroid").css("display", "block");
    else
        $("#btnNextPolaroid").css("display", "none");

    if (clientIndex > 0)
        $("#btnPreviousPolaroid").css("display", "block");
    else
        $("#btnPreviousPolaroid").css("display", "none");
}