﻿jQuery.noConflict();

//slide show object
Type.registerNamespace("Tag");

Tag.Rotator = function(slideSelector, rotatorSelector, pos, createPager, createIndicator) {
    this.hndRotator = null;
    this.slides = [];
    this.start = pos;
    this.coercestartvalue();
    this.rotatorParent = jQuery(rotatorSelector);
    if (slideSelector == null) {
        slideSelector = 'div.rotator > div';
    }
    this.isSliding = false;
    this.makePager = createPager;
    this.makeIndicator = createIndicator;
    this.init(slideSelector);
    this.callback = null;
    this.slideBegin = null;
}

Tag.Rotator.prototype = {
    init: function(slideSelector) {
        var arr = jQuery(this.rotatorParent).find(slideSelector);
        // this reference changes within the each function so a temp array is used before assigning to the object
        var temp = [];
        jQuery.each(arr, function(index, element) {
            jQuery(element).attr('n', index);
            Array.add(temp, element);
        });
        this.slides = temp;

        //rotator needs total width for divs to display inline
        this.hndRotator = jQuery(this.rotatorParent).find('div.rotator');
        this.hndRotator.css('width', this.rotatorParent.width() * this.slides.length);

        //slide are full width of the parent
        this.hndRotator.children('div').css('width', this.rotatorParent.width());

        if (this.makePager) {
            var pager = document.createElement("div");
            pager.setAttribute('class', 'rotator-control');

            var link = document.createElement("a");
            link.setAttribute('class', 'rotator-left');
            pager.appendChild(link);

            link = document.createElement("a");
            link.setAttribute('class', 'rotator-right');
            pager.appendChild(link);
            this.rotatorParent.append(pager);

            var instance = this;
            this.rotatorParent.find('a.rotator-left').click(function(ev) {
                instance.slideLeft();
                ev.stopPropagation();
            });

            this.rotatorParent.find('a.rotator-right').click(function(ev) {
                instance.slideRight();
                ev.stopPropagation();
            });

            this.rotatorParent.mouseover(function(ev) {
                jQuery(this).children('div.rotator-control').css('visibility', 'visible');
                ev.stopPropagation();
            });
            this.rotatorParent.mouseout(function(ev) {
                jQuery(this).children('div.rotator-control').css('visibility', 'hidden');
            });
        }

        if (this.makeIndicator) {
            var ul = document.createElement("ul");
            ul.setAttribute('class', 'rotator-indicator');
            for (var i = 0; i < this.slides.length; i++) {
                var li = document.createElement("li");
                li.setAttribute('class', 'rotator-indicator-item');
                ul.appendChild(li);
            }
            this.rotatorParent.append(ul);
        }
        this.selectIndicator();
        this.setIndicator(0);
    },
    coercestartvalue: function() {
        if (this.start < 0) {
            this.start = 0; //this.slides.length - 1;
        }
        if (this.start >= this.slides.length) {
            this.start = 0;
        }
    },
    slideRightCoerced: function() {
        if (this.isSliding) {
            return;
        }
        this.start++;
        this.coercestartvalue();
        //reset to start
        this.slide();
        this.selectIndicator();
    },
    slideLeftCoerced: function() {
        if (this.isSliding) {
            return;
        }
        this.start--;
        this.coercestartvalue();
        this.slide();
        this.selectIndicator();
    },
    slide: function(posleft) {
        var posleft = this.start * this.rotatorParent.width();

        if (this.hndRotator) {
            var op = null;
            var diff = Math.abs(this.hndRotator.position().left) - posleft;
            if (diff < 0) {
                op = '-=';
            } else {
                op = '+=';
            }

            var instance = this;
            instance.isSliding = true;
            this.hndRotator.animate(
            {
                left: op + Math.abs(diff)
            },
             500,
             'linear',
             function() {
                 instance.isSliding = false;
             });

        }
    },
    selectIndicator: function() {
        var items = this.rotatorParent.find('li.rotator-indicator-item');
        jQuery(items).removeClass('current-indicator-item');
        jQuery(items[this.start]).addClass('current-indicator-item');
    },
    showslide: function() {
        this.coercestartvalue();
        this.slide();
    },
    startAt: function(pos) {
        this.start = pos;
        this.coercestartvalue();
        this.slide();
    },
    dispose: function() {
    }
    , slideRight: function() {
        if (this.slides.length < 2) {
            return;
        }
        if (this.isSliding) {
            return;
        }
        this.onSlideBegin();
        var instance = this;
        instance.isSliding = true;
        var prev = jQuery('div.rotator > div:first-child');
        prev.animate({
            marginLeft: '-=' + this.rotatorParent.width()
        },
        500,
        'linear',
        function() {
            instance.hndRotator.append(prev.detach().css('margin-left', 0));
            var slideno = jQuery('div.rotator > div:first-child').attr('n');
            instance.setIndicator(slideno);
            instance.onSlideChange(slideno);
            instance.isSliding = false;
        });
    }
    , slideLeft: function() {
        if (this.slides.length < 2) {
            return;
        }
        if (this.isSliding) {
            return;
        }
        this.onSlideBegin();
        var instance = this;
        instance.isSliding = true;
        var last = jQuery('div.rotator > div:last-child');
        instance.hndRotator.prepend(last.detach().css('margin-left', 0 - this.rotatorParent.width()));
        last.animate({
            marginLeft: '+=' + this.rotatorParent.width()
        },
        500,
        'linear',
        function() {
            var slideno = last.attr('n');
            instance.setIndicator(slideno);
            instance.onSlideChange(slideno);
            instance.isSliding = false;
        });
    },
    setIndicator: function(position) {
        var items = this.rotatorParent.find('li.rotator-indicator-item');
        jQuery(items).removeClass('current-indicator-item');
        jQuery(items[position]).addClass('current-indicator-item');
    },

    onSlideChange: function(slideNo) {
        var handler = this.callback;
        if (handler) {
            handler(this, slideNo);
        }
    },

    onSlideBegin: function(slideNo) {
        var handler = this.slideBegin;
        if (handler) {
            handler(this);
        }
    }

}
Tag.Rotator.registerClass('Tag.Rotator', null, Sys.IDisposable);

// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();





