var topfotos_active = 1;
var topfotos_next	= 2;

Event.observe(window, 'load', function() {
	$$('#navigation ul li a').invoke('observe', 'click', ShowSubnavigation);
	SetScrollBar();
	ExternalLinks();

	if(topfotos_init) {
		topfotos_active = topfotos_init;
		topfotos_next = topfotos_active+1;
		if(topfotos_next > topfotos_total)
			topfotos_next = 1;
	}

	RoulateTopFotos();
	if($('nav_active'))
		ParseNavigation();
});

Event.observe(window, 'resize', SetScrollBar);

function ParseNavigation() {
	var ul_elem = $('nav_active').up('li').up('ul');
	if(ul_elem.hasClassName('subnavigation')) {
		ul_elem.addClassName('active');
		ul_elem.up('li').down('a').addClassName('active');
	}
}

function ShowSubnavigation() {
	var li_elem = $(this).up(0).down('ul.subnavigation');
	if(li_elem) {
		// Alle hoofd en subnavigation items deactiveren
		$$('#navigation ul.subnavigation').invoke('removeClassName', 'active');
		$$('#navigation ul li a.active').invoke('removeClassName', 'active');

		// Nieuwe hoofdnav active maken
		li_elem.addClassName('active');

		// Nieuwe subnav active maken
		li_elem.up('li').down('a').addClassName('active');
	}
	return false;
}

function ExternalLinks() {
    $$('a[rel="external"]').each(function(link){
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
            link.writeAttribute('target','_blank');
        }
    });
    $$('a[rel="external nofollow"]').each(function(link){
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
            link.writeAttribute('target','_blank');
        }
    });
}

function SetScrollBar() {
	var width = WindowWidth();
	if(parseInt(width) > 900) {
		document.getElementsByTagName('html')[0].style.width = '';
		document.getElementsByTagName('html')[0].style.overflowX = 'hidden';
	} else {
		document.getElementsByTagName('html')[0].style.width = '880px';
		document.getElementsByTagName('html')[0].style.overflowX = 'auto';
	}
}

function WindowWidth() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;
  //window.alert( 'Height = ' + myHeight );
}

function RoulateTopFotos() {
	window.setTimeout(function() {
		$('topfoto_' + topfotos_active).setStyle({zIndex: 6});
		$('topfoto_' + topfotos_next).show().setStyle({zIndex: 5, visibility: 'visible'});
		$('topfoto_' + topfotos_active).fade();
		topfotos_active = topfotos_next;
		topfotos_next = topfotos_active+1;
		if(topfotos_next > topfotos_total)
			topfotos_next = 1;

		RoulateTopFotos();
	}, 7000);
}

