﻿/// <reference name="MicrosoftAjax.js"/>
parseData = function (data) {




    document.datasource = data;

    if (data.news != null && data.news.news.length == null) {
        data.news.news = [data.news.news];
    }
    if (data.products != null && data.products.product.length == null) {
        data.products.product = [data.products.product];
    }
    if (data.workshops != null && data.workshops.workshop.length == null) {
        data.workshops.workshop = [data.workshops.workshop];
    }
    if (data.companies != null && data.companies.company.length == null) {
        data.companies.company = [data.companies.company];
    }




    document.datasource.animation = {};
    document.datasource.animation.speed = {};
    document.datasource.animation.speed.toolBar = 1000;
    document.datasource.animation.speed.listExpanderIn = 300;
    document.datasource.animation.speed.listExpanderOut = 100;
    document.datasource.animation.speed.listContainer = 600;
    document.datasource.animation.speed.listExpanderClose = 300;
    document.datasource.animation.speed.productListItem = 1000;
    document.datasource.animation.speed.productListClose = 300;
    document.datasource.animation.speed.productListScroll = 600;
    document.datasource.animation.speed.productOut = 100;
    document.datasource.animation.speed.productIn = 500;
    document.datasource.animation.speed.backgroundOut = 100;
    document.datasource.animation.speed.backgroundIn = 500;
    document.datasource.animation.speed.contactOut = 100;
    document.datasource.animation.speed.contactIn = 500;
}



Type.registerNamespace("MXA.FP.Application");

MXA.FP.Application = function (element) {
    MXA.FP.Application.initializeBase(this, [element]);

    this._views = {};
    this._viewContainer = document.createElement('div');
    this._viewContainer.className = 'appViewContainer';
    this._activeViewName = null;

    var bgManager = document.createElement('div');
    element.appendChild(bgManager);
    this._bgManager = $create(MXA.FP.Control.BGManager, null, null, null, bgManager);

    element.appendChild(this._viewContainer);

    var menuElement = document.createElement('div');
    element.appendChild(menuElement);
    this._menu = $create(MXA.FP.Control.Menu, null, null, null, menuElement);
    var _this = this;
    this._menu.add_itemClick(Function.createDelegate(_this, _this._onMenuClick));
    $addHandler(window, 'resize', Function.createDelegate(_this, _this._onResize));

    if (document.__trackerID != null) {
        this._tracker = _gat._getTracker(document.__trackerID);
    }
}

MXA.FP.Application.init = function (readyHandler) {
    var app = $create(MXA.FP.Application, null, null, null, document.body);
    app._onReadyHandler = readyHandler;
}

MXA.FP.Application.prototype =
{
    initialize: function () {
        this._createView('info', MXA.FP.View.ArticleView);
        this._createView('assortiment', MXA.FP.View.ProductView);
        this._createView('workshops', MXA.FP.View.WorkshopView);
        this._createView('toerisme', MXA.FP.View.SuggestedCompanyView);
        this._menu.addItem('bestellen', 'order');
        this._createView('contact', MXA.FP.View.ContactView);
        MXA.FP.Application.callBaseMethod(this, 'initialize');
        this._loadImages();

        this._readyCheckInterval = setInterval(Function.createDelegate(this, this._checkReadyState), 100);

    },

    _checkReadyState: function () {
        if (this.get_isReady() == true) {
            clearInterval(this._readyCheckInterval);
            this._onReady();
        }
    },

    _onReady: function () {
        this._show();
        if (this._onReadyHandler != null) {
            this._onReadyHandler();
        }
    },

    get_isReady: function () {
        return this._bgManager.get_isReady();
    },

    _loadImages: function () {

        for (var i in document.datasource.news.news) {
            var item = document.datasource.news.news[i];
            this._bgManager.addBackground(item.image);
        }

        for (var i in document.datasource.products.product) {
            var item = document.datasource.products.product[i];
            this._bgManager.addBackground(item.image);
        }

        for (var i in document.datasource.workshops.workshop) {
            var item = document.datasource.workshops.workshop[i];
            this._bgManager.addBackground(item.image);
        }

        for (var i in document.datasource.backgrounds.background) {
            var item = document.datasource.backgrounds.background[i];
            this._bgManager.addBackground(item.image);
        }

        for (var i in document.datasource.backgrounds.background) {
            var item = document.datasource.backgrounds.background[i];
            this._bgManager.addBackground(item.image);
        }
    },

    _show: function () {
        var _this = this;
        _this._menu.show();
        setTimeout(function () { _this._menu.hide() }, 5000);
        this._bgManager.onResize();
        _this.goto('info');
    },

    _createView: function (name, type) {
        var container = document.createElement('div');
        this._viewContainer.appendChild(container);
        var view = $create(type, null, null, null, container);
        view._bgManager = this._bgManager;
        this._views[name] = view;
        this._menu.addItem(name, name);
        view.databind();
    },

    _onResize: function (evt) {
        this._bgManager.onResize();

        var view = this._views[this._activeViewName];

        if (view != null) {
            view.onResize();
        }
    },

    goto: function (path) {
        var newView = this._views[path];
        var oldView = this._views[this._activeViewName];
        if (newView == oldView) {
            return;
        }

        if (oldView != null) {
            var _this = this;
            oldView.deActivate(function () {
                _this._activeViewName = path;
                var activeView = _this._views[path];
                if (activeView != null) {
                    activeView.onSelected();
                    //activeView.databind();
                    var bg = activeView.getBackgroundUrl();
                    _this._bgManager.showBackground(bg, function () {
                        activeView.activate();
                    });

                }
            });
        }
        else {
            var _this = this;
            this._activeViewName = path;
            var activeView = this._views[path];
            if (activeView != null) {
                //activeView.databind();
                var bg = activeView.getBackgroundUrl();
                _this._bgManager.showBackground(bg, function () {
                    activeView.activate();
                });
            }
        }

        if (this._tracker != null) {
            this._tracker._trackPageview('/' + path);
        }
    },

    _onMenuClick: function (sender, args) {
        if (args.commandArgument == "order") {
            window.open('http://frissenpieters.bloemplein.nl/bloemist/');
        }
        else if (args.commandArgument != this._activeViewName) {
            var _this = this;
            this._menu.hide(function () {
                _this.goto(args.commandArgument);
            });
        }
    },

    dispose: function () {
        MXA.FP.Application.callBaseMethod(this, 'dispose');
    }
}

MXA.FP.Application.registerClass('MXA.FP.Application', Sys.UI.Control);
