/*
Created By: Devon Beck
Website: http://www.401creative.com
Date: 10/21/2008
*/

var a = [];
var l = [];
var cindex = 0;
var canimating = false;
var carousel_main;

var carousel = Class.create();
carousel.prototype = {

	initialize: function(container,options) {
		if (!$(container)) {
			return false;
		}
		
		this.container = container;
		
		canimating = false;
		
		this.options = Object.extend({
			speed : 8,
			width : 600,
			classNames : {
				trigger : 'carousel-trigger',
				main_content : 'carousel_container',
				prev_btn : 'previous_button',
				next_btn : 'next_button'
			}
		}, options || {});
		
		this.duration = ((11-this.options.speed)*0.15);

		var triggers = $$('.'+this.options.classNames.trigger);
		triggers.each(function(trigger) {
			var i = $(trigger).identify();
			a.push( i );
			alen = a.length-1;
			Event.observe(trigger, 'click', this.goto.bind(this,trigger,alen), false);
			trigger.onclick = function() {return false;};
		}.bind(this));
		
		var p_btns = $$('.'+this.options.classNames.prev_btn);
		p_btns.each(function(prev) {
			Event.observe(prev, 'click', this.prev.bind(this,prev), false);
			prev.onclick = function() {return false;};
		}.bind(this));
		
		var n_btns = $$('.'+this.options.classNames.next_btn);
		n_btns.each(function(next) {
			Event.observe(next, 'click', this.next.bind(this,next), false);
			next.onclick = function() {return false;};
		}.bind(this));
		
		carousel_main = $$('.'+this.options.classNames.main_content)[0];
		
		this.setup();
		
	},
	
	setup: function(){
		var len = a.length;
		for(var i=0; i<len; i++){
			var html = "<li></li>";
			$(carousel_main).insert(html, 'bottom');
		}
		this.insert(cindex);
	},
	
	goto: function(trigger,alen){
		$(this.container).fire("carousel:init",cindex);
		if(!canimating){
			cindex = alen;
			var i = a[cindex];
			this.animateTo( -alen*this.options.width );
		}
		this.insert(cindex);
	},
	
	insert: function(i){
		var found = in_array(i, l);
		if(!found){
			l.push(i);
			var id = a[i];
			var li = $(carousel_main).childElements()[i];
			new Ajax.Request('required/carousel-ajax.php', {
				parameters: 'id='+id+'&index='+i,
				onSuccess: function(c) {
					li.update( c.responseText );
				}
			});
		}
		var id = a[i];
		$(this.container).fire("carousel:insert",id);
	},
	
	prev: function(){
		if(!canimating){
			var newindex = cindex-1;
			if(newindex>=0){
				this.goto(null,newindex);
			} else {
				this.goto(null,(a.length-1));
			}
		}
	},
	
	next: function(){
		if(!canimating){
			var newindex = cindex+1;
			if(newindex<=(a.length-1)){
				this.goto(null,newindex);
			} else {
				this.goto(null,0);
			}
		}
	},
	
	animateTo: function(i){
		canimating = true;
		new Effect.Opacity($(carousel_main), { from:1, to: 0.5, duration: 0.2, queue: { position: 'front', scope: 'carousel' } });
		new Effect.Move($(carousel_main), { x: i, y: 0, duration:this.duration, delay:0.2, mode: 'absolute', transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'carousel' } });
		new Effect.Opacity($(carousel_main), { from:0.5, to: 1, duration: 0.2, delay:0.2, queue: { position: 'end', scope: 'carousel' }, afterFinish: this.updateMove });
	},
	
	updateMove: function(){
		canimating = false;
	}
	
}

function in_array(needle, haystack, argStrict) {
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}