/**
 * Images rotator
 * @author: Michal Wolinski <michal.wolinski@hotmail.com>
 */

function getMethodReference(object, method, arguments) {
	if(!(method instanceof Function)) {
		method = object[method];
	}
	return function() {
		return method.apply(object, arguments);
	}
}

function getTarget(event) {
	var target = null;
	if(event == null) {
		target = window.event.srcElement
	} else {
		target = event.target;
	}
	if (!target && window.event.srcElement){
			target = window.event.srcElement;
	}
	if(target.nodeType == 3) {
		target = target.parentNode;
	}
	return target;
}


function ImageSlider(photos,container,delay,dimensions){
	this.photos = photos;
	this.container = container;
	this.handle = document.getElementById(this.container);
	this.dimensions = dimensions;
	this.delay = delay;
	this.t;
	this.wait = delay;
	this.count = this.photos.length;
	if (this.load()){
		this.slide();
	}
	return this;
}

ImageSlider.prototype.load = function(){
	//this.handle.style['position'] = 'relative';
	this.handle.style['overflow'] = 'hidden';
	for (var i=0; i<this.count; i++){
		var photo = document.createElement('div');
		photo.id = 'is'+i;
		photo.style['background'] = 'url('+this.photos[i]+')';
		photo.style['width'] = this.dimensions[0]+'px';
		photo.style['height'] = this.dimensions[1]+'px';
		photo.style['position'] = 'absolute';
		photo.style['cursor'] = 'pointer';
		photo.onclick = function(){ document.location.href = '/galeria.html';};
		this.setOpacity(photo,0);
		photo.style['zIndex'] = 4;
		this.handle.appendChild(photo);
	}
	this.setOpacity(document.getElementById('is0'),1);
	this.activeImage = 0;
	return true;
}

ImageSlider.prototype.slide = function(){
	this.wait--;
	if (this.wait<=0){
		var curImage = document.getElementById('is'+this.activeImage);
		var nextImageId = (parseFloat(this.activeImage)+1 == this.count) ? 0 : parseFloat(this.activeImage)+1;
		if (this.slideToId){
			nextImageId = parseFloat(this.slideToId);
		}
		var nextImage = document.getElementById('is'+nextImageId);
		this.setOpacity(curImage,parseFloat(curImage.style['opacity'])-0.05);
		this.setOpacity(nextImage,parseFloat(nextImage.style['opacity'])+0.05);
		if (curImage.style['opacity'] <= 0){
			this.activeImage = nextImageId;
			this.wait = this.delay;
			if (this.slideToId){
				this.slideToId = false;
			}
		}
	}
	this.t = setTimeout(getMethodReference(this,this.slide),1);
}

ImageSlider.prototype.setOpacity = function(elem, opacity){
	elem.style['opacity'] = opacity;
	elem.style['filter'] = "alpha(opacity="+parseFloat(opacity)*100+")";
}

ImageSlider.prototype.slideTo = function(event){
	var target = getTarget(event);
	var id = target.id.replace(/iss/,'');
	clearTimeout(this.t);
	this.slideToId = id;
	this.wait = 0;
	this.t = setTimeout(getMethodReference(this,this.slide),1);
}

window.onload = function(){
	if (document.getElementById('rotator')) {
		var photos = ['/p/rotator/1.jpg', '/p/rotator/2.jpg', '/p/rotator/3.jpg', '/p/rotator/4.jpg', '/p/rotator/5.jpg'];
		var dimensions = [570, 320];
		var slider = new ImageSlider(photos, 'rotator', 400, dimensions);
	}
}