/*
Copyright (c) 2006 Dan Webb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.
*/
DomBuilder = {
  IE_TRANSLATIONS : {
    'class' : 'className',
    'for' : 'htmlFor'
  },
  ieAttrSet : function(a, i, el) {
    var trans;
    if (trans = this.IE_TRANSLATIONS[i]) el[trans] = a[i];
    else if (i == 'style') el.style.cssText = a[i];
    else if (i.match(/^on/)) el[i] = new Function(a[i]);
    else el.setAttribute(i, a[i]);
  },
	apply : function(o) { 
	  o = o || {};
		var els = ("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|code|" + 
					   "h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|" + 
					   "select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|acronym|" +
					   "script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|caption|" + 
					   "label|dfn|kbd|samp|var").split("|");
    var el, i=0;
		while (el = els[i++]) o[el.toUpperCase()] = DomBuilder.tagFunc(el);
		return o;
	},
	tagFunc : function(tag) {
	  return function() {
	    var a = arguments, at, ch; a.slice = [].slice; if (a.length>0) { 
	    if (a[0].nodeName || typeof a[0] == "string") ch = a; 
	    else { at = a[0]; ch = a.slice(1); } }
	    return DomBuilder.elem(tag, at, ch);
	  }
  },
	elem : function(e, a, c) {
		a = a || {}; c = c || [];
		var isIE = navigator.userAgent.match(/MSIE/)
		var el = document.createElement((isIE && a.name)?"<" + e + " name=" + a.name + ">":e);
		for (var i in a) {
		  if (typeof a[i] != 'function') {
		    if (isIE) this.ieAttrSet(a, i, el);
		    else el.setAttribute(i, a[i]);
		  }
	  }
		for (var i=0; i<c.length; i++) {
			if (typeof c[i] == 'string') c[i] = document.createTextNode(c[i]);
			el.appendChild(c[i]);
		} 
		return el;
	}
}

/* end of DOMBUILDER */

function turnGray(nom) {
	fle = document.images[nom].src;
	document.images[nom].src = fle.substring(0,(fle.indexOf("1.gif"))) + "0.gif";
}

function turnColored(nom) {
	fle = document.images[nom].src;
	document.images[nom].src = fle.substring(0,(fle.indexOf("0.gif"))) + "1.gif";
}

function gmap(elem, lat, lon, name, adress1, adress2, adress3, link, mapType){
      if (GBrowserIsCompatible()) {
      
      var html = DomBuilder.apply();

	var txt = html.DIV({style:'margin:0;padding:0;width:20em;height:8em;'}, 
	    html.P({style:'margin:0;padding:0;font-size:105%;text-ident:0;color:#019;'},name),	    
	    html.P({style:'margin:0;padding:0;font-size:95%'}, adress1+', '+adress2),
	    html.P({style:'margin:0;padding:0;font-size:95%;font-weight:bold;'},adress3),
	    html.P({style:'margin:0;padding:0;font-size:70%;text-align:center;'}, html.A({href:link, title:'View this map in google maps'},'Show me a bigger map'))
	);
	elem.className='loadedGmap';


        var map = new GMap2(elem);
	map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(lat,lon), 13, mapType);
	map.openInfoWindow(map.getCenter(),txt);
	return true;
      }
}

function showArticle(artid)
{
  var iWidth = 640;
  var iHeight = 480;
  var sTb;
  var iTop=(screen.height-iHeight)/2;
  var iLeft=(screen.width-iWidth)/2;
  sTb = "status=yes,scrollbars=yes,resizable=no,height=" + iHeight + ",width=" + iWidth + ",left=" + iLeft + ",top=" + iTop;
  window.open(artid, "Popup", toolbar=sTb);
}

function showPic (whichpic, imageContainerId) {
 if (document.getElementById) {
  document.getElementById(imageContainerId).src = whichpic.href;
  if (whichpic.title) {
   document.getElementById('desc').childNodes[0].nodeValue = whichpic.title;
  } else {
   document.getElementById('desc').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
  }
  return false;
 } else {
  return true;
 }
}
function switchClass(elem, from, to) {
var re = new RegExp (from, 'gi') ;
elem.className = elem.className.replace(re, to) ;
return elem;
}

foldImg = function() {
//remove underlying image
this.onclick=unfoldImg;
this.removeChild(this.getElementsByTagName('img').item(0));
switchClass(this, 'unfolded-img', 'folded-img');
return false;
}

unfoldImg=function () {
//function bound to links to folded images, activated on click
//inserts the image and blocks the link
//if js is disabled, this is a regular link to an image
 if (document.getElementById) {
 //create the new image and get the src from the href of the link
 newimg=document.createElement('img');
 newimg.src= this.href;
 //modify style and insert the image
 switchClass(this, 'folded-img', 'unfolded-img');

 //this.insertBefore(newimg, this.getElementsByTagName('p').item(0)); 
 this.insertBefore(newimg, this.childNodes[0]); 
 this.onclick=foldImg; 
  return false;
 } else {
  return true;
 }
}

loadFlash = function () {
window.open(this.href, "flash", "height=768, width=1024");
return false;
}

foldContent = function() {
//remove underlying image
this.onclick=unfoldContent;
switchClass(this, 'unfolded-content', 'folded-content');
return false;
}
unfoldContent=function () {
 if (document.getElementById) {
 //modify style and insert the image
 switchClass(this, 'folded-content', 'unfolded-content');
 this.onclick=foldContent;
  return false;
 } else {
  return true;
 }
}

startList = function() {
//Function hooked to the page load



//IE hack to make menus work
	if (document.all&&document.getElementById) {
	
		navRoot = document.getElementById("nav");
		if (navRoot){
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if ((node.nodeName=="LI")||(/folded-content/.test(node.className))) {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}	
		}
	}
	//add hooks to links on images
	links=document.getElementsByTagName('a');
	for (i=0; i<links.length; i++) {
		node = links.item(i);		
		if (/folded-img/.test(node.className)) {
			node.onclick=unfoldImg;
		}
	}
	//add hooks to folded content
	links=document.getElementsByTagName('div');
	for (i=0; i<links.length; i++) {
		node = links.item(i);		
		if (/folded-content/.test(node.className)) {
			node.onclick=unfoldContent;
		}
	}
	//add hook for flash movie chooser

	flashChooser = document.getElementById("flash-chooser");
	if (flashChooser){
	for (i=0; i<flashChooser.childNodes.length; i++) {
		sub = flashChooser.childNodes.item(i);						
		for (j=0; j<sub.childNodes.length; j++) {
			subsub = sub.childNodes.item(j);
			if (subsub.nodeName=="A"){ 
				subsub.onclick=loadFlash;
			}
		}		
	}
	}

	
	//get all links in div#sheet2
	//if they are flashes, make the window pop !
	sheet = document.getElementById("sheet2");
	
	if (sheet){

		links = sheet.getElementsByTagName ("A");
		
		for (i=0; i<links.length; i++) {
				
			link =  links.item(i);	
				
			if (/.*swf/.test(link.href)) {	
			link.onclick=loadFlash;
		}
		}
	}
	//add a js class to the body, used in the js-only CSS
	bodyElt = document.getElementsByTagName("body").item(0);
	bodyElt.className = bodyElt.className +" js";
}
window.onload=startList;
