if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: false,
	// init, something like a constructor
	init: function() {
		// Layout for 'standpunten'
		$('#issues.browse').masonry();

		// fix the a/img problem (generally)
		$('a > img').parent().css('border', 'none');

		// share
		jsFrontend.share.init();
		
		$(".imageList a[rel='videos']").fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '';
			}
		});
		
		$(".imageList a[rel='pictures']").fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
		
		// slideshow?
		if($('#slideshow').length > 0) jsFrontend.slideshow.init();
	},
	// end
	_eoo: true
}	

jsFrontend.share = {
	// init, something like a constructor
	init: function() {
		// links open in new window
		$('.shareList > li > a').each(function() { $(this).attr('target', '_blank'); });
	
		// hide all dropdowns (should already be done in css already however)
		$('.shareList').hide();
	
		// collapse/expand the "dropdown"
		var changeShareDropDown = function(show) {
			if (typeof(show) == 'undefined') show = false;
			if ($(this) != null && $(this).find('.toggleShareList').attr('id') == '') var show = true;
			$('.toggleShareList').attr('id', '');
			$('.shareList').hide();
			if (show) {
				$(this).find('.toggleShareList').attr('id', 'shareSelected');
				$(this).siblings('.shareList').show();
			}
		}
	
		// cancel default behaviour (don't bubble, don't propagate default action)
		var preventDefaultAction = function(e) {
			e.cancelBubble = true;
			e.returnValue = false;
			if (e.stopPropagation) {
				e.stopPropagation();
				e.preventDefault();
			}
		}
	
		// click handler: prevent default behaviour (following the "#"-link, bubbling through 'document' and thus closing the "dropdown")
		$('.share a').click(function(e) {
			if(!e) var e = window.event;
			preventDefaultAction(e);
		});
	
		// mousedown handler: open the "dropdown" when our mouse is down (this event is called before focus)
		$('.share a').mousedown(function(e) {
			changeShareDropDown.call(this.parentNode);
		});
	
		// focus handler: open "dropdown" on focus
		$('.share a').focus(function(e) {
			changeShareDropDown.call(this.parentNode, true);
		});
	
		// handle focusses: close "dropdown" when focus is no longer there
		$('*').focus(function(e) {
			if ($(this).parents('.share').length <= 0 && $(this).parents('.shareList').length <= 0) changeShareDropDown.call(null);
		});
	
		// click handler for 'anywhere in the document': close "dropdown" (but not if it's a rightclick)
		$(document).click(function(e) {
			if (!e) var e = window.event;
			var rightclick = false;
			if (e.which) rightclick = (e.which == 3);
			else if (e.button) rightclick = (e.button == 2);
			if (!rightclick) changeShareDropDown.call(null);
		});	
	},
	// end
	_eoo: true
}

jsFrontend.slideshow = {
	// datamembers
	useAnimation: true,
	current: 0,
	items: new Array(),
	interval: null,
	// init, something like a constructor
	init: function() {
		if($('#slideshowNavigation').length > 0) { jsFrontend.slideshow.load(); }
	},
	
	load: function() {
		$('#slideshowNavigation a').each(function() {
			var slide = new Object();
			
			slide.title = $(this).attr('rel').split('|')[0];
			slide.image = $(this).attr('rel').split('|')[1];
			slide.button = $(this).attr('rel').split('|')[2];
			slide.link = $(this).attr('rel').split('|')[3];

			// preload
			var image = new Image();
			image.src = slide.image
			
			jsFrontend.slideshow.items.push(slide); 
		});
		
		// init
		$('#slideshowSlideVisible img').attr('src', jsFrontend.slideshow.items[0].image);
		jsFrontend.slideshow.current = 0;

		if(jsFrontend.slideshow.items[jsFrontend.slideshow.current].type == 'video') $('#slideshow .playHolder').fadeIn(2250);
		$($('#slideshowNavigation a')[jsFrontend.slideshow.current]).parent().addClass('selected')

		$('#slideshowSlideVisible img').bind('click', function(evt) { jsFrontend.slideshow.stop(evt) });
		$('#slideshowNavigation a').bind('click', function(evt) { jsFrontend.slideshow.click(evt, (parseInt($(this).attr('href').split('#')[1]) - 1)); });

		// set timeout
		jsFrontend.slideshow.interval = setInterval('jsFrontend.slideshow.next()', 5500);
	},
	
	click: function(evt, index) {
		evt.preventDefault();
		clearInterval(jsFrontend.slideshow.interval);
		jsFrontend.slideshow.useAnimation = false;
		jsFrontend.slideshow.next(index);
	},
	
	next: function(newIndex) {
		// calc new index
		if(newIndex == undefined) newIndex = jsFrontend.slideshow.current + 1;
		else newIndex = newIndex;
		if(newIndex == jsFrontend.slideshow.items.length) newIndex = 0;

		// remove movie
		$('#slideshowMovie').remove();
		$('.imageHolder').removeClass('selected');
		
		// set new background
		$('#slideshowSlideLower img').attr('src', jsFrontend.slideshow.items[newIndex].image);
		$('#slideshowSlideVisible').removeClass('image').removeClass('video');
		$('#slideshowSlideVisible').addClass(jsFrontend.slideshow.items[newIndex].type);

		// fade out
		if(jsFrontend.slideshow.useAnimation) {
			$('#slideshowSlideVisible').fadeOut(3000, function() {
				$('#slideshowSlideVisible img').attr('src', jsFrontend.slideshow.items[newIndex].image);
				$(this).fadeIn();
			});
		} else {
			$('#slideshowSlideVisible img').attr('src', jsFrontend.slideshow.items[newIndex].image);
		}

		// set selected
		$('#slideshowNavigation li').removeClass('selected')
		$($('#slideshowNavigation a')[newIndex]).parent().addClass('selected')
		$('#slideshowButton a').attr('href', jsFrontend.slideshow.items[newIndex].link);
		$('#slideshowButton a .middle').html(jsFrontend.slideshow.items[newIndex].button);
		
		// reset current
		jsFrontend.slideshow.current = newIndex;
	},
	
	stop: function(evt) {
		evt.preventDefault();
		clearInterval(jsFrontend.slideshow.interval);
		jsFrontend.slideshow.useAnimation = false;
	},
	
	// end
	_eoo: true
}

$(document).ready(function() { jsFrontend.init(); });