﻿(function($){
    //
    // CONTINUOUS MARQUEE
    //
    $.fn.marquee = function(p){
        var defaults = {
            spacing: 5,
            scrollDelay: 1,
            scrollAmount: 1,
            direction: 'left'
        };
        defaults = $.extend(defaults, p);
        
        return this.each(function() {
            this.p = defaults;
			$($('#' + this.id)[0]).css('display', '');
			
            // reconfig width and left for imgs
            _spacing = this.p.spacing;
            _left = 0;
            if(this.p.direction == 'left'){
                _left = 0;
                $('#' + this.id + ' a').each(function() {
                    $(this).css('left', _left + 'px');
                    $(this).css('position', 'absolute');
					$(this).css('border', 'solid 2px rgb(106, 175, 231)');
	                $(this).css('border-width', '2px 1px');
                    _left = _left + $(this).width() + _spacing;
                });
            }
            else{
                _left = $(this).width();
                $('#' + this.id + ' a').each(function() {
                    _left = _left - $(this).width() - _spacing;
                    $(this).css('left', _left + 'px');
                    $(this).css('position', 'absolute');
					$(this).css('border', 'solid 2px rgb(106, 175, 231)');
	                $(this).css('border-width', '2px 1px');
                });
            }

            this.rotate = function() {
                var roller = $('#' + this.id)[0];
                var _amount = this.p.scrollAmount;
                var _spacing = this.p.spacing;
                var _elem = null;
                
                if(this.p.direction == 'left'){
                    var _max = 0;
                    //Move all child to the left
                    $('#' + this.id + ' a').each(function() {
                        $(this).css('left', ($(this).position().left - _amount) + 'px');
                        if($(this).position().left + $(this).width() > _max){
                            _max = $(this).position().left + $(this).width();
                        }
                        if (($(this).position().left + $(this).width()) < 0) {
                            _elem = this;
                        }
                    });
                    //Check and re-positioning element if needed
                    if (_elem != null){
                        $(_elem).css('left', _max + _spacing + 'px');
                    }
                }
                else{
                    var _min = 9999;
                    var _maxLeft = $(this).width();
                    //Move all child to the right
                    $('#' + this.id + ' a').each(function() {
                        $(this).css('left', ($(this).position().left + _amount) + 'px');
                        if($(this).position().left < _min){
                            _min = $(this).position().left;
                        }
                        if ($(this).position().left > _maxLeft) {
                            _elem = this;
                        }
                    });
                    
                    //Check and re-positioning element if needed
                    if (_elem != null) {
                        $(_elem).css('left', (_min - $(_elem).width() - _spacing) + 'px');
                    }
                }

				setTimeout('$j("#' + this.id + '")[0].rotate()', this.p.scrollDelay);
            };

            this.rotate();
        });
    }
})(jQuery);
