function setFader(obj) {
    var fader = {
        
        obj: null,
        speed: 1,
        currentOpacity: 100,
        _fadeAction: null,
        _watchers: new Array(),
        
        setOpacity: function(opacity) {
        	this.currentOpacity = opacity;
            //If IE
            if(this.obj.filters) {
            
                //If the filter was not previously set, we initialize it
                if(!this.obj.filters.alpha) { this.obj.style['filter'] = 'alpha(opacity=' + this.currentOpacity + ');'; }
                
                //Set new opacity
                this.obj.filters.alpha.opacity =  this.currentOpacity;
            }
            
            //Set opacity in CSS3
            this.obj.style.opacity = (this.currentOpacity / 100);
            
            //Set opacity in Mozilla
            this.obj.style.MozOpacity = (this.currentOpacity / 100);
            
            //Set opacity in Konkerer
            this.obj.style.KhtmlOpacity = (this.currentOpacity / 100);
		},
        
        fadeIn: function() {
            //increase opacity
            if(this.speed <= 0) this.speed = 1;
            if(this.speed > 100) this.speed = 100;
            this.currentOpacity += 10;
                    
            //correct values over 100%
            if(this.currentOpacity > 100) this.currentOpacity = 100;

            //If IE
            if(this.obj.filters) {
            
                //If the filter was not previously set, we initialize it
                if(!this.obj.filters.alpha) { this.obj.style['filter'] = 'alpha(opacity=' + this.currentOpacity + ');'; }
                
                //Set new opacity
                this.obj.filters.alpha.opacity =  this.currentOpacity;
            }
            
            //Set opacity in CSS3
            this.obj.style.opacity = (this.currentOpacity / 100);
            
            //Set opacity in Mozilla
            this.obj.style.MozOpacity = (this.currentOpacity / 100);
            
            //Set opacity in Konkerer
            this.obj.style.KhtmlOpacity = (this.currentOpacity / 100);
            
            //If necesary, set the recurring action
            if(this.currentOpacity < 100) {
                clearTimeout(this._fadeAction);
                var timeOut = ( 101 - this.speed );
              
                this._fadeAction = setTimeout("document.getElementById('" + this.obj.id + "').fader.fadeIn()",timeOut);
            } else {
                clearTimeout(this._fadeAction);
                this._fadeAction = null;
                for(var i in this._watchers){
                    try {
                        this._watchers[i].notify(this);
                    } catch(e) {
//                        alert("Wrong watcher interface\n"+e+"\n I'm " + this.obj.id);
                    }
                }
            }
          
        },

        fadeOut: function () {
            //reduce opacity
            if(this.speed <= 0) this.speed = 1;
            if(this.speed > 100) this.speed = 100;
            this.currentOpacity -= 10;

            //correct negative values
            if(this.currentOpacity < 0) this.currentOpacity = 0;

            //If IE
            if(this.obj.filters) {
            
                //If the filter was not previously set, we initialize it
                if(!this.obj.filters.alpha) { this.obj.style['filter'] = 'alpha(opacity=' + this.currentOpacity + ');'; }

                //Set new opacity
                this.obj.filters.alpha.opacity =  this.currentOpacity;
            }
            
            //Set opacity in CSS3
            this.obj.style.opacity = (this.currentOpacity / 100);
            
            //Set opacity in Mozilla
            this.obj.style.MozOpacity = (this.currentOpacity / 100);
            
            //Set opacity in Konkerer
            this.obj.style.KhtmlOpacity = (this.currentOpacity / 100); 
            
            //If necesary, set the recurring action
            if(this.currentOpacity) 
            {
                clearTimeout(this._fadeAction);
                var timeOut = ( 101 - this.speed );
                this._fadeAction = setTimeout("if (document.getElementById('" + this.obj.id + "')) document.getElementById('" + this.obj.id + "').fader.fadeOut()",timeOut);
            } else {
                clearTimeout(this._fadeAction);
                this._fadeAction = null;
                for(var i in this._watchers){
                    try {
                        this._watchers[i].notify(this);
                    } catch(e) {
//                        alert("Wrong watcher interface\n I'm " + this.obj.id);
                    }
                }
            }
            
        },
        
        _setObj: function (obj) {

            /*
             *  Try to detect opacity set by CSS
             */

            //Check If IE
            if(obj.filters) {
            
                //check If alpha is set by CSS
                if(obj.filters.alpha) { this.currentOpacity = obj.filters.alpha.opacity; } 
                else { this.currentOpacity = 100; }
                
            } 
            
            //Check for FF
            else if (window.getComputedStyle) 
            {
            
                //Get Mozilla opacity set in css (if any)
                var MozOpacity = document.defaultView.getComputedStyle(obj,null).getPropertyValue("-moz-opacity");
                if(MozOpacity.toString().length) { this.currentOpacity = parseInt(MozOpacity) * 100; }
                else { 
                    var CSS3Opacity = document.defaultView.getComputedStyle(obj,null).getPropertyValue("opacity");
                    if(CSS3Opacity.toString().length) { this.currentOpacity = parseInt(CSS3Opacity) * 100; }
                    else { this.currentOpacity = 0; }
                }
                
            } else {
                alert('Not compatible browser');
            }
            
            this.obj = obj;
        },
        
        addWatcher: function (objWatcher) {
            this._watchers.push(objWatcher);
        }
    };

    fader._setObj(obj);
    obj.fader = fader;
    return;
}

function preload(images) {
	for (var a in images) {
		var p_image = new Image();
		p_image.src = base_url + images[a];
		collection[a] = p_image;
	}
}
var collection = new Array();
var next = 0;
var target;
var obs = {
	repeat: true,
	notify: function(fader){
		if (document.getElementById('main'))
			var obj = document.getElementById('main'); 
		else if (document.getElementById('image' + (next - 2)))
			var obj = document.getElementById('image' + (next - 2));
		obj.parentNode.removeChild(obj);
	}
}
	


function load(image) {
	if (!collection[image]) return false;
	if (!target) target = document.getElementById('main').parentNode;
	var img = document.createElement('img');
	img.onload = function() {
		this.style.display='none';
		var width = this.width;
		var height = this.height;
		var this_top ;

		if (height > max_height || width > max_width) {
			if (height / max_height < width / max_width) {
				height = height * max_width / width;
				width = max_width;
			} else {
				width = width * max_height / height;
				height = max_height;
			}
		}

		if (top + ((max_height - height) / 2) > 25 && top + ((max_height - height) / 2) < 64)  this_top = 64;
		else this_top = top + ((max_height - height) / 2);

		this_top = parseInt(this_top);

		this.style.height = '0' + height + 'px';
		this.style.width = '0' + width + 'px';
		this.style.top = '0' + this_top + 'px';
		this.style.left = '0' + (left + ((max_width - width) / 2)) + 'px';
		this.style.display='block';
		if (document.getElementById('description')) {
			document.getElementById('description').style.display = 'block';
			document.getElementById('description').style.top= (height + top + 69) + 'px';
			document.getElementById('description').style.left= (left) + 'px';
//			document.getElementById('description').style.top = (height + top + 10) + 'px';
		}
	}
	img.src = collection[image].src;
/*	img.style.position = 'absolute';
	img.style.left = '230px';
	img.style.top = '121px';
	img.style.width = '527px';
	img.style.height = '357px';
	img.style.border = '1px solid #3CDDFF';*/
	img.className = 'main';
	img.id = "image" + next++;
	
	if (window.text_collection && text_collection[image] && document.getElementById('description')) {
		document.getElementById('description').style.display = 'none';
		document.getElementById('description').innerHTML = text_collection[image];
	}

	target.appendChild(img)
	setFader(img);
	img.fader.setOpacity(0);
	img.fader.fadeIn();
	img.fader.onclick = function () { return false; }
	img.fader.addWatcher(obs);
	if (document.getElementById('main'))
		document.getElementById('main').fader.fadeOut();
	else if (document.getElementById('image' + (next - 2)))
		document.getElementById('image' + (next - 2)).fader.fadeOut();
}