Scroller = Class.create();
Scroller.prototype = {
    
    initialize: function(nsId, campusId, totalSets, itemsPerSet) {
	    this.total_sets = totalSets;
        this.nsId = nsId;
        this.items_per_set = itemsPerSet;
        this.item_width = this.set_width / itemsPerSet;
        this.current_set = 0;
		this.other_set = 1;
        this.set_width = 600;
    	this.source_url = Powered.contextPath+'/files/fragments/fileSet.jsp?campusId='+campusId+'&namespaceId='+this.nsId;
		this.set_event_handlers();
        this.cur_new_pos = 0;
        this.set_prefix='set_'+this.nsId+'_';
    },
    
    set_event_handlers: function() {
		Event.observe('scroller_next_'+this.nsId, 'click', this.next_set.bindAsEventListener(this));
		Event.observe('scroller_previous_'+this.nsId, 'click', this.prev_set.bindAsEventListener(this));
	},

    parse_set_data: function(req) {
		var set_id = this.other_set_id(this.scroll_direction, this.current_set);
        var ul = document.createElement('div');
		ul.id = this.set_prefix+set_id;
		ul.style.display = 'none';
        Element.addClassName(ul, 'scroller_div');
        ul.innerHTML = req.responseText;
		$('scroller_viewport_'+this.nsId).appendChild(ul);
        this.start_animation();
    },

 	next_set: function() {
        this.scroll_strip('left', this.set_width);
    },

	prev_set: function() {
		this.scroll_strip('right', this.set_width);
    },
 
	other_set_id: function(direction, cur_set) {
		if ((direction == 'next') || (direction == 'left')) {
			if (cur_set >=(this.total_sets-1)) {
				return 0;
			} else {
				return cur_set + 1;
			}
		} else {
			if (cur_set == 0) {
				return this.total_sets-1;
			} else {
				return cur_set - 1;
			}
		}
	},



    scroll_strip: function(direction, distance) {
        if (this.total_sets<2) return;
       this.stop_animation();
        this.scroll_distance = distance;
        this.scroll_direction = direction;
        var other_set_id = this.other_set_id(this.scroll_direction, this.current_set);
        if ($(this.set_prefix+other_set_id)==null) {
            now =new Date();
            new Ajax.Request( this.source_url + "&offset="+other_set_id*this.items_per_set +"&version="+ now.getTime(),
            { method: 'get',
                onSuccess: this.parse_set_data.bind(this)
            });
        }
        else {
            this.start_animation();
        }
    },

	stop_animation: function() {
		clearInterval(this.scroll_interval);
		this.scroll_interval = null;
	},

	start_animation: function() {
		this.pixels_for_movement = 20;
        this.cur_new_pos = 0;
        this.other_set = this.other_set_id(this.scroll_direction, this.current_set);
        this.scroll_interval = setInterval(this.animation_movement.bind(this), 10);
	},

	animation_movement: function() {
			var oth_new_pos = null;
			var end_set_scroll = false;
			var end_of_set = false;

			if ((this.scroll_distance - this.pixels_for_movement) <= 0) {
				this.pixels_for_movement = this.scroll_distance;
				this.scroll_distance = 0;
				end_set_scroll = true;
			}
			// Calculate new positions
			if (this.scroll_direction == 'left') {
                this.cur_new_pos = this.cur_new_pos - this.pixels_for_movement;
				oth_new_pos = this.cur_new_pos + this.set_width;

				if (oth_new_pos <= 0) {
                    end_of_set = true;
				}
			} else {
                this.cur_new_pos = this.cur_new_pos + this.pixels_for_movement;
				oth_new_pos = this.cur_new_pos - this.set_width;

				if (oth_new_pos >= 0) {
					end_of_set = true;
				}
			}

			if ($(this.set_prefix+this.other_set) != undefined) {
				this.set_x_pos($(this.set_prefix+this.current_set),this.cur_new_pos);
				this.set_x_pos($(this.set_prefix+this.other_set), oth_new_pos);

				if (this.scroll_distance > 0) {
					this.scroll_distance -= this.pixels_for_movement;
				}
			}

			if (end_of_set == true) {
                Element.hide($(this.set_prefix+this.current_set));
				this.current_set = this.other_set;
				this.other_set = null;
                this.stop_animation();
            }
			// Stop if we've reached the end of the distance and are in set mode
			if (end_set_scroll == true) {
                this.stop_animation();
			}
		return true;
	},
    
    set_x_pos:  function(obj, new_pos) {
        obj.style.left = new_pos + 'px';
        if (obj.style.display=="none") {
            Element.show(obj);
        }
    },

    get_x_pos: function (obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	}     
}

