﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("MXA.Website.Kaldenbroeck");

MXA.Website.Kaldenbroeck.TextTemplate = function (element) {

    MXA.Website.Kaldenbroeck.TextTemplate.initializeBase(this, [element]);
    this.addCssClass('TextTemplate');

    this._intro = document.createElement('div');
    this._intro.className = 'tarIntro';
    this._intro.innerHTML = '';
    this._contentContainer.appendChild(this._intro);

    this._body = document.createElement('div');
    this._body.className = 'tarBody';
    this._body.innerHTML = '';
    this._contentContainer.appendChild(this._body);

    this._activeIntro = null;
    this._activeBody = null;
}

MXA.Website.Kaldenbroeck.TextTemplate.prototype =
{

    initialize: function () {
        MXA.Website.Kaldenbroeck.TextTemplate.callBaseMethod(this, 'initialize');
    },

    dispose: function () {
        MXA.Website.Kaldenbroeck.TextTemplate.callBaseMethod(this, 'dispose');
    },

    getHeight: function () {
        return this._contentContainer.scrollHeight;
    },

    databind: function () {
        MXA.Website.Kaldenbroeck.TextTemplate.callBaseMethod(this, 'databind');
        var source = this._datasource;

        if (this._activeIntro != null) {
            this._activeIntro.parentNode.removeChild(this._activeIntro);
        }
        if (this._activeBody != null) {
            this._activeBody.parentNode.removeChild(this._activeBody);
        }

        if (source._introCache != null) {
            this._intro.appendChild(source._introCache);
        }
        else {
            var element = document.createElement('div');
            this._intro.appendChild(element);
            element.innerHTML = source.intro;
            source._introCache = element;
            Cufon.replace(element);
        }

        if (source._bodyCache != null) {
            this._body.appendChild(source._bodyCache);
        }

        else if (source.body != null) {
            var element = document.createElement('div');
            this._body.appendChild(element);
            element.innerHTML = source.body['#cdata-section'];
            source._bodyCache = element;
            Cufon.replace(element);
            //this._body.innerHTML = source.body['#cdata-section'];
        }
        else {
            //this._body.innerHTML = '';
        }

        this._activeIntro = source._introCache;
        this._activeBody = source._bodyCache;

    }
}



MXA.Website.Kaldenbroeck.TextTemplate.registerClass('MXA.Website.Kaldenbroeck.TextTemplate', MXA.Website.Kaldenbroeck.Template);
