function get_id(id) {
	if (document.getElementById)
		return document.getElementById(id);
	else
		return false;
}

function preloadImages() {
	var d=document;
	if(d.images){
		if(!d.pics) d.pics=new Array();
		var i , j=d.pics.length, a=preloadImages.arguments;
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0) {
			d.pics[j]=new Image;
			d.pics[j++].src=a[i];
		}
	}
}

function set_opacity(obj, opacity) {
	opacity = (opacity == 100) ? 99.999 : opacity;
	obj.style.filter = "alpha(opacity: " + opacity + ")";  //Internet Explorer
	obj.style.KHTMLOpacity = opacity/100;                  //Safari 1.2<, Konqueror
	obj.style.MozOpacity = opacity/100;                    //Mozilla & Firefox (older)
	obj.style.opacity = opacity/100;                       //Mozilla & Firefox & CSS3
}

function img_gallery(images_in, btns_in, names_in, name_id_in) {
	this.index = 0;
	this.images = images_in;
	this.btns = btns_in;
	this.names = names_in;
	this.name_id = name_id_in;
	for (var i = 0; i< this.images.length; i++)
		preloadImages(this.images[i]);
	this.fade_img = function (index_in) {
		if(!window.lock) {
			window.lock = true;
			for(var i = 0; i < this.btns.length; i++)
				get_id(this.btns[i]).className = "thumb_button";
			get_id(this.btns[index_in]).className = "thumb_button_sel";
			get_id(this.name_id).innerHTML = this.names[index_in];
			set_opacity(get_id(this.images[index_in]), 0);
			get_id(this.images[index_in]).style.display = "block";
			get_id(this.images[index_in]).style.zIndex = "100";
			fade(this.images[index_in], 0, 1);
			for(var i = 0; i < this.images.length; i++) {
				if(i != index_in) {
					get_id(this.images[i]).style.display = "none";
					get_id(this.images[i]).style.zIndex = "1";
				}
			}
		}
	}
}

function fade(obj_id, opacity, direction) {
	if (document.getElementById) {
		var obj = get_id(obj_id);
		if (opacity <= 100 && direction == 1) {
			set_opacity(obj, opacity);
			opacity += 5;
			window.setTimeout("fade('"+obj_id+"',"+opacity+",1)", 35);
		}
		else if (opacity >= 0 && direction == 0) {
			set_opacity(obj, opacity);
			opacity -= 5;
			window.setTimeout("fade('"+obj_id+"',"+opacity+",0)", 35);
		}
		else {
			window.lock = false;
		}
	}
}

function img_list(images_in) {
	this.images = images_in;
	this.index = 0;
	this.get_next = function () {
		if(this.index < this.images.length - 1) {this.index++;}
		else {this.index = 0;}
		return this.images[this.index];
	};
	this.get_index = function (index_in) {
		this.index = index_in;
		return this.images[this.index];
	};
	this.get_curr = function () {return this.images[this.index];};
}

function img_autocycle(images_in, div_outer_id, div_inner_id) {
		var imgs = new img_list(images_in);
		for (var i = 0; i< imgs.images.length; i++)
			preloadImages(imgs.images[i]);
		var div_outer = get_id(div_outer_id);
		set_opacity(div_outer, 0);
		div_outer.style.visibility = 'visible';
		div_outer.style.background = "url("+imgs.get_curr()+")";
		fade(div_outer_id, 0, 1);
		if(imgs.images.length > 1) {
			var div_inner = document.getElementById(div_inner_id);
			div_inner.style.background = "url("+imgs.get_curr()+")";
			div_inner.style.visibility = 'visible';
			window.setTimeout(function () {next_bg(div_inner_id, div_outer_id, imgs);}, 3000);
		}
}

function next_bg(div_inner_id, div_outer_id, imgs) {
	get_id(div_outer_id).style.background = "url("+imgs.get_next()+")";
	fade_out(div_inner_id, div_outer_id, 100, imgs);
}

function fade_out(div_inner_id, div_outer_id, opacity, imgs){
	var div_inner = get_id(div_inner_id);
	if (opacity >= 0) {
		set_opacity(div_inner, opacity);
		opacity -= 5;
		window.setTimeout(function () {fade_out(div_inner_id, div_outer_id, opacity, imgs);}, 20);
	}
	else {
		div_inner.style.background = "url("+imgs.get_curr()+")";
		set_opacity(div_inner, 100);
		window.setTimeout(function () {next_bg(div_inner_id, div_outer_id, imgs);}, 3000);
	}
}

function cap_list(captions_in) {
	this.captions = captions_in;
	this.index = 0;
	this.get_next = function () {
		if(this.index < this.captions.length - 1) {this.index++;}
		else {this.index = 0;}
		return this.captions[this.index];
	};
	this.get_index = function (index_in) {
		this.index = index_in;
		return this.captions[this.index];
	};
	this.get_curr = function () {return this.captions[this.index];};
}

function img_autocycle_caption(images_in, div_outer_id, div_inner_id, captions_in, caption_id) {
		var imgs = new img_list(images_in);
		var caps = new cap_list(captions_in);
		for (var i = 0; i< imgs.images.length; i++)
			preloadImages(imgs.images[i]);
		var div_outer = get_id(div_outer_id);
		set_opacity(div_outer, 0);
		div_outer.style.visibility = 'visible';
		div_outer.style.background = "url("+imgs.get_curr()+")";
		fade(div_outer_id, 0, 1);
		if(imgs.images.length > 1) {
			var div_inner = document.getElementById(div_inner_id);
			div_inner.style.background = "url("+imgs.get_curr()+")";
			div_inner.style.visibility = 'visible';
			window.setTimeout(function () {next_bg_caption(div_inner_id, div_outer_id, imgs, caps, caption_id);}, 3000);
		}
}

function next_bg_caption(div_inner_id, div_outer_id, imgs, caps, caption_id) {
	get_id(div_outer_id).style.background = "url("+imgs.get_next()+")";
	get_id(caption_id).innerHTML = caps.get_next();
	fade_out_caption(div_inner_id, div_outer_id, 100, imgs, caps, caption_id);
}

function fade_out_caption(div_inner_id, div_outer_id, opacity, imgs, caps, caption_id){
	var div_inner = get_id(div_inner_id);
	if (opacity >= 0) {
		set_opacity(div_inner, opacity);
		opacity -= 5;
		window.setTimeout(function () {fade_out_caption(div_inner_id, div_outer_id, opacity, imgs, caps, caption_id);}, 20);
	}
	else {
		div_inner.style.background = "url("+imgs.get_curr()+")";
		set_opacity(div_inner, 100);
		window.setTimeout(function () {next_bg_caption(div_inner_id, div_outer_id, imgs, caps, caption_id);}, 3000);
	}
}
