var Uitgesproken = {};

jQuery.fn.limitMaxlength = function(options){

	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keyup(onEdit)
				.keydown(onEdit)
				.focus(onEdit)
				.live('input paste', onEdit);
}

jQuery().ready(function()
{

  // max input reacties
  var onEditCallback = function(remaining){
		$(this).siblings('.charsRemaining').text("Characters remaining: " + remaining);

		if(remaining > 0){
			$(this).css('background-color', 'white');
		}
	}

	var onLimitCallback = function(){
		$(this).css('background-color', 'red');
	}

	$('textarea[maxlength]').limitMaxlength({
		onEdit: onEditCallback,
		onLimit: onLimitCallback
	});


	// twitter
        var options = {
                //hier config-opties
                username: [],
                searchterm: [],
                hashtag: "uitgesprokeneo",
                count: 4,
                refresh_rate: 300
            };
        $("#twitter").twitter(options);
        
	// Accordion
	$('.accordion').each(function(key, wrapper)
	{
		var togglers = $('.toggler', wrapper);
		var elements = $('.element', wrapper);
		
		$(elements).each(function(elKey, toggledElement)
		{
			var html = $(toggledElement).html();
			$(toggledElement).html('<div>' + html + '<div class="clear"></div></div>');
			if (elKey > 0){ $('div', toggledElement).hide(); }
			else { $(togglers[elKey]).addClass('active'); }
		});
		$(togglers).each(function(tKey, toggler)
		{
			$(toggler).click(function()
			{
				$(elements).each(function(eKey, el)
				{
					if (eKey != tKey)
					{
						$('div', el).slideUp();
						$(togglers[eKey]).removeClass('active');
					}
				});
				
				$(this).addClass('active');
				$('div', elements[tKey]).slideDown();
				return false;
			});
		});
	});
	
	// Tabs
	$('.tabs').each(function(key, tabwrapper)
	{
		var buttons = $('ul.tabnav li a', tabwrapper);
		var tabs = $('div.tab', tabwrapper);
		
		$(buttons).each(function(bKey, button)
		{
			$(button).click(function()
			{
				$(tabs).hide();
				$(tabs[bKey]).show();
				
				$(buttons).parent().removeClass('active');
				$(button).parent().addClass('active');
				return false;
			});
			if (bKey == 0)
			{
				$(button).trigger('click');
			}
		});
	});
	
	// Reset InputFields
	$('.resetInput').each(function(key, input)
	{
		var defaultValue = $(input).val();
		$(input).click(function()
		{
			if ($(this).val() == defaultValue){ $(this).val(''); }
		});
		$(input).blur(function()
		{
			if ($(this).val().length == 0){ $(this).val(defaultValue); }
		});
	});
	
	// Custom Dropdown	
	$('.custom_dropdown').each(function(key, dropdown)
	{
		if (jQuery.browser.msie && jQuery.browser.version > 0 && jQuery.browser.version < 7){ return false; }
		$(dropdown).wrap('<div class="custom_dropdown_wrapper"></div>');		
		$(dropdown).before('<span class="label"></span>');
		
		$(dropdown).change(function()
		{
			$('span.label', $(dropdown).parent()).text($('option:selected', dropdown).text());
		});
		$(dropdown).trigger('change');
		
		$(dropdown).hover(
			function (){ $(this).parent().addClass('hover'); },
			function (){ $(this).parent().removeClass('hover'); }
		);	
	});
	
	$('.shortenText').each(function(key, link)
	{
		var open = false, smallHeight = 40;
		var element = $('#' + $(link).attr('rel'));
		var height = $(element).height();
		$(element).css(
		{
			height: smallHeight,
			overflow: 'hidden',
			'margin-bottom': 16
		});
		$(link).click(function()
		{
			if (open)
			{
				$(element).animate({ height: smallHeight });
				open = false;
				$(link).text('Lees meer').removeClass('actv');
			}
			else
			{
				$(element).animate({ height: height });
				open = true;
				$(link).text('Lees minder').addClass('actv');
			}
			return false;
		});
	});
	
});
