/**
 * @author christian
 */
function addListener(element, type, expression, bubbling) {
  bubbling = bubbling || false;
  if(window.addEventListener)	{ // Standard
    element.addEventListener(type, expression, bubbling);
    return true;
  } else if(window.attachEvent) { // IE
    element.attachEvent('on' + type, expression);
    return true;
  } else return false;
}

var ImageLoader = function(url){
  this.url = url;
  this.image = null;
  this.loadEvent = null;
};

ImageLoader.prototype = {
  load:function(){
    this.image = document.createElement('img');
    var url = this.url;
    var image = this.image;
    var loadEvent = this.loadEvent;
    addListener(this.image, 'load', function(e){
      if(loadEvent != null){
        loadEvent(url, image);
      }
    }, false);
    this.image.src = this.url;
  },
  getImage:function(){
    return this.image;
  }
};

function Gallery(imageElm,transition,withStartUp) {
	this.data = [];
	this.imageElm = imageElm;
	this.currentCategory = null;
	
	if (transition == 'doubleFadeTransition') {
		this.onImageLoad = this[transition];
		this.onStartUp = this.startUpDoubleFade;
		this.onStartUpTransition = this.startUpDoubleFadeTransition;
		this.doubleFadeTransitionConstructor();
	}
	else if (transition == 'simpleFadeTransition') {
		this.onImageLoad = this[transition];
		this.onStartUp = this.startUpSimpleFade;
		this.onStartUpTransition = this.startUpSimpleFadeTransition;
	}
	else {
		this.onImageLoad = null;
		this.onStartUp = null;
		this.onStartUpTransition = null;
	}
	
	this.onImageCalc = null;
	this.onImageChange = null;
	this.onCategoryChange = null;
	this.loader = null;
	this.isStartUp = withStartUp || false;
	this.isActive = false;
}

Gallery.prototype.startUp = function (catId,imgId) {
	if (!this.isStartUp) {
		return;
	}
	catId = catId || 0;
	imgId = imgId || 0;
	this.setCategory(catId);
	this.data[this.currentCategory].current = imgId;
	if (this.onStartUp) {
		this.onStartUp();
	}
	this.changeImage(this.data[this.currentCategory][this.data[this.currentCategory].current].src);
};

Gallery.prototype.addCategory = function(arr) {
	arr = arr || [];
	var id = this.data.push(arr);
	id = id-1;
	this.data[id].current = 0;
	return id;
};

Gallery.prototype.hasCategory = function (catId) {
	return this.isset(this.data[catId]);
};

Gallery.prototype.setCategory = function (catId) {
	if (this.hasCategory(catId)) {
		this.currentCategory = catId;
		if (this.onCategoryChange) {
			this.onCategoryChange(catId);
		}
	}
};

Gallery.prototype.resetCategory = function() {
}

Gallery.prototype.addImage = function (catId,src,width,height) {
	if (this.hasCategory(catId)) {
		this.data[catId].push({
			'src': src,
			'width': width,
			'height': height
		});
		return true;
	}
	return false;
};

Gallery.prototype.hasImage = function(imgId,catId) {
	catId = catId || this.currentCategory;
	return this.isset(this.data[catId][imgId]);
};

Gallery.prototype.setImage = function(imgId) {
	if (this.hasImage(imgId)) {
		this.data[this.currentCategory].current = imgId;
		this.changeImage(this.data[this.currentCategory][this.data[this.currentCategory].current].src);
		if (this.onImageChange) {
			this.onImageChange(imgId,this.currentCategory);
		}
	}
};

Gallery.prototype.changeImage = function(src) {
	var transition = this.loadImage(src);
};

Gallery.prototype.hasNext = function() {
	return this.hasImage(this.data[this.currentCategory].current+1);
};

Gallery.prototype.getNext = function() {
	if (this.hasNext()) {
		this.setImage(this.data[this.currentCategory].current + 1);
	}
};

Gallery.prototype.hasPrev = function() {
	return this.hasImage(this.data[this.currentCategory].current-1);
};

Gallery.prototype.getPrev = function() {	
	if (this.hasPrev()) {
		this.setImage(this.data[this.currentCategory].current - 1);
	}
};

Gallery.prototype.getCurrentImage = function() {
		return this.data[this.currentCategory].current;
};

Gallery.prototype.calcImageSize = function(image) {
	if (this.onImageCalc) {
		image = this.onImageCalc(image);	
	}
	return image;
};

Gallery.prototype.setImageProperties = function(newImage,elm) {
	elm = elm || this.imageElm;
	elm.width = newImage.width;
	elm.height = newImage.height;
	elm.src = newImage.src;
};
	
Gallery.prototype.getPageInfo = function(catId) {
	catId = catId || this.currentCategory;
	return {current :this.data[catId].current+1, max : this.data[catId].length};
};

Gallery.prototype.getInfo = function(imgId,catId,property) {
	catId = catId || this.currentCategory;
	imgId = imgId || this.data[catId].current;
	if (this.data[catId][imgId][property]) {
		return this.data[catId][imgId][property];
	}
}

Gallery.prototype.isset = function(arg) {
	return (arg) ? true:false;
};

Gallery.prototype.loadImage = function(src) {
	this.loader = null;
	this.loader = new ImageLoader(src);
	this.loader.loadEvent = AJS.bind(this.loadImageComplete,this);
	this.loader.load();	
};

Gallery.prototype.loadImageComplete = function(src,image) {
	image = this.calcImageSize(image);
	if (this.isStartUp) {
		this.isStartUp = false;
		if (this.onStartUpTransition) {
			this.onStartUpTransition(image);
		}
		else {
			this.setImageProperties(image);
		}
	}
	else if (this.onImageLoad) {
		this.onImageLoad(image);
	}
	else {
		this.setImageProperties(image);
	}
};

Gallery.prototype.startUpSimpleFade = function () {
	AJS.setOpacity(this.imageElm,0);
};

Gallery.prototype.startUpSimpleFadeTransition = function(image) {
	this.setImageProperties(image);
	var fx = new AJS.fx.fadeIn(this.imageElm);
};

Gallery.prototype.simpleFadeTransition = function(image) {
	var elm = this.imageElm;
	var fx1 = new AJS.fx.fadeOut(elm,{duration : 800});
	var scope = this;
	fx1.options.onComplete = function(){
		scope.setImageProperties(image);
		var fx2 = new AJS.fx.fadeIn(elm,{duration : 800});
	};
};

Gallery.prototype.startUpDoubleFade = function () {
	AJS.setOpacity(this.inactive,0);
};

Gallery.prototype.startUpDoubleFadeTransition = function(image) {
	this.setImageProperties(image,active);
	var fx = new AJS.fx.fadeIn(active);
};

Gallery.prototype.doubleFadeTransitionConstructor = function () {
	var id =  this.imageElm.id;
	var copyElm = new Image();
	copyElm.className = this.imageElm.className;
	copyElm.id = id+'_2';
	this.imageElm.id = id+'_1';
	AJS.insertAfter(copyElm,this.imageElm);
	this.active = AJS.$(id+'_1');
	this.inactive =  AJS.$(id+'_2');
	AJS.setOpacity(this.inactive,0);
};

Gallery.prototype.doubleFadeTransition = function(image) {
	var on 	= this.active;
	var off = this.inactive;
	this.active 	= off;
	this.inactive 	= on;
	
	this.setImageProperties(image,off);
	var fx1 = new AJS.fx.fadeIn(off);
	var fx2 = new AJS.fx.fadeOut(on);
};


