// js/view/Abstract.js
//
// Created: 29.03.2011 (pj)
///////////////////////////////////////////////////////////////////////////////


pj.view.Abstract = function(viewName)
{
    this.constructor = pj.core.Object;
    this.constructor(viewName);


    ///////////////////////////////////////////////////////////////////////////
    // Events:

    this.beforeLoad = new pj.event.Dispatcher("beforeLoad");
    this.loaded = new pj.event.Dispatcher("loaded");
    this.beforeUnload = new pj.event.Dispatcher("beforeUnload");
    this.unloaded = new pj.event.Dispatcher("unloaded");


    this.cache = null;


    this.load = function()
    {
        timeStarted = +new Date();
        var evt = {
            started: timeStarted
        };
        that.beforeLoad.dispatch(evt);
    };

    this.unload = function()
    {
        that.beforeUnload.dispatch(null);

        that.notifyUnloaded();
    };

    this.notifyLoaded = function()
    {
        // verify that all data has been loaded:
        for (var key in that.cache) {
            var obj = that.cache[key];
            if (!obj) return;
        }

	    timeFinished = +new Date();
	    var evt = {
	        started: timeStarted,
	        finished: timeFinished
	    };
	    that.loaded.dispatch(evt);
		that.hideContent();

		that.checkImagesLoaded();
    };

    this.notifyUnloaded = function()
    {
        that.unloaded.dispatch(null);
    };

    this.initCache = function()
    {
        that.cache = { };
    };

    this.appendContent = function(html)
    {
        jQuery("#content").append(html);
    };

    this.clearContent = function()
    {
        jQuery("#content").empty();
    };

	this.hideContent = function()
	{
		jQuery("#content").children().hide();
	};

	this.showContent = function()
	{
		jQuery("#content").children().show();
	};

	this.showLoadingAnimation = function()
    {
        jQuery("#loading-animation").show();
    };

    this.hideLoadingAnimation = function()
    {
        jQuery("#loading-animation").hide();
        jQuery("#loading-animation-progress").html("");
    };

    this.setLoadingAnimationProgress = function(progress)
    {
        jQuery("#loading-animation-progress").html(progress + "%");
    };

    this.checkImagesLoaded = function(callback)
    {
        var images = document.images,
            timeout = 250;

        var func = function() {
            var imagesLoaded = 0,
                imagesLoading = 0;

            // wait for all images to be loaded:
            if (images) {
                for (var idx = 0, len = images.length; idx < len; idx++) {
                    var img = images[idx];
                    if (!img.complete) {
                        imagesLoading++;
                    } else {
                        imagesLoaded++;
                    }
                }
            }
            var progress = (imagesLoaded / (imagesLoaded + imagesLoading)) * 100;
            that.setLoadingAnimationProgress(progress.toPrecision(3));

            if (imagesLoading > 0) {
                // Not all images have yet been loaded completely,
                // so try again later:
                setTimeout(func, timeout);
            } else {
                // All images have been loaded,
                // now display the content:
                that.hideLoadingAnimation();
                that.showContent();

                if (callback) callback();
            }
        };

        // the DOM tree already contains the new images,
        // but the JavaScript side may not know the new images now.
        // So give it some time to update:
        setTimeout(func, timeout);
    };


    ///////////////////////////////////////////////////////////////////////////
    // Default Event Listeners:

    function onBeforeLoad(evt)
    {
        that.clearContent();
        that.showLoadingAnimation();
    }

    function onBeforeUnload(evt)
    {
    }

    function onLoaded(evt)
    {
        that.trace("onLoaded", "Loading took " + (evt.finished - evt.started) + " milliseconds");
    }

    function onUnloaded(evt)
    {
        that.trace("onUnloaded", "View <" + viewName + "> was unloaded");
    }


    function init()
    {
        that.initCache();

        that.beforeLoad.addListener(function(evt) { onBeforeLoad(evt); });
        that.beforeUnload.addListener(function(evt) { onBeforeUnload(evt); });
        that.loaded.addListener(function(evt) { onLoaded(evt); });
        that.unloaded.addListener(function(evt) { onUnloaded(evt); });
    }


    var that = this;
    var timeStarted = null;
    var timeFinished = null;

    init();
};

