﻿/**
 * static object that handles page logic
 * @class 
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var Main = function ($) {

    /**
    * @namespace Private methods and variables
    */
    var priv = {
        /**
        * Selector string to select the item of the div
        * @type String
        * @private
        */
        searchElement: ".searchexample",

        bindSearchText: function () {
            $(priv.searchElement).example(Globals.searchSampleText);
        }
    };

    /** @scope Main */
    return {

        /**
        * Initializes the logic for the current page
        * to be called on $(document).ready
        */
        OnReady: function () {
            priv.bindSearchText();

            // logic for items (list of items, like on the home page)
            if (typeof (Items) != "undefined") {
                Items.OnReady();
            }

            if (typeof (Google) != "undefined") {
                Google.OnReady();
            }

            if (typeof (Banners) != "undefined") {
                Banners.OnReady();
            }

            if (typeof (Photos) != "undefined") {
                Photos.OnReady();
            }

            // logic for popups
            if (typeof (Popups) != "undefined") {
                /**
                * Initialize the popups.
                * Called on $(document).ready
                */
                $("#photoOptions a.send-to-friend").bind('click', function () {
                    Popups.showSendToFriend($(this).attr('href'));
                    return false;
                });
            }

            $(".scrollable").scrollable({
                circular: true
            });

            $("#carousel").scrollable({
                items: "#tiles",
                circular: true
            }).autoscroll({
                autoplay: true,
                autopause: true,
                steps: 1,
                interval: 5000
            }).navigator("#carousel-thumbs").data("carousel");
        }
    };
} (jQuery);


// The supplied function is executed when dom is ready. In this case,
// we execute the Main.OnReady function, which will execute all functions
// necessary for the page to work correctly
$(document).ready(function() {
    Main.OnReady();
});

function loadScript(fileName) {
    if (!loadScript.loaded)
        loadScript.loaded = {};
    if (!(fileName in loadScript.loaded)) {
        loadScript.loaded[fileName] = true;
        var js = document.getElementsByTagName('script'), i = js.length;
        while (i--)
            if (js[i].src && js[i].src.indexOf(fileName) > -1)
                return;
        var script = document.createElement('script');
        script.async = true;
        script.src = /^https?:\/\//.test(fileName) ? fileName : '/js/' + fileName;
        js[0].parentNode.insertBefore(script, js[0]);
    }
}
