$(document).ready(function(){
	// add js_active to body of every page
	$('body').addClass('js_active');
	// lang navs copy
	$('#header_lang').html( $('#footer_lang').html() );
	// Country selections copy
	switch($('body').attr('id')){
		case 'home': $('#footer_country .choice_maker .nav_country_cntr').html($('#home_country .nav_country_cntr').html()); break;
		case 'topic': $('#topic_country .choice_maker .nav_country_cntr').html($('#footer_country .choice_maker .nav_country_cntr').html()); break;
		//case 'country': break;
	}

	// initialise choice maker listeners
	//$('.choice_maker').parent().css('z-index',10);
	$('#footer_site .choice_maker').add('#header .choice_maker').mouseenter(function(){
		// on mouseEnter set open class, take height of ul set to 0 and animate to old height
		var current_height = $('ul',this).height();
		$('ul',this).stop(true,true);		
		$(this).addClass('open').parent().css('z-index',100);
		var height = $('ul',this).height();
		if(current_height==height) current_height=0;
		$('ul',this).height(current_height).animate({height:height+"px"},250);
	}).mouseleave(function(){
		// on mouseLeave remove open class and stop animation cancelling waiting animations and jump to end (old height)
		//$(this).removeClass('open').parent().css('z-index',0);
		$(this).parent().css('z-index','');
		var current_height = $('ul',this).height();
		$('ul',this).stop(true,true);		
		var height = $('ul',this).height();							   
		$('ul',this).height(current_height).animate({height:"0px"},150,function(){
			$(this).parent().parent().removeClass('open');
			$(this).height(height);
		});
	});
	// special case on topic pages, differant html structure
	$('#topic_country').mouseenter(function(){
		// on mouseEnter set open class, take height of ul set to 0 and animate to old height
		var current_height = $('ul',this).height();
		$('ul',this).stop(true,true);		
		$('.choice_maker',this).addClass('open').parent().css('z-index',100);
		var height = $('ul',this).height();
		if(current_height==height) current_height=0;
		$('ul',this).height(current_height).animate({height:height+"px"},250);
	}).mouseleave(function(){
		// on mouseLeave remove open class and stop animation cancelling waiting animations and jump to end (old height)
		//$(this).removeClass('open').parent().css('z-index',0);
		$('.choice_maker',this).parent().css('z-index','');
		var current_height = $('ul',this).height();
		$('ul',this).stop(true,true);		
		var height = $('ul',this).height();							   
		$('ul',this).height(current_height).animate({height:"0px"},150,function(){
			$(this).parent().parent().removeClass('open');
			$(this).height(height);
		});
	});
	// youtube video change code
	var playercopy = $('#player').html();
	$('#video_button').click(function(){
		$('#player').html(playercopy);
	});
	$('#video_nav a').click(function(){
		var oldV = $('#video_nav a.active').attr('href').substring($('#video_nav a.active').attr('href').indexOf('=')+1);
		var newV = $(this).attr('href').substring($(this).attr('href').indexOf('=')+1);
		var source = $('#player').html();
		if(source.indexOf('&autoplay=1')==-1) newV = newV + '&autoplay=1';
		while(source.indexOf(oldV)!=-1) source=source.replace(oldV,newV);
		$('#player').html(source);		
		$('#video_nav a.active').removeClass('active');
		$(this).addClass('active');
		return false;							 
	});	
});

$(function() {

	// setup button action. it will fire our overlay 
	$("#footer_video a[rel]").overlay({
		
		// use the Apple effect for overlay
		mask: {
		color: '#cedeeb',
		loadSpeed: 200,
		opacity: 0.9
	  },
		effect: 'apple',
		fixed: false,
		
		// when overlay is opened, load our player
		onLoad: function() {
			//player.load();
		},
		
		// when overlay is closed, unload our player
		onClose: function() {
			//player.unload();
			//$.expose.close();
			playercopy = $('#player').html();
		$('#player').html('');
		}
	});
	

			
  });


$(document).ready(function(){
                if( $('#content_detail').html().length<20 ) {
                               $('#content_detail').prev().find('A').click(function(){
                                               if( !$(this).attr('href').match( /\/[a-zA-Z]{2,2}\//g ) ) return false;
                                               var detailLang = $(this).attr('href').match(/\/[a-zA-Z]{2,2}\//g)[0];
                                               $.cookie("detailLang", detailLang, { path: "/" });                                        
                                               var standardContentUrl = $(this).attr('href');
                                               getDetailedContent(standardContentUrl);        
                                               $('.detailed_lang_tabs a.active').removeClass('active');
                                               $(this).addClass('active')                             
                                               return false;
                               });                           
                               var cookie_detail_lang = $.cookie("detailLang");
                               if(cookie_detail_lang==null){    
                                               var standardContentUrl = $('#content_detail').prev().find('A:first').attr('href');
                                               getDetailedContent(standardContentUrl);
                               }else{
                                               var flag = false;
                                               $('.detailed_lang_tabs a').each(function(){
                                                               if( $(this).attr('href').indexOf(cookie_detail_lang)!=-1 ) {
                                                                              var standardContentUrl = $(this).attr('href');
                                                                              getDetailedContent(standardContentUrl);
                                                                              $('.detailed_lang_tabs a.active').removeClass('active');
                                                                              $(this).addClass('active');
                                                                              flag = true;
                                                               }
                                               });
                                               if(flag==false){
                                                               var standardContentUrl = $('#content_detail').prev().find('A:first').attr('href');
                                                               getDetailedContent(standardContentUrl);
                                               }
                               }              
                }
});
function getDetailedContent(standardContentUrl){
                $.ajax({
                  type: "GET",
                  url: standardContentUrl,
                  success: function(data){
                               var zoekstr = '<div id="content_detail">';
                               var start = data.indexOf(zoekstr)+zoekstr.length;
                               var eind = data.indexOf("</div>",start);
                               var content = data.substr(start,eind-start);
                               $('#content_detail').html(content);
                  }
                });
}

