/*
---

script: mootools.slideGallery.js

description: Multifunctional slide carousel for MooTools.

license: MIT-style license

authors:
- Sergii Kashcheiev

requires:
- core/1.2.4: Fx.Tween
- core/1.2.4: Fx.Transitions

provides: [slideGallery, fadeGallery]

...
*/

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}


var slideGallery = new Class({
    Version: "1.2.3",
    Implements: [Options],
    options: {
        holder: ".holder",
        elementsParent: "ul",
        elements: "li",
        nextItem: ".next",
        prevItem: ".prev",
        stop: ".stop",
        start: ".start",
        speed: 600,
        duration: 4000,
        steps: 1,
        current: 0,
        transition: Fx.Transitions.linear,
        direction: "horizontal",
        mode: "callback",
        disableClass: "disable",
        currentClass: "current",
        random: false,
        paging: false,
        autoplay: false,
        autoplayOpposite: false,
        onStart: function (B, C, A) {},
        onPlay: function (B, C, A) {}
    },
    initialize: function (A, B) {
     //   pausecomp(1000);
        this.gallery = A;
        
        this.setOptions(B);
        this.holder = this.gallery.getElement(this.options.holder);
        this.itemsParent = this.holder.getElement(this.options.elementsParent);
        this.items = this.itemsParent.getElements(this.options.elements);
        this.next = this.gallery.getElement(this.options.nextItem);
        this.prev = this.gallery.getElement(this.options.prevItem);
        this.stop = this.gallery.getElement(this.options.stop);
        this.start = this.gallery.getElement(this.options.start);
        this.current = this.options.current;
		this.sumall = 0;
		var i = 0;
        this.bound = {
            rotate: this.rotate.bind(this)
        };
        if (this.options.direction == "horizontal") {
            this.direction = "margin-left";
			if (this.items[0]) this.size = this.items[0].getWidth();
			else this.size = 0;      
            this.visible = Math.ceil(this.holder.getWidth() / this.size);
			//sprawdzam sume dlugosci wszystkich elementow li
			this.items.each(function(el){	
				i = i + el.getWidth();
				});
			this.sumall = i;
			//console.log(this.sumall);	
			
        } else {
            this.direction = "margin-top";
            this.size = this.items[0].getHeight();
            this.visible = Math.round(this.holder.getHeight() / this.size)
        }
        if (this.next == null) {
            this.next = new Element("a").injectInside(this.gallery)
        }
        if (this.prev == null) {
            this.prev = new Element("a").injectInside(this.gallery)
        }
        this.next.cl = this.next.className;
        this.prev.cl = this.prev.className;
	//	console.log(this.sumall);
	//	console.log(this.holder.getWidth());
        if (this.sumall > this.holder.getWidth()) {
            if (this.options.random) {
                Array.prototype.shuffle = function (D) {
                    var G = this.length,
                        E, F;
                    while (G) {
                        E = Math.floor((G--) * Math.random());
                        F = D && typeof this[G].shuffle !== "undefined" ? this[G].shuffle() : this[G];
                        this[G] = this[E];
                        this[E] = F
                    }
                    return this
                };
                this.items.shuffle(this.items);
                this.hidden = new Element("div");
                this.items.each(function (E, D) {
                    this.wrap = new Element("div").adopt(E);
                    this.hidden.set("html", this.hidden.get("html"), this.wrap.get("html"))
                }.bind(this));
                this.itemsParent.set("html", this.hidden.get("html"));
                this.items = this.itemsParent.getElements(this.options.elements)
            }
            this.options.steps = this.options.steps > this.visible ? this.visible : this.options.steps;
            this.options.duration = this.options.duration < 1000 ? 1000 : this.options.duration;
            this.options.speed = this.options.speed > 6000 ? 6000 : this.options.speed;
            for (var C = 0; C < this.items.length; C++) {
                if (this.items[C].hasClass(this.options.currentClass)) {
                    this.current = C
                }
            }
            if (this.options.mode != "circle") {
                if (this.visible + this.current >= this.items.length) {
                    this.margin = (this.items.length - this.visible) * this.size;
					
                    this.current = this.items.length - this.visible
                } else {
                    this.margin = this.current * this.size
                }
                if (this.options.paging) {
                    this.paging = new Element("ul").injectInside(this.gallery).addClass("paging");
                    for (var C = 0; C < Math.ceil((this.items.length - this.visible) / this.options.steps) + 1; C++) {
                        this.paging.innerHTML += '<li><a href="#">' + parseInt(C + 1) + "</a></li>"
                    }
                    this.paging = this.paging.getElements("a");
                    this.paging.each(function (E, D) {
                        E.addEvent("click", function () {
                            if (D * this.options.steps + this.visible >= this.items.length) {
                                this.margin = (this.items.length - this.visible) * this.size;
                                this.current = this.items.length - this.visible
                            } else {
                                this.current = D * this.options.steps
                            }
                            this.margin = this.current * this.size;
                            this.play(this.options.speed);
                            return false
                        }.bind(this))
                    }.bind(this))
                }
                this.play(0)
            } else {
                for (; this.items.length < this.options.steps + this.visible;) {
                    this.items.clone().inject(this.itemsParent, "bottom");
                    this.items = this.itemsParent.getElements(this.options.elements)
                }
                if (this.current > this.items.length - 1) {
                    this.current = this.items.length - 1
                }
                if (this.current < 0) {
                    this.current = 0
                }
                for (var C = 0; C < this.current; C++) {
                    this.items[C].inject(this.itemsParent, "bottom")
                }
                this.options.paging = false
            }
            this.next.addEvent("click", function () {
                if (this.options.mode != "circle") {
                    if (this.visible + this.current >= this.items.length) {
                        if (this.options.mode == "callback") {
                            this.margin = 0;
                            this.current = 0
                        }
                    } else {
                        if (this.visible + this.current + this.options.steps >= this.items.length) {
                            this.margin = (this.items.length - this.visible) * this.size;
                            this.current = this.items.length - this.visible
                        } else {
                            this.current = this.current + this.options.steps;
                            this.margin = this.current * this.size
                        }
                    }
					
                    this.play(this.options.speed)
                } else {
					var D = this;
					if (D.current >= D.items.length) {
                    	D.current = 0
                    }

					this.size = this.items[D.current].getWidth();
			//		console.log(this.size);
					this.visible = Math.round(this.holder.getWidth() / this.size);
			//		console.log(this.holder.getWidth());
				//	console.log(this.visible);
                    
                    this.margin = this.size * this.options.steps;
                    this.itemsParent.set("tween", {
                        duration: this.options.speed,
                        transition: this.options.transition,
                        property: this.direction,
                        onComplete: function () {
                            for (var E = 0; E < D.options.steps; E++) {
                                if (D.current >= D.items.length) {
                                    D.current = 0
                                }
                                D.current++;
                                D.items[D.current - 1].inject(D.itemsParent, "bottom")
                            }
                            this.set(0);
                            D.options.onPlay(D.current, D.visible, D.items.length)
                        },
                        onCancel: function () {
                            this.onComplete()
                        }
                    });
                    this.itemsParent.tween(-this.margin)
                }
                return false
            }.bind(this));
            this.prev.addEvent("click", function () {
                if (this.options.mode != "circle") {
                    if (this.current <= 0) {
                        if (this.options.mode == "callback") {
                            this.margin = (this.items.length - this.visible) * this.size;
                            this.current = this.items.length - this.visible
                        }
                    } else {
                        if (this.current - this.options.steps <= 0) {
                            this.margin = 0;
                            this.current = 0
                        } else {
                            this.current = this.current - this.options.steps;
                            this.margin = this.current * this.size
                        }
                    }
                    this.play(this.options.speed)
                } else {
                    for (var D = 0; D < this.options.steps; D++) {
                        if (this.current - 1 < 0) {
                            this.current = this.items.length
                        }--this.current;
                        this.items[this.current].inject(this.itemsParent, "top")
                    }
                    this.itemsParent.setStyle(this.direction, -this.size * this.options.steps + "px");
					
                    this.margin = 0;
                    this.play(this.options.speed)
                }
                return false
            }.bind(this));
            if (this.options.autoplay || this.start || this.stop) {
                if (!this.options.autoplay) {
                    this.gallery.addClass("stopped")
                }
                this.timer = this.bound.rotate.delay(this.options.duration);
				
				
                this.gallery.addEvent("mouseenter", function () {
                    this.options.autoplay = false;
                    $clear(this.timer)
                }.bind(this));
                this.gallery.addEvent("mouseleave", function () {
                    if (!this.gallery.hasClass("stopped")) {
                        $clear(this.timer);
                        this.options.autoplay = true;
                        this.timer = this.bound.rotate.delay(this.options.duration)
                    }
                }.bind(this))
            }
            if (this.stop) {
                this.stop.addEvent("click", function () {
                    this.gallery.addClass("stopped").fireEvent("mouseenter");
                    return false
                }.bind(this))
            }
            if (this.start) {
                this.start.addEvent("click", function () {
                    this.gallery.removeClass("stopped").fireEvent("mouseenter");
                    return false
                }.bind(this))
            }
        }
		else if (this.sumall == 0){
				this.next.addClass(this.next.cl + "-" + this.options.disableClass);
            	this.prev.addClass(this.prev.cl + "-" + this.options.disableClass);
			}
		else {          
		//	this.next.addClass(this.next.cl + "-" + this.options.disableClass);
       //     this.prev.addClass(this.prev.cl + "-" + this.options.disableClass);
            this.next.addEvent("click", function () {
                return false
            }.bind(this));
            this.prev.addEvent("click", function () {
                return false
            }.bind(this));
            if (this.stop) {
                this.stop.addEvent("click", function () {
                    return false
                }.bind(this))
            }
            if (this.start) {
                this.start.addEvent("click", function () {
                    return false
                }.bind(this))
            }
            this.gallery.addClass("stopped")	
        }
        this.options.onStart(this.current, this.visible, this.items.length)
    },
    play: function (A) {
        this.sidesChecking();
        this.itemsParent.set("tween", {
            duration: A,
            transition: this.options.transition
        });
		
        this.itemsParent.tween(this.direction, -this.margin);
        if (this.options.paging) {
            this.paging.removeClass("active");
            this.paging[Math.ceil(this.current / this.options.steps)].addClass("active")
        }
        this.options.onPlay(this.current, this.visible, this.items.length)
    },
    rotate: function () {
        if (this.options.autoplay) {
            if (!this.options.autoplayOpposite) {
                this.next.fireEvent("click")
            } else {
                this.prev.fireEvent("click")
            }
            this.timer = this.bound.rotate.delay(this.options.duration)
        }
    },
    sidesChecking: function () {
        if (this.options.mode == "line") {
            this.next.removeClass(this.next.cl + "-" + this.options.disableClass);
            this.prev.removeClass(this.prev.cl + "-" + this.options.disableClass);
            if (this.visible + this.current >= this.items.length) {
                this.next.addClass(this.next.cl + "-" + this.options.disableClass)
            } else {
                if (this.current == 0) {
                    this.prev.addClass(this.prev.cl + "-" + this.options.disableClass)
                }
            }
        }
    }
});
var fadeGallery = new Class({
    Extends: slideGallery,
    initialize: function (A, B) {
        this.previous = null;
        if (B.mode == "circle") {
            B.mode = "callback"
        }
        B.steps = 1;
        this.parent(A, B);
        this.visible = 1
    },
    play: function (A) {
        if (this.previous == null) {
            this.previous = this.items
        }
        this.sidesChecking();
        this.previous.set("tween", {
            duration: A,
            transition: this.options.transition
        });
        this.previous.tween("opacity", 0);
        this.items[this.current].set("tween", {
            duration: A,
            transition: this.options.transition
        });
        this.items[this.current].tween("opacity", 1);
        if (this.options.paging) {
            this.paging.removeClass("active");
            this.paging[Math.ceil(this.current / this.options.steps)].addClass("active")
        }
        this.previous = this.items[this.current];
        this.options.onPlay(this.current, this.visible, this.items.length)
    }
});
