

$( function()
{
	
	// IE6?
	var ie = /MSIE 6/.test( navigator.userAgent );

	fixHeight();
	
	if (!ie) $(window).bind('resize scroll', function() { fixHeight(); });
	
	// Set action for [meer] button to open the full item
	$('.meer').click( function()
	{
		// Set aliases
		var t = $(this);
		var o = t.parent().parent();	// Offset parent
		var i = o.find('.extra_image');
		var m = o.find('.moretext');	// More
		var am = $('.moretext');		// All more
		var at = $('.meer');			// All [meer]'s
		var ai = $('.extra_image');		// All images
		
		// Close all moretext's, show all [meer] buttons and hide the current
		ai.hide(); am.hide(); at.show(); t.hide();
		
		// Open the full text and fix the height after
		m.fadeIn( 1000, function(){ fixHeight(); });
		i.fadeIn( 1000, function(){ fixHeight(); });
		 
		//return false;
	});
	
	
	$('.filter_opdrachtgever, .filter_categorie')
		.change( function() {
			$('form[name="filters"]').submit();
	});
	
	// Set timeout on fixHeight function to fix late elements
	//setTimeout( fixHeight, 1500 );
	
	// Check if the #view-more tag isset
	var viewmore = /#view-more/.test( window.location.href );
	
	// Actions for unpopular tags
	if ( $('.unpop').length > 0 )
		$('.view-all').show();
	else
		$('.clear').css( 'marginTop', '3.4em' );
	
	if ( $('.unpop .active_tag').length > 0 || viewmore )
	{
		$('.unpop').show();
		$('.view-all img:first').hide();
		$('.view-all img:last').show();
		$('.clear').attr( 'href', $('.clear').attr('href')+'#view-more' );
		$('.tags li a').each( function() {
			var href = $(this).attr('href');
			$(this).attr('href', href+'#view-more');
		});
	}
	
	$('.view-all').click( function()
	{
		// Get the current href of clear
		var href = $('.clear').attr( 'href' );
		var href_wo_viewmore = (href) ? href.replace( '#view-more', '' ) : false;
		
		// Check existence of #view-more tag in the href
		if (href)
			var viewmore_isset = /#view-more/.test( href );
	
		if ( $('.unpop:hidden').length > 0 )
		{
			$('.unpop').show();
			$('.view-all img:first').hide();
			$('.view-all img:last').show();
			
			if ( !viewmore_isset )
				$('.clear').attr( 'href', href+'#view-more' );
				
			// Fix the tags
			$('.tags li a').each( function() {
				var href = $(this).attr('href');
				if ( ! /#view-more/.test(href) )
					$(this).attr('href', href+'#view-more');
			});
		}
		else
		{
			$('.unpop').hide();
			$('.view-all img:first').show();
			$('.view-all img:last').hide();
			
			if ( viewmore_isset )
				$('.clear').attr( 'href', href_wo_viewmore );
			
			$('.tags li a').each( function() {
				var href = $(this).attr('href');
				var href_new = href.replace('#view-more', '');
				$(this).attr('href', href_new);
			});
		}
			
		return false;
	});
});

$(document).ready(function() {
    $('#sidebar').scrollToFixed(  );
});



