var cSlide_arrow_url;
var cSlide_dot_character;

var cSlide_global_array = new Array();

jQuery((document)).ready(function () {
    jQuery(".conveyor").each(function () {
        var this_cleverslide = new cleverSlide(this);
    });
});

function cleverSlide(obj) {
    var obj_ref = this;
    cSlide_global_array.push(this);
    this.attached_element = obj;
    this.parentWidth = jQuery((obj.parentNode)).width();
    this.slides = new Array();
    this.current_index = 0;
    this.previous_slide = null;

    jQuery((obj)).find(".slide").each(function () {
        obj_ref.slides.push(this);
        if (obj_ref.slides.length == 1) {
            Add_Slide_Style(this, true);
            obj_ref.previous_slide = this; //the previous slide is set to the first slide, as this is the one that is showing by default
        }
        else {
            Add_Slide_Style(this, false);
        }
        Add_cSlide_Navigation(this, obj_ref);
    });

    //this function handles overflows that are greater than the maximum allowed count, translating them into an array position
    this.Calculate_Overflow = function (proposed_index, max) {
        var return_value = 0;
        //alert("Calculate overflow called with proposed index " + proposed_index + " and max index " + max);
        for (var i = 0; i < proposed_index; i++) {
            if (return_value == (max)) {
                return_value = 0;
            }
            else {
                return_value++;
            }
        }
        //alert("Calculate overflow is going to return " + return_value);
        return return_value;
    }
    //This function manages a proposed index that is less than 0, translating it into the correct array position
    this.Calculate_Underflow = function (proposed_index, max) {
        var return_value = 0;
        var range_variable = 0;
        for (var i = 0; i > proposed_index; i -= 1) {
            if (i < 0) {
                range_variable -= 1;
            }
            var translated_index = max + range_variable;
            if (translated_index == -1) {
                range_variable = 0;
                translated_index = max + range_variable;
            }
            return_value = translated_index;
        }
        return return_value;
    }
    //Get_Index acts as a controller for the Get_Overflow and Get_Underflow functions, which translates a change in index to an array index
    this.Get_Index = function (proposed_index) {
        var max = obj_ref.slides.length - 1;
        if (proposed_index > 0) {
            return obj_ref.Calculate_Overflow(proposed_index, max);
        }
        if (proposed_index < 0) {
            return obj_ref.Calculate_Underflow(proposed_index, max);
        }
        return proposed_index;
    }
    //Change Index is the characteristic function of the script: it will accept an index and translate this index into a "current" and "previous" slide
    this.Change_Index = function (index, direction) {
        obj_ref.current_index = obj_ref.Get_Index(index);
        var current_index = obj_ref.current_index;
        var current_slide = obj_ref.slides[current_index]//extracts the current slide;
        var previous_index = obj_ref.Get_Index(current_index - 1);
        var previous_slide = obj_ref.previous_slide;
        if (previous_slide == null) {
            previous_slide = obj_ref.slides[0];
            previous_index = 0;
        }

        var width = obj_ref.parentWidth;


        jQuery((previous_slide)).css("left", "0px");
        jQuery((previous_slide)).animate({ left: (width * -direction)}, 1000, 'linear', null);

        jQuery((current_slide)).css("left", (width * direction) + "px");
        jQuery((current_slide)).animate({ left: "0" }, 1000, 'linear', null);

        obj_ref.previous_slide = current_slide;
    }
    this.Slide_Forward = function () {
        var index = obj_ref.current_index + 1;
        obj_ref.Change_Index(index, 1);
    }
    this.Slide_Backward = function () {
        var index = obj_ref.current_index - 1;
        obj_ref.Change_Index(index, -1);
    }
}

function Add_Slide_Style(obj, isfirst) {
    obj.style.position = "absolute";
    obj.parentNode.style.overflow = "hidden";
    if (isfirst) {
        obj.style.left = "0px";
        obj.style.top = "0px";
    } else {
        obj.style.left = jQuery((obj.parentNode)).width() + "px";
        obj.style.top = "0px";
    }
    obj.style.width = "100%";
    obj.style.height = "100%";
    if (obj.parentNode.style.position != "absolute") {
        obj.parentNode.style.position = "relative";
    }
    if ((obj.parentNode.style.width == "" || obj.parentNode.style.width == null) || (obj.parentNode.style.height == "" || obj.parentNode.style.height == null)) {
        obj.parentNode.style.width = "100%";
        obj.parentNode.style.height = "100%";
    }
}

//accepts a dom element and returns the cSlide that uses this dom element

function Add_cSlide_Navigation(slide_dom_element, cSlide) {
    var conveyor = cSlide.attached_element;
    jQuery((slide_dom_element)).find(".slideLeftArrow").each(function () {
        this.onclick = function () {
            cSlide.Slide_Backward();
        }
    });
    jQuery((slide_dom_element)).find(".slideRightArrow").each(function () {
        this.onclick = function () {
            cSlide.Slide_Forward();
        }
    });
     jQuery((slide_dom_element)).find("[class*=slideNav]").each(function () {
         this.onclick = function () {
             var to_index = 0;
             var classes = this.className.split(" ");
             for (var i = 0; i < classes.length; i++) {
                 if (classes[i].indexOf("slideNav") == 0) {
                     to_index = parseInt(classes[i].replace("slideNav", ""));
                 }
             }
             cSlide.Change_Index(to_index, 1);
         }
     });
}
