﻿
function sldshow() {
    this.isSliding = false;
    this.hndrttr = null;
    this.slides = [];
    this.init = function(slideSelector) {
        var arr = jQuery(this.rttrParent).find(slideSelector);
        // this reference changes within the each function so a temp array is used before assigning to the object
        var temp = [];
        var imgs = jQuery('img.slide-image');
        jQuery.each(arr, function(index, element) {
            Array.add(temp, element);
        });
        this.slides = temp;

        //rttr needs total width for divs to display inline
        this.hndrttr = jQuery(this.rttrParent).find('div.rttr');
        this.hndrttr.css('width', this.rttrParent.width() * this.slides.length);

        //slide are full width of the parent
        this.hndrttr.children('div').css('width', this.rttrParent.width());

        instance = this;

        var divthumbs = [];
        imgs.each(function(i, elm) {
            var pos = eval(elm.getAttribute("pos"));
            if (pos != undefined) {
                var thumb = document.createElement('div');
                thumb.setAttribute("style", String.format("background:url(\"{0}\") no-repeat {1}px {2}px;", elm.src, pos[0], pos[1]));
                thumb.setAttribute("class", "thumb-image");
                Array.add(divthumbs, thumb);
            } else {
                var image = document.createElement('img');
                image.setAttribute("src", elm.src);
                image.setAttribute("class", "thumb-image");
                Array.add(divthumbs, image);
            }
        });
        jQuery(this.rttrThumbs).append(divthumbs);

        //jQuery(this.rttrThumbs).append(imgs[i].clone(true).removeClass('slide-image').addClass('thumb-image').attr('style', ''));
        jQuery(this.rttrThumbs).click(function(ev) {
            if (instance.isSliding) {
                return;
            }

            var p = 0;
            jQuery(this).children().each(function(i, elem) {
                if (ev.target == elem) {
                    p = i + 1;
                }
            });

            instance.start = p;
            instance.slide();
            window.clearInterval(instance.timer);
            instance.timer = window.setInterval(autoSlide, 20 * 1000);
        });
    }

    this.coercestartvalue = function() {
        if (this.start < 0) {
            this.start = 0; //this.slides.length - 1;
        }
        if (this.start >= this.slides.length) {
            this.start = 0;
        }
    }

    this.slideRight = function() {
        if (this.isSliding) {
            return;
        }
        this.start++;
        this.coercestartvalue();
        //reset to start
        this.slide();
        this.setIndicator();
    }
    this.slideLeft = function() {
        if (this.isSliding) {
            return;
        }
        this.start--;
        this.coercestartvalue();
        this.slide();
        this.setIndicator();
    }
    this.slide = function(posleft) {
        var posleft = this.start * this.rttrParent.width();
        var offleft = this.hndrttr.offset().left;

        if (this.hndrttr) {
            var op = null;
            var diff = Math.abs(this.hndrttr.position().left) - posleft;
            if (diff < 0) {
                op = '-=';
            } else {
                op = '+=';
            }

            var instance = this;
            instance.isSliding = true;
            this.hndrttr.animate(
            {
                left: op + Math.abs(diff)
            },
             500,
             'linear',
             function() {
                 //set height to slide content height
                 var ch = instance.hndrttr.height();
                 instance.hndrttr.css('height', '100%');
                 var sh = jQuery(instance.slides[instance.start]).height();
                 instance.hndrttr.css('height', ch);
                 var d = sh - ch;
                 var op = '';
                 if (d < 0) {
                     op = '-=';
                 } else {
                     op = '+=';
                 }
                 instance.hndrttr.animate({ height: op + Math.abs(d) }, 200, 'linear', function() { instance.isSliding = false; });
             });

        }
    }
    this.showslide = function() {
        this.coercestartvalue();
        this.slide();
    }
    this.startAt = function(pos) {
        this.start = pos;
        this.coercestartvalue();
        this.slide();
    }

}

var KVP = function(k, v) {
    this.k = k;
    this.v = v;
}
function autoSlide() {
    r.start++;
    r.coercestartvalue();
    r.slide();
}


function selectSlide() {
    var url = document.location.href;
    var h = url.indexOf("#");
    var prmStr = '';
    if (h > -1) {
        prmStr = url.substr(h + 1);
    }

    if (prmStr.length > 0) {
        var kvpArr = prmStr.split(';');
        var params = [];
        for (var i = 0; i < kvpArr.length; i++) {
            var kvp = kvpArr[i].split('=');
            Array.add(params, new KVP(kvp[0], kvp[1]));
        }

        var val = null;
        for (var i = 0; i < params.length; i++) {
            if (params[i].k == 'slide') {
                val = params[i].v;
                break;
            }
        }

        if (val != null) {
            r.start = val;
            r.slide();
        }
    }
}

window.onhashchange = selectSlide;
