newSlideshow = function(class_name,duration,speed,instance_name) {
    //Create new instance of slideshow
    window[instance_name] = new objSlideshow(class_name,duration,speed,instance_name);
    window[instance_name].buildNav();
    window[instance_name].play();
}

objSlideshow = function(class_name,duration,speed,instance_name) {
    //Initialize variables
    this.obj_name = instance_name;
    this.interval_id = instance_name + '_iid';
    this.currentSlide = 0;
    this.status = 'paused';
    this.slideDuration = duration;
    this.transitionSpeed = speed;
    this.slides = $$('div.' + class_name);

    this.buildNav = function()
    {
        this.navCode  = '<div id="' + instance_name + 'Nav" class="slideshowNav">\n';
        this.navCode += '	<div class="slides">\n';
        //Setup Slides
        for (i=0;i<this.slides.length;i++) {
            if(i == 0) {
                this.navCode += '		<a id="' + instance_name + 'Nav_0" href="javascript:' + instance_name + '.goto(' + i + ');" class="active">1</a>\n';
                this.slides[i].style.display = 'block';
                this.slides[i].style.zIndex = '500';
                this.slides[i].style.opacity = '1.0';
            }
            else if(i == 1) {
                this.navCode += '		<a id="' + instance_name + 'Nav_1" href="javascript:' + instance_name + '.goto(' + i + ');" class="inactive">2</a>\n';
                this.slides[i].style.display = 'block';
                this.slides[i].style.zIndex = '450';
                this.slides[i].style.opacity = '1.0';
            }
            else {
                this.navCode += '		<a id="' + instance_name + 'Nav_' + i + '" href="javascript:' + instance_name + '.goto(' + i + ');" class="inactive">' + (i+1) + '</a>\n';
                this.slides[i].style.display = 'block';
                this.slides[i].style.zIndex = '400';
                this.slides[i].style.opacity = '1.0';
            }
        }
        this.navCode += '	</div>\n';
        this.navCode += '	<div class="btns">\n';
        this.navCode += '		<a class="prev" href="javascript:' + instance_name + '.prev();"><img src="media/mktg/home/slideNav-btn-prev.png" width="16" height="12" /></a>\n';
        this.navCode += '		<a id="' + instance_name + '-playpause-link" class="playpause" href="javascript:' + instance_name + '.play();"><img id="' + instance_name + '-playpause-img" src="media/mktg/home/slideNav-btn-play.png" width="16" height="12" /></a>\n';
        this.navCode += '		<a class="next" href="javascript:' + instance_name + '.next();"><img src="media/mktg/home/slideNav-btn-next.png" width="16" height="12" /></a>\n';
        this.navCode += '	</div>\n';
        this.navCode += '</div>\n';
        $(this.obj_name).insert({
            'top':this.navCode
            });
    }

    this.initMove = function(action,gotoSlideNum) {
        switch(action) {
            case "prev"://Previous Slide
                this.slidePrev().style.zIndex = '475';
                break;
            case "goto"://Goto Specific Slide
                this.slides[gotoSlideNum].style.zIndex = '475';
                break;
            default://Next Slide
                break;
        }
        var slideshowObj = this;
        var gotoSlideNumber = gotoSlideNum;
        $(this.slides[this.currentSlide].id).fade({
            duration: this.transitionSpeed,
            afterFinish: function(){
                slideshowObj.move(action,gotoSlideNumber)
                }
            });
    }

    this.move = function(action,gotoSlideNum) {
        switch(action)	{
            case "prev"://Previous Slide
                this.slidePrev().style.zIndex = '500';
                this.slideNow().style.zIndex = '450';
                this.slideNow().style.display = 'block';
                this.slideNow().style.opacity = '1.0';
                this.slideNext().style.zIndex = '400';
                this.slideNextCue().style.zIndex = '400';
                //Modify Active Nav Class to Inactive
                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'inactive';
                //Change Current Slide
                if(this.currentSlide == 0) {//You are on the first slide - start over
                    this.currentSlide = this.slides.length - 1;
                }
                else {//You are not on the first slide - prev slide
                    --this.currentSlide;
                }
                //Modify Active Nav Class to Inactive
                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'active';
                break;
            case "goto":
                this.slides[gotoSlideNum].style.zIndex = '500';
                this.slideNow().style.zIndex = '400';
                this.slideNow().style.display = 'block';
                this.slideNow().style.opacity = '1.0';
                if(gotoSlideNum == this.slides.length - 1)	{//You are on the last slide - start over
                    this.slides[0].style.zIndex = '450';
                }
                else {//You are not on the last slide - next slide
                    this.slides[gotoSlideNum + 1].style.zIndex = '450';
                }
                //Modify Active Nav Class to Inactive
                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'inactive';
                //Set Current Slide to Goto Slide
                this.currentSlide = gotoSlideNum;
                //Modify Active Nav Class to Inactive
                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'active';
                break;
            default://Next Slide
                this.slideNext().style.zIndex = '500';
                this.slideNow().style.zIndex = '400';
                this.slideNow().style.display = 'block';
                this.slideNow().style.opacity = '1.0';
                this.slideNextCue().style.zIndex = '450';
                //Modify Active Nav Class to Inactive

                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'inactive';
                //Change Current Slide
                if(this.currentSlide == this.slides.length - 1)	{//You are on the last slide - start over
                    this.currentSlide = 0;
                }
                else {//You are not on the last slide - next slide
                    ++this.currentSlide;
                }
                //Modify Active Nav Class to Inactive
                $(this.obj_name + 'Nav_' + this.currentSlide).className = 'active';
                break;
        }
    }

    this.slideNow = function() {
        return this.slides[this.currentSlide];
    }

    this.slideNext = function()	{
        //Check to see if you are on the last slide
        if(this.currentSlide == this.slides.length - 1){//You are on the last slide - start over
            return this.slides[0];
        }
        else{//You are not on the last slide - next slide
            return this.slides[this.currentSlide + 1];
        }
    }

    this.slideNextCue = function()	{
        //Check to see if you are on the last slide
        if(this.currentSlide == this.slides.length - 2){//You are on the second-to-last slide - start over
            return this.slides[0];
        }
        else if(this.currentSlide == this.slides.length - 1){//You are on the last slide - start over + 1
            return this.slides[1];
        }
        else{//You are not on the last slide - after next slide
            return this.slides[this.currentSlide + 2];
        }
    }

    this.slidePrev = function()	{
        //Check to see if you are on the first slide
        if(this.currentSlide == 0){//You are on the first slide - start over
            return this.slides[this.slides.length - 1];
        }
        else{//You are not on the first slide - prev slide
            return this.slides[this.currentSlide - 1];
        }
    }

    this.play = function() {
        this.interval_id = setInterval(this.obj_name + ".initMove('next')",this.slideDuration * 1000);
        this.status = "playing";
        $(this.obj_name + "-playpause-img").src = $(this.obj_name + "-playpause-img").src.replace("play","pause");
        $(this.obj_name + "-playpause-link").href = "javascript:" + this.obj_name + ".pause();";
    }

    this.pause = function()	{
        clearInterval(this.interval_id);
        this.status = "paused";
        $(this.obj_name + "-playpause-img").src = $(this.obj_name + "-playpause-img").src.replace("pause","play");
        $(this.obj_name + "-playpause-link").href = "javascript:" + this.obj_name + ".play();";
    }

    this.next = function() {
        clearInterval(this.interval_id);
        this.initMove('next');
        if(this.status == "playing") {
            this.interval_id = setInterval(this.obj_name + ".initMove('next')",this.slideDuration * 1000);
        }
    }

    this.prev = function() {
        clearInterval(this.interval_id);
        this.initMove('prev');
        if(this.status == "playing") {
            this.interval_id = setInterval(this.obj_name + ".initMove('next')",this.slideDuration * 1000);
        }
    }

    this.goto = function(slideNum) {
        clearInterval(this.interval_id);
        this.initMove('goto',slideNum);
        if(this.status == "playing") {
            this.interval_id = setInterval(this.obj_name + ".initMove('next')",this.slideDuration * 1000);
        }
    }
}
