﻿///<reference path="jquery-1.2.6-vsdoc.js" />
/// <reference path="KMDA.Babyolifant.DashboardItem.js" />
/// <reference path="Utils.js" />

Type.registerNamespace("KMDA.Babyolifant");

// Constructor
KMDA.Babyolifant.Dashboard = function(element) {

    //element.control = null;

    KMDA.Babyolifant.Dashboard.initializeBase(this, [element]);
    this.SelectedDate = new Date();
    this.DashboardItems = new Array();
    this.DashboardItems = new Array();
    this.VisibleItemTypes = new Array();
    this.Language = "";
}

KMDA.Babyolifant.Dashboard.prototype = {
    SelectedDate: null,
    VisibleItemTypes: new Array(),
    LoadedLayout: null,
    DashboardItems: new Array(),
    Language: "",


    FillDashboardItems: function(newdate) {


        $.ajax(
        {
            type: "POST",
            url: "KMDAService.asmx/GetVisibleItems",
            data: '{'
            + '"itemtypes":' + Utils.ToJSONString(this.VisibleItemTypes)
            + ',"itemdate":"' + newdate.format('MM/dd/yyyy') + '"'
            //+ ',"itemdate":"10/10/2009"'
            + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: this.DrawContent(),
            error: errorFunction,
            async: true
        });

    },
    DrawContent: function() {

        var obj = this;
        return function(data) {

            var externalid = "";
            for (var x = 0; x < obj.DashboardItems.length; x++) {
                if (obj.DashboardItems[x].Visible) {
                    externalid = "";
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].ItemType == obj.DashboardItems[x].DashboardItemType) {
                            externalid = data[i].ExternalId;
                        }
                    }

                    if ((obj.DashboardItems[x].ExternalId != externalid && externalid != "") || (externalid == "" && obj.DashboardItems[x].ExternalId == "")) {

                        obj.DashboardItems[x].RenderContent(obj.get_element(), externalid);
                        if (externalid != "") {
                            obj.DashboardItems[x].ExternalId = externalid;
                        } else {
                            obj.DashboardItems[x].ExternalId = "_";
                        }


                    }
                }
            }
        }

    },
    LoadLayout: function(layout, newdate) {


        this.LoadedLayout = layout.Name;
        this.SelectedDate = newdate;

        for (var i = 0; i < this.DashboardItems.length; i++) {
            this.DashboardItems[i].Visible = false;
        }

        for (var i = 0; i < layout.Sections.length; i++) {
            var newdashboarditem = new KMDA.Babyolifant.DashboardItem(layout.Sections[i]);
            this.AddOrUpdateDashboardItem(newdashboarditem);
        }

        this.ArrangeDashboard();
        this.FillDashboardItems(newdate);

    },
    AddOrUpdateDashboardItem: function(newdashboarditem) {


        for (var i = 0; i < this.DashboardItems.length; i++) {
            if (this.DashboardItems[i].DashboardItemType == newdashboarditem.DashboardItemType) {

                newdashboarditem.Visible = true;

                newdashboarditem.ExternalId = this.DashboardItems[i].ExternalId;
                newdashboarditem.HtmlTemplate = this.DashboardItems[i].HtmlTemplate;

                this.DashboardItems[i] = newdashboarditem;

                return false;
            }
        }
        //retrieve the item template
        newdashboarditem.HtmlTemplate = new HtmlTemplate("templates/" + this.Language + "/" + newdashboarditem.DashboardItemType + ".aspx").Html;
        if(newdashboarditem.ExternalScript)
            newdashboarditem.HtmlTemplate += new HtmlTemplate("templates/" + newdashboarditem.DashboardItemType + ".js").Html;
        newdashboarditem.Visible = true;

        this.DashboardItems.push(newdashboarditem);
    },
    ArrangeDashboard: function() {

        this.VisibleItemTypes = new Array();
        for (var i = 0; i < this.DashboardItems.length; i++) {
            //render each active item
            var currentdashboarditem = this.DashboardItems[i];

            var itemdiv = $('#' + currentdashboarditem.DashboardItemType);

            //if element does not exist, create it
            if (currentdashboarditem.Visible) {
                this.VisibleItemTypes.push(currentdashboarditem.DashboardItemType);
                if (itemdiv.length == 0) {
                    currentdashboarditem.CreateDashboardItem($(this.get_element()));
                } else {

                    currentdashboarditem.MoveOrHideDashboardItem(itemdiv);
                }
            } else {
                currentdashboarditem.RemoveDashboardItem(itemdiv);
            }
        }
        //attach fancybox handlers
        //attach fancybox handlers

    }
}

KMDA.Babyolifant.Dashboard.registerClass('KMDA.Babyolifant.Dashboard', Sys.UI.Control);


// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
