// ------------------------ 10util.js starts here -----------------------------
/*              Utility functions                    */


function addEvent(obj, eventType, fn){
  /* adds an eventListener for browsers which support it
     Written by Scott Andrew: nice one, Scott */

  if( eventType === "load" ) {
  	//hack me
  	loadEventList.addLoadEvent(fn);
  	return true;
  }

  if (obj.addEventListener){
    obj.addEventListener(eventType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+eventType, fn);
    return r;
  } else {
	return false;
  }
}

var loadEventList = [];
loadEventList.addLoadEvent = function( fn ) {
	loadEventList[ loadEventList.length ] = fn;
}

loadEventList.fireLoadEvents = function() {
	for(var i=0; i<loadEventList.length; i++) {
		loadEventList[i]();
	}
}

/* the following is a hack to replicate DOMContentLoaded in browsers
   other than Firefox.  It is basically copied from
   http://dean.edwards.name/weblog/2006/06/again/
*/
if(/WebKit/i.test(navigator.userAgent)) { // Safari
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			loadEventList.fireLoadEvents(); // call the onload handler
		}
	}, 100);
} else if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", loadEventList.fireLoadEvents, false);
} else {
  // IE HACK
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id='__ie_onload' defer='defer' src='javascript:void(0)'><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
	if (this.readyState == "complete") {
		loadEventList.fireLoadEvents(); // call the onload handler
	}
};
/*@end @*/
}


// ------------------------- 10util.js ends here ------------------------------

// ------------------------ caption.js starts here ----------------------------
addEvent(window, "load", caption);

function caption ()  {
	var imageList=document.getElementsByTagName('li');


	for(var i=0; i<imageList.length; i++){

		if(imageList[i].className.match(/\brollover\b/))  {
			imageList[i].onmouseover= function () { changeState(this, 'over') };
			imageList[i].onmouseout=  function () { changeState(this,'off') };
			imageList[i].onfocus= function () { changeState(this, 'over') };
			imageList[i].onblur=  function () { changeState(this, 'off') };
			}
		}
}

function changeState(element,state)  {
	var existingClassName=oldClassName(element,'static-state');

	if (element.className.match(/c-[0-9]+-c/)) {
		var timeOutHide=element.className.match(/[0-9]+/);
		timeOutHide=parseInt(timeOutHide);
		clearInterval(timeOutHide);
	}
	if(element.getElementsByTagName('div')[1].className.match('legende')) {
		var elementRef=element.getElementsByTagName('div')[1];
		var timeOutRef=setInterval(function (){ show(elementRef, state) }, 10);
		element.className=existingClassName+"c-"+timeOutRef+"-c";
	}
}



function show(element, state)  {
        var startPosition=-20;
        var endPosition=1;
        var rate=1;

        var captionPosition=parseInt(element.style.marginTop);
        if(isNaN(captionPosition)) {
                captionPosition=startPosition;
        }else if(state=='over'){
                captionPosition=captionPosition+rate;
        }else{
                captionPosition=captionPosition-rate;
        }

        if (captionPosition<endPosition && state=='over' || captionPosition>startPosition && state=='off') {
                element.style.marginTop=captionPosition+'em';
        }else{
                var timeOut=element.parentNode.className.match(/c-[0-9]+-c/);
                timeOut=(timeOut+'').match(/[0-9]+/);
                //alert(element.parentNode.className); alert(timeOut);
                var existingClassName=oldClassName(element.parentNode,'static-state');
                element.parentNode.className=existingClassName+"static-state";
                timeOut=parseInt(timeOut);
                if(!isNaN(timeOut)) clearInterval(timeOut);
        }


}





/*
	for later use?
*/
function oldClassName (element, modificationClass)  {
	var oldClass='';

	if (element.className) {
		oldClass=element.className;
	}else{
		return oldClass;
	}
	if (oldClass.match(/(\bmodificationClass\b)|(c-[0-9]+-c)/))  {
		var newClass=oldClass.replace(/(\bmodificationClass\b)|(c-[0-9]+-c)/, "")
		 newClass=newClass.replace(modificationClass, "")
	} else {
		var newClass=oldClass+" ";
	}

	return newClass;
}
