// JavaScript Document

var Slideshow = new Class({
  
	Implements: [Options],
	
	options: {
		wrapper: 'slide-wrapper',
		slide: '.slide',
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut,
		start: 0,
		time: 3000
  },
	
	initialize: function(container, options){
		this.container = $(container);
		this.setOptions(options);
		
		this.wrapper = $(this.options.wrapper);
		this.slides = this.container.getElements(this.options.slide);
		this.count = this.slides.length;
		this.current = this.options.start;
		this.preload = true;
		this.imgCount = 1;
		
		
		this.slideWidth = 0;
		if (this.count > 0){
			this.slideWidth = this.slides[0].getSize().x;
		}
		
		this.slideFx = new Array();
		// prepare slides
		this.slides.each(function(slide, index){
		  slide.setStyles({
			  'position' : 'absolute',
				'top' : 0,
				'left' : this.slideWidth
  		});
			this.slideFx[index] = new Fx.Tween(slide, { property: 'left', duration: this.options.duration, transition: this.options.transition, link: 'chain' });

      // display first only
			if (index == this.options.start){
				this.slideFx[index].set(0);
			} else {
				this.slideFx[index].set(this.slideWidth);
			}
		}, this);
		
  	// get img filename pattern
		this.pattern = this.slides[0].getElement('img').get('src').replace(/\./,'{id}.');
		
		// get img name and path and check for img count
		this.name = this.pattern.slice(this.pattern.lastIndexOf('/')+1, this.pattern.indexOf('{'));
		this.path = this.pattern.slice(0,this.pattern.lastIndexOf('/'));
		this.req = new Request({
		  url: '/count.php',
			method: 'get',
			data: 0,
			link: 'cancel',
			onSuccess: function(response){
				this.imgCount = response.toInt();
				this.update();
			}.bind(this)
		}).send('p='+this.path+'&n='+this.name);
		
		// buttons
		this.buttons = new Element('div', {
		  'id' : 'slide-buttons',
			'html' : '<span class="prev"></span><span class="count"></span><span class="next"></span>'
		});
		this.buttons.inject(this.wrapper.getParent('div'));
		this.prev = this.buttons.getElement('.prev');
		this.next = this.buttons.getElement('.next');
		
		this.prev.addEvent('click', function(){
		  this.move('prev');
		}.bind(this));
		
		this.next.addEvent('click', function(){
		  this.move('next');
		}.bind(this));
	  
		// preload next image
		this.getImage();
		
		// run slideshow (if more than one slide available)
 		this.repeater = this.move.periodical(this.options.time,this);
		
		
	},
	
	move: function(dir){
		switch (dir){
			case 'prev':
			  $clear(this.repeater);
				var next = (this.current - 1)%this.count;
				this.slideOut(this.current, 1);
				this.slideIn(next, 1);
				this.current = next;
				this.update();
			break;
			case 'next':
			  $clear(this.repeater);
			  var next = (this.current + 1)%this.count;
				this.slideOut(this.current);
				this.slideIn(next);
				this.current = next;
				this.update();
			break;
			default:
				if (this.count > 1){
					var next = (this.current + 1)%this.count;
					this.slideOut(this.current);
					this.slideIn(next);
					this.current = next;
					this.update();
				}
		}
	},
	
	slideIn: function(slideID, rev){
		var rev = (rev || 0);
		rev ? this.slideFx[slideID].set(-this.slideWidth) : this.slideFx[slideID].set(this.slideWidth);
		this.slideFx[slideID].start(0);
	},
	
	slideOut: function(slideID, rev){
		var rev = (rev || 0);
		rev ? this.slideFx[slideID].start(this.slideWidth) : this.slideFx[slideID].start(-this.slideWidth);
	},
	
	addSlide: function(img){
		var slide = new Element('div', {
		  'class': this.options.slide.replace('.',''),
			'html': '&nbsp;<span></span>&nbsp;'
		});
		img.inject(slide.getElement('span'));
		slide.inject(this.wrapper);
		this.count++;
		slide.setStyles({
		  'position' : 'absolute',
			'top' : 0,
			'left' : this.slideWidth
		});
		this.slideFx[this.count-1] = new Fx.Tween(slide, { property: 'left', duration: this.options.duration, transition: this.options.transition, link: 'chain' });
		this.slideFx[this.count-1].set(this.slideWidth);
	},
	
	getImage: function(){
		var img = new Asset.image(this.pattern.replace('{id}', this.count), {
			onload: function(img){
				if (img.width > 0) {
					this.addSlide(img);
					this.getImage();
				} else {
					this.preload = false;
				}
			}.bind(this),
			onerror: function(){
				this.preload = false;
			}
		});
	},
	
	update: function(){
		if (this.imgCount > 1) {
			this.buttons.setStyle('visibility', 'visible');
			this.buttons.getElement('.count').set('text', (this.current+1) + '/' + this.imgCount);
			this.current+1 == this.imgCount ? this.next.setStyle('visibility', 'hidden') : this.next.setStyle('visibility', 'visible');
			this.current == 0 ? this.prev.setStyle('visibility', 'hidden') : this.prev.setStyle('visibility', 'visible');
		}
  }

});

var References = {
	
	options: {
		max: 375,
		min: 23
	},
	
	init: function(){

    this.container = $('references');
		// disabled
		if (this.container.hasClass('disabled')) return true;
		
		this.slide = this.container.getElement('div.slide');
		this.slideFx = new Fx.Tween(this.container, { property: 'width', duration: 500, transition: 'quad:in:out', link: 'cancel' });
		this.slideFx.set(this.options.min);
		this.closed = true;
		
		this.toggler = $('references-btn');
		this.toggler.addEvent('click', function(event){
			if (this.closed){
				this.closed = false;
				this.slideFx.start(this.options.max);
				this.toggler.removeClass('closed');
			} else {
				this.closed = true;
				this.slideFx.start(this.options.min);
				this.toggler.addClass('closed');
			}
		}.bind(this));
		
		this.container.addEvent('mouseenter', function(){
		  this.closed = false;
			this.slideFx.start(this.options.max);
			this.toggler.removeClass('closed');
		}.bind(this));
		
		this.slide.addEvent('mouseleave', function(){
			if (this.container.getSize().x == this.options.max){
				this.closed = true;
				this.slideFx.start(this.options.min);
				this.toggler.addClass('closed');
			}
		}.bind(this));
		
		// keep open
		if (this.container.hasClass('disabled')){
			this.slideFx.set(this.options.max);
			this.toggler.removeClass('closed');
		}
	}
};

window.addEvent('domready', function(){
																		 
	if ($('slideshow')){
		new Slideshow('slideshow');
	}
	
	if ($('references')) References.init();
	
	var textSlides = $$('.slide-text');
	var togglers = $$('.toggler');
	togglers.each(function(toggler, index){
	  var slideFx = new Fx.Slide(textSlides[index], { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeInOut});
		slideFx.hide();
		toggler.addEvent('click', function(event){
		  event.stop();
			slideFx.toggle();
		});
	});

});


// Email.js version 5
var tld_ = new Array()
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";

function fcuk(name, dom, tl, params)
{
	var s = e(name,dom,tl);
	document.write('<a href="'+m_+s+params+'">'+s+'</a>');
}
function e(name, dom, tl)
{
	var s = name+a_;
	if (tl!=-2)
	{
		s+= dom;
		if (tl>=0)
			s+= d_+tld_[tl];
	}
	else
		s+= swapper(dom);
	return s;
}
function swapper(d)
{
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}
