'Welfast'.namespace();

Welfast.HpSlides = new Class({

	Extends: slideGallery,

    list: [],

    initialize: function(gallery, options) {

        this.parent(gallery, options);

        this.list = this.items.map(function(item){return item;});
        this.createPaging();
        this.options.paging = true;

        this.continueRotate = true;

        this.fx.addEvent('complete', function(){
            clearTimeout(this.timer);
            if (this.continueRotate) {
                this.timer = this.bound.rotate.delay(this.options.duration);
            }
        }.bind(this));
    },

	createPaging: function() {
		this.paging = new Element("table");
		var pagingHold = this.gallery.getElement(this.options.pagingHolder);
		if(pagingHold != null) this.paging.injectInside(pagingHold);
		else this.paging.injectInside(this.gallery).addClass("paging");

		var length = Math.ceil((this.items.length-this.visible)/this.options.steps)+1;
		var str = "<tbody><tr>";
		for(var i=0; i<length; i++) {
			str += '<td><a href="#"></a></td>';
		}
		str += '</tr></tbody>';
		this.paging = this.paging.set("html", str).getElements("a");
		this.paging.each(function(el, i) {

			el.addEvent(this.options.pagingEvent, function() {

                this.current = i;
                clearTimeout(this.timer);
				this.play(true);
				return false;
			}.bind(this));
		}.bind(this));
        this.paging[0].addClass('active');
		return this;
	},

    nextSlide: function() {
        this.parent();
        this.setActivePaging();
        return this;
    },

    prevSlide: function() {
        this.parent();
        this.setActivePaging();
        return this;
    },

    setActivePaging: function() {
        this.paging.each(function(el, i) {
            el.removeClass('active');
            if (i == this.current) {
                el.addClass('active');
            }
        }, this);
    }

});

Welfast.HP = new Class({

	Implements: Options,

	options: {
		// nastaveni pro prochazeni nahledu sipkami
		carousel: {
			element: null,
			options: {
				// mode: 'circle',
				holder: null,
				nextItem: null,
				prevItem: null,
				paging: true
			}
		}
	},

	/**
	 * Konstruktor.
	 *
	 * @param {Object} options
	 */
	initialize: function (options) {
		this.setOptions(options);

		var holder = this.options.carousel.options.holder;
		if (!holder) return;

		var loader = new Welfast.Spinner();
		var list = holder.getElement('ul');
			list.setStyle('visibility', 'hidden');

		loader.inject(holder, 'bottom');

		var loaded = false;

		var imgs = this.options.carousel.options.holder.getElement('img');
		imgs.addEvent('load', function(){

			loaded = true;
			this.startCarousel(loader, holder, list);

		}.bind(this));

		var timetStart = function () {
			if (!loaded) {
				this.startCarousel(loader, holder, list);
			}
		}.bind(this);

		timetStart.delay(2500);
	},

	startCarousel: function(loader, holder, list) {

		loader.dispose();
		holder.setStyle('background-color', 'transparent');
		list.setStyle('visibility', 'visible');

		new Welfast.HpSlides(
			this.options.carousel.element,
			this.options.carousel.options
		);
	}

});

