/**
 * SlideShow
 *
 * @author Boz
 * @classDescription A class based slide show UI component
 * @css
 *      .navitem - navitems
 *      #nav00 - convention for mapping navitems to slides
 *      .slide - slides
 *      #slide00 - convetion for mapping slides to navitems
 *      .current - denotes the currently visible slide
 *      .pre - elements that invoke getPrev()
 *      .nxt - elements that invoke getNext()
 *
 * Assumptions:
 *  - There will be the same # of navitmes as there are slides
 *
 **/

mdp.app.SlideShow = function(slideShowId,options){
    /* ---[ CLASS VARIABLES ]--- */
    var self = this;
    this.slideshow;
    this.length;
    this.index=1;
    
    /* ---[ CONSTRUCTOR ]--- */
    function init(){

        /* initialization code */
        setupEventListeners();

        /* display slideshow length */
        $$(".lengthDisplay").setHTML(self.length);
    }

    /* ---[ PUBLIC METHODS ]--- */

    /* ---[ PRIVATE METHODS ]--- */
    function getSlide(index){
        /* hide current slide */
        var lastSlide = getCurrent().removeClass("current").addClass("hide");

        /* show slide by index */
        var current = $("slide" + index).addClass("current");
        if(!window.ie6 && !window.ie7){
            lastSlide.setOpacity(0);
            var fx = new Fx.Style(current,"opacity",{duration:getDuration(),onStart:function(){
                current.removeClass("hide");
            }}).start(0,1);
        }
        else{
            current.removeClass("hide");    
        }

        /* update navigation */
        updateNavigation(index);

        /* sendPageEvent */
        report(index);

        /* check the state of the slideshow */
        if(typeof(options) == 'undefined' || options != null){

            if(index == 1){
                (typeof(options.onPrevDisable) == 'undefined')? null:options.onPrevDisable();
                (typeof(options.onNextEnable) == 'undefined')? null:options.onNextEnable();
            }

            else if(index == self.length){
                (typeof(options.onPrevEnable) == 'undefined')? null:options.onPrevEnable();
                (typeof(options.onNextDisable) == 'undefined')? null:options.onNextDisable();
            }

            else{
                (typeof(options.onPrevEnable) == 'undefined')? null:options.onPrevEnable();
                (typeof(options.onNextEnable) == 'undefined')? null:options.onNextEnable();
            }

        }

        /* display index */
        self.index = index;
        $$(".indexDisplay").setHTML(self.index);
    }

    function getNext(){
        /* get the current index */
        var current = getCurrent();
        var index = current.id.replace(/[^0-9]/g, "");
        if(index < self.length){
            index = parseInt(index,10) + 1;
            if(index <10){ index = "0" + index; }
        }

        /* update navigation */
        updateNavigation(index);

        /* show the slide */
        if($("slide" + index) != null){
            /* hide current slide */
            var lastSlide = current.removeClass("current").addClass("hide");

            /* show next slide */
            current = $("slide" + index).addClass("current");
            if(!window.ie6 && !window.ie7){
                lastSlide.setOpacity(0);
                new Fx.Style(current,"opacity",{duration:getDuration(),onStart:function(){
                    current.removeClass("hide");
                }}).start(0,1);
            }
            else{
                current.removeClass("hide");
            }

            /* sendPageEvent */
            report(index);

            /* check the state of the slideshow */
            index = parseInt(index,10);
            if(typeof(options) == 'undefined' || options != null){

                if(index == 2){
                    (typeof(options.onPrevEnable) == 'undefined')? null:options.onPrevEnable();
                }

                if(index == self.length){
                    (typeof(options.onNextDisable) == 'undefined')? null:options.onNextDisable();
                }

                (typeof(options.onPreload) == 'undefined')? null:options.onPreload();
            }

            /* display index */
            self.index = index;
            $$(".indexDisplay").setHTML(self.index);
        }
    }

    function getPrev(){
        /* get current index */
        var current = getCurrent();
        var index = current.id.replace(/[^0-9]/g, "");
        if(index > 1){
            index = parseInt(index,10) - 1;
            if(index <10){ index = "0" + index; }
        }

        /* update navigation */
        updateNavigation(index);

        /* show the slide */
        if($("slide" + index) != null){
            /* hide current slide */
            var lastSlide = current.removeClass("current").addClass("hide");

            /* show previous slide */
            current = $("slide" + index).addClass("current");

            if(!window.ie6 && !window.ie7){
                lastSlide.setOpacity(0);
                new Fx.Style(current,"opacity",{duration:getDuration(),onStart:function(){
                    current.removeClass("hide");
                }}).start(0,1);
            }
            else{
                current.removeClass("hide");
            }

            /* sendPageEvent */
            report(index);

            /* check the state of the slideshow */
            index = parseInt(index,10);
            if(typeof(options) == 'undefined' || options != null){

                if(index == 1){
                    (typeof(options.onPrevDisable) == 'undefined')? null:options.onPrevDisable();
                }

                if(index == self.length-1){
                    (typeof(options.onNextEnable) == 'undefined')? null:options.onNextEnable();
                }

            }

            /* display index */
            self.index = index;
            $$(".indexDisplay").setHTML(self.index);
        }
    }

    function updateNavigation(index){
        var current = $E(".selected",$(slideShowId));
        if(current != null){
            current.removeClass("selected");
            var navitem = $("nav" + index);
            if(navitem != null){ navitem.addClass("selected"); }
        }
    }

    function getCurrent(){
        return $E(".current",$(slideShowId));
    }

    function findParent(el,_class){
        if(el.hasClass(_class)){
            return el;
        }
        else{
            return findParent(el.getParent(),_class);
        }
    }

    function getDuration(){
        if(typeof(options) == 'undefined' || options == null){
            return 300;
        }
        else{
            return (typeof(options.duration) == 'undefined')? 300 : options.duration;
        }
    }

    function report(index){

        if(typeof(options) != "undefined" && !options.disableAdRefresh){

            if(typeof(s) == "object"){
                var pagename = "";
                var pagenamevals = s.pageName.split(":");

                /* insert new slide index */
                pagenamevals[2] = "Slide"+parseInt(index,10);

                /* rebuild page name string */
                for(var i=0; i<pagenamevals.length; i++){
                    pagename += pagenamevals[i];
                    if(i != pagenamevals.length-1){
                        pagename += ":";
                    }
                }

                /* set omniture values */
                s.eVar9 = pagename;
                if(typeof(sendPageEvent) == 'function'){
                    sendPageEvent('',pagename);
                }
            }

        }

    }

    /* ---[ EVENT LISTENERS ]--- */
    function setupEventListeners(){

        /* find the slideshow */
        this.slideshow = $(slideShowId);

        /* find pre within the slideshow */
        $ES(".pre",this.slideshow).each(function(pre,i){

            /* attach getPrev() to pre click events */
            pre.addEvent("click",function(){
                getPrev();
            });

        });

        /* find nxt within the slideshow */
        $ES(".nxt",this.slideshow).each(function(nxt,i){

            /* attach getNext() to nxt click events */
            nxt.addEvent("click",function(){
                getNext();
            });

        });

        /* find all nav items within slideshow */
        $ES(".navitem",this.slideshow).each(function(nav,i){

            /* attach getSlide() to nav item click events */
            nav.addEvent("click",function(){
                var index = this.id.replace(/[^0-9]/g, "");
                getSlide(index);
            });

        });

        /* get slideshow length */
        var slides = $ES(".slide",this.slideshow);
        self.length = slides.length;

        /* set opacity for smooth animation */
        if(!window.ie6 && !window.ie7){
            slides.each(function(slide,index){
                if(index != 0){
                    slide.setOpacity(0);
                }
            });
        }
    }

    /* ---[ RUN ]--- */
    init();
};

