/*
*   Author: Corné Harmsen <corne@octavalent.nl>
*   WWW: http://www.octavalent.nl
*   
*   CONSTRUCTOR:
*       image:      image id to be slide
*       path:       shortcut path for the images (optional)
*       transtime:  time in milliseconds to transform to the next image (optional, default 1000)
*       showtime:   time in milliseconds for showing the image (optional, default 3000)
*
*   ADD:
*       src:        path of the image
*       title:      title of the image (optional)
*       url:        link by clicking the image (optional)
*
*   USAGE:
*   in the HEADER of the HTML:
*   <script type="text/javascript" src="slider.js"></script>
*   <script type="text/javascript">
*       //declare the slider
*		var s = new slider({ image: 'header', path: 'images/', transtime: 1000, showtime: 5000 });
*
*		// add images
*		s.Add('foulkeslaan.png', 'Home', '/index.html');
*		s.Add('overmij2.png', 'Over mij', '/overmij.html');
*		s.Add('gasten.png', 'Reacties', '/reacties.php');
*		s.Add('verhalen.png', 'Verhalen', '/verhalen.html');
*		s.Add('denkenzet.png', 'Schaken', '/schaken.html');
*		s.Add('tjippy.png', 'Zeilen', '/zeilen.html');
*		s.Add('zwemmen.png', 'Zwemmen', '/zwemmen.html');
*
*       // create the slider
*		s.Create();
*	</script>

    <body>
        <div style="position:relative;"><img /></div>
*/
function slider(params) { 
    this.id = params["image"] ? params["image"] : null;
    this.path = params["path"] ? params["path"] : '';
    this.delay = params["transtime"] ? params["transtime"] : 1000;
    this.visible = params["showtime"] ? params["showtime"] : 3000;
    this.handler = params["handler"] ? params["handler"] : null;
    this.height  = params["height"] ? params["height"] : 800;
    this.width  = params["width"] ? params["width"] : 167;

	this.helper = false;
	this.url = false;
	this.opacity = 0;
	this.count = 0;
	this.index = 0;
	this.images = new Array();
	this.key = slider.register.length;
	slider.register[this.key] = this;
	this.gRef = "Slider_" + this.key; 
	eval(this.gRef + "=this");	
	
	if(this.handler != null)
	    sendAJAX3(this.handler, '', this.gRef + '.Load(httpRequest.responseText)');
}
slider.register = new Array();
slider.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
slider.prototype = {
	Add : function(img, description, height, width, url) {
	    if(!height) height = this.height;
	    if(!width) width = this.width;
		this.images[this.count] = new Object();
		this.images[this.count].img = new Image;
		this.images[this.count].img.src = this.path + img;
		this.images[this.count].img.onload = new Function(this.gRef + '.ok(' + this.count + ');');
		this.images[this.count].img.width = width;
		this.images[this.count].img.height = height;
		this.images[this.count].desc = description;
		this.images[this.count].url = url;
		this.count++;
	},
	Load : function(response) {
	    var data = JSON.parse(response);
	    var images = data.images;
	    for(var i=0; i<images.length; i++) {
	        this.Add(images[i].path + images[i].image, images[i].title, images[i].height, images[i].width); 
	    }
	    this.Create();
	},
	Create : function() {
		var el = document.getElementById(this.id);
		if(!el) { setTimeout(this.gRef + '.Create()', 100); return; }
		this.image = el;
		this.link = el.parentNode;
		if(this.link.tagName == 'A')
		    this.url = true;
		this.index = 0;
		setTimeout(this.gRef + '.Show()', 0);
	},
	Show : function() {
		if(this.helper == false) {
			var bg = document.getElementById('imgBlender');
			if(!bg) {
				bg = document.createElement('img');
				bg.id = 'imgBlender';
				bg.style.position = 'absolute';
				bg.style.left = 0;
				if(slider.is_ie){bg.style.filter="alpha(opacity=0)";}else{bg.style.opacity=0;}
				this.image.parentNode.appendChild(bg);
			}
			this.helper = bg;
		}
		// start blender
		if(this.opacity == 0) {
			this.helper.src = this.images[this.index].img.src;
			this.helper.width = this.images[this.index].img.width;
			this.helper.height = this.images[this.index].img.height;
		}
		
		if(this.opacity < 100) {
			this.opacity += 5;
			if(slider.is_ie) {
				this.image.style.filter = "alpha(opacity=" + 100 - this.opacity + ")";
				this.helper.style.filter = "alpha(opacity=" + this.opacity + ")";
			} else {
				this.image.style.opacity = (100 - this.opacity) / 100;
				this.helper.style.opacity = this.opacity / 100;
			}
			setTimeout(this.gRef + '.Show()', this.delay / 20);
		} else {
			this.opacity = 0;
			this.image.src = this.images[this.index].img.src;
			this.image.title = this.images[this.index].desc;
			this.image.alt = this.images[this.index].desc;
			if(this.url) {
			    this.link.href = this.images[this.index].url;
			    this.link.title = this.images[this.index].desc;
			} 
			this.index++; if(this.index >= this.count) this.index = 0;

			if(slider.is_ie) {
				this.image.style.filter = "alpha(opacity=100)";
				this.helper.style.filter = "alpha(opacity=0)";
			} else {
				this.image.style.opacity = 1;
				this.helper.style.opacity = 0;
			}
			setTimeout(this.gRef + '.Show()', this.visible);
		}
	},
	ok : function(index) { 
		this.images[index].loaded = 'ok';
	}
};
