﻿/// <reference path="jquery.js" />
/// <reference path="jquery.center.js" />
/// <reference path="jquery.fitToParent.js" />

var $tmPhoto = {
    showLoading: function() {
        $("#tmp-content").children().hide(); //.fadeOut("normal");
        $("#tmp-content").addClass("loading");
        //        console.log("show loading complete");
    },
    pageRenderComplete: function(fadeInSelector) {
        $("#tmp-content").removeClass("loading");
        var elemToFadeIn = $(fadeInSelector);
        elemToFadeIn.css({ visibility: "visible", display: "none" });   // Ensure element is visible, but display is none
        elemToFadeIn.fadeIn("normal");                                  // Fade in
    },
    initHome: function() {
        var imgs = $("#imageRotater img");
        var numFulls = imgs.size();

        imgs.each(function(pos) {
            var img = $(this);
            var fnOnLoad = function() {
                img.fitToParent().center();
                //if (pos == 0) {
                //    img.fadeIn("slow");    // Fade in the first/top image ...
                //}

                // Last one ... start the show ...
                if (--numFulls == 0) {
                    $tmPhoto.pageRenderComplete("#frontPageContent");
                    //                    $('#frontPageContent').css({ visibility: "visible", display: "none" });
                    //                    $('#frontPageContent').fadeIn("normal");
                    $('#imageRotater').cycle();
                }
            };

            if (this.complete)
                fnOnLoad();
            else
                img.load(fnOnLoad);

        });
    },

    initPage: function() {

        if ($(".std-page-copy-scrollbar").length) {
            var copy = $(".std-page-copy");
            var scrollHeight = $(".std-page-copy").parent().height();

            $(".std-page-copy-scrollbar").slider({
                animate: false,
                slide: function(e, ui) {
                    var maxScroll = copy.height() - scrollHeight;
                    $(".std-page-copy-scroller").scrollTop(ui.value * (maxScroll / 100));
                },
                change: function(e, ui) {
                    var maxScroll = copy.height() - scrollHeight;
                    $(".std-page-copy-scroller").scrollTop(ui.value * (maxScroll / 100));
                }
            });
        }

        var imgs = $(".std-page-photo img");
        var numImgs = imgs.size();

        imgs.each(function(pos) {
            var img = $(this);

            var fnOnLoadComplete = function() {

                if (--numImgs == 0) {
                    $tmPhoto.pageRenderComplete(".std-page-content");
                    //                    $('.std-page-content').css({ visibility: "visible", display: "none" });
                    //                    $('.std-page-content').fadeIn("normal");
                }
            };

            if (this.complete)
                fnOnLoadComplete();
            else
                img.load(fnOnLoadComplete);

        });
    },

    initGalleryIndex: function() {
        //        console.log("init gallery called");

        var imgs = $("#gallery-list img");
        var numImgs = imgs.size();

        //console.log("wait for " + numImgs + " images");

        imgs.each(function(pos) {
            var img = $(this);

            var fnOnLoadComplete = function() {
                //console.log("event triggered");
                if (--numImgs == 0) {
                    //console.log("numImages = " + numImgs);
                    $tmPhoto.pageRenderComplete("#gallery-index");
                    //                    $("#gallery-index").css({ visibility: "visible", display: "none" });
                    //                    $("#gallery-index").fadeIn("normal");
                }
            };

            if (this.complete) {
                //console.log("process image " + pos + " (complete -- call direct)");
                fnOnLoadComplete();
            } else {
                //console.log("process image " + pos + " (not complete -- set event)");
                img.load(fnOnLoadComplete);
            }

        });
    },

    initGallery: function() {
        $("#gallery").gallery();
    },
    
    sendMailComplete: function() {
        $tmPhoto.initPage();
        alert("Your email has been sent.  Thank you!");
    },
    
    sendMailError: function() {
        alert("There was a problem with the form sending email.  Please try again later or use the direct email address.  Sorry for the inconvenience!");
    }

}
