function unscrambleLink (id, sUrl, sText, isEmail) {
	var obj = document.getElementById (id);
	if (obj == null) {
		alert ('unscrambleLink: Object not found (' + id + ')');
		return;
	}
	var parent = obj.parentNode;
	if (parent == null) {
		alert ('unscrambleLink: Parent object not found');
		return;
	}
	// to create standards compliant xhtml, use <div><a></a><-- scramble --></div>
	if (parent.nodeName.toLowerCase () == 'div') parent = parent.firstChild;
	if (parent.nodeName.toLowerCase () != 'a') {
		alert ('unscrambleLink: Parent object is not an anchor tag');
		return;
	}
	if (sUrl != null && sUrl.length > 0) {
		eval ('var txt = String.fromCharCode(' + sUrl + ');');
		parent.href = txt;
	}
	if (sText != null && sText.length > 0) {
		var firstChild = parent.firstChild;
		//First node is a text node with input
		if (firstChild && firstChild.nodeType == 3 && firstChild.nodeValue != null && firstChild.nodeValue.replace (' ', '').length > 0) return;
		eval ('var txt = String.fromCharCode (' + sText + ');');
		txt = txt.replace (/&amp;/g, '&');
		parent.innerHTML = txt;
		if (parent.title != null && parent.title.length == 0) {
			parent.title = txt;
		}
	}
}


var fugu = {
	COLOR_OUT : 'FFFFFF',	
	COLOR_OVER : 'A4AD00',
	handler : function(elm, type) {
		elm.style.backgroundColor = '#' + this.getColor(type == 'out');
	},
	getColor : function(isOut) {
		return (isOut == true) ? this.COLOR_OUT : this.COLOR_OVER;
	}
};



/**
 * @author christian
 */


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.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);
};
