﻿var LimeBox = new Class({
	Implements: [Options],
	
	options: {
		'container'	: 'slider',
		'sticker'	: 'slider_text',
        'slides'    : false,
		'slideW'	: -960
	},
	
	initialize: function(options)
	{
		
		this.setOptions(options);
        if( this.options.slides.length > 1){
            new Element('div', {'id':'slider_navi'}).adopt(
                $$(
                    this.options.prv = new Element('a', {'id':'slide_prev','class':'prev_off','events':{'click':this.prev.bind(this)}}),
                    this.options.nxt = new Element('a', {'id': 'slide_next','class':'next','events':{'click':this.next.bind(this)}}),
                    this.options.countdiv = new Element('div', {'id': 'slide_count'})
                ),this
            ).inject(document.id(this.options.container).parentNode);
            
            this.fx = {
                slider: new Fx.Tween(this.options.container, { property: 'left',  duration: 600 },this),
                sticker: new Fx.Tween(this.options.sticker, { property: 'opacity', duration: 400 },this)
            }
            window.addEvent('load',this.setup.bind(this));
        }
	}, 
   
	setup: function(){
        /***
        create elements
        */
        
        Array.each(this.options.slides, function(slide,i){
            if(i>0){
                new Element('img', {'src':slide.img}).inject(document.id(this.options.container));
            }
            this.options.slides[i] = '<h4>'+slide.title+'</h4><p>'+slide.text+'</p>';
        },this);
        
		this.count = 0;
		document.id(this.options.container).style.left = 0;
		this.slides = document.getElements('#slider img').length;
		
		if(this.slides>1){

			document.id(this.options.container).style.width = (this.slides*(-this.options.slideW)) + 'px';
			this.indexes = [];
			while (this.count<this.slides) {
				var slide = new Element('b', {'events':{'click':this.indexer.bind(this,[this.count])}}).inject(this.options.countdiv);
				slide.innerHTML = '.';
				this.indexes.push(slide);
				this.count += 1;
			} 
			this.count = 0;
			this.indexes[this.count].className = 'current';
		}
		
        periodical = undefined;
        this.slideShow(true);
        
		return this.slides;
	},
    
    slideShow:function(init){
		if(init){
			slideShow = this.slideShow.bind(this)
			periodical = slideShow.periodical(4000);
		} else {
			this.direct = this.count == this.slides-1 ? -1 : 1;
			
            this.changeImage(this.count+1*(this.direct),true);
		}
	},
    
	next: function(){
		return this.changeImage(this.count+1,false);
	},
	
	prev: function(){
		return this.changeImage(this.count-1,false);
		
	},
	
	indexer: function(i){
		if( i == this.count ) return false;
		return this.changeImage(i);
	},
	
	changeImage: function(i,auto){
		if (i == -1 || i == this.slides) return false;
        if(this.animate || !this.indexes) return false;
       
        this.animate = true;
        

		this.indexes[i].className = 'current';
		this.indexes[this.count].className = 'inactive';
		
		this.fx.sticker.set(0);
		document.id(this.options.sticker).innerHTML = this.options.slides[i];
		this.fx.sticker.start(0,1);
        
       
        if( !auto && periodical ){ 
            periodical = clearTimeout(periodical);
            //this.slideShow.delay(10000,this,[true]);  //включает слайдшоу снова
        }

		this.fx.slider.start(this.count * this.options.slideW, i*this.options.slideW).chain(function(){
			this.animate = false;
			this.count = i;
		}.bind(this))
	
        
		if(this.options.hasNavigation){
			switch(i){
				case (this.slides-1):{
					this.options.nxt.className = 'next_off';
					this.options.prv.className = (this.slides==1) ? 'prev_off' : 'prev';
				}break;
				case (0):{
					this.options.prv.className = 'prev_off';
					this.options.nxt.className = 'next';
				}break;
				default : {
					this.options.nxt.className = 'next';
					this.options.prv.className = 'prev';
				}
			}
		}
		
		return false;
	}
});



window.addEvent('domready',function(){
    if(typeof(slides) != 'undefined' ){
        pr=new LimeBox({
            'slides': slides
        });
    }
});
