/*--------------------------------------------------------------------------*/

var SlideShow = function(container, slides, delay, initial) {
    this.container = container;
    this.slides = slides;
    this.delay = delay;
    this.current = initial;
    this.show = null;
    this.hide = null;
    this.togglers = this.container.find(".controls li");
    this.setup();
};
SlideShow.prototype = {
    setup: function(){
			var self = this;
			this.showOnly($(this.slides[this.current]));
			$(this.togglers[this.current]).addClass('active');
			
			var togglers = this.togglers.click(function() {
				var index = togglers.index(this);
				self.goTo(index);
			});

			this.start();
    },
    showOnly: function(el){
        this.slides.hide();
        el.show();
    },
    goTo: function(idx){
			

			
        var self = this;
        if (idx === this.current) {
            return;
        }
        $(this.togglers[this.current]).removeClass('active');
        $(this.slides[this.current]).fadeOut(400);
        this.current = idx;
        $(this.togglers[this.current]).addClass('active');
        $(this.slides[this.current]).fadeIn(400);
        self.start();
    },
    start: function(){
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    pause: function(){
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        if (this.hide) {
            this.hide.cancel();
        }
        else {
            return;
        }
    },
    back: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        if (this.current === 0) {
            this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        this.start();
        ev.stop(); 
    },
    forward: function(ev){
        if (this.show || this.hide) {
            return;
        }
        this.pause();
        this.next();
        ev.stop(); 
    },
    next: function(){
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};

/* tabs */
var Tabs = function (container) {
	this.container = $(container);
	this.togglers = this.container.find('.toggle');
	this.tabs = this.container.find('.tab');
	this.setup();
};
Tabs.prototype = {
	setup: function () {
		var self = this;
		this.show(0);
		this.togglers.each(function (i) {
			$(this).click(function(ev) {
				ev.preventDefault();
				self.show(i);
			});
		});
	},
	show: function (idx) {
		this.togglers.removeClass('active');
		$(this.togglers[idx]).addClass('active');
		this.tabs.hide();
		$(this.tabs[idx]).show();
		//this.container.css("height", $(this.tabs[idx]).height());
	}
};

$(document).ready(function() {
	if ($("#slideshow").length > 0) {
		new SlideShow($("#slideshow"), $("#slideshow .slide"), 5, 0);
	}
	
	if ($("#cinema_facts").length > 0) {
		new SlideShow($("#cinema_facts"), $("#cinema_facts .slide"), 5, 0);
	}
});
