// 
// Copyright 2011 Cindee Gardner
// 

jQuery.fn.cindeegardnerSlideshow = function(images, options) {
    
    // 
    // jQuery slideshow plug-in
    // 
    
    var timer;
    var containerElement = $("#" + options["elementID"]);
    var controlsElement;
    var indexElement;
    var prevElement;
    var nextElement;
    var imagesElement;
    var imageIndex;
    var showImage = function(index) {
        if (index >= images.length) {
            index = 0;
        } else if (index < 0) {
            index = images.length - 1;
        }
        imagesElement.children("a").each(function() {
            if ($(this).is(":visible")) {
                $(this).fadeOut();
            }
        });
        imagesElement.children("a").eq(index).fadeIn();
        indexElement.children("a").removeClass("selected");
        indexElement.children("a").eq(index).addClass("selected");
        imageIndex = index;
        if (options["autoInterval"] > 0) {
            clearTimeout(timer);
            timer = setTimeout("$(\"#" + options["elementID"] + "-next\").click()", options["autoInterval"] * 1000);
        }
    }
    
    if (typeof options == "undefined" || typeof images == "undefined" || images.length < 1 || containerElement.attr("id") == null) {
        return;
    }
    
    containerElement.css({
        "display": "block",
        "width": options["width"] + "px"
    });
    
    containerElement.append("<span id=\"" + options["elementID"] + "-controls\"></span>");
    controlsElement = $("#" + options["elementID"] + "-controls");
    controlsElement.css({
        "text-align": "center",
        "display": "block",
        "padding": "6px"
    });
    if (! options["showControls"]) {
        controlsElement.hide();
    }
    
    controlsElement.append("<a id=\"" + options["elementID"] + "-prev\" href=\"#\">previous</a>");
    prevElement = $("#" + options["elementID"] + "-prev");
    prevElement.css({
        "float": "left",
        "display": "none"
    }).click(function() {
        showImage(imageIndex - 1);
        return false;
    });
    
    controlsElement.append("<a id=\"" + options["elementID"] + "-next\" href=\"#\">next</a>");
    nextElement = $("#" + options["elementID"] + "-next");
    nextElement.css({
        "float": "right",
        "display": "none"
    }).click(function() {
        showImage(imageIndex + 1);
        return false;
    });
    
    controlsElement.append("<span id=\"" + options["elementID"] + "-index\"></span>");
    indexElement = $("#" + options["elementID"] + "-index");
    for (var i = 0; i < images.length; i++) {
        indexElement.append("<a href=\"#" + i + "\">&bull;</a>");
    }
    indexElement.children("a").css({
        "padding": "0 4px"
    }).click(function() {
        showImage(parseInt($(this).attr("href").substr(1)));
        return false;
    });
    containerElement.prepend("<span id=\"" + options["elementID"] + "-images\"></span>");
    imagesElement = $("#" + options["elementID"] + "-images");
    imagesElement.css({
        "display": "block",
        "width": options["width"] + "px",
        "height": options["height"] + "px",
        "overflow": "hidden",
        "position": "relative"
    });
    var image;
    for (i = 0; i < images.length; i++) {
        imagesElement.append("<a href=\"" + images.image(i)[1] + "\"><img src=\"" + images.image(i)[0] + "\"></a>");
    }
    imagesElement.children("a").css({
        "position": "absolute",
        "top": "0px",
        "left": "0px",
        "display": "none"
    });
    
    showImage(0);
}

var cindeegardnerSlideshowImages = function(images) {
    
    // 
    // Images array for slideshow plug-in 
    // 
    
    this.images = [];
    this.length = 0;
    this.addImage = function(image) {
        this.images.push(image);
        this.length++;
    }
    this.image = function(index) {
        return this.images[index];
    }
    
    if (typeof images != "undefined" && images.length > 0) {
        this.images = images;
        this.length = images.length;
    }
}

var cindeegardnerSlideshowOptions = function(options) {
    
    // 
    // Options array for slideshow plug-in
    // 
    
    this.elementID;
    this.width = 400;
    this.height = 300;
    this.showControls = true;
    this.autoInterval = 0;
    
    if (typeof options != "undefined") {
        this.elementID = options["elementID"];
        this.width = options["width"];
        this.height = options["height"];
        this.showControls = options["showControls"];
        this.autoInterval = options["autoInterval"];
    } 
}
