currentObj=0;

function js_openCloseDiv(obj) {
	if (obj.tagName!='DIV') obj=obj.parentNode;
	min=obj.getAttribute('min');
	max=obj.getAttribute('max');
	//alert(obj.getElementsByTagName('img')[0].style.top)
	if (obj.getAttribute('fading')!='1' && obj.getAttribute('fading')!='-1') {
		currentObj=obj;
		if (obj.style.height==''+max+'px') {		// will close the div
			obj.setAttribute('to',min);
			obj.setAttribute('current',max);
			obj.setAttribute('fading','-1');
		} else {									// will open the div
			obj.setAttribute('to',max);
			obj.setAttribute('current',min);
			obj.setAttribute('fading','1');
			//obj.getElementsByTagName('img')[0].style.top='0px';
		}
		js_startOpenClose();
	}
}

function js_startOpenClose() {
	if (obj=currentObj) {
		to=parseInt(obj.getAttribute('to'));
		current=parseInt(obj.getAttribute('current'));
		if (obj.getElementsByTagName('img')[0].style.top!='') styletop=parseInt(obj.getElementsByTagName('img')[0].style.top); else styletop=0;
		if (obj.getAttribute('moveUp')) moveUp=-parseInt(obj.getAttribute('moveUp')); else moveUp=0;
		if (obj.getAttribute('fading')=='-1' && styletop>moveUp) {
			styletop-=10;
			obj.getElementsByTagName('img')[0].style.top=styletop+"px";
		}
		if (obj.getAttribute('fading')=='1' && styletop<0) {
			styletop+=10;
			obj.getElementsByTagName('img')[0].style.top=styletop+"px";
		}
		//alert('to='+to+',current='+current)
		if (Math.abs(current-to)>2) {
			current=parseInt((to-current)/20+current);
			if (current<to) current+=4; else current-=4;
			obj.setAttribute('current',current);
			obj.style.height=''+current+'px';
			setTimeout("js_startOpenClose()",10);
		} else {
			obj.style.height=''+to+'px';
			//If needed, move up the image 
			if (obj.getAttribute('fading')=='-1') obj.getElementsByTagName('img')[0].style.top=moveUp+"px";
			if (obj.getAttribute('fading')=='1') obj.getElementsByTagName('img')[0].style.top="0px";
			obj.setAttribute('fading',0);
		}
	}
}

function js_slideShow(obj) {
	src=obj.src.replace("thb", "img");
	div=document.getElementById('bmImages');
	if (obj.getAttribute('moveUp')) div.setAttribute('moveUp',obj.getAttribute('moveUp')); else div.setAttribute('moveUp','0');
	div.getElementsByTagName('img')[1].src=src;
	js_slideShowFading();
	if (div.style.height!=div.getAttribute('max')+'px') js_openCloseDiv(div);
	//if (obj.getAttribute('alt')!='') div.getElementsByTagName('span')[0].innerHTML=obj.getAttribute('alt');
	if (obj.getAttribute('alt')!='') div.setAttribute('legend',obj.getAttribute('alt'));
	div.getElementsByTagName('span')[0].innerHTML="";
	js_startSlideShowLegend();
}

function js_slideShowNextPrev(direction) {
	src=document.getElementById('bmImages').getElementsByTagName('img')[0].src.replace("img", "thb");
	imgs=document.getElementById('bmThumbnails').getElementsByTagName('img');
	for (i=0;i<imgs.length;i++) {
		if (src==imgs[i].src) {
			next=(i+direction) % imgs.length;
			i=imgs.length;		//go out of the loop
			js_slideShow(imgs[next])
			/*
			src=imgs[next].src.replace("thb", "img");
			if (imgs[next].getAttribute('moveUp')) document.getElementById('bmImages').setAttribute('moveUp',obj.getAttribute('moveUp')); else document.getElementById('bmImages').setAttribute('moveUp','0');
			document.getElementById('bmImages').getElementsByTagName('img')[1].src=src;
			js_slideShowFading();
			*/
		}
	}
}

function js_slideShowFading() {
	var div=document.getElementById('bmImages');
	var img0=div.getElementsByTagName('img')[0];
	var img1=div.getElementsByTagName('img')[1];
	if (!img1.complete) {			//Check that the image is loaded, if not, wait...
		setTimeout("js_slideShowFading()",20);
	} else {
		opac=parseInt(img1.getAttribute('opac'))		// get the current opacty and if <100, change it and recall, else stop and show only the 1st image
		if (opac<100) {
			opac+=5;
			img1.setAttribute('opac',opac);
			js_changeOpac(img1,opac);
			setTimeout("js_slideShowFading()",10);
		} else if (opac<105) {
			img0.src=div.getElementsByTagName('img')[1].src;
			opac+=5;
			img1.setAttribute('opac',opac);
			setTimeout("js_slideShowFading()",500);		//for some reason, need to set opac attribute after a short time
		} else {
			img1.setAttribute('opac',0);
			js_changeOpac(img1,0);
		}
	}
}

function js_startSlideShowLegend() {
	var div=document.getElementById('bmImages');
	var span=div.getElementsByTagName('span')[0];
	var legend=div.getAttribute('legend');
	if (span.innerHTML.length<legend.length) {
		span.innerHTML=legend.substr(0,span.innerHTML.length+1);
		setTimeout("js_startSlideShowLegend()",10);
	}
}

function js_changeOpac(obj,opacity)  {  
	obj.style.opacity = (opacity / 100);  
	obj.style.MozOpacity = (opacity / 100);  
	obj.style.KhtmlOpacity = (opacity / 100);  
	obj.style.filter = "alpha(opacity=" + opacity + ")";
	//document.getElementById('bmAnnex').innerHTML=opacity
}  

/*-------- ajax function ---------*/

function is_debug() {
	return true;
}

function ajax_functions(action,obj,args){
	var xmlHttp;
	try {  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {  // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			result=xmlHttp.responseText;
			if (is_debug()) alert("result:"+result);
			switch (action) {
				case "dataset":
				case "dataset_block":
				case "dataset_data":
					obj.innerHTML=result;
					break;
				case "fileprogress":
					alert(result);
					break;
				default:
			}
		}		
	}

	url="?ajax&rnd="+new Date().getTime()+"&action="+action+"&"+args;
	if (is_debug()) alert("url:"+url);
	switch (action) {
		case "example_post":
			params="sql="+args;
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(params);
			break;
		case "login_post":
			l=obj.parentNode.getElementsByTagName('input')[0].value;
			p=obj.parentNode.getElementsByTagName('input')[1].value;
			params="l="+escape(l)+"&p="+escape(p)
			alert(params)
			url="http://www.brillantmont.ch/gateway.asp"
			params=args;
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(params);
			break;
		case "dataset_data":
		case "dataset":
			url+="&dataset="+document.getElementById('dataset').getAttribute('dataset')+"&ref="+document.getElementById('dataset').getAttribute('ref')
			if (is_debug()) alert("url:"+url);
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
			break;
		default:
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
			break;
	}
	return false;
}
