/**
 * Script, that activated home ainmation
*/

(function($) {
$.slideshow = {
    els: [],
    timer: null,
    count: null,
    speed: 'slow',
    delay: 0,
    current:0,
    
    turnNext: function () {
        this.els[this.current++].fadeOut($.slideshow.speed);
        this.current = this.current > this.count-1 ? 0 : this.current;
        this.els[this.current].fadeIn($.slideshow.speed);
        this.timer = setTimeout('$.slideshow.turnNext()', $.slideshow.delay);
    },
    
    turnPrev: function () {
        this.els[this.current--].fadeOut($.slideshow.speed);
        this.current = this.current < 0 ? this.count-1 : this.current;
        this.els[this.current].fadeIn($.slideshow.speed);
        this.timer = setTimeout('$.slideshow.turnNext()', $.slideshow.delay);
    }
};

$.fn.slideshow = function (options){
    
    function createSlideshow(box, settings) {
        var $$ = $(box);
        
        var els = $('li', $$);
        
        $.slideshow.count = count = els.size();
        $.slideshow.delay = settings.delay;
        $.slideshow.current = (settings.start < 1 ? 0 : ( settings.start > count ? count-1 : settings.start-1) );
        $.slideshow.speed = settings.speed;
        i = -1;
        
        els.each( function() {
            ++i != $.slideshow.current ? $(this).hide() : void(0);
            $(this).css({ 'position': 'absolute', 'left':'0', 'top':'0'})
            $.slideshow.els.push( $(this) );
        });
        
        if (settings.prev) {
            $.slideshow.prev = $(settings.prev);
            $.slideshow.prev.attr('href', 'javascript:void(0);').unbind().bind('click', function() {
                clearTimeout($.slideshow.timer);
                $.slideshow.turnPrev();
            });
            $('img', $.slideshow.prev).hide();
        }
        
        if (settings.next) {
            $.slideshow.next = $(settings.next);
            $.slideshow.next.attr('href', 'javascript:void(0);').unbind().bind('click', function() {
                clearTimeout($.slideshow.timer);
                $.slideshow.turnNext();
            });
            $('img', $.slideshow.next).hide();
        }
        
        $(settings.next).bind('mouseover', function() {
            $('img',$(this)).show();
        }).bind('mouseout', function() {
            $('img',$(this)).hide();
        });
        
        $(settings.prev).bind('mouseover', function() {
            $('img',$(this)).show();
        }).bind('mouseout', function() {
            $('img',$(this)).hide();
        });
        
        $.slideshow.timer = setTimeout('$.slideshow.turnNext()', $.slideshow.delay);
    }
    
    var settings = {
        start: 1,
        delay: 5000,
        speed: 'slow',
        prev: null,
        next: null
	};
	
    if ( options && typeof(options) != 'string' )
		$.extend(settings, options);
            
	return this.each( function() {
		if (!$(this).is('.fnSlideShow')) {
			createSlideshow(this, settings);				
		}
		
	});
    
}
})(jQuery);

