﻿//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 (o) {return this._init(o);}
Slide.prototype = {
    _init: function (o) {

        //token, title, description, image, link, transition
        if (o.token === undefined) { o.token = ""; };
        if (o.title === undefined) { o.title = ""; };
        if (o.description === undefined) { o.description = ""; };
        if (o.image === undefined) { o.image = ""; };
        if (o.link === undefined) { o.link = ""; };
        if (o.linktext === undefined) { o.linktext = ""; };
        if (o.transition === undefined) { o.transition = ""; };

        this.token = o.token;
        this.title = o.title;
        this.description = o.description;
        this.image = o.image;
        this.link = o.link;
        this.linktext = o.linktext;
        this.transition = o.transition;


        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 (o) {

    var me = this;

    if (o.target === undefined) { return false; };
    if (o.interval === undefined) { o.interval = 8000; };
    if (o.transDuration === undefined) { o.transDuration = 2000; };
    if ((o.direction === undefined) || (o.direction != "<")) { o.direction = ">" };

    if (o.clicktoduration === undefined) { o.clicktoduration = o.transDuration; };
    if (o.thumbnails === undefined) { o.thumbnails = false; };
    if (o.thumbnailtext === undefined) { o.thumbnailtext = true; };
    if (o.thumbnailcount === undefined) { o.thumbnailcount = -1; };
    if (o.thumbnailwidth === undefined) { o.thumbnailwidth = -1; };
    if (o.asbackground === undefined) { o.asbackground = false; };
    if (o.navigationtarget === undefined) { o.navigationtarget = o.target; };
    if (o.uselinktext === undefined) { o.uselinktext = false; };
    if (o.navstyle === undefined) { o.navstyle = "default"; };
    if (o.navclass === undefined) { o.navclass = ""; };
    if (o.navimage === undefined) { o.navimage = ""; };

    //this.target = target;
    this.t = new Timer();
    this.slides = []; //array of slides
    this.currentIndex = 0;
    this.target = o.target;
    this.interval = o.interval;
    this.transDuration = o.transDuration;
    this.direction = o.direction;
    this.clicktoduration = o.clicktoduration;
    this.thumbnails = o.thumbnails;
    this.thumbnailtext = o.thumbnailtext;
    this.thumbnailcount = o.thumbnailcount;
    this.thumbnailwidth = o.thumbnailwidth;
    this.navigationtarget = o.navigationtarget;
    this.uselinktext = o.uselinktext;
    this.asbackground = o.asbackground;
    this.navstyle = o.navstyle;
    this.navclass = o.navclass;
    this.navimage = o.navimage;

    this.firstrun = true;
    this.thumbmargin = 0;

    //slide			slide object
    //index			integer	to insert slide						(optional : appends to end)
    this.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);
        var title = newSlide.title
        if (this.thumbnailtext == false) { title = ""; };

        if (hasIndex) { $(this.target + " .slideNav").eq(index).insertBefore("<li>" + title + "</li>"); }
        else { $(this.target + " .slideNav").append("<li>" + title + "</li>"); }

        if (this.thumbnails == true) {
            $(this.target + " .slideNav li").eq(index).html('<img src="' + this.slides[index].image + '" alt="' + title + '"/>');

        }

    }

    //newSlide		slide object
    //index			index of slide to replace
    //TODO: Allow token instead of index (find index by token)
    this.replaceSlide = function (newSlide, index) {
        if ((newSlide === undefined) || (index === undefined)) { return false; };
        this.slides.splice(index, 1, newSlide);
        $(this.target + " .slideNav li").eq(index).html(newSlide.title);
    }

    //index			index of slide to remove
    //TODO: Allow token instead of index (find index by token)
    this.removeSlide = function (index) {
        if (index === undefined) { return false; };
        this.slides.splice(index, 1);
        $(this.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)
    this.start = function (index, direction) {
        if (index === undefined) { index = this.currentIndex; };
        if (direction === undefined) { } else { this.direction = direction; };
        this.currentIndex = index;
        this.setSlide(this.clicktoduration);
        this.t.restart();
    }

    //stop slideshow on current slide
    this.stop = function () { this.t.stop(); }

    //moves to next slide
    this.nextSlide = function () {
        if ((me.currentIndex + 1) == me.slides.length) { me.currentIndex = 0; }
        else { me.currentIndex++; }
        me.setSlide(this.clicktoduration);
    }

    //moves to previous slide
    this.previousSlide = function () {
        if (me.currentIndex == 0) { me.currentIndex = me.slides.length - 1; }
        else { me.currentIndex--; }
        me.setSlide(this.clicktoduration);
    }

    //shows slide at current index
    //transitions using the slides defined transition
    this.setSlide = function (duration) {

        var trans = this.slides[this.currentIndex].transition;
        if (duration == undefined) {
            duration = this.transDuration;
        }

        if (this.firstrun == true) {
            duration = 10;
            trans = "show"
            $(this.target + " .old .image").removeClass("preload");
            $(this.target + " .new .image").removeClass("preload");
            this.firstrun = false;

            if (this.asbackground == true) {
                $(this.target + " .new").css("background-image", "url(" + this.slides[this.currentIndex].image + ")");
            }
        }

        //copies all new slide info to the old slide
        $(this.navigationtarget + " .old .title").html($(this.target + " .new .title").html());
        $(this.navigationtarget + " .old .description").html($(this.target + " .new .description").html());

        if (this.asbackground == true) {
            $(this.target + " .old").css("background-image", $(this.target + " .new").css("background-image"));

        } else {
            $(this.target + " .old .image").attr("src", $(this.target + " .new .image").attr("src"));
        }

        $(this.navigationtarget + " .old .link").attr("href", $(this.target + " .new .link").attr("href"));
        if (this.uselinktext == true) {
            $(this.navigationtarget + " .old .link").html($(this.target + " .new .link").html());
        }



        $(this.target + " .new").hide();

        //inserts all the new slide info to the new slide
        $(this.navigationtarget + " .new .title").html(this.slides[this.currentIndex].title);
        $(this.navigationtarget + " .new .description").html(this.slides[this.currentIndex].description);


        if (this.asbackground == true) {
            $(this.target + " .new").css("background-image", "url(" + this.slides[this.currentIndex].image + ")");
        } else {
            $(this.target + " .new .image").attr("src", this.slides[this.currentIndex].image);
        }


        if (this.slides[this.currentIndex].link.length > 0) {
            $(this.target + " .new .link").attr("href", this.slides[this.currentIndex].link);
        } else {
            $(this.target + " .new .link").removeAttr("href");
        }

        if (this.uselinktext == true) {
            $(this.target + " .new .link").html(this.slides[this.currentIndex].linktext);
        }

        //uses defined transition

        switch (this.slides[this.currentIndex].transition) {
            case "fade":
                $(this.target + " .new").fadeIn(duration);
                break;

            case "show":
                $(this.target + " .new").show(duration);
                break;

            case "slidedown":
                $(this.target + " .new").slideDown(duration);
                break;

            default:
                $(this.target + " .new").fadeIn(duration);
        }

        //adds a "selected" class to the associated nav item
        //shift nav based on index
        var m = (parseInt(this.currentIndex / this.thumbnailcount) * this.thumbnailcount) * this.thumbnailwidth;

        //width of the remainder space
        var p = parseInt(this.slides.length - this.thumbnailcount) * this.thumbnailwidth;

        //width of the overall space
        if (m > p) { m = p; }

        $(this.navigationtarget + " .slideNav").css("margin-left", "-" + m + "px");

        $(this.navigationtarget + " .slideNav li").removeClass("selected");
        $(this.navigationtarget + " .slideNav li").eq(this.currentIndex).addClass("selected");



    }



    //INIT


    if (this.direction == "<") { this.t = new Timer(o.interval, this.previousSlide); }
    else { this.t = new Timer(o.interval, this.nextSlide); }

    //adds a click event for the nav items
    $(this.target + " .slideNav li").live("click", function () { me.start($(this).index()); });
    $(this.target + " .slidecontrols .slideprevious").live("click", function () { me.previousSlide(); });
    $(this.target + " .slidecontrols .slidenext").live("click", function () { me.nextSlide(); });
}

