dojo.require('dojo.fx');
var pod = {
    container        : null,
    control          : null,
    displayControl   : null,
    displayContainer : null,
    displayWidth     : 940,
    pods             : null,
    titles           : null,
    titleNode        : null,
    podWidth         : 315,
    currentPod       : 0,
    timeout          : null,
    delay            : 7000,
    load : function() {
        pod.control   = dojo.byId('control');
        pod.container = dojo.byId('podContainer');
        pod.pods = dojo.query('div.pod', pod.container);
        pod.container.marginLeft = '0';
        pod.displayControl = dojo.byId('podDisplayControl');
        pod.displayContainer = dojo.byId('podDisplayContainer');
        pod.displayContainer.marginLeft = '0';
        pod.titles      = dojo.query('img', pod.displayContainer);
        pod.titleNode   = dojo.byId('podTitle');
        dojo.query('h2', pod.titleNode)
            .forEach(function(e){e.innerHTML = pod.titles[pod.currentPod].alt});
        clearInterval(pod.timeout);
        pod.timeout = setTimeout('pod.controls.auto()', pod.delay);
    },
    animate : function(){

    },
    controls : {
        left : function() {
            if (null == pod.pods)
                pod.load();
            if ((pod.currentPod + 2) <= (pod.pods.length/2)) {
                target = - ((pod.currentPod + 1) * 2 * pod.podWidth);
                displayTarget = - ((pod.currentPod + 1) * pod.displayWidth);
                dojo.fx.chain([
                dojo.fadeOut({node: pod.titleNode, duration:300}),
                dojo.fx.combine([
                    dojo.animateProperty({ node: pod.container,
                                       duration: 1000,
                                       properties: {
                                           marginLeft: {
                                               end   : target,
                                               unit  : 'px'
                                           }
                                       }}),
                    dojo.animateProperty({ node: pod.displayContainer,
                                       duration : 1000,
                                       properties: {
                                           marginLeft: {
                                               end  : displayTarget,
                                               unit : 'px'
                                           }
                                       },
                                       onEnd: function(){
                                          dojo.query('h2', pod.titleNode).forEach(function(e){e.innerHTML = pod.titles[pod.currentPod].alt})
                                       }
                                       })
                ]),
                dojo.fadeIn({node: pod.titleNode, duration:300})
                ]).play();
                       
                pod.currentPod += 1;
                clearInterval(pod.timeout);
                pod.timeout = setTimeout('pod.controls.auto()', pod.delay);
                return true;
            }
            return false;

        },
        right : function() {
            if (null == pod.pods)
                pod.load();
            if ((pod.currentPod - 1) >= 0) {
                target = - ((pod.currentPod - 1) * 2 * pod.podWidth );
                displayTarget = - ((pod.currentPod - 1) * pod.displayWidth);
                dojo.fx.chain([
                dojo.fadeOut({node: pod.titleNode, duration:300}),
                dojo.fx.combine([
                dojo.animateProperty({ node: pod.container,
                                       duration: 1000,
                                       properties: {
                                           marginLeft: {
                                               end   : target,
                                               unit  : 'px'
                                           }
                                       }}),
                dojo.animateProperty({ node: pod.displayContainer,
                                       duration : 1000,
                                       properties: {
                                           marginLeft: {
                                               end  : displayTarget,
                                               unit : 'px'
                                           }
                                       },
                                       onEnd: function(){
                                          dojo.query('h2', pod.titleNode).forEach(function(e){e.innerHTML = pod.titles[pod.currentPod].alt})
                                       }}),
                ]),
                dojo.fadeIn({node: pod.titleNode, duration:300})
                ]).play();
                pod.currentPod -= 1;
                clearInterval(pod.timeout);
                pod.timeout = setTimeout('pod.controls.auto()', pod.delay);
            return true;
            }
            return false;
        },
        reset : function()
        {
            if (null == pod.pods)
                pod.load();
            dojo.fx.chain([
                dojo.fadeOut({node: pod.titleNode, duration:300}),
                dojo.fx.combine([
                    dojo.animateProperty({ node: pod.container,
                                           duration: 1000,
                                           properties: {
                                               marginLeft: {
                                                   end   : 0,
                                                   unit  : 'px'
                                               }}
                                           }),
                   dojo.animateProperty({  node: pod.displayContainer,
                                           duration : 1000,
                                           properties: {
                                               marginLeft: {
                                                   end  : 0,
                                                   unit : 'px'
                                               }},
                                           onEnd: function(){
                                              dojo.query('h2', pod.titleNode).forEach(function(e){e.innerHTML = pod.titles[pod.currentPod].title})
                                           }
                                           })]),
                   dojo.fadeIn({node: pod.titleNode, duration:300}),
            ]).play();
            pod.currentPod = 0;
            clearInterval(pod.timeout);
            pod.timeout = setTimeout('pod.controls.auto()', pod.delay);
        },
        auto : function() {
            if (!pod.controls.left()) {
                pod.controls.reset();
            }
        }
    }

};
setTimeout('pod.load()', 100);
