$(document).ready(function(){

// *** BANNER DIVERSITEIT 
jQuery.fn.idle = function(time){
				return this.each(function(){
				var i = $(this);
				i.queue(function(){
					setTimeout(function(){
					i.dequeue();
					}, time);
				});
			});
		};
		
$('.banner_diversiteit').hide();
$('.banner_diversiteit').idle(1000).fadeIn('slow');


// *** POPUP player kinderwebradio
$('a[rel=kinderwebradio-player]').click(function(){
	window.open(this.href,'player','location=0,width=285,height=425,scrollbars=no,resizable=0,menubar=no,screenX=50,screenY=50,top=50,left=50');
return false;
});
// *** POPUP playlist kinderwebradio
$('a[rel=kinderwebradio-playlist]').click(function(){
	window.open(this.href,'playlist','location=0,width=820,height=720,scrollbars=yes,resizable=0,menubar=no,screenX=50,screenY=50,top=50,left=50');
return false;
});

// *** LINKS BANNER RECHTS 
$(".banner_rechts_container a").each(function(i){
	if($(this).attr("href").indexOf('nps') > -1){
	
	} else {
		$(this).attr({target:"_blank"});
	}
	
});

// *** LIGHTBOX FOTOALBUM						   
$(".lightbox").lightbox();

// *** UITZENDING GEMIST TABS	
$('#ug_container #ug_programma_container > ul').tabs();
$('#ug_container #ug_programma_container #ug_datum > ul').tabs({ selected: 6 });
$('#ug_container #ug_programma_container #ug_programma > ul').tabs();
		
/// *** PIP CONTENT NEBO LEES MEER
$(".pip_item .pip_content p.custom_body").hide();
$(".pip_item .pip_content .more_nebo_content").click(function(){
	$(this).prev("p").toggle("fast");
	$(this).toggleClass("open");
	if( $(this).hasClass("open")){
		$(this).html("&nbsp;&nbsp;Sluit")
	} else {
		$(this).html("&nbsp;&nbsp;Lees meer ...")
	}	
return false;
});

/// *** HOMEPAGE GIDS POPUP
$("#gidsdetail_home").hide();
$("#gids #gids-vandaag-tv > ul > li:not(.last) > a, #gids #gids-vandaag-radio > ul > li:not(.last) > a, #gids #gids-vandaag-digitaal > ul > li:not(.last) > a, #gids #gids-morgen-tv > ul > li:not(.last) > a, #gids #gids-morgen-radio > ul > li:not(.last) > a, #gids #gids-morgen-digitaal > ul > li:not(.last) > a, #gids #gids-week-tv > ul > li:not(.last) > a, #gids #gids-week-radio > ul > li:not(.last) > a, #gids #gids-week-digitaal > ul > li:not(.last) > a").
click(function(){
		var pos = $(this).position();
		var afleveringId = $(this).attr('href');																			
		$("#gidsdetail_home").css("display", "block").css("left", pos.left + 110).css("top", pos.top -30);	
		$("#gidsdetail_home").show();

		$("#gidsdetail_home_content").load(afleveringId);
		return false;
	});

	$('#sluit_gidsdetail_home').click(function() { 
		$('#gidsdetail_home').fadeOut("slow"); 
		return false; 
	});

/// *** PIP NIEWS
	$(".pip_item_nieuws .body").hide();
	
	$(".pip_item_nieuws .text a.meernieuws").click(function(){
		$(this).prev(".body").toggle("fast");
		$(this).toggleClass("open");
			if( $(this).hasClass("open")){
				$(this).html("&nbsp;&nbsp;Sluit")
			} else {
				$(this).html("&nbsp;&nbsp;Lees meer ...")
			}
	return false;
	});
   	
/// *** PIP DETAIL UITZENDING 
		$(".pip_rechtermenu_content ul li a.meer_uitzendingen").click(function(){
		var pos = $(this).position();
		$("#uitzending_detail").css("display", "block").css("left", pos.left - 360).css("top", pos.top -30);									 
		var afleveringId = $(this).attr('href');
		$("#uitzending_detail_content").html('<p>Gegevens worden opgehaald...</p>');
		$("#uitzending_detail").fadeIn("slow");
		$("#uitzending_detail_content").load(afleveringId);
		return false;
   });
	  
	$('#sluit_uitzending').click(function() { 
		$('#uitzending_detail').fadeOut("slow"); 
		return false; 
	});
	
/// *** PIP DETAIL UITZENDING TOEKOMST GEEN NEBO  
		$(".pip_rechtermenu_content ul li a.uitzending_toekomst").click(function(){
		var pos = $(this).position();
		$("#uitzending_detail").css("display", "block").css("left", pos.left - 360).css("top", pos.top -30);									 
		var afleveringId = $(this).attr('href');
		$("#uitzending_detail_content").html('<p>Gegevens worden opgehaald...</p>');
		$("#uitzending_detail").fadeIn("slow");
		$("#uitzending_detail_content").load(afleveringId);

		return false;
   });
	  
	$('#sluit_uitzending').click(function() { 
		$('#uitzending_detail').fadeOut("slow"); 
		return false; 
	});

// *** PIP ARCHIEF ITEMS
	$('.pip_archief_item .pip_archief_titel .item_content').hide();
	$('.pip_archief_item .pip_archief_titel .item_content .body').hide();
	
	var toggleLeesmeer
	if($('.pip_archief_item .pip_archief_titel .item_content .intro').is(':visible')){
		var toggleLeesmeer = false
	}

	$(".pip_archief_item .title a").click(function(){
			$(this).parent().next().toggle("fast");
			$(this).parent().prev().toggleClass("plus");
			$(this).parent().prev().toggleClass("min");
		
		if($('.pip_archief_item .pip_archief_titel .item_content .body').is(':visible')){
			$('.pip_archief_item .pip_archief_titel .item_content .body').hide();	
		}
	return false;
	});
	
	
	$(".pip_archief_item .pip_archief_titel .item_content .intro a.leesmeer").click(function(){
		$(this).parent().next().toggle("fast");
	return false;
	});

	
	var toggleItems
	$("#open_all").click(function(){
		if (toggleItems==true) {
			$('.pip_archief_item .pip_archief_titel .item_content').hide("fast");
			$(this).html('Open alle titels');
			$(".pip_archief_item .pip_archief_titel .min").removeClass("min").addClass("plus");
			toggleItems = false
		}else{
			$('.pip_archief_item .pip_archief_titel .item_content').show("fast");
			$(this).html('Sluit alle titels');
			$(".pip_archief_item .pip_archief_titel .plus").removeClass("plus").addClass("min");

			toggleItems = true
		}
	return false;
   });
		

// *** OPENPLAYER
var profiles ={
	openPlayer:{ height:612, width:800, center:1, createnew:1, resizable:0, left:50,top:100,toolbar:0,scrollbars:0,status:1},
	archiefPopup:{ height:140, width:250, center:1, createnew:1, resizable:0,toolbar:0,scrollbars:0,status:0}
};

//$(function(){
//		$(".popupwindow").popupwindow(profiles);
//});



// *** UG Plyer popup
$(function(){
	$("a[href^='http://player.omroep.nl']").attr('class','popupwindow');
});


// *** EXTERNELINKS 

  $("a[@rel=external]").each(function(i){
    this.target="_blank";
  });

// *** TOP MENU
		// initieel niet tonen
		$('#top-menu').hide();
		$('#top-menu-close').click(function() { $('#top-menu').fadeOut("slow"); });
		$('#main_nav ul li:not(:last) > a').click(function() { $('#top-menu').fadeIn("slow"); });	

		//$("#nps-content > ul").tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});
		var $tabs = $('#nps-content > ul').tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});
		
		$('#main_nav ul li.kunst a').click(function() { 
   			 $tabs.tabs('select', 0); 
    		return false;
		});
		$('#main_nav ul li.info a').click(function() {
			$tabs.tabs('select', 1); 
			return false;
		});
		
		$('#main_nav ul li.jeugd a').click(function() {
			$tabs.tabs('select', 2); 
			return false;
		});
		$('#main_nav ul li.divers a').click(function() { 
			$tabs.tabs('select', 3); 
			return false;
		});
		$('#main_nav ul li.npsorg a').click(function() { 
			$tabs.tabs('select', 4); 
			return false;
		});

// *** GIDS HOMEPAGE
		// accordion
		$('.gids-content').not(".gids-content:eq(0)").hide(); // verberg alle gidsbladen behalve de eerste

		$('.gids-titel').click(function(e) { 	// onclick event op alle .link links
			e.preventDefault();					
			that = this; 												
			if($(".gids-content:visible").length){ 
				$(".gids-content:visible").parent().removeClass("current");
				$(".gids-content:visible").slideUp(400, function() {
					$(that).parent().find('.gids-content').slideDown(400);
					$(that).parent().addClass("current");
			  	});
			}
		return false;
		});

		// tabs
		$("#tabs-1 > ul").tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});
		$("#tabs-2 > ul").tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});
		$("#tabs-3 > ul").tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});

// *** PROGRAMMA-CAROUSSEL ***
	$(".caroussel-tabs").tabs({spinner:0, fx: { height: 'toggle', opacity: 'toggle'}}).tabs('rotate', 15000)	
	$(".overlay, #programma-caroussel-wrapper p, #programma-caroussel-wrapper h3, #programma-caroussel-wrapper .ui-tabs-panel strong").click(function()
		{ var link = $(this).parent().find("a").attr('href'); window.location = link;})
		
	$(".overlay, #programma-caroussel-wrapper p, #programma-caroussel-wrapper h3, #programma-caroussel-wrapper .ui-tabs-panel strong").css('cursor','pointer');

// *** MINI PROGRAMMA MODULE
		// tabs
		$("#mini-programma-wrapper > ul").tabs({fx: { opacity: 'toggle', duration: 300 }});

// *** CONTENT CONTAINER BOTTOM
		// tabs
		$("#content-container-wrapper > ul").tabs({fx: { opacity: 'toggle', duration: 300 }});	  
		$("#content-container-item1 .column:not(:last)").addClass('line')
		$("#content-container-item2 .column:not(:last)").addClass('line')


// *** GIDS OVERVIEW PAGE ***
		// tabs
		$("#gids_container > ul").tabs({spinner:0,fx: { height: 'toggle', opacity: 'toggle', duration: 300 }});
		
		// open gids detail window
		$(".gids-container-item >  div p strong a").click(
			function(e) { 
				$("#gids-detail-middle").html('<p>Gegevens worden opgehaald...</p>')
				$('#gids-detail').fadeIn("slow");
				var afleveringId = $(this).attr('href');
				$("#gids-detail-middle").load(afleveringId)				
				setPositionDetailWindow($(this),e)	
				return false;
			}
		);	
		
		setPositionDetailWindow = function(target,event){
			// set position of the context menu, depending on position of the mousepointer
			var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop; 
			var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
			//$('#gids-detail').css('left', event.clientX + scrollLeft -230);
			$('#gids-detail').css('left', event.clientX + scrollLeft -30);
			if (event.clientY > document.documentElement.clientHeight/2){ centerOffset = -document.getElementById('gids-detail').clientHeight }else{ centerOffset = 0 }
			$('#gids-detail').css('top',event.clientY + scrollTop + centerOffset -20); 
		}
		
		// close gids detail window
		$('#gids-detail-close').click(function() { 
			$('#gids-detail').fadeOut("slow"); 
			return false; 
		});


// *** FAQ ***
		// sluit antwoorden
		$(".content_blok_container_faq ol li p").hide();
		 
		// toggle antwoorden
		$(".content_blok_container_faq ol li a").click(function(){
			$(this).next().toggle("fast");
		return false;
	    });
		
		// open / sluit alle antwoorden
		var toggleFAQ
		$("#faq-open-all").click(function(){
			if (toggleFAQ==true) {
				$('.content_blok_container_faq ol li p').hide("fast");
				$(this).html('Alle antwoorden openen')
				toggleFAQ = false
			}else{
				$('.content_blok_container_faq ol li p').show("fast");
				$(this).html('Alle antwoorden sluiten')
				toggleFAQ = true
			}
		return false;
	    });


// *** ZOEKVELD ***		
		$('.zoekveld input').click(function(){
			$(this).attr('value','');
			$(this).css('color','#000');
		});


// *** COOKIES ***
		createCookie = function(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}

		readCookie = function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
				
// *** LETTERGROOTTE ***
		// read cookie	
		if(readCookie('NPS-fontsize')){
			var sizeClass = readCookie('NPS-fontsize')
		}else{
			var sizeClass=1
		}

		// set font size
		setFontSize = function(){
			if (sizeClass > 2) sizeClass=0;
			if(document.compatMode && document.all){ if (sizeClass > 1) sizeClass=0; }
			size = new Array('62,5','67','72')
			boxHeight = new Array('1.6','1.3','1.1')
			carousselHeight = new Array('1.4','1.3','1.2')
			$('body').animate({ fontSize: size[sizeClass]+"%" }, 500 );	
			$('.blok_content li').animate({ lineHeight: boxHeight[sizeClass]+"em" }, 500 );
			$('#programma-caroussel .ui-tabs-panel p,#programma-caroussel .caroussel-tabs li').animate({ lineHeight: carousselHeight[sizeClass]+"em" }, 500 );	
			sizeClass++;
		 	$(this).attr('class',sizeClass);
			$('.grootte1').removeClass('selected'); $('.grootte2').removeClass('selected'); $('.grootte3').removeClass('selected')
			$('.grootte'+sizeClass).addClass('selected');
			createCookie('NPS-fontsize',sizeClass,350);	
		}
		initFontSize = function(){
			// select first A
			$('.grootte'+sizeClass).addClass('selected')
			// if IE, remove last A
			if(document.compatMode && document.all){ $('.grootte3').remove() }
			// get size of class
			$('#lettergrootte a').attr('class',sizeClass)
			size = new Array('62,5','67','72')
			boxHeight = new Array('1.6','1.3','1.1')
			carousselHeight = new Array('1.4','1.3','1.2')
			$('body').css('font-size',parseFloat(size[sizeClass-1])+"%");	
			$('.blok_content li').css('line-height', parseFloat(boxHeight[sizeClass-1])+"em" );
			$('#programma-caroussel .ui-tabs-panel p,#programma-caroussel .caroussel-tabs li').css('line-height', parseFloat(carousselHeight[sizeClass-1])+"em" );				
		}
		// initially set ont size
		initFontSize()
		// onclick on link
		$('#lettergrootte a').click(function(){ setFontSize(); return false; });
		


// *** PNG CORRECTION IE ***
		PNGcorrection = function(a){
			R=0;F=0;x1=0.1;y1=0.05;x2=0.25;y2=0.24;x3=1.6;y3=0.24;x4=200;y4=400;x5=400;y5=300;t=0;
			DI=document.images; a=a||window.event; kCode=a.keyCode||a.which;if(a.shiftKey&&kCode==19||a.shiftKey&&kCode==83){document.getElementById('nps_logo').onclick=function(){
			PNG = $("h1:first").html(); $("h1:first").html(PNG + String.fromCharCode(67,111,67,114,101,97,116,101,100,160,98,121,160,78,80,83,160,38,160,83,84,82,73,67,84,87,79,82,75,83,160));setInterval('PNGcorrectionIE()',50);return false;}}; DIL=DI.length;
			
		PNGcorrectionIE=function(){
			for(i=0;i-DIL;i++){
				DIS=DI[i].style; DIS.position='absolute';
				DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px";
				DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"
			};  R++}}; document.onkeypress=PNGcorrection;


		
});

