function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function log( t ) {
  getObj( 'log' ).innerHTML+= '<br/>' + t; 
}

function getHashValue( str ) {
  var hashv;
  if( ( str.match( /#(\w.*)/ ) != null ) )
    hashv = str.match( /#(\w.*)/ )[1];
  return hashv;  
}

function init() {
  get_calendar();
}

function get_calendar() {
  FL.responseHandler = 'show_calendar';
  FL.handle( 'retrieve', 'modules/site/showcalendar.php' ); 
}

function show_calendar( response ) {
  getObj( 'content' ).innerHTML = URLDecode( response );
  fixPNG();
}



function loadContent( id ) {
  FL.responseHandler = 'showContent';
  FL.responseType = 'text';
  FL.handle( 'retrieve', '/entry.php?mediaID=' + id + '&mapstate=' + mapstate ); 
  ml_a.zoomTo( id ); 
  /*
  if( isArray( slides.photos ) ) {
    for( i = 0; i < slides.photos.length; i++ ) {
      if( slides.photos[i]['mediaID'] == id ) {
        slides.click( i );  
      }
    }  
  }  
  */
  window.location.hash = id;
}

function showContent( resp ) {
  /*
  var d = getObj( 'content' );
  d.innerHTML = URLDecode( resp );
  */
}

function setCollectionTitle( str ) {
  var d = getObj( 'collectionTitle' );
  d.innerHTML = str;  
}

function readCookie(cname) {
    var cookieName = cname + '=';
    var cookies = document.cookie;
    var pos = cookies.indexOf(cookieName);

    if (pos != -1) {
        var start = pos + cookieName.length;
        var end = cookies.indexOf(';', start);

        if (end == -1) {
            end = cookies.length;
        }
        return unescape(cookies.substring(start, end));
    }
}

function setCookie(pName, pValue, expire_period) {
    var expireDate = new Date();
    expireDate.setTime( (new Date()).getTime() + expire_period );
    document.cookie = pName + '=' + escape(pValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}

function setFocus( obj ) {
  getObj( obj ).focus();
}

function catchKey( evt, func, params ) {
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt) {
    var charCode = (evt.charCode) ? evt.charCode :
                   ((evt.keyCode) ? evt.keyCode :
                   ((evt.which) ? evt.which : 0));
    if (charCode == 13) func(params);
  }  
}

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};
function getObj(object) {
  if(document.all) myObject=eval('document.all[object]');
  else if(document.layers) myObject=eval('document.layers[object]');
  else if(document.getElementById && !document.all) myObject=eval('document.getElementById(object)');
  else return;
  return myObject;
}
function isFunction(a) { return typeof a=='function'; }
function isObject(a) { return a&&typeof a=='object'||isFunction(a); }
function isArray(a) { return isObject(a)&&a.constructor==Array; }
function isUndefined(a) { return typeof a=='undefined'; }
function isDefined(a) { return !isUndefined(a); }
function isString(a) { return typeof a=='string'; }
function isDigit(num) { if (num.length>1) return false; var string="1234567890"; return string.indexOf(num)!=-1; }
function isNumber(o) {return 'number' == typeof o && isFinite(o);}

function fixPNG() {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])
  
  if ((version >= 5.5) && (document.body.filters)) 
  {
     for(var i=0; i<document.images.length; i++)
     {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
        
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML

         i = i-1
      }
    }
  }
  
}
function getStyle(x,styleProp) {
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function shrinkTo( id, w, h ) {
  var elm = getObj( id );
  if( isObject( elm ) ) {
    var hide = false;
    
    var rw = w;
    if( w == 0 ) {
      rw = 1;
      hide = true;
    }
    var rh = h;
    if( h == 0 ) {
      rh = 1;
      hide = true;
    }    
    
    var cw = parseInt( getStyle( elm, 'width' ).substring( 0, getStyle( elm, 'width' ).indexOf( 'p' ) ) );
    var ch = parseInt( getStyle( elm, 'height' ).substring( 0, getStyle( elm, 'height' ).indexOf( 'p' ) ) );
    var a= false;
    if( rw > 0 && cw > rw ) {
      neww = cw - 10;
      if( neww < rw ) neww = rw;
      elm.style.width = neww + 'px';
      a = true;
    }
    if( rh > 0 && ch > rh ) {
      newh = ch - 10;
      if( newh < rh ) newh = rh;
      elm.style.height = newh + 'px';
      a = true;
    }
    if( a ) window.setTimeout( 'shrinkTo( "' + id + '", "' + w + '", "' + h + '" )', 0.5 );
    else if( hide ) {
      elm.style.visibility = 'hidden';
    }
  }  
}
function growTo( id, w, h ) {
  var elm = getObj( id );
  if( elm.style.visibility != 'visible' )
    elm.style.visibility = 'visible';
  
  var cw = parseInt( getStyle( elm, 'width' ).substring( 0, getStyle( elm, 'width' ).indexOf( 'p' ) ) );
  var ch = parseInt( getStyle( elm, 'height' ).substring( 0, getStyle( elm, 'height' ).indexOf( 'p' ) ) );
  var a = false;
  if( w > 0 && cw < w ) {
    neww = cw + 10;
    if( neww > w ) neww = w;
    elm.style.width = neww + 'px';
    a = true;
  }
  if( h > 0 && ch < h ) {
    newh = ch + 10;
    if( newh > h ) newh = h;
    elm.style.height = newh + 'px'; 
    a = true; 
  }
  
  if( a ) window.setTimeout( 'growTo( "' + id + '", "' + w + '", "' + h + '" )', 0.5 );  
}

function loadMap1() {
  ml_a.name = 'ml_a';
  ml_a.mapdiv_id = 'mapdiv';
  ml_a.init();
  ml_a.setupMarkers();
  
  GEvent.addListener( ml_a.map, 'move', move_a );
  GEvent.addListener( ml_a.map, 'moveend', endmove_a );
}

function loadMap2() {
  ml_b.name = 'ml_b';
  ml_b.mapdiv_id = 'mapdiv_big';
  ml_b.init();
  ml_b.setupMarkers();

  GEvent.addListener( ml_b.map, 'move', move_b );
  GEvent.addListener( ml_b.map, 'moveend', endmove_b );

}

function move_a() {
  ml_a.moving = true;
  if( !ml_b.moving ) {
    ml_b.map.setCenter( ml_a.map.getCenter() );
    ml_b.map.setZoom( ml_a.map.getZoom() );
  }
}

function endmove_a() {
  ml_a.moving = false;
}

function move_b() {
  ml_b.moving = true;
  if( !ml_a.moving ) {
    ml_a.map.setCenter( ml_b.map.getCenter() );
    ml_a.map.setZoom( ml_b.map.getZoom() );
  }
}
function endmove_b() {
  ml_b.moving = false;  
}
            
var map_visible = 'a';
 
function switchmap() {
  var ms = getObj( 'mapswitcher' );
  
  if( map_visible == 'a' ) {
    growTo( 'bigmap-container', 621, 220 );
    growTo( 'mapdiv_big', 621, 220 );
    
    shrinkTo( 'smallmap-container', 0, 0 );
    shrinkTo( 'mapdiv', 0, 0 );   
    
    map_visible = 'b';
    
    ms.innerHTML = 'Minder kaart';
  } else if( map_visible == 'b' ) {
    
    growTo( 'smallmap-container', 250, 150 );
    growTo( 'mapdiv', 250, 150 );
    
    shrinkTo( 'mapdiv_big', 0, 0 );
    shrinkTo( 'bigmap-container', 0, 0 );
    
    map_visible = 'a';          
    
    ms.innerHTML = 'Meer kaart';
  }
}

function moremap() {
  //change classnames of content and map, set content-img width
  var smc = getObj( 'smallmap-container' );
  var bmc = getObj( 'bigmap-container' );
  bmc.innerHTML = smc.innerHTML;
  smc.innerHTML = '';
  
  
  shrinkTo( 'smallmap-container', 1, 1 );
  bmc.style.visibility = 'visible';
  growTo( 'bigmap-container', 621, 220 );
  growTo( 'mapdiv', 621, 220 );
  
  ml_s.map.checkResize();
  
  var a = getObj( 'mapswitcher' );
  a.href = "javascript:lessmap();";
  a.innerHTML = 'Minder kaart';
  
  //ml.map.removeControl( ml.map.mapctrl );
  //ml.map.mapctrl = new GLargeMapControl();
  //ml.map.addControl( ml.map.mapctrl );
  mapstate = 'large';
}
function lessmap() {
  //change classnames of content and map, set content-img width
  var smc = getObj( 'smallmap-container' );
  var bmc = getObj( 'bigmap-container' );
  smc.innerHTML = bmc.innerHTML;
  bmc.innerHTML = '';
    
  growTo( 'smallmap-container', 250, 150 );
  shrinkTo( 'bigmap-container', 1, 1 );
  shrinkTo( 'mapdiv', 250, 150 );
  
  bmc.style.visibility = 'hidden';    
    
  var a = getObj( 'mapswitcher' );
  a.href = "javascript:moremap();";
  a.innerHTML = 'More map!';
  
  mapstate = 'normal';
}