window.addEvent('domready',function(){
  Cufon.replace('.title');
  Cufon.replace('.pf_link');
  Cufon.replace('#menu a', {hover:true});
  Cufon.replace('h1');

  $$('#menu li').each(function(el){
    var iWidth = parseInt(el.getStyle('width'))+5;
    el.setStyle('width',iWidth);

    el.addEvent('mouseover',function(){
      el.addClass('hover');
    });

    el.addEvent('mouseover',function(){
      el.removeClass('hover');
    });
  });

  //initMouseovers();

  $$('a[rel=external]').addEvent('click', function(e) {
    this.target = '_blank';

  });
  //resizeColumn();
});

window.addEvent('resize', function() {
  resizeColumn();
})

function resizeColumn() {
  var fotoKolomWidth = 0;
  $$('div.foto').each(function(el) {
    el.getElements('img').each(function(afb) {
      el.setStyle('width', parseInt(afb.getStyle('width')));
      fotoKolomWidth += parseInt(afb.getStyle('width'))+15;
    });
  });

  if($('fotokolom')) {
    $('fotokolom').setStyle('width',fotoKolomWidth);
  }
}

function initMouseovers() {
  var fotoKolomWidth = 0;

  $$('div.foto').each(function(el) {
    el.getElements('img').each(function(afb) {
      //alert(parseInt(afb.getStyle('width')));
      el.setStyle('width', parseInt(afb.getStyle('width')));
      fotoKolomWidth += parseInt(afb.getStyle('width'))+10;
    });

    el.addEvent('mouseover', function() {
      var contentId = 'mouseover'+el.id.substring(4);

      if($(contentId)) {
        $(contentId).setStyle('width', el.getElements('img').getStyle('width'));
        $(contentId).setStyle('height', el.getElements('img').getStyle('height'));
        $(contentId).setStyle('display','block');
      }
    });

    el.addEvent('mouseout', function() {
      var contentId = 'mouseover'+el.id.substring(4);
      if($(contentId)) {
        $(contentId).setStyle('display','none');
      }
    });
  });

  //alert(fotoKolomWidth);
  if($('fotokolom')) {
    $('fotokolom').setStyle('width',fotoKolomWidth);
  }
}

function initMouseover(fotoID) {
  var divFoto = $('foto'+fotoID);

  divFoto.getElements('img').each(function(afb) {
    //alert(parseInt(afb.getStyle('width')));
    divFoto.setStyle('width', parseInt(afb.getStyle('width')));
  });

  divFoto.addEvent('mouseover', function() {
    var contentId = 'mouseover'+divFoto.id.substring(4);

    if($(contentId)) {
      $(contentId).setStyle('width', divFoto.getElements('img').getStyle('width'));
      $(contentId).setStyle('height', divFoto.getElements('img').getStyle('height'));
      $(contentId).setStyle('display','block');
    }
  });

  divFoto.addEvent('mouseout', function() {
    var contentId = 'mouseover'+divFoto.id.substring(4);
    if($(contentId)) {
      $(contentId).setStyle('display','none');
    }
  });
}

/**
 * Preload images
 **/
function loadImages(images) {
    var loadedImages = new Array();

	/* preloading */
	var loader = new Asset.images(images, {
		onProgress: function(counter) {
          loadedImages[counter] = this;
		},
		onComplete: function() {
          loadedImages.each(function(im) {
            fotoID  = im.src.split('/')[5].split('.')[0];

            $('img'+fotoID).set('src', im.src);
            initMouseover(fotoID);
          });

          resizeColumn();
		}
	});
}

