﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("MXA.Website.Kaldenbroeck");

parseData = function (data) {
    if (data.categories != null) {
        if (data.categories.category.length == null) {
            data.categories.category = [data.categories.category];
        }

        for (var i in data.categories.category) {
            var item = data.categories.category[i];
            if (item.articles.article.length == null) {
                item.articles.article = [item.articles.article];


            }

            for (var j in item.articles.article) {
                var iItem = item.articles.article[j];

                if (iItem.images != null) {
                    if (iItem.images.image.length == null) {
                        iItem.images.image = [iItem.images.image];
                    }
                }

            }

        }
    }
    if (data.events != null) {
        if (data.events.event.length == null) {
            data.events.event = [data.events.event];
        }
    }
    if (data.backgrounds != null && data.backgrounds.image.length == null) {
        data.backgrounds.image = [data.backgrounds.image];
    }

    document.datasource = data;
}

MXA.Website.Kaldenbroeck.Application = function (element) {

    MXA.Website.Kaldenbroeck.Application.initializeBase(this, [element]);
    this.addCssClass('Application');
    this.addCssClass(navigator.platform);
    var bgManagerElement = document.createElement('div');
    element.appendChild(bgManagerElement);
    var _this = this;

    var lightboxElement = document.createElement('div');
    element.appendChild(lightboxElement);
    this._bgManager = $create(MXA.Website.Kaldenbroeck.BGManager, null, null, null, bgManagerElement);
    this._lightbox = $create(MXA.Website.Kaldenbroeck.LightBox, null, null, null, lightboxElement);

    var menuElement = document.createElement('div');
    element.appendChild(menuElement);
    this._menu = $create(MXA.Website.Kaldenbroeck.MenuControl, null, null, null, menuElement);
    this._menu.add_itemClick(Function.createDelegate(_this, _this._menuClick));

    var logoElement = document.createElement('div');
    logoElement.className = 'appLogo';
    element.appendChild(logoElement);

    this._templates = {};
    this._activeTemplate = null;
    this._templateContainer = document.createElement('div');
    this._templateContainer.className = 'appTemplates';
    element.appendChild(this._templateContainer);

    var overview = this._loadTemplate('', MXA.Website.Kaldenbroeck.CategoryView);
    overview.add_itemClick(Function.createDelegate(_this, _this._blaClick));
    this._loadTemplate('Articles', MXA.Website.Kaldenbroeck.ArticleView);
    this._loadTemplate('Events', MXA.Website.Kaldenbroeck.EventView);
    this._slug = window.location.hash;
    this._loadTemplate('Contact', MXA.Website.Kaldenbroeck.ContactView);
    if (document.__trackerID != null) {
        this._tracker = _gat._getTracker(document.__trackerID);
    }
}

MXA.Website.Kaldenbroeck.init = function (container) {
    $create(MXA.Website.Kaldenbroeck.Application, null, null, null, container);
}

MXA.Website.Kaldenbroeck.Application.prototype =
{

    initialize: function () {
        MXA.Website.Kaldenbroeck.Application.callBaseMethod(this, 'initialize');
        if (document.datasource != null && document.datasource.organisation != null) {
            //document.title = document.datasource.organisation.name;
        }
        var _this = this;
        $app = this;
        $addHandler(window, 'scroll', Function.createDelegate(_this, _this._onScroll));
        $addHandler(window, 'resize', Function.createDelegate(_this, _this._resize));
        var slug = window.location.hash.substring(1);
        var path = null;
        if (slug.indexOf('/') == -1) {
            path = slug;
        }
        else {
            path = slug.substring(0, slug.indexOf('/'));
        }

        var source = this._getSourceForSlug(path);

        this.goto(slug, source);
        this._lightbox.setSize();

        setInterval(Function.createDelegate(_this, _this._checkSlug), 200);
    },

    setHistoryUrl: function (location) {
        this._slug = location;
        window.location.hash = location;
    },

    _checkSlug: function () {
        if (this._slug != window.location.hash.substring(1)) {
            var slug = window.location.hash.substring(1);
            var path = slug.substring(0, slug.indexOf('/'));
            var source = this._getSourceForSlug(path);
            $app.goto(slug, source);
        }

    },

    dispose: function () {
        MXA.Website.Kaldenbroeck.Application.callBaseMethod(this, 'dispose');
    },

    _resize: function () {
        this._activeTemplate.setSize();
        this._lightbox.setSize();
        this._bgManager.setSize();
    },

    _menuClick: function (sender, args) {
        this.goto(args.commandArgument, args.datasource);
    },


    _loadTemplate: function (name, type) {

        var templateElement = document.createElement('div');
        this._templateContainer.appendChild(templateElement);

        var template = $create(type, null, null, null, templateElement);
        this._templates[name] = template;

        return template;
    },

    _blaClick: function (sender, args) {
        var p = 'Articles/' + args.datasource.title;

        var v = this.goto(p, document.datasource.categories.category);
        v.setStyle(args.datasource.style);
    },

    _getSourceForSlug: function (slug) {

        if (document.datasource == null) {
            return null;
        }
        switch (slug) {
            case '':
                if (document.datasource.categories != null) {
                    return document.datasource.categories.category;

                }
                break;

            case 'Articles':
                if (document.datasource.categories != null) {
                    return document.datasource.categories.category;
                }
                break;

            case 'Events':
                if (document.datasource.events == null) {
                    return null;
                }
                return document.datasource.events.event;
                break;

            case 'Contact':
                return document.datasource.organisation;
                break;
        }
    },

    goto: function (location, source) {
        this._slug = location;
        var l = location.split('/');
        var view = l[0];
        if (view != 'Articles') {
            this._bgManager.activateType(view);
        }
        var subPath = [];
        for (var i = 1; i < l.length; i++) {
            subPath.push(l[i]);
        }
        this._lightbox.hide();
        window.location.hash = location;
        var current = this._activeTemplate;
        var val = this._showView(view, source, subPath);

        this._menu.updateSelectionState(l);

        if (this._tracker != null) {
            this._tracker._trackPageview('/' + location);
        }

        return val;

    },

    _showView: function (type, data, p) {

        if (this._activeTemplate != null) {

        }


        var template = this._templates[type];
        template.clear();
        template.show();

        if (template == this._activeTemplate) {
            template.setPath(p);
            template.updatePath();

        }
        else {
            if (this._activeTemplate != null) {
                this._activeTemplate.hide();
            }
            template.set_datasource(data);
            template.setPath(p);
            template.databind();

        }
        this._activeTemplate = template;

        template.setSize();
        document.documentElement.scrollTop = '0';
        return template;
    },

    _onScroll: function () {
        var selectedIndex = this._bgManager.get_selectedIndex();
        var count = this._bgManager.get_backgroundCount();
        var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
        var h = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
        var percent = scrollTop / (h - document.documentElement.clientHeight);
        var index = Math.round((count - 1) * percent);

        if (selectedIndex != index) {
            this._bgManager.activateIndex(index);
        }

    }
}



MXA.Website.Kaldenbroeck.Application.registerClass('MXA.Website.Kaldenbroeck.Application', Sys.UI.Control);

function parseData(data) {
    document.datasource = data;
}
