/* helper function to load multiple functions, courtesy Simon Wilson */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function e_portfolio(lnk)
{
  if(confirm('Weet u zeker dat u een E-portfolio bestand wilt downloaden?'))
  {
    popup(lnk,600,150,false);
  }
  return false;
}

function popup(url,w,h,scroll)
{
  var l = (screen.width-w)/2;
  var t = (screen.height-h)/2;

  window.open(url,'_blank','resizable=yes,location=no,menubar=no,scrollbars='+scroll+',status=no,toolbar=no,fullscreen=no,dependent=no,width='+w+',height='+h+',left='+l+',top='+t);
}

//get basepath
var site_base = "";
if(a=document.getElementsByTagName("link")[0])
{
  site_base = a.href.substr(0,a.href.indexOf('/resources'));
}


window.addEvent('domready', function(){
  if($('content'))
  {
    new Fx.Font($('content'),$('shrink'),$('grow'),25);
  }
});

//menu
navHover = function()
{
  if(document.getElementById("nav"))
  {
    var lis = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<lis.length; i++) {
      lis[i].onmouseover=function() {
        this.className+=" iehover";
      }
      lis[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp(" iehover\\b"), "");
      }
    }
  }
}
if (window.attachEvent) {window.attachEvent("onload", navHover);} //only needed in IE

//fontsize
Fx.Font = new Class({
  initialize: function(element, sid, gid, growsize)
  {
    this.growsize = growsize || 25;
    this.element = element;
    this.sid = sid;
    this.gid = gid;

    var currentSize = this.getsize();

    //check if you have the Cookie module and if fontSize is set and if so if it's set to 'large'
    if(Cookie.get != null && (size = Cookie.get('fontSize')) && size == 'large')
    {
      this.element.setStyle('font-size',currentSize+this.growsize+'%');
      this.bigactive(false);
    }
    else
    {
      this.bigactive(true);
    }
    $(gid).onclick = function(){this.grow()}.bind(this);
    $(sid).onclick = function(){this.shrink()}.bind(this);
  },
  getsize: function()
  {
    return Math.max(this.element.getStyle('font-size').toInt(),100);
  },
  grow: function()
  {
    if(this.getsize() == 100)
    {
      this.element.effect('font-size',{duration:300,unit:'%'}).start(100,100+this.growsize);
    }
    this.bigactive(false);

    //remember the users preference, keep the data for 1 day
    Cookie.set('fontSize','large',1);
  },
  shrink: function()
  {
    if(this.getsize() == 100+this.growsize)
    {
      this.element.effect('font-size',{duration:300,unit:'%'}).start(100+this.growsize,100);
    }
    this.bigactive(true);

    //remember the users preference, keep the data for 1 day
    Cookie.set('fontSize','small',1);
  },

  bigactive: function(bigact)
  {
    if(bigact)
    {
      this.sid.className='inactiveicon';
      this.gid.className='';
    }
    else
    {
      this.sid.className='';
      this.gid.className='inactiveicon';
    }
  }
});




/*** INFOICON ***/

addLoadEvent(function()
{
  var myInfoIcons = new InfoIcon($$('img.infoicon'));

  var selectFocus = null;
  
  if(window.attachEvent) //IE Only
  {
    var Allselects = $$('select');
    if(Allselects.length>0)
    {
      document.addEvent('mousedown',function(e)
      {
        el = ( e.srcElement || e.target );

        if(selectFocus!=null && el!=selectFocus)
        {
          selectFocus.tween.cancel();
          selectFocus.tween.start(selectFocus.offsetWidth,selectFocus.retrieve('w'));
          selectFocus = null;
        }
      });
      Allselects.each(function(el)
      {
        el.store('w',el.offsetWidth);
        el.setStyle('width','auto');
        el.store('ww',el.offsetWidth>600? 600:el.offsetWidth);
        el.setStyle('width',el.retrieve('w'));
        
        if( el.retrieve('ww')<el.retrieve('w')+5)
        {
          el.addEvent('mouseover',function(){
            if(selectFocus!=null)
            {
              selectFocus.tween.cancel();
              selectFocus.tween.start(selectFocus.offsetWidth,selectFocus.retrieve('w'));
              selectFocus = null;
            }
          });
          return;
        }
        
        el.tween = new Fx.Tween(el, 'width', {duration:200});
        
        new Element('div',{'styles': {'width':el.offsetWidth
                                     ,'height':el.offsetHeight
                                     ,'display':'inline-block'
                                     ,'z-index':'10000'
                                     ,'position':'relative'}}).wraps(el);
        el.setStyles({'position':'absolute'
                     ,'top':0
                     ,'left':0});

        el.addEvent('mouseover',function(){
          if(selectFocus!=null && selectFocus!=el)
          {
            selectFocus.tween.cancel();
            selectFocus.tween.start(selectFocus.offsetWidth,selectFocus.retrieve('w'));
          }
          this.tween.cancel();
          this.tween.start(this.offsetWidth,this.retrieve('ww'));
          selectFocus = this;
        });
        el.addEvent('change',function(e){
          this.tween.cancel();
          this.tween.start(this.offsetWidth,this.retrieve('w'));
          selectFocus = null;
        });
      });
    }
  }

  (function(){$$('div.msg_fadeout').each(function(el) {

    el.tween = new Fx.Tween($(el), 'opacity', {duration:600});

    var dym    = el.getSize();
    var availx = $(el.parentNode).getSize().x;

    $(el).setStyles({'top' :(window.getScrollTop() + ((window.getHeight()-dym.y)/2) - el.parentNode.getTop())+'px'
                    ,'left':((availx-dym.x)/2)+'px'
                    });
    el.tween.set(0);
    el.tween.start(1);
    (function(){el.tween.start(0);}).delay(2000, this);

  });}).delay(100); //set delay to retrieve the correct scrollheight
});

var InfoIcon = new Class({
  initialize: function(elements)
  {
    this.boxname = ['infobox','infoboxsmall'];
    this.infobox = new Element('div', {
      'class': this.boxname[0],
      'styles': {
        'position': 'absolute'
       ,'top': '0'
       ,'left': '0'
       ,'height': '0'
       ,'overflow': 'hidden'
      }
    }).inject(document.body);
    this.infotext = new Element('div',{'class':'infotxt'}).inject(this.infobox);
    this.slider = new Fx.Tween($(this.infobox), 'height', {duration:200});

    $$(elements).each(this.build, this);
  },

  build: function(el)
  {
    el.txt = el.alt.replace(/\n/g,'<br />');
    el.removeAttribute('alt');

    el.addEvent('mouseenter', function(event){
      this.infobox.className = this.boxname[el.offsetWidth>10?0:1];
      this.position(el);
      this.start(el);
    }.bind(this));
    var end = this.end.bind(this);
    el.addEvent('mouseleave', end);
    el.addEvent('trash', end);
  },

  start: function(el)
  {
    this.infotext.empty();
    this.infotext.set('html', el.txt);
    $clear(this.timer);
    this.timer = this.show();
  },

  end: function(event)
  {
    $clear(this.timer);
    this.timer = this.hide();
  },

  position: function(element)
  {
    var pos    = element.getPosition();
    var dyn    = element.offsetWidth;
    var winx   = window.getWidth();
    var infow  = this.infobox.offsetWidth;
    var alignr = (pos.x+infow>winx)? true : false;

    this.infobox.setStyles({
       'left': pos.x-Math.floor(alignr? (infow-(dyn/2+12)) : (13 - dyn/2))
      ,'top': pos.y+dyn
      ,'background-position': (alignr?'-300px 0px':'0px 0px')
    });
  },

  show: function()
  {
    this.slider.cancel();
    this.slider.start(0,this.infobox.scrollHeight);
  },

  hide: function()
  {
    this.slider.cancel();
    this.slider.start(this.infobox.scrollHeight,0);
  }

});
InfoIcon.implement(new Events, new Options);
