Event.observe(window, 'load', function(){
	$$(".rounded").each(function(el){
		try{
			makeRoundedCorners(el.id);
		} catch(err) {
			
		}
	});
	
	$$('.nieuws_link').each(function(el){
		$(el.id).observe('click', refreshBericht);
	});
	
	$$(".h2link").each(function(el){
		$(el.id).observe('click', wijzig_tabblad);
	});
	
	if($('example_video_1')){
		try {
			var myPlayer = VideoJS.setup("example_video_1");
		} catch (err) {
			//return true;
		}
	}
	if($('example_video_2')){
		try {
			var myPlayer = VideoJS.setup("example_video_2");
		} catch (err) {
			//return true;
		}
	}
	
	$$('.route_link').each(function(el){
		$(el.id).observe('click', refresh_contact_video);
	});
	
	if($('contact_links_container')){
		try {
			var myPlayer = VideoJS.setup("example_video_1", {
				controlsBelow: false,
				controlsHiding: false,
				controlsAtStart: false
			});
		} catch (e) {
			// TODO: handle exception
		}
	}
		
   
	
	

	
/*
 * onderstaand ter debug, om de schermbreedte weer te geven rechtsboven
 */	
//	var vp = screen.width;
//	$('debug').update(vp  + "<br/>" + $('main_container').getWidth());
//	
//	Event.observe(window, 'resize', function(){
//		var vp = screen.width;
//		$('debug').update(vp  + "<br/>" + $('main_container').getWidth());		
//	});
	
	setOrientation();
	syncFotoContainers();
	Event.observe(window, 'orientationchange', setOrientation); 
	new PeriodicalExecuter(refreshFrontPagePhoto, 15);
});

function setOrientation() {
	var orient = Math.abs(window.orientation % 180) === 0 ? 'portrait' : 'landscape';
	//var cl = document.body.className;
	//cl = cl.replace(/portrait|landscape/, orient);
	document.body.className = orient;
	if($('currentOrientation')){
		$('currentOrientation').update("orientatie: " + window.orientation);
	}
	syncFotoContainers();
}

function syncFotoContainers(){
	if($('foto_container')){
		//new PeriodicalExecuter(refreshFrontPagePhoto, 2);
		//globale vars voor foto_wissel op voorpagina
		voorpag_wissel_width = parseInt($('foto_container').getStyle('width'));
		voorpag_wissel_height = parseInt($('foto_container').getStyle('height'));
		voorpag_wissel_top = $('foto_container').offsetTop;
		voorpag_wissel_left = $('foto_container').offsetLeft;

		if($('foto_container2')){
			$('foto_container2').remove();
		}
		
		var f2 = new Element('div', {
			'id':		'foto_container2',
			'class':	'img_25_div rounded r_10',
			'style': 	'position: absolute; top: ' + voorpag_wissel_top + 'px; left: ' + voorpag_wissel_left + 'px; width: ' + voorpag_wissel_width + 'px; height: ' + voorpag_wissel_height + 'px; z-index: -1'
		});
		$('foto_container').insert({before: f2});
		new Ajax.Updater('foto_container2', 'includes/refresh_foto_container.php', {
			onComplete: function(){
			makeRoundedCorners('foto_container2');
		}
		});
	}
}

function refreshFrontPagePhoto(){
	if($('foto_container')){
		$('foto_container').fade({ 
			duration: 2.0, from: 1, to: .01,
			afterFinish: function(){
			$('foto_container').innerHTML = $('foto_container2').innerHTML;
			$('foto_container').fade({ 
				duration: 1.0, from: .01, to: 1,
				afterFinish: function(){
					new Ajax.Updater('foto_container2', 'includes/refresh_foto_container.php', {
						onComplete: function(){
						makeRoundedCorners('foto_container2');
						}
					});
				}
			});		
		}
		});
	}
}

function refresh_video(video_id){
	if($('tab_1').getStyle('display') == 'none'){
		var teller = 0;
		$$('.h2link').each(function(el){
			if(teller == 0){
				el.addClassName('active');
			} else {
				el.removeClassName('active');
			}
			teller++;
		});
		$('tab_1').show().addClassName('active');
		$('tab_2').hide();
		$('tab_3').hide();
		
	}
	new Ajax.Updater('tab_1', 'includes/refresh_video_tab.php', {
		method: 'get',
		parameters: {movie: video_id},
		onComplete: function(){
			var myPlayer = VideoJS.setup("example_video_1");
		}
	});
}

function refresh_contact_video(e){
	//alert(e.target.rel);
	new Ajax.Updater('video_js_box', 'includes/refresh_route_video.php', {
		method: 'get',
		parameters: {route: e.target.rel},
		onComplete: function(){
			var myPlayer = VideoJS.setup("example_video_2");
			
		}
	});
}

function wijzig_tabblad(e){
	var geklikt = e.target.id.split("_");
	var tabblad_open = geklikt[2];
	$$('.tab_div').each(function(el){
		$(el.id).hide();
	});
	$('tab_' + tabblad_open).show();
	//afbeeldingen afronden, kon niet bij display:hidden
	$$('#tab_' + tabblad_open + " .rounded").each(function(el){
		makeRoundedCorners(el.id);
	});
	
	
	$(e.target.id).addClassName("active").siblings().each(function(el){
		try {
			$(el.id).removeClassName("active");
		} catch(err){
			
		}
	});
}



function makeRoundedCorners(el_id){
	var afmeting = $(el_id).getDimensions();
	var round_value = 0;
	
	//bij padding moet deze in mindering worden gebracht op de position
	var pt = pr = pb = pl = 0;
	pt += parseInt($(el_id).getStyle('padding-top').gsub('px', ''));
	pr += parseInt($(el_id).getStyle('padding-right').gsub('px', ''));
	pb += parseInt($(el_id).getStyle('padding-bottom').gsub('px', ''));
	pl += parseInt($(el_id).getStyle('padding-left').gsub('px', ''));
	
	
	var cl_n = $w($(el_id).className); 
	cl_n.each(function(el){
		if(el.match(/r_./)){
			round_value = el.replace(/r_/, '');
		}
	});
	
	var styleString = "position: absolute; top: 0px; left: 0px; background-color: transparent; width:" + afmeting.width + "px ; height:"  + afmeting.height +  "px ;";
	var roundedOverlay = new Element('div', {
		'style': styleString
	});
	
	var tl = new Element('img', {
		'src': 'img/layout/rc_tl.png',
		'style': 'position: absolute; top: ' + pt + 'px; left: ' + pl + 'px',
		'height': round_value + 'px',
		'width': round_value + 'px'
	});
	var tr = new Element('img', {
		'src': 'img/layout/rc_tr.png',
		'style': 'position: absolute; top:  ' + pt + 'px; right:  ' + pr + 'px',
		'height': round_value + 'px',
		'width': round_value + 'px'
	});
	var bl = new Element('img', {
		'src': 'img/layout/rc_bl.png',
		'style': 'position: absolute; bottom:  ' + pb + 'px; left:  ' + pl + 'px',
		'height': round_value + 'px',
		'width': round_value + 'px'
	});
	var br = new Element('img', {
		'src': 'img/layout/rc_br.png',
		'style': 'position: absolute; bottom:  ' + pb + 'px; right:  ' + pr + 'px',
		'height': round_value + 'px',
		'width': round_value + 'px'
	});
	
	$(el_id).insert({bottom: tl});
	$(el_id).insert({bottom: tr});
	$(el_id).insert({bottom: bl});
	$(el_id).insert({bottom: br});
	
	return;
}

function refreshBericht(e){
	$('nieuws_update_div').show();
	new Ajax.Updater('nieuws_update_div', e.target.href, {
		onComplete: function(){
			if(!($('closeButton'))){
				var closeButton = new Element('button', {
					'id': 'closeButton',
					'onclick': "closeBericht()",
					'style': "position: absolute; bottom: 0px; right: 10px; border: 1px solid #ccc; padding: 3px; cursor: pointer;"
				}).update('close');
			}
			$('nieuws_update_div').insert({bottom: closeButton});
		}
	});
	
	return false;
}

function closeBericht(){
	$('nieuws_update_div').hide();
}


function refresh_dir_playlist(d, st){
	new Ajax.Updater('directors_playlist_container', 'includes/directors_playlist.php', {
		parameters: {start: st, d_id: d},
		method: 'get',
		onComplete: function(){
			$$('#directors_playlist_container .rounded').each(function(el){
				try {
					makeRoundedCorners(el.id);
				} catch(err){
					
				}
			});
		}
	});
}


