
var globals = {};

function initFlowplayerMovies(){
	$(".flowplayer").each(function(){
		flowplayer($(this).attr('id'), {
			src:siteroot+"swf/flowplayer-3.1.5.swf",
			wmode:'opaque'
		}, {
			clip:{
				url:$(this).attr('title'),
				autoPlay:false,
				autoBuffering:false
			}, plugins:{
				controls:{
					backgroundColor:'#999999',
					timeColor:'#ffffff',
					volume:false
				}
			}
		});
	}).removeClass('flowplayer');
}

function initFancybox(){
	if($(".playlist a.overlayed").length > 0){
		$(".playlist a.overlayed").fancybox({
			'hideOnContentClick' : true,
			'padding' : 0,
			'zoomSpeedIn' : 350
		}).removeClass('overlayed');
	}
}

$(function(){
	/* FOLDEDCORNER */
	$("body .foldedcorner img").pngfix();
	globals.foldedcorner = new FoldedCorner();
	
	/* KALENDER */
	function formatDate(dateObj){
		return dateObj.getFullYear()+"-"+(dateObj.getMonth()+1)+"-"+dateObj.getDate();
	}
	$(".agenda").fullCalendar({
		monthNames: new Array("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"),
		dayNamesShort: new Array("zo","ma","di","wo","do","vr","za"),
		firstDay: 1,
		timeFormat: {'':'HH:mm'},
		events: function(start, end, callback){
		//	if(true || $(".agenda").hasClass("noload")){
				$.getJSON(siteroot+"ajax/events", {
					start: formatDate(start),
					end: formatDate(end)
				}, function(result){
					callback(result);
					$(".fc-event.event"+$("input[name=current_event_id]").val()).addClass("active");
				});
//				$(".agenda").removeClass("noload");
				
		//		return;
		//	}
		/*
			$(".right_content_container").empty()
				.append("<div class='agenda_spacer'>&nbsp;</div>")
				.append("<div class='agenda_loader'>Agenda items laden...</div>");
			
			$.getJSON(siteroot+"ajax/events", {
				start: formatDate(start),
				end: formatDate(end)
			}, function(result){
				$(".right_content_container").empty()
					.append("<div class='agenda_spacer'>&nbsp;</div>")
					.append("<p><i>Selecteer een agenda item</i></p>")
					.append("<a href='"+siteroot+"agenda/item/toevoegen'><img src='"+siteroot+"images/bnrs/incentive2.gif'/></a>");
				callback(result);
			});*/
		}, eventClick: function(item){
			document.location = "#event_"+item.id;
//			renderEvent(item.id);
		}
	});
	$(".fc-button-prev a span").text('«');
	$(".fc-button-next a span").text('»');
	function renderEvent(eventId){
		$(".fc-event").removeClass("active");
		
		$(".right_content_container").empty()
			.append("<div class='agenda_spacer'>&nbsp;</div>")
			.append("<div class='agenda_loader'>Agenda item laden...</div>")
			.load(siteroot+'ajax/event/'+eventId, function(){
				$(".fc-event.event"+eventId).addClass("active");
				var dates = ($(".agenda_item span.date").text()).split('-');
				if(dates && dates.length == 3){
					$(".agenda").addClass("noload").fullCalendar('gotoDate', new Date(dates[2], dates[1]-1, dates[0]));
				}
			});
	}
	
	/* PAGES SORTER */
	if(typeof $("#sortable").sortable != 'undefined'){
		$("#sortable").sortable();
		$("#sortable").disableSelection();

		$("#wa_form_pagessorter").submit(function(){
			var count = 0;
			$("#wa_form_pagessorter li input").each(function(){
				$(this).val(count);
				count++;
			});
			return true;
		});
	}
	
	/* GOOGLEMAPS */
	globals.googlemap = new GoogleMap();
	
	/* SOUNDMANAGER */
	//soundManager.url = siteroot+'swf/';
	//soundManager.debugMode = false;
	
	/* MOVIES */
	initFlowplayerMovies();
	
	/* TOOLTIP */
	if($.browser.msie &&  $.browser.version < 7) {
		//$(".tooltip").css('background-image','url("'+siteroot+'images/tooltip.gif")');
		
		var shadow_height = $(".body_container").height();
		$(".body_container").css('height', shadow_height+'px');
	//	$(".shadow_left, .shadow_right").css('height', shadow_height+'px');
	}
	var tooltop = $(".show_tooltip:contains(...)").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fixPNG: false,
		showBody: false,
		extraClass: "balloon"
	//	top: 60,
	//	left: 30
	});
	/*var tooltip = $(".show_tooltip").tooltip({
		tip: '.tooltip',
		offset: [-5,0]
	});*/
//	$(".tooltip").mouseover(function(){
	//	$(this).hide();
//	});
	
	/* OVERLAY IMAGES */
/*	if($(".playlist a.overlayed").length > 0){
		$(".simple_overlay .next, .simple_overlay .prev, .simple_overlay .info, .simple_overlay .close").css('opacity', 0.8);
		$(".playlist a.overlayed").overlay({
		    target: '#image_overlay'
		}).gallery({
		    speed: 1
		});
	}*/
	
	initFancybox();
	
	/* SIFR */
	if(typeof sIFR == "function"){
		/* FIX SIFR PADDING BUG */
		$("h1").css('width', ($("h1").width() - 20) + 'px' );
	
		sIFR.replaceElement("h1", named({
			sFlashSrc: siteroot+"swf/circle20.swf",
			sWmode: 'transparent'
		}));
		sIFR.replaceElement("h2", named({
			sFlashSrc: siteroot+"swf/circle20.swf",
			sWmode: 'transparent'
		}));
		/*
		$(".menu a").each(function(){
			var pos = $(this).position();
			$(this).after(
				$("<div></div>").addClass("item").text($(this).text())
			).css({
				'position':'absolute',
				'top':'0px',
				'color':'#db4342',
				'left': pos.left+'px'
			});
		});
		sIFR.replaceElement(".menu div.item", named({
			sFlashSrc: siteroot+"swf/circle20.swf",
			sWmode: 'transparent',
			sColor: '#ffc2c2'
		}));
		$(".menu a").each(function(){
			var width = $(this).next("div").width();
			var pos = $(this).next("div").position();
			$(this).text('_').css({
				'width': width+'px',
				'left': pos.left+'px'
			});
			
		});*/
		
//		sIFR.setup();
//		sIFR.debug();
	};
	
	/* SUBJECTS ON DOSSIERPAGE */
	$(".subjects:not(.nojs) a.subject").click(function(){
		$(".subjects .pages.open").show().slideUp('normal');
		
		if($(this).hasClass('open')){
			$(".subjects .open").removeClass('open');
		} else {
			$(".subjects .open").removeClass('open');
			$(this).addClass('open');
			$(this).parent().next('.pages').addClass('open').hide().slideDown('normal');
		}
		return true;
	});
	function onHashChange(hash){
		$(".subjects:not(.nojs) .pages.open").hide();
		$(".subjects:not(.nojs) .open").removeClass('open');
		var url = document.location.toString();
		if(url.match("#subj[0-9]+")){
			var alink = $('.subjects:not(.nojs) a[href="#' + url.split('#')[1] + '"]');
			alink.addClass('open').parent().next('.pages').show().addClass('open');
		}
		var event = url.match("#event_[0-9]+");
		if(event){
			var event = (event[0].split('_')[1]);
			renderEvent(event);
		}
	}
	onHashChange();
	$(window).hashchanged(onHashChange);
	
	/* FORM SPECIFIC */
	$("select[name$=_dossier]").change(function(){
		/* when dossier-page-form dossier dropdown is changed */
		$("select[name$=_subject]").load(siteroot+'ajax/dossier_subject_selector', {
			id: $(this).val()
		})
	});
	
	/* form button which links to an url */
	$("input[type=button][name^=/]").click(function(){
		if(confirm($(this).attr('alt')+'?')){
			document.location = $(this).attr('name');
		}
		return false;
	});
	
	/* leaving the form without saving */
	$("input[type=submit][name$=_action_nextstep]").click(function(){
		var file = $('input[type=hidden][name$=new_0_original]');
		var ready = $('input[type=submit][name$=_action_ready]');
		if( file.length>0 && file.val() != '' && ready.length==0 ){
			return confirm('Weet u zeker dat u deze stap wilt overslaan?\n\nHet fragment dat u nu ingevoerd heeft is nog niet opgeslagen. Gebruik de knop opslaan om dit te doen.');
		}
		return true;
	});
	
	/* magic comments */
	$("#wa_form_comment_form_loggedin, #wa_form_comment_form_notloggedin").each(function(){
		var action = $(this).attr('action');
		$(this).attr('action', action+"#comments");
	});
	
	/* TAG AUTOCOMPLETE */
	$("input[name$=tagstr]").bind("keydown", function(e){
		return (e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which) != 13;
	}).autocomplete(siteroot+'ajax/tagcomplete', {
		width: '280px',
		dataType:'json',
		multiple: true,
		matchContains: true,
		multipleSeparator: ",",
		scrollHeight: 300,
		parse: function(data){
			var len = data.length;
			for(var i=0;i<len;i++){
				data[i] = {
					data: data[i],
					value: data[i].name,
					result: data[i].name
				}
			}
			return data;
		},
		formatItem: function(row){
			return row.name;
		}
	});
	
	/* TAGCLOUD */
	$("ul.tags").tagcloud({
		type:"list",
		sizemin:10,
		sizemax: 20,
		colormax:"004a73",
		colormin:"0080c6"
	}).find("ul.tags li.tag");
	
	/* BRABANT VERKENNER */
	if($(".brabant_verkenner").length > 0){
		$(".body_container").css('height', $(window).height()+'px');
		$(".body_container").animate({
			width:'100%'
		}, 'slow', function(){
			$(".shadow_left, .shadow_right, .shadow_bottom").remove();
			$(window).resize();
			try{globals.googlemap.reCenter();}catch(e){}
		});
		
		$(window).resize(function(){
			$(".body_container").css('height', $(window).height()+'px');
			$(".brabant_verkenner").css({
				'height': ($(window).height()-240)+'px',
				'width': ($(window).width()-20)+'px'
			});
			try{globals.googlemap.map.checkResize();}catch(e){}
		});
	}
	
	/* RSS feed magic */
	$(".rssfeed").each(function(){
		var refElm = this;
		var target = $(this).attr('target');
		var getData = {};
		if(target == 'vpro'){
			getData = {
				'url': 'http://geschiedenis.vpro.nl/themasites/rss/magazine.jsp',
				'rssnr': '23219239'
			}
		} else if(target == 'brabantsheem'){
			getData = {
				'url': 'http://www.brabantsheem.nl/feed'
			}
		} else if(target == 'bhic'){
			getData = {
				'url': 'http://www.bhic.nl/rss/wijzigingen.rss.php',
				'per': '14',
				'nws': '1',
				'vhl': '1',
				'rel': '0'
			}
		} else {
			return;
		}
	
		$.getFeed({
			url: getfeed_script,
			data: getData,
			success: function(feed){
			//feed.link
				$(refElm).append( $('<h2></h2>').text(feed.title).append( $('<a></a>').attr('href', feed.link).text('link').addClass('rss float_right') ) );
				var result = $('<ul></ul>');
				for(var i = 0; i < feed.items.length && i < 5; i++){
					var item = feed.items[i];
					var theDate = new Date(item.updated);
					var niceDate = theDate.getDate()+'/'+(theDate.getMonth()+1)+'/'+theDate.getFullYear();
					var niceText = item.description
						.replace( new RegExp('(<([^>]+)>)', 'ig'), ' ')
						.replace( new RegExp('&nbsp;', 'ig'), ' ')
						.replace( new RegExp('&#822[01];', 'ig'), '"')
						.replace( new RegExp('&[^;]+;', 'ig'), '');
					result.append(
						$('<li></li>').append(
							$('<div></div>').append(
								$('<a></a>').attr('href', item.link).attr('target', '_blank').text(item.title)
							).addClass('title')
						).append(
							$('<div></div>').text(niceDate+' - '+niceText)
						)
					);
				}
				$(refElm).append(result);
	 	 	}
		});
	});
	
	/* minimum height left column */
	var right_height = 0;
	$(".right_content_container").each(function(){
		right_height += $(this).height();
	});
	if(right_height > 0){
		$(".left_content_container:last").css('min-height', right_height+'px');
	}
	
});


	  
