Image_gallery_slider = Class.create({
	initialize: function(containerId, options) {
		this.setOptions(options);
		
		this.currentIndex = 0;
		this.liSize = 0;
		this.animDone = true;
		
		this.containerId = containerId;

		this.updateCount();
		
		//this.buttonLeft = $(this.options.leftId);
		//this.buttonRight = $(this.options.rightId);
	
		//this.buttonLeft.observe('click', this.moveLeft.bindAsEventListener(this));
		//this.buttonRight.observe('click', this.moveRight.bindAsEventListener(this));

		if(this.options.show_selected)
			this._show_selected();
		else			
			this.moveLeft(); // force the left button to start disabled

    this.automove(5000);
	},

automove: function(timeout){
	if (this.listCount > 2) {
	    var obj = this;

	    clearTimeout(obj.current_timer);

	    if(!timeout)
		var timeout = 4500;

	    if(obj.currentIndex  == obj.listCount-1){
			obj.current_timer = setTimeout(function() {obj.allLeft();}, timeout);
	    }
	    else {
		obj.current_timer = setTimeout(function() {obj.moveRight();}, timeout);
	    }
	}
},
	
	updateCount: function() {
		this.ul = $$('#' + this.containerId + ' ul')[0];
		this.listCount = this.ul.getElementsByTagName("li").length;
		
		if(this.listCount < this.options.numVisible) {
			this.options.numVisible = this.listCount;
		}
	},
	
	moveRight: function() {

		if (this.animDone != true) 
			return false;
		//alert(this.currentIndex +"+"+ this.options.numVisible +"+"+ this.options.increment);
		if (this.currentIndex + this.options.numVisible + this.options.increment <= this.listCount+1)
			this._scroll(-this.options.increment);
		else 
			this._scroll(-(this.listCount - (this.currentIndex + this.options.numVisible)));
		
		if(this.listCount == this.currentIndex + this.options.numVisible)
			Element.addClassName(this.buttonRight, this.options.disabledClass);
		
		Element.removeClassName(this.buttonLeft, this.options.disabledClass);

    this.automove();

	},
	
	moveLeft: function() {
		if (this.animDone != true)
			return false;
		
		var inc = this.options.increment;

		if (this.currentIndex - inc < 0)
			inc = this.currentIndex;
		
		this._scroll(inc);
		
		if (this.currentIndex == 0)
			Element.addClassName(this.buttonLeft, this.options.disabledClass);
		
		Element.removeClassName(this.buttonRight, this.options.disabledClass);

    this.automove();
		
	},
	
	allRight: function() {
		if (this.animDone != true)
			return false;
	
		var inc = this.listCount - this.currentIndex - this.options.numVisible;
		this._scroll(-inc);
		
		Element.addClassName(this.buttonRight, this.options.disabledClass);
		Element.removeClassName(this.buttonLeft, this.options.disabledClass);

    this.automove();
	},
	
	allLeft: function() {
		if (this.animDone != true)
			return false;
		//this.moveTo(0);return;
		this._scroll(this.currentIndex, true);

    this.automove(200);
	},
	
	_show_selected: function() {

		var selected = this.ul.select('.' + this.options.selected_class)[0];
		
		var index = this.ul.childElements().indexOf(selected);
		
		var offset = this.options.show_offset;
		
		if(index > offset){

			if((this.listCount - index) < offset)
				offset = this.options.numVisible - (this.listCount - index);

			if(offset > 0)
				index = -index + offset;

			if (index + this.options.numVisible > this.listCount)
				index = this.listCount - this.options.numVisible;
			
			if (index < 0)
				index = 0;
			
			this._scroll(-1 * index);

		}
	},
	
	_scroll: function(delta, effect_duration) { 
		this.animDone = false;

    if(!effect_duration)
	var effect_duration = 0.5;
    else
	var effect_duration = 0.0
		
		if(this.liSize <= 0) 
			this.liSize = this._getLiElementSize();

    new Effect.MoveBy(this.ul, 0, delta * this.liSize, {duration: effect_duration, afterFinish: function() {this.animDone = true}.bind(this)});

    this.currentIndex -= delta;

    this._select_current_nav_item(this.currentIndex);

	},
	
	_getLiElementSize: function() {
		var li = $(this.ul.getElementsByTagName("li")[0]);
		return li.getDimensions().width + parseFloat(li.getStyle("margin-left")) + parseFloat(li.getStyle("margin-right"));
	},

moveTo: function(list_position){
		if (this.animDone != true)
			return false;

    var delta = this.currentIndex - list_position;

    //this will happen if we're at the last element (which is fake)
    if(delta == this.listCount){
	list_position = 0;
	delta = this.currentIndex - this.listCount;
    }

    this._scroll(delta);

    this.automove();

},

_select_current_nav_item: function(current_list_position) {
    if(current_list_position == this.listCount-1)
	current_list_position = "0";

    $$('#image_gallery_slider_nav .image_gallery_slider_nav_item').each(function(nav_item) {
	nav_item.removeClassName('selected');
	nav_item.addClassName('not_selected');
    });

	if ($('image_gallery_slider_nav_pos_' + current_list_position))
	{
	    $('image_gallery_slider_nav_pos_' + current_list_position).removeClassName('not_selected');
		$('image_gallery_slider_nav_pos_' + current_list_position).addClassName('selected');
	}
},

	setOptions: function (options) {
		this.options = {
			numVisible: 1,
			slider_element: 'ul',
			increment: 1,
			leftId: 'slide_left',
			rightId: 'slide_right',
			disabledClass: 'slide_disabled',
			show_selected: false,
			selected_class: 'selected',
			show_offset: 0,
	instanceName: 'slideGallery' 
		};
		Object.extend(this.options, options || {});
	}
});

/*
 * Restrictive Wrapper for Google Analytics pageTracker._trackEvent()
 *
 * String   category The general event category (e.g. "Videos").
 * String   action The action for the event (e.g. "Play").
 * String   label An optional descriptor for the event.
 * Int      value An optional value to be aggregated with 
 */

function trackEvent(category, action, label, value)
{
    if(typeof(pageTracker) == "object"){
        return pageTracker._trackEvent(category, action, label, value);
    }

    return true;
}


