﻿function onLoad(){
	cssLoader();
	dojo.require("dijit.layout.AccordionContainer");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("dojox.image.SlideShow");
	dojo.require("dojo.data.ItemFileReadStore");
	dojo.require("dijit.Tooltip");
	dojo.require("dojo.parser");
	dojo.addOnLoad(function() {
                var imageItemStore = new dojo.data.ItemFileReadStore({"url":"resources/images.json"}, 'imageItemStore');
		var slideshow = new dojox.image.SlideShow({autoStart:"true",
			noLink:"true",loop:"true", autoLoad:"true", slideshowInterval:"4", fixedHeight:"true"},
			'slideshow');
		
                //This function runs when the widget has finished constructing
                        //It sets the datastore to read the image data from
                        dijit.byId('slideshow').setDataStore(
                                //The image store object, defined in the DIV above this one
                                imageItemStore,
                                //The request object to use,
                                { query: {}, count:4 },
                                //The names of the attributes to read from the data store
                                {
                                    imageThumbAttr: "thumb",
                                    imageLargeAttr: "thumb"
                                }
                        );
                });

	toolTip();
}

function mycarousel_initCallback(carousel, state)
{
    // Lock until all items are loaded. That prevents jCarousel from
    // setup correctly and we have to do that in the ajax callback
    // function with carousel.setup().
    // We're doing that because we don't know the exact height of each
    // items until they are added to the list.
    carousel.lock();

    jQuery.get(
            'resources/events.xml',
            {'data' : 'data'},
            function(xml) {
                mycarousel_itemAddCallback(carousel, xml);
            },
            'xml'
        );
};

function mycarousel_itemAddCallback(carousel, xml)
{
    var $items = jQuery('item', xml);

    $items.each(function(i) {
        carousel.add(i + 1, mycarousel_getItemHTML(this));
    });

    carousel.size($items.size());

    // Unlock and setup.
    carousel.unlock();
    carousel.setup();
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    var html_text = '<h3><a class="linkEvent" href="'+$('link', item).text()+'">'+$('title', item).text()+'</a></h3>';
    if($('description', item).text())
	html_text += '<p>'+mycarousel_truncate($('description', item).text(), 120)+'<br>';
    html_text += '<span class="eventFecha">['+$('pubDate', item).text()+']</span></p>';
    return html_text;
};

/**
 * Utility function for truncating a string without breaking words.
 */
function mycarousel_truncate(str, length, suffix) {
    if (str.length <= length) {
        return str;
    }

    if (suffix == undefined) {
        suffix = '...';
    }

    return str.substr(0, length).replace(/\s+?(\S+)?$/g, '') + suffix;
};

