$(document).ready(function()
{
    $.ajax({
        url: "sites/all/files/data.xml",
        type: 'GET',
        dataType: 'xml',
        success: writeContent
    });

    function writeContent(xml)
    {
        var arr = [];

        $(xml).find('slide').each(function()
        {
            var obj = new Object();
            obj['loc'] = $(this).children('loc').text();
            obj['maploc'] = $(this).children('continent').text();
            obj['href'] = $(this).children('href').text();
            obj['imgsrc'] = $(this).children('imgsrc').text();

            var img = new Image();
            img.src = obj['imgsrc'];
            //$(img).load(function() {
                obj['img'] = img;
                arr.push(obj);
            //});
        }); //endfor

        $(arr).each(function(i, obj)
        {
            //obj['img'].load(function() {
                $('#slideshow_container ul#slides').append('<li></li>');
                $('#slideshow_container ul#slides li:last')
                    .append('<p><a href="'+obj['href']+'"><img src="'+obj['img'].src+'" width="615" height="405" alt="" /></a></p>')
                    .append('<div class="infobar"><p class="maploc"><img src="sites/all/themes/denali/images/nda24oct/map_'+obj['maploc']+'.png" width="94" height="36" alt="'+obj['maploc']+'" /></p><p>'+obj['loc']+'</p></div>');
            //});
        });

        enableSlideshow();
    }
	
    // enable scroller functionality
    function enableSlideshow()
    {
        var slider = $('#slideshow_container ul#slides');
        var playcontrols = $('#slideshow_container .controls .stoppause a');
        var prevSlide = 0;
        $(playcontrols).addClass('playing');

        var slideshow = $(slider).cycle({
            fx: 'scrollHorz',
            easing: 'linear',
            timeout: 4000,  // milliseconds between slide transitions (0 to disable auto advance)
            speed: 500,     // speed of the transition (any valid fx speed value)
            prev: '#slideshow_container .controls .prev a',
            next: '#slideshow_container .controls .next a',
            after: function (currSlideElement, nextSlide) { // determine index of previous slide
                var i = $(slider).children().index(nextSlide);

                if (i==0) {
                    prevSlide = $(slider).children().length-1; // scroll to the last slide
                    //prevSlide = 0;
                }
                else {
                    prevSlide = i-1;
                }
            }
        })
        .hover(
            function() {
                slide_pause($(this));
            },
            function() {
                if ($(playcontrols).hasClass('playing')) {
                    slide_play($(this));
                    $(playcontrols).attr('title','click to pause');
                }
            }
        );

        $(playcontrols).hover(
            function() {
                slide_pause($(slider));

                if ($(this).hasClass('playing')) {
                    $(this).click(function() {
                        slide_pause($(slider));
                        $(this).removeClass('playing');
                        $(this).attr('title','click to play');
                    });
                }
                else {
                    $(this).click(function() {
                        slide_play($(slider));
                        $(this).addClass('playing');
                        $(this).attr('title','click to play');
                    });
                }
            },
            function() {
                if ($(this).hasClass('playing')) {
                    slide_play($(slider));
                    $(this).attr('title','click to pause');
                }
            }
        );

        // keyboard shortcuts
        $('body').keyup(function(e)
        {
            //alert(e.which);
            if (e.which == 82) { //left arrow
                $(slider).cycle('resume', true);
            }
            else if (e.which == 92) { //right arrow
                //alert('ehre');
                $(slider).cycle('resume', true);
            }
        });

        function slide_pause(e) {
            $(slider).cycle('pause');
            $(playcontrols).css('background-position','bottom left');
        }

        function slide_play(e) {
            $(slider).cycle('resume');
            $(playcontrols).css('background-position','top left');
        }
    }

});

// do not modify
$.fn.cycle.transitions.scrollHorz = function($cont, $slides, opts) {
    $cont.css('overflow','hidden').width();
    opts.before.push(function(curr, next, opts, fwd) {
        $.fn.cycle.commonReset(curr,next,opts);
        opts.cssBefore.left = fwd ? (next.cycleW-1) : (1-next.cycleW);
        opts.animOut.left = fwd ? -curr.cycleW : curr.cycleW;
    });
    opts.cssFirst = { left: 0 };
    opts.cssBefore= { top: 0 };
    opts.animIn   = { left: 0 };
    opts.animOut  = { top: 0 };
};

