﻿(function($) {
    $.fn.SlideAd = function(options) {
        var defaults = {
            Speed: 5000,
            Easing: 'linear',
            SlideDirection: 'left',
            ContainerHeight: 0,
            FirstCycle: true
        };
        options = $.extend(defaults, options);

        this.each(function() {
            var anim;
            var cssReset;
            var cssResetVal;
            switch (options.SlideDirection) {
                case 'left':
                    var totalWidth = 0;
                    $(this).children('.adItem').each(function() {
                        totalWidth += $(this).outerWidth(true);
                    });
                    var parentWidth = $(this).parent().width();
                    var anLeft = -1 * totalWidth;
                    anim = { left: anLeft + 'px' };
                    cssReset = 'left';
                    cssResetVal = parentWidth + 'px';
                    break;
                case 'right':
                    var totalWidth = 0;
                    $(this).children('.adItem').each(function() { totalWidth += $(this).outerWidth(true); });
                    var parentWidth = $(this).parent().width();
                    var anVal = -1 * totalWidth;
                    cssReset = 'right';
                    cssResetVal = parentWidth + 'px';
                    // handle the IE 6/7
                    var vrs = $.browser.version.substr(0, 1);
                    if ($.browser.msie && (vrs == '6' || vrs == '7')) {
                        anVal = -1 * parentWidth;
                        cssResetVal = (totalWidth) + 'px';
                        if (options.FirstCycle) $(this).css('right', (totalWidth - parentWidth) + 'px');
                    }
                    anim = { right: anVal + 'px' };
                    $(this).css('visibility', 'visible');
                    break;
                case 'up':
                    var totalHeight = 0;
                    $(this).children('.adItem').each(function() { totalHeight += $(this).outerHeight(true); });
                    var parentHeight = $(this).parent().height();
                    var anTop = -1 * totalHeight;
                    anim = { top: anTop + 'px' };
                    cssReset = 'top';
                    cssResetVal = parentHeight + 'px';
                    break;
                case 'down':
                    var totalHeight = 0;
                    $(this).children('.adItem').each(function() { totalHeight += $(this).outerHeight(true); });
                    var parentHeight = $(this).parent().height();
                    var anBot = -1 * totalHeight;
                    anim = { bottom: anBot + 'px' };
                    cssReset = 'bottom';
                    cssResetVal = parentHeight + 'px';
                    break;
            }
            
            $(this).animate(anim, options.Speed, options.Easing, function() {
                $(this).css(cssReset, cssResetVal);
                $(this).SlideAd({ Speed: options.Speed, Easing: options.Easing, SlideDirection: options.SlideDirection, FirstCycle: false });
            });
        });
    }
})(jQuery);

if (typeof (Sys) != "undefined" && typeof (Sys.Application) != "undefined") {
    Sys.Application.notifyScriptLoaded();
}
