var acupccSlideshow = 
{
    current: 1,
    playing: false,
    timer: null,
    total: null,
    $slideContainer: null,
    $slideDivs: null,
    $controls: null,
    $gotoControls: null,
    $playPause: null,
    
    init: function()
    {
        this.$slideContainer = $("#slides");
        this.$slideDivs = $("#slides div");
        this.$controls = $("#slide-controls");
        this.total = this.$slideDivs.length;
        this.current = this.getCurrentFromCounter();
        
        this.addPlaybackControls();
        this.$slideContainer.height(this.calculateHeight());
        this.playPause();
    },
    
    getCurrentFromCounter: function()
    {
        var cookies = document.cookie.split(";"),
        currentcount = 0,
        view, current;
        
        for(var i = 0; i < cookies.length; i++)
        {
            var cookie = cookies[i].split("=");
            cookie[0] = cookie[0].replace(/^\s+/,"");
            
            if(cookie[0] == "slidecounter")
            {
                currentcount = Number(cookie[1]);
                break;
            }
        }
        
        view = this.incrementCounter(currentcount);
        current = view % this.total; // will return 0 when you want total, hence ternary below
        
        return (current == 0) ? this.total : current;
    },
    
    incrementCounter: function(currentcount)
    {
        var expiry = new Date(),
        newcount = currentcount + 1;
        
        expiry.setTime(expiry.getTime()+1000*60*60*24*30);
        
        document.cookie = "slidecounter="+newcount+";expires="+expiry.toUTCString();
        
        return newcount;
    },
    
    addPlaybackControls: function()
    {
        for(var i = 1; i <= this.total; i++)
        {
            this.$controls.append($("<span />").text(i).addClass("goto").click(function(slideshow)
            {
                return function() { slideshow.goToSlide($(this).text()); };
            }(this)));
        }
        
        this.$playPause = $("<span>Pause</span>").click(function(slideshow)
        {
            return function() { slideshow.playPause(); };
        }(this));
        
        this.$controls.append(this.$playPause);
        
        this.$gotoControls = $("span.goto", this.$controls);
    },
    
    next: function()
    {
        if(this.current == this.total)
        {
            return 1;
        }
        
        return this.current+1;
    },
    
    calculateHeight: function()
    {
        var max = 0, currentheight;
        
        for(var i = 1; i <= this.total; i++)
        {
            currentheight = this.$slideDivs.eq(i).height();
            
            if(currentheight > max)
            {
                max = currentheight;
            }
        }
        
        return max;
    },
    
    hideAll: function()
    {
        this.$slideDivs.hide();
    },
    
    goToSlide: function(index)
    {
        this.clearTimer();
        this.hideAll();
        
        this.current = Number(index);
        this.$slideDivs.eq(index - 1).fadeIn(500);
        this.$gotoControls.removeClass("active");
        this.$gotoControls.eq(index - 1).addClass("active");
        
        if(this.playing)
        {
            this.setTimer();
        }
    },
    
    clearTimer: function()
    {
        window.clearTimeout(this.timer);
    },
    
    setTimer: function()
    {
        this.timer = window.setTimeout(function(slideshow, index)
        {
            return function() { slideshow.goToSlide(index); };
        }(this, this.next()), 7500);
    },
    
    play: function()
    {
        this.playing = true;
        this.goToSlide(this.current);
    },
    
    pause: function()
    {
        this.playing = false;
        this.clearTimer();
    },
    
    playPause: function()
    {
        if(this.playing)
        {
            this.pause();
            this.$playPause.addClass("active");
        }
        
        else
        {
            this.play();
            this.$playPause.removeClass("active");
        }
    }
};

