/* JS-jQuery Animationen der Testimonials-Struktur */

jQuery(document).ready(
	function(){
		jQuery('div#picnav').each(function(i) {
			new TestimonialCollection(jQuery(this));
		});
	}
);

function Testimonial(main, div, c) {
	var instance = this;
	this.main = main;
	this.c = c;
	
	this.div = div;
	this.mousearea = jQuery('div.persona', this.div);
	this.pic = jQuery('div.persona a', this.div);
	this.text = jQuery('div.persona_descr', this.div);
	

	this.pos_pic_off = {
			'top': '0px'
		};
	this.pos_pic_on = {
			'top': '105px'
		};
	
	this.pos_text_off = {
			'top': '105px',
			'opacity': 0
		};
	this.pos_text_on = {
			'top': '210px',
			'opacity': '1'
		};
	
	this.mousearea.css({
		'height': '210px'
	})
	this.div.css({
		'position': 'relative'
	});
	this.pic.css(this.pos_pic_off);
	this.text.css(this.pos_text_off);
	
	
	this.debug = this.main.debug;
	this.log = function(str) {
		if (this.debug && typeof console != 'undefined') {
			console.log('Testimonial: ' + str);
		}
	}
	
	this.show = function() {
		//this.log(' > ' + this.c);
		this.text.stop(true);
		this.pic.stop(true).animate(this.pos_pic_on, 500, 'easeInQuad', 
			function() {
				instance.text.animate(instance.pos_text_on, 500, 'easeOutQuad');
			}
		);
	}
	this.hide = function() {
		//this.log(' < ' + this.c);
		this.pic.stop(true);
		this.text.stop(true).animate(this.pos_text_off, 500, 'easeInQuad', 
			function() {
				instance.pic.animate(instance.pos_pic_off, 500, 'easeOutQuad');
			}
		);
	}
	
	this.mousearea.hover(
			function() {
				instance.main.mouseStart(instance.c);
				instance.show();
			}, 
			function() {
				instance.main.mouseStop(instance.c);
				instance.hide();
			}
		);
}
function TestimonialAutoplayer(main, testimonials) {
	var instance = this;
	this.main = main;
	this.testimonials = testimonials;
	
	this.interval = null;
	
	this.current = -1;

	this.debug = this.main.debug;
	this.log = function(str) {
		if (this.debug && typeof console != 'undefined') {
			console.log('TestimonialAutoplayer: ' + str);
		}
	}
	
	this.open = false;
	this.next = function() {
		clearInterval(this.interval);
		if (this.open) {
			this.log('close ' + this.current);
			this.testimonials[this.current].hide();
			this.interval = setInterval(function() {instance.next()}, 1000 * 6);
			this.open = false;
		} else {
			this.current ++;
			if (this.current == this.testimonials.length) this.current = 0;
			this.log('open ' + this.current);
			this.testimonials[this.current].show();
			this.interval = setInterval(function() {instance.next()}, 1000 * 8);
			this.open = true;
		}
		
	}
	
	this.start = function() {
		this.log('start');
		clearInterval(this.interval);
		this.interval = setInterval(function() {instance.next()}, 1000 * 4);
		
	}
	this.stop = function(c) {
		this.log('stop' + c);
		clearInterval(this.interval);
		if (c != this.current && this.open) {
			this.testimonials[this.current].hide();
		}
		this.current = c;
		this.open = false;
	}
	
}
function TestimonialCollection(div) {
	var instance = this;
	this.div = div;
	
	this.testimonials = new Array();
	
	this.debug = true;
	this.log = function(str) {
		if (this.debug && typeof console != 'undefined') {
			console.log('TestimonialCollection: ' + str);
		}
	}
	
	jQuery('div.unit', this.div).each(function(i) {
		var testimonial = new Testimonial(instance, jQuery(this), i);
		instance.testimonials.push(testimonial);
	});
	
	this.log(this.div.children().length);
	this.log(this.testimonials.length);

	
	this.autoplayer = new TestimonialAutoplayer(this, this.testimonials);
	
	this.mouseStart = function(c) {
		this.autoplayer.stop(c);
	}
	this.mouseStop = function(c) {
		this.autoplayer.start();
	}

	this.autoplayer.start();
}
