/********************************************************
           (c)2006 http://www.godsmurf.com

   Feel free to take what you want, but credit me pls.
*********************************************************/


/***********
  VARIOUS
***********/

// getel(): shorthand for document.getElementById()
function getel(rid) {
  return document.getElementById(rid);
}

// flip(): open/close a div element (rid)
function flip(rid) {
  var element = getel(rid);
  element.style.display = (element.style.display == 'none') ? 'block' : 'none';
}

// identifyBrowser(): "ie" or "moz"
function identifyBrowser() {
  if(document.all) { return "ie"; }
  return "moz";
}


// findPosX(), findPosY(): find the browser coordinates of an object
//   (c) Peter-Paul Koch (www.quirksmode.org) & Alex Tingle (blog.firetree.net)
function findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) {
    while (1) {
      curleft+=obj.offsetLeft;
      if (!obj.offsetParent) {
        break;
      }
      obj=obj.offsetParent;
    }
  } else if (obj.x) {
    curleft+=obj.x;
  }
  return curleft;
}
function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (1) {
      curtop+=obj.offsetTop;
      if (!obj.offsetParent) {
        break;
      }
      obj=obj.offsetParent;
    }
  } else if (obj.y) {
    curtop+=obj.y;
  }
  return curtop;
}

// enkel getest in firefox, voor admin
function resizeTextarea(t) {
  if ( !t.initialRows ) t.initialRows = t.rows;

  a = t.value.split('\n');
  b=0;
  for (x=0; x < a.length; x++) {
    if (a[x].length >= t.cols) b+= Math.floor(a[x].length / t.cols);
  }
  b += a.length;

  if (b > t.rows || b < t.rows) t.rows = (b < t.initialRows ? t.initialRows : b);
}



/***********
  TOOLTIP
************/

/*
  based on the Tooltip-with-shadow script on www.dhtmlgoodies.com
    http://www.dhtmlgoodies.com/index.html?whichScript=tooltip_shadow
  requires the styles #gstooltip and #gstooltipShadow
*/

var gstooltip = false;
var gstooltipShadow = false;
var gstooltip_shadowSize = 6;
var gstooltipMaxWidth = 400;
var gstooltipMinWidth = 100;
var gstooltip_iframe = false;

// showTooltip(), moveTooltip(), hideTooltip()
//

function showTooltip(tooltipTxt) {
  // create tooltip if it doesn't exist yet
  if(!gstooltip){
    gstooltip = document.createElement('DIV');
    gstooltip.id = 'gstooltip';
    gstooltipShadow = document.createElement('DIV');
    gstooltipShadow.id = 'gstooltipShadow';
    document.body.appendChild(gstooltip);
    document.body.appendChild(gstooltipShadow);
    gstooltip.style.left = '10px';
//    gstooltip.style.top = posY + 'px';
  }

  // show tooltip
  gstooltip.style.display = 'block';
  gstooltipShadow.style.display = 'block';

  // fill tooltip
  gstooltip.style.width = null; // Reset style width if it's set
  gstooltip.innerHTML = tooltipTxt;

  // width between min and max
  if (gstooltip.offsetWidth < gstooltipMaxWidth)
    gstooltip.style.width = gstooltipMinWidth + 'px';
  else if (gstooltip.offsetWidth > gstooltipMaxWidth)
    gstooltip.style.width = gstooltipMaxWidth + 'px';

  // position it
  moveTooltip;
}


function moveTooltip(e) {
  var browserWidth = Math.max(document.body.clientWidth, document.documentElement.clientWidth) - 20;
  var posX = 0, posY = 0;   // tooltip position

  // get scrolltop
  var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  if (navigator.userAgent.toLowerCase().indexOf('safari')>=0) st=0;

  if (!e) e = window.event;

  // position
  posX = e.clientX + 10;
  posY = e.clientY + 10 + st;

  // keep tooltip away from right edge
  var spill = posX + gstooltip.offsetWidth - browserWidth;
  if (spill > 0) {
    posX -= spill;
    posY += Math.min(spill, 20); // move tooltip lower when it can't go right anymore
  }
  gstooltip.style.left = posX + 'px';
  gstooltip.style.top = posY + 'px';

  // set shadow
  gstooltipShadow.style.width = gstooltip.offsetWidth + 'px';
  gstooltipShadow.style.height = gstooltip.offsetHeight + 'px';
  gstooltipShadow.style.left =  posX + gstooltip_shadowSize + 'px';
  gstooltipShadow.style.top = posY + gstooltip_shadowSize + 'px';
}

function hideTooltip() {
  gstooltip.style.display='none';
  gstooltipShadow.style.display='none';
}



/**********************
   MAGIC THUMBS v1.2
***********************/

/*
  Made by Godsmurf - http://www.godsmurf.com

  Based on the ImageExpander script by Guyon Roche
    http://www.webreference.com/programming/javascript/gr/column8/

  Instructions:
  - General: tweak the configuration constants if you understand them
  - Spinner:
    - include "<div id="MTspinner"></div>" in your html within the main wrapper div
    - make sure the #MTspinner style (which links to the spinner image) is included
    - set the spinner size in the Configuration part below
*/

/*
 * CONFIGURATION
 */
var mt_main_div_id = "main";    // id of div that wraps the gallery page
var mtdiv_border_width = 1;     // width of border around thumb, needed for precise positioning in IE
var spinner_size = 32;          // size of spinner div (32 for 32x32)
// movement control: check the function fMove to see how these constants are used
// (default values: 8, 0.02, 3, 3)
var mt_speed = 8           // thumb grows 1/x of the remaining distance each tick
var mt_tinflate = 0.02     // thumb grows toward x% beyond real target (=snappier arrival)
var snap_expand = 3;       // target reached if distance < x pixels (when expanding)
var snap_reduce = 3;       // target reached if distance < x pixels (when shrinking)

/*
 * Init - don't touch
 */
var mt_browser_id = identifyBrowser();
var mt_border_add = (mt_browser_id=="ie") ? mtdiv_border_width : 0;
var mt_z_index = 10001;             // z_index for growing images, +1 for each new img
var spinner;

/*
 * Functions
 */

function MagicThumb(oThumb, sImgSrc) {
  // store thumbnail image and overwrite its onclick handler.
  this.maindiv = document.getElementById(mt_main_div_id);
  this.oThumb = oThumb;
  this.oThumb.expander = this;
  this.oThumb.onclick = function() { MTexpand(this.expander); }

  // record original size
  this.smallWidth = oThumb.offsetWidth;
  this.smallHeight = oThumb.offsetHeight;

  // current condition
  this.bExpand = true;
  this.bTicks = false;

  // self organized list
  if ( !window.aMagicThumbs ) {
    window.aMagicThumbs = new Array();
  }
  window.aMagicThumbs.push(this);

  // show spinner
  spinner = document.getElementById("MTspinner");
  spinner.style.visibility = "visible";
  spinner.style.left = findPosX(oThumb) - findPosX(this.maindiv) + mt_border_add +
    Math.round((this.smallWidth - spinner_size)/2) + "px";
  spinner.style.top = findPosY(oThumb) + mt_border_add +
    Math.round((this.smallHeight - spinner_size)/2) + "px";

  // create and load the full sized image.
  this.oImg = new Image();
  this.oImg.expander = this;
  this.oImg.onload = function(){MTonload(this.expander);}
  this.oImg.src = sImgSrc;
}

MTonload = function(MT) {
  // hide spinner
  document.getElementById("MTspinner").style.visibility = "hidden";
  // Create DIV for image in main_div
  MT.oDiv = document.createElement("div");
  // Store Image big size
  MT.bigWidth = MT.oImg.width;
  MT.bigHeight = MT.oImg.height;
  // Prevent flicker
  MT.oDiv.style.visibility = "hidden";
  MT.oImg.style.visibility = "hidden";
  MT.oImg.style.width = "1px";
  MT.oImg.style.height = "1px";
  MT.oImg.title = "";
  // Add DIV to page
  MT.maindiv.appendChild(MT.oDiv);
  // Add Img to DIV
  MT.oImg.className = "MagicThumbImg";
  MT.oDiv.appendChild(MT.oImg);
  // Set DIV properties
  MT.oDiv.style.position = "absolute";
  MT.oDiv.expander = MT;
  MT.oDiv.onclick = function() {MTtoggle(this.expander);};

  if ( MT.bExpand ) {
    MTexpand(MT);
  } else {
    MT.oDiv.style.visibility = "hidden";
    MT.oImg.style.visibility = "hidden";
  }
}

MTtoggle = function(MT) {
  MT.oDiv.style.zIndex = ++mt_z_index;  // pull to top

  // toggle expansion - reduction
  MT.bExpand = !MT.bExpand;
  if ( MT.bExpand ) {
    for ( var i in window.aMagicThumbs )
      if ( window.aMagicThumbs[i] !== MT )
        MTreduce(window.aMagicThumbs[i]);
  }
}

MTexpand = function(MT) {
  MT.oDiv.style.zIndex = ++mt_z_index;  // pull to top

  // set direction of expansion.
  MT.bExpand = true;

  // set all other images to reduce
  for ( var i in window.aMagicThumbs )
    if ( window.aMagicThumbs[i] !== MT )
      MTreduce(window.aMagicThumbs[i]);

  // if not loaded, don't continue just yet
  if ( !MT.oDiv ) return;

  // hide the thumbnail
  MT.oThumb.style.visibility = "hidden";

  // calculate initial dimensions
  MT.x = findPosX(MT.oThumb) - findPosX(MT.maindiv) + mt_border_add;
  MT.y = findPosY(MT.oThumb) + mt_border_add;
  MT.w = MT.oThumb.clientWidth;
  MT.h = MT.oThumb.clientHeight;

  // set post and size
  MT.oDiv.style.left = MT.x + "px";
  MT.oDiv.style.top = MT.y + "px";
  MT.oImg.style.width = MT.w + "px";
  MT.oImg.style.height = MT.h + "px";
  MT.oDiv.style.visibility = "visible";
  MT.oImg.style.visibility = "visible";

  // start the animation engine OR DON'T
  if ( true && !MT.bTicks ) {
    MT.bTicks = true;
    var pThis = MT;
    window.setTimeout(function(){MTtick(pThis);},25);
  }
}

MTreduce = function(MT) {
  // set direction of expansion.
  MT.bExpand = false;
}

MTtick = function(MT) {
  // calculate screen dimensions
  var maxw = document.documentElement.clientWidth;    // browser width
  var cw = MT.maindiv.offsetWidth - 5;              // container (main div) width
  var ch = document.documentElement.clientHeight - 5;
  var cx = document.documentElement.scrollLeft + cw / 2;
  var top = (document.documentElement && document.documentElement.scrollTop) ?
    document.documentElement.scrollTop : document.body.scrollTop;   // scrolltop
  var cy = top + ch / 2;

  // calculate target (t* = target)
  var tw,th,tx,ty;
  if ( MT.bExpand ) {
    // expanding
    tw = MT.bigWidth;
    th = MT.bigHeight;
    if ( tw > maxw ) {
      th *= maxw / tw;
      tw = maxw;
    }
    if ( th > ch ) {
      tw *= ch / th;
      th = ch;
    }
    tx = cx - tw / 2;
    ty = cy - th / 2;
  } else {
    // reducing
    tw = MT.smallWidth;
    th = MT.smallHeight;
    tx = findPosX(MT.oThumb) - findPosX(MT.maindiv) + mt_border_add;
    ty = findPosY(MT.oThumb) + mt_border_add;
  }

  // move closer to target
  var nHit = 0;   //
  var xxfMove = function(n,tn) {
    var dn = tn - n;
    if ( Math.abs(dn) < 3 ) {
      nHit++;
      return tn;
    } else {
      return n + dn / 10;
    }
  }
  var xxx20061110xxxfMove = function(n, tn, expand) {
    // n = current n, tn = target n, returns new n
    var rn = tn - n;    // rn = remaining distance
    if ( expand && Math.abs(rn) < 5 )
      nHit++;           // target almost hit (when expanding)
    if ( !expand && Math.abs(rn) < 3 )
      nHit++;           // target almost hit (when shrinking)
    // approach target
    return n + rn/7;
  }
  var fMove = function(n, tn, expand, on) {
    // n = current n, tn = target n, expand = true/false, on = origin n (of thumbnail)
    // => returns new n
    var rn = tn - n;    // rn = remaining distance
    if ( expand && Math.abs(rn) < snap_expand )
      nHit++;           // target almost hit (when expanding)
    if ( !expand && Math.abs(rn) < snap_reduce )
      nHit++;           // target almost hit (when shrinking)
    // when expanding, grow towards widened target
    if (expand) {
      rn = (tn + (tn-on)*mt_tinflate) - n;  // put target mt_tinflate% further
    }
    // approach target
    var dn = rn / mt_speed;
    if ( Math.abs(dn) > Math.abs(tn - n) ) {
      // don't overshoot - return tn
      return tn;
    } else {
      return n + dn;
    }
  }
  // new coordinates
  MT.x = fMove(MT.x, tx, MT.bExpand, findPosX(MT.oThumb) - findPosX(MT.maindiv) + mt_border_add);
  MT.y = fMove(MT.y, ty, MT.bExpand, findPosY(MT.oThumb) + mt_border_add);
  MT.w = fMove(MT.w, tw, MT.bExpand, MT.smallWidth);
  MT.h = fMove(MT.h, th, MT.bExpand, MT.smallHeight);
  // snap into place if all 4 are close
  if (nHit == 4) {
    MT.x = tx; MT.y = ty; MT.w = tw; MT.h = th;
  }
  // set coordinates
  MT.oDiv.style.left = MT.x + "px";
  MT.oDiv.style.top = MT.y + "px";
  MT.oImg.style.width = MT.w + "px";
  MT.oImg.style.height = MT.h + "px";

  // if reducing and size/position is a match, stop the tick
  if ( !MT.bExpand && (nHit == 4) ) {
    MT.oImg.style.visibility = "hidden";
    MT.oDiv.style.visibility = "hidden";
    MT.oThumb.style.visibility = "visible";
    MT.bTicks = false;
  }

  if ( MT.bTicks ) {
    var pThis = MT;
    window.setTimeout(function(){MTtick(pThis);},25);
  }
}



/**********************
   GALLERY 2.1 (2007)
***********************/
// params: style = suffix to be added to stylenames

// Init
var MTcount = 0;   // counts magic thumbs added

// Gallery = 1 row of magic thumbs
function beginGallery(al) {
  if (!al) al="center"
  return '<table align="'+al+'"><tr><td>';
}
function endGallery() {
  return '</td></tr></table><div style="clear:left; height:1px;"></div>';
}

function MTDivEmpty(style) {
  // empty invisible square for layout purposes
  if (!style) style='';
  return '<div class="MagicThumbDivEmpty'+style+'"></div>';
}
function MTDiv(pic, cap, thumb, style) {
  if (!style) style='';
  // next thumb
  MTcount++;
  var divname = "MTDiv"+MTcount;
  // div
  var str = "";
  str += '<div class="MagicThumbDiv'+style+'" id="'+divname+'"><table class="MagicThumbTable'+style+'"><tr><td class="MagicThumbTD'+style+'">';
  str += strMTImg(pic, cap, thumb, divname, style);
  str += '</td></tr></table></div>';
  return str;
}
function MTImg(pic, cap, thumb, style) {
// returns an image with a magic thumb, not inside a div
  if (!style) style='';
  // next thumb
  MTcount++;
  var str = strMTImg(pic, cap, thumb, '', style);
  return str;
}
function strMTImg(pic, cap, thumb, divname, style) {
// returns the MT image
// !!! only call this from MTDiv or MTImg!!!
  if (!style) style='';
  var imgname = "MTImg"+MTcount;
  // default thumb location/name
  if (!thumb) thumb='thumbs/tn_'+pic;
  //
  var str = "";
  str += '<a href="'+pic+'" onclick="return newMT(\''+imgname+'\', \''+pic+'\');">';
  str += '<img id="'+imgname+'" class="MagicThumbImg'+style+'" src="'+thumb+'"  onmouseover="';
  // to launch MT from img:   onclick="new MagicThumb(this, \''+pic+'\');"
  if (divname) str += 'getel(\''+divname+'\').className=\'MagicThumbDivHL'+style+'\';';
  if (cap) str += 'showTooltip(\''+cap+'\');';
  str += 'return false;" onmouseout="';
  if (cap) str += 'hideTooltip();';
  if (divname) str += 'getel(\''+divname+'\').className=\'MagicThumbDiv'+style+'\';';
  str += '"';
  if (cap) str += ' onmousemove="moveTooltip(event);"';
  str += '></a>';
  return str;
}
function newMT(t,f) {
// launches the magicthumb
// t = id-name of the thumbnail image, f = filename of the picture
  timg = document.getElementById(t);
  timg.blur();
  new MagicThumb(timg, f);
  return false;
}


// TODO: Caption en divHL


/*************
  NEWS THUMBS (Old)
*************/

function NewsThumb(tw,th,pw,ph,d,p,c) {
// d = dir (relative from news)
// thumbs in subdir /thumbs
  var str;
  str="";
  str=str+"<td class=TDNewsThumb align=center><center>";
  str=str+'<a href="javascript:NewsShowPicture(' +pw+ ',' +ph+ ',';
  str=str+"'" +d+ "','"+p+ "','" +c+ "')";
  str=str+'" border="0">';
  str=str+'<img src="'+d+'/thumbs/tn_' +p+ '" width="' +tw+ '" height="' +th+ '" alt="' +c+ '" title="' +c+ '" class=IMGNewsThumbNSel onmouseout="this.className=';
  str=str+"'IMGNewsThumbNSel';";
  str=str+'" onmouseover="this.className=';
  str=str+"'IMGNewsThumbSel';";
  str=str+'"></a></center></td>   ';
  return str;
}

// Image popup script
//
function NewsShowPicture(w,h,d,p,c) {
  var breedte;
  var hoogte;
  var resizable;
  var scrollbars;
  resizable = "yes";
  scrollbars = "no";
  breedte=w;
  hoogte=h;
  if (document.layers)
    breedte+=18;
  if (c!=null)
    hoogte+=55;
  if (c!=null&&w<h)
    hoogte+=20;
  if (c.length>100&&w<500)
    hoogte+=20;
  if (c.length > 200)
    hoogte+=20;
  if (c.length > 300)
    hoogte+=20;
  if (c.length > 400)
    hoogte+=20;
  if (c.length > 500)
    hoogte+=20;
  // scrollbars?
  if (screen.availheight < hoogte) {
    hoogte = screen.availheight - 30;
    breedte+=20;
    if (screen.availwidth < breedte)
      breedte = screen.availwidth - 10;
    scrollbars = "yes";
  } else {
    if (screen.availwidth < breedte) {
      breedte = screen.availwidth - 10;
      hoogte+=20;
      if (screen.availheight < hoogte)
        hoogte = screen.availheight - 20;
      scrollbars = "yes";
    }
  }
  //
  if (typeof(win) == "undefined")
    win = window.open("","","directories=no,location=no,menubar=no,resizable="+resizable+",scrollbars="+scrollbars+",status=no,toolbar=no,height="+hoogte+",width="+breedte);
  else
  {
    if (win.closed)
      win = window.open("","","directories=no,location=no,menubar=no,resizable="+resizable+",scrollbars="+scrollbars+",status=no,toolbar=no,height="+hoogte+",width="+breedte);
    else
    {
      if (document.all)
        hoogte+=29;
      win.resizeTo(breedte,hoogte);
      win.document.clear();
    }
  }
  win.document.writeln('<HTML>');
  win.document.writeln('<HEAD>');
  win.document.writeln('<TITLE>Photo - click to close</TITLE>');
  win.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=">');
  win.document.writeln('<link rel="stylesheet" href="/stylesheets/popup_photo.css" type="text/css">');
  win.document.writeln('</HEAD>');
  win.document.writeln('<BODY bgcolor="#000000" text="#FFFFCC" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" leftmargin="0" topmargin="0">');
  win.document.writeln('<center><IMG SRC="'+d+'/'+p+'" WIDTH="'+w+'" HEIGHT="'+h+'" onclick="window.close()"></center>');
  if (c!=null)
  {
    win.document.writeln('<DIV class=DIVPicCaption>');
    win.document.writeln(c);
    win.document.writeln('</DIV>');
  }
  win.document.writeln('</BODY>');
  win.document.write('</HTML>');
  win.document.close();
  win.focus();
  // return false;
}



/**********************
       DEV ZONE
***********************/
