dojo.provide("nl.conclusion.Carousel"); //, null, {
dojo.declare("nl.conclusion.Carousel", null, {

    statics: { idCounter: 0, subscribed: "nl/conclusion/Carousel/selectElement" },
    flashId: "",
    carousel: undefined,
    data: undefined,
    base: undefined,
    fadedOut: false,
    checkId: undefined,

    constructor: function(baseElement) {
        this.base = baseElement;

        if (this.base.id == "") {
            this.base.id = this.__generateId();
        }
        this.loadExternalData();
    },

    __generateId: function() {
        return "qi_carousel_generatedId" + this.statics.idCounter++;
    },

    loadExternalData: function() {
        //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
        var xhrArgs = {
            url: "/index.aspx?chapterid=24&contentid=51",
            handleAs: "json",
            load: dojo.hitch(this, this.onDataReady),
            error: dojo.hitch(this, this.onError)
        }

        //Call the asynchronous xhrGet
        var deferred = dojo.xhrGet(xhrArgs);
    },

    onSelect: function(carouselId, id) {
        if (this.base.id == carouselId) {
            this.fillObject(id);
        }
    },
    onDataReady: function(data) {
        this.data = data;
        dojo.animateProperty({
            node: this.base.id,
            properties: {
                opacity: { end: 0 }
            },
            onEnd: dojo.hitch(this, this.onEndFadeOut)
        }).play();
    },

    onSWFReady: function(e) {
        this.carousel = e.ref;
        if (this.fadedOut) {
            this.checkId = setInterval(dojo.hitch(this, this.checkReady), 100);
        }
    },

    initCarousel: function() {
        var counter = 0;
        dojo.forEach(this.data.people, dojo.hitch(this, function(person) {
            if (person) {
                this.carousel.addPerson(counter, person.image);
                counter++;
            }
        }));
        //~ alert("nl.conclusion.Carousel.selectElement: "+ nl.conclusion.Carousel.selectElement);
        this.carousel.finalizePersonList("nl.conclusion.Carousel.selectElement", this.base.id);
    },

    checkReady: function() {
        if (this.fadedOut && this.carousel && this.carousel.addPerson && this.carousel.finalizePersonList) {
            clearInterval(this.checkId);
            this.checkId = undefined;
            this.initCarousel();
            this.fillObject();
            dojo.subscribe(this.statics.subscribed, this, this.onSelect);
        }
    },

    onEndFadeOut: function() {
        this.fadedOut = true;
        var flashHolders = dojo.query(".pictureCarousel>div", this.base);

        if (flashHolders.length) {
            var holder = flashHolders[0];

            if (holder.id == "") {
                holder.id = this.__generateId();
            }

            var flashId = holder.id;
            var flashvars = {}; //domain: '*', uid: 'NUWA-YS'};
            var params = { allowscriptaccess: 'always', menu: 'false', wmode: 'transparent' };
            var attributes = { id: flashId };

            swfobject.embedSWF("/Sites/Conclusion/swf/carousel.swf", flashId, "188", "188", "10.0.0", "images/expressInstall.swf", flashvars, params, attributes, dojo.hitch(this, this.onSWFReady));
        }
    },

    fillObject: function(index) {

        if (index == undefined || index == null) {
            index = 0;
        }

        var person = this.data.people[index]

        dojo.query(".quoteContent", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.quote);
        }));

        dojo.query(".personInfo ul:nth-child(2) li:nth-child(1)", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.expert.name);
        }));

        dojo.query(".personInfo ul:nth-child(2) li:nth-child(2)", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.expert.jobTitle);
        }));

        dojo.query(".personInfo ul:nth-child(2) li:nth-child(3)", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.contactInfo.email);
        }));

        dojo.query(".personInfo ul:nth-child(4) li:nth-child(2)", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.contactInfo.phone);
        }));

        dojo.query(".personInfo ul:nth-child(4) li:nth-child(1)", this.base).forEach(dojo.hitch(this, function(e) {
            e.innerHTML = decodeURI(person.contactInfo.email);
        }));

        dojo.query(".personInfo .linkedIn", this.base).forEach(dojo.hitch(this, function(e) {
            e.href = decodeURI(person.contactInfo.linkedIn);
        }));

        if (dojo.style(this.base.id, "opacity") < 1) {
            dojo.animateProperty({
                node: this.base.id,
                properties: {
                    opacity: { end: 1 }
                }
            }).play();
        }

    },
    onError: function(error) {
        // error!!!
        alert("error: " + error);
    }
});

nl.conclusion.Carousel.selectElement = function(carouselId, id) {
    dojo.publish("nl/conclusion/Carousel/selectElement", [carouselId, id]);
};

dojo.addOnLoad(function() {

    dojo.query(".quoteCarousel").forEach(function(element) {
        new nl.conclusion.Carousel(element);
    });

});

