var KSlider = Class.create({
    
    initialize: function(options)
    {
		this.element = this.element || null;    	
        this.options = Object.extend({
	        w: 181,
	        h: 150,
	        num_elem: 4,
			total: 0,
			url: '',
	        direction: 'right',
	        wrapper: 'slide_container',
	        duration: 2000,
	        interval: 3000,
	        auto: 1
	    },options||{});
        
        if (this.options.total){
        	if (this.options.total < this.options.num_elem) this.options.num_elem = this.options.total;
        	this.elements = new Array(this.options.total);
        }else
        	this.elements = new Array();
	    
        this.current = 0;
		$(this.options.wrapper).style.width = this.options.w*this.options.num_elem;

		this.ef_l = {};
		this.ef_r = {};
        for(i=0;i<=this.options.num_elem;i++) {
    		this.ef_l[i] = { 'left': [ i*this.options.w, (i-1)*this.options.w] };
    		this.ef_r[i] = { 'left': [ (i-1)*this.options.w, i*this.options.w] };
        }
    },
    
    getFx: function(){
        if (this.options.direction == 'left') {
            return this.ef_l;
        } else {
            return this.ef_r;
        }
    },
    
	update: function (text, i){
        divobj = new Element('DIV', {'id':'jsslide_' + i, 'class':'jsslide'});
        divobj.innerHTML = text;
        divobj.style.position = 'absolute';
        divobj.style.width = this.options.w;
        divobj.style.height = this.options.h;
        this.elements[i] = divobj;
        this.hide(this.elements[i]);
        $(this.options.wrapper).insert(divobj);
	},
	
    hide: function (el) {
        el.style.top = '0';
        el.style.left = '-999em';
    },
    
    setPos: function (elems) {
		if (!elems) elems = this.getRunElems();
        for(i=0;i<elems.length;i++) {
			el = elems[i];
			if (el){
				if (this.options.direction == 'left') {
					el.style.left = this.options.w*i;
				}else{
					el.style.left = this.options.w*(i-1);
				}
			}
		}
    },

	getRunElems: function(){
        var objs = new Array();
		if(this.options.direction=='left' || this.options.direction=='up'){
			adj = 0;
		}else{
			adj = this.elements.length-1;
		}
      	for(i=0;i<=this.options.num_elem;i++) {
            objs[i] = this.elements[(this.current+i+adj) % this.elements.length];
        }
        if (this.options.total <= this.options.num_elem) {
            if(this.options.direction=='left' || this.options.direction=='up'){
                objs[this.options.num_elem] = null;
            }else{
                objs[0] = null;
            }
        }
		return objs;		
	},
	
    start: function () {
		if (!this.elements[this.next()]) {
			this.nextRun();
			return;
		}
        var objs = this.getRunElems();
		this.running = 1;
		test = this.getFx();
		
		objs.each(function(s, index) {
			$(s.id).style.left = test[index].left[0];
			move = test[index].left[1] - test[index].left[0];
		});
		
		Effect.multiple(objs, Effect.Move, {
			x: move, 
			y: 0, 
			mode: 'relative', 
			speed: 0, 
			duration: (this.options.duration / 1000),
			transition: Effect.Transitions.sinoidal
		});
		
		this.current = this.nextCurr();
		setTimeout(this.runDone.bind(this), this.options.duration);
    },
    
    runDone: function () {
    	this.running = 0;
    	this.nextRun();
    },
    
    nextRun: function () {
    	if (this.running) return;
		clearTimeout(this.timeOut);
		if (this.options.total <= this.options.num_elem) return;
		if (this.options.auto){
			this.timeOut = setTimeout(this.start.bind(this),this.options.interval);
		}		
    },
	
	nextCurr: function () {
		if(this.options.direction=='left' || this.options.direction=='up'){
	        next = (this.current+1) % this.elements.length;
		} else {
	        next = (this.current+this.elements.length-1) % this.elements.length;			
		}
		return next;
	},

	next: function () {
		if(this.options.direction=='left' || this.options.direction=='up'){
	        next = (this.current+this.options.num_elem) % this.elements.length;
		} else {
	        next = (this.current+this.elements.length-1) % this.elements.length;			
		}
		return next;
	}
});

var kslider = null;

function sliderInit (){
	container = $('slide_container');
	if (container) {
		elems = $('slide_container').getElementsByClassName('ks_el');
		
		kslider = new KSlider({
			total: elems.size,
			running: false
		});
		
		for(i=0;i<elems.length;i++){
			kslider.update (elems[i].innerHTML, i);
			kslider.hide(elems[i]);
		}
		kslider.setPos(null);
		kslider.nextRun();
	}
}

function setDirection(direction,ret){
	kslider.options.direction = direction;
	if(ret){
		kslider.options.interval = 3000;
		kslider.options.duration = 2000;
	} else {
		kslider.options.interval = 800;
		kslider.options.duration = 500;
		kslider.nextRun();
	}
}

