﻿//token				a token identifier instead of index
//title				a string
//description		a string
//image				a fully qualified path						(either "http://" or "/")
//link				a fully qualified path						(either "http://" or "/")
//transition		"fade" || "flash" || "slide"				(optional : "fade")
var Slide = function (token, title, description, image, link, transition, navlink, navlinkText) { return this._init(token, title, description, image, link, transition, navlink, navlinkText); }
Slide.prototype = {
    _init: function (token, title, description, image, link, transition, navlink, navlinkText) {
        this.token = token;
        this.title = title;
        this.description = description;
        this.image = image;
        this.link = link.toLowerCase();
        this.transition = transition;

        // sort of a hack but must parse out IndustryLanding
        if (navlink.indexOf("/IndustryLanding") != -1) {
            navlink = navlink.replace("/IndustryLanding", "")
        }

        this.navlinkText = navlinkText;
        this.navlink = navlink.toLowerCase();              

        return this;
    }
}

//target			a qualified jquery selector					(parent container)
//interval			length in milliseconds for slide to show	(optional : 8000)
//transduration		length in milliseconds of transition		(optional : 2000)
//direction			which way to iterate						(optional : "<" or ">" : ">")
var Slideshow = function (target, interval, transDuration, direction) {var me;return  this._init(target, interval, transDuration, direction);}
Slideshow.prototype = {
    _init: function (target, interval, transDuration, direction) {
        me = this;
        if (target === undefined) { return false; };
        if (interval === undefined) { interval = 8000; };
        if (transDuration === undefined) { transDuration = 2000; };
        if ((direction === undefined) || (direction != "<")) { direction = ">" };

        this.target = target;
        this.t = new Timer();
        this.slides = []; //array of slides
        this.currentIndex = 0;
        this.target = target;
        this.interval = interval;
        this.transDuration = transDuration;
        this.direction = direction;

        if (this.direction == "<") { this.t = new Timer(interval, this.previousSlide); }
        else { this.t = new Timer(interval, this.nextSlide); }

        //adds a click event for the nav items
        $(target + " .slideNav li").live("click", function () { me.start($(this).index()); });

        return this;
    },

    //slide			slide object
    //index			integer	to insert slide						(optional : appends to end)
    addSlide: function (newSlide, index) {
        var hasIndex = false;
        if (newSlide === undefined) { return false; };
        if (index === undefined) { index = this.slides.length; } else { hasIndex = true; };
        this.slides.splice(index, 0, newSlide);
       
        if (hasIndex) { $(this.target + " .slideNav").eq(index).insertBefore("<li>" + newSlide.title + "</li>"); }
        //else { $(this.target + " .slideNav").append("<li>" + "</li>"); }        
        else { $(this.target + " .slideNav").append("<li>" + newSlide.title + "</li>"); }
    },

    //newSlide		slide object
    //index			index of slide to replace
    //TODO: Allow token instead of index (find index by token)
    replaceSlide: function (newSlide, index) {
        if ((newSlide === undefined) || (index === undefined)) { return false; };
        this.slides.splice(index, 1, newSlide);
        $(target + " .slideNav li").eq(index).html(newSlide.title);
    },

    //index			index of slide to remove
    //TODO: Allow token instead of index (find index by token)
    removeSlide: function (index) {
        if (index === undefined) { return false; };
        this.slides.splice(index, 1);
        $(target + " .slideNav li").eq(index).remove();
    },

    //index			index of slide to start on					(optional : current index)
    //direction		which way to interate						(optional "<" or ">" : current direction)				
    //TODO: Allow token instead of index (find index by token)
    start: function (index, direction) {
        if (index === undefined) { index = this.currentIndex; };
        if (direction === undefined) { } else { this.direction = direction; };
        this.currentIndex = index;
        this.setSlide();
        this.t.restart();
    },

    //stop slideshow on current slide
    stop: function () { this.t.stop(); },

    //moves to next slide
    nextSlide: function () {
        if ((me.currentIndex + 1) == me.slides.length) { me.currentIndex = 0; }
        else { me.currentIndex++; }
        me.setSlide();
    },

    //moves to previous slide
    previousSlide: function () {
        if (me.currentIndex == 0) { me.currentIndex = me.slides.length - 1; }
        else { me.currentIndex--; }
        me.setSlide();
    },

    //shows slide at current index
    //transitions using the slides defined transition
    setSlide: function () {
        //copies all new slide info to the old slide
        $(this.target + " .old .title").html($(this.target + " .new .title").html());
        $(this.target + " .old .description").html($(this.target + " .new .description").html());
        $(this.target + " .old .image").attr("src", $(this.target + " .new .image").attr("src"));
        $(this.target + " .old .link").attr("href", $(this.target + " .new .link").attr("href"));
        $(this.target + " .old .navlink").attr("href", $(this.target + " .new .navlink").attr("href"));
        $(this.target + " .new").hide();

        //inserts all the new slide info to the new slide
        $(this.target + " .new .title").html(this.slides[this.currentIndex].title);
        $(this.target + " .new .description").html(this.slides[this.currentIndex].description);
        $(this.target + " .new .image").attr("src", this.slides[this.currentIndex].image);
        $(this.target + " .new .link").attr("href", this.slides[this.currentIndex].link.toLowerCase());
        $(this.target + " .new .navlink").attr("href", this.slides[this.currentIndex].navlink.toLowerCase());
        $(this.target + " .new .navlink").html(this.slides[this.currentIndex].navlinkText);
        

        //uses defined transition
        if (this.slides[this.currentIndex].transition == "fade") { $(this.target + " .new").fadeIn(this.transDuration); }
        else if (this.slides[this.currentIndex].transition == "flash") { }
        else if (this.slides[this.currentIndex].transition == "slide") { }
        else { }

        //adds a "selected" class to the associated nav item
        $(this.target + " .slideNav li").removeClass("selected");
        $(this.target + " .slideNav li").eq(this.currentIndex).addClass("selected");
    }
}
