var ns = (document.layers ? true : false);
var ie = (document.all ? true : false);
var navig = navigator.userAgent;
var moz = (navig.indexOf("Gecko")>=0?true:false);

var FadingIn = false;
var FadingOut = false;

var currentExpoDetails;

function show(divName, isShown) {
    var elem = document.getElementById(divName);
    if (isShown) elem.style.display="block";
    else elem.style.display="none";
}

function isDivShown(divName) {
	return document.getElementById(divName).style.display!="none";
}

function showFaded(divName, isShown) {
    if (!FadingIn &&
	    isShown &&
		(getopacity(divName)<1 || !isDivShown(divName))) {
		FadingIn = true;
		setopacity(divName, 0);
		show(divName, true);
		FadeIn(divName, 7, 200,
		       function() {FadingIn = false});
	} else if (!FadingOut &&
	           !isShown &&
			   getopacity(divName)>0 &&
			   isDivShown(divName)) {
		FadingOut = true;
		FadeOut(divName, 7, 200,
		        function() {show(divName, false);
				            setopacity(divName, 1);
							FadingOut = false});
	}
}

function setopacity(id, opacity)
{
	var el=document.getElementById(id); 
	if (ie) {
		el.filters.alpha.opacity=opacity*100;
	} else if (ns||moz) {
		el.style.MozOpacity=opacity;
	}	
}

function getopacity(id)
{
	var el=document.getElementById(id); 
	if (ie && el.filters.alpha) {
		return el.filters.alpha.opacity/100;
	} else if (ns||moz) {
		return el.style.MozOpacity;
	} else return 1;	
}

function FadeIn(id, steps, time, onceDoneFunction) 
{
	if (FadingOut) return onceDoneFunction();
	
	var el=document.getElementById(id); 
	var inc = 100/steps;
	if(ie) { 
		if (el.filters.alpha.opacity < 100)  { 
			el.filters.alpha.opacity=el.filters.alpha.opacity+inc; 
			//setTimeout("FadeIn('"+id+"',"+steps+","+time+")",time/steps); 
			setTimeout(function() {FadeIn(id,steps,time,onceDoneFunction)},time/steps);
			return; 
		} else {
		    if (onceDoneFunction) return onceDoneFunction();
	    }
	} 
	else if (moz||ns) { 
		if (el.style.MozOpacity < 1) { 
			el.style.MozOpacity=parseFloat(el.style.MozOpacity)+inc/100; 
			//setTimeout("FadeIn('"+id+"',"+steps+","+time+")",time/steps); 
			setTimeout(function() {FadeIn(id,steps,time,onceDoneFunction)},time/steps);
			return; 
		} else {
		    if (onceDoneFunction) return onceDoneFunction();
	    }
	} 
}

function FadeOut(id, steps, time, onceDoneFunction) 
{
	var el=document.getElementById(id); 
	var inc = 100/steps;
	if(ie) { 
		if (el.filters.alpha.opacity >0)  { 
			el.filters.alpha.opacity=el.filters.alpha.opacity-inc; 
			//setTimeout("FadeOut('"+id+"',"+steps+","+time+")",time/steps); 
			setTimeout(function() {FadeOut(id,steps,time,onceDoneFunction)},time/steps); 
			return; 
		} else {
		    if (onceDoneFunction) onceDoneFunction();
	    }
	} 
	else if (moz||ns) { 
		if (el.style.MozOpacity > 0) { 
			el.style.MozOpacity=parseFloat(el.style.MozOpacity)-inc/100; 
			//setTimeout("FadeOut('"+id+"',"+steps+","+time+")",time/steps); 
			setTimeout(function() {FadeOut(id,steps,time,onceDoneFunction)},time/steps);
			return; 
		} else {
		    if (onceDoneFunction) onceDoneFunction();
	    }
	} 
}

function registerEventHandler(id, eventName, handler)
{
    var obj=document.getElementById(id); 
    if (ie) obj.attachEvent('on'+eventName, handler);
    else obj.addEventListener(eventName, handler,false);
}

function overloadFunc(oldfunc, newfunc)
{
    if (oldfunc) return function() {oldfunc(); newfunc();};
    else return newfunc;
}


// ----------------------------
// Paintings specific functions
// ----------------------------

function addPainting(idx, imgSrc, imgTitle, imgWidth, imgHeight)
{ 
	document.paintingsArray[idx] = new Painting(imgSrc, imgTitle, imgWidth, imgHeight);
}

function Painting(imgSrc, imgTitle, imgWidth, imgHeight)
{
	this.src=imgSrc;
	this.title=imgTitle;
	this.width=imgWidth;
	this.height=imgHeight;
}

function switchPaintingTo(paintNb)
{
	p = document.paintingsArray[paintNb];
	document.getElementById('Painting').src=p.src;
	setSpanText('PaintTitle', p.title);
	setSpanText('PaintSize', '[ '+p.width+'x'+p.height+' ]');
}

function setSpanText(spanId, text)
{
	span_el = document.getElementById(spanId);
	if (ie) {
		span_el.innerText=text;
	} else {
		var new_txt = document.createTextNode(text);
		span_el.replaceChild(new_txt, span_el.childNodes[0]);
	}
}

function active(id)
{
    document.getElementById(id).style.backgroundImage="url(images/"+id+"_on.gif)";
}

function passive(id)
{
    document.getElementById(id).style.backgroundImage="url(images/"+id+"_off.gif)";
}

function showTitle(sectionId, lang)
{
	document.getElementById('titleImage').src="data/"+lang+"/"+sectionId+"_title.jpg";
	document.getElementById('smallTitleImage').src="data/"+lang+"/"+sectionId+"_title_small.jpg";
}

function showDetails(expoId)
{
	var curDiv = document.getElementById(currentExpoDetails+'_details');
	if (curDiv) {
		//document.getElementById(currentExpoDetails).style.textIndent='0px';
		document.getElementById(currentExpoDetails+'_arrow').style.display='none';
		curDiv.style.display='none';
	}
	currentExpoDetails = expoId;
	//document.getElementById(expoId).style.textIndent='10px';
	document.getElementById(expoId+'_details').style.display='block';
	document.getElementById(expoId+'_arrow').style.display='inline';
}