(function ($) {

    var Config = {
        ClientURL: '/Plugins/comHome/Home.htm',
        ServerURL: '',
        StyleURL: '/Plugins/comHome/Home.css',
        Container: '',
        Plugins_Count: 3,
        Plugins_LoadedCount: 0,
        Plugins_LoadedError: false
    };

    $.fn.Home = function (options) {
        //Extending the configuration
        if (options) {
            Config = $.extend(Config, options);
        }
        Config.Container = $(this);
        Init();
    };

    $.fn.Home.GetConfig = function () {
        return Config;
    }


    $.fn.Home.Destroy = function () {
        // distruzione degli oggetti interni al plugin
        Config = {
            ClientURL: '/Plugins/comHome/Home.htm',
            ServerURL: '',
            StyleURL: '/Plugins/comHome/Home.css',
            Container: '',
            Plugins_Count: 3,
            Plugins_LoadedCount: 0,
            Plugins_LoadedError: false
        };
    }

    //Add event listners
    //Initialize other plugins needed for the Home plugin
    function Init() {

        //Try to load the html code
        Config.Container.load(Config.ClientURL, function () {

            $.fn.Utility.CaricaBanner("#banner_static", "/prezzo.htm");

            Config.Container = $(this);
            //After loading the code I add the listner for the event 'ModuleLoaded' for each plugin from the home page. 

            Config.Container.find('#box_offerte_banners').bind('ModuleLoaded', function (event) {
                PluginLoaded(event);
            });
            Config.Container.find('#box_offerte_box').bind('ModuleLoaded', function (event) {
                PluginLoaded(event);
            });
            Config.Container.find('#box_ricerca').bind('ModuleLoaded', function (event) {
                PluginLoaded(event);
            });

            Config.Plugins_LoadedCount = 0;
            Config.Plugins_LoadedError = false;

            //Initialize each of the 3 plugins for the Home plugin.
            $(this).find('#box_offerte_banners').OfferteBanners({ RoundCorners: false, idArea: 2, numMaxOfferte: 8 });
            $(this).find('#box_offerte_box').OfferteBox({ RoundCorners: false, idArea: 1, numMaxOfferte: 999 });
            $(this).find('#box_ricerca').Ricerca({ RoundCorners: false });
            $(this).find('#box_promo').Promo();
            $(this).find('#registrazioneClubOrizzonti').AreaClub();

            $.fn.Utility.UpdateSkypeLinks();
        });
    }

    //Function is called whenever a plugin is loaded
    //I don't pass any parameters because the end user doesn't need to know any information on a certain plugin
    function PluginLoaded(event) {

        event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();

        //Increment the number of loaded plugins
        ++Config.Plugins_LoadedCount;
        //Test to see if all plugins were loaded  
        if (Config.Plugins_LoadedCount == Config.Plugins_Count) {
            //Trigger the event 'ComponentLoaded' for the Home plugin            
            $(Config.Container).trigger("ComponentLoaded");
            $.fn.Travelmind.SaveState("Home", Config);
        }
    }

    function ShowMessage(title, msg) {
        var msgBox = $("<div/>");
        $(Config.Container).append(msgBox);
        $(msgBox).html(msg).dialog({
            width: 500,
            title: title,
            modal: true,
            draggable: true,
            buttons: {
                "Chiudi": function () { $(this).dialog("close"); }
            },
            close: function () {
                $(this).dialog("destroy");
            }
        });
        $(msgBox).dialog('open');
    }




})(jQuery);



