$(document).ready(function() {
	
	/* lower the fontsize of the name area in the progress widget */
	/* to prevent the widget from breaking when people have a very long name */
	var fontSize = 100;
	
	while ($('#fullName span').height() > $('#fullName').height() && fontSize > 10)
	{
		fontSize = fontSize - 10;
		$('#fullName span').css('font-size', fontSize + '%');
	}
	
	/* check if the last 5 blog messages fit inside the div */
	/* if not remove the last */
	while ($('#lastBlogMessagesInnerContainer').height() > $('#lastBlogMessagesContainer').height())
	{
		$('.lastBlog:last').remove();
	}
	
	while ($('#forumInnerContainer').height() > $('#forumTopics').height())
	{
		$('#forumInnerContainer > dl:last').remove();
	}
	
	/* fancybox */
	$('.useFancyBox').fancybox();
	
	/* tooltip */
	$('.toolTip').tooltip({
		showURL: false,
		positionRight: true,
		bodyHandler: function() {
			var hovertext = $(this).attr("hovertext");
			hovertext = hovertext.replace("&lt;","<");
			hovertext = hovertext.replace("&gt;",">");
			return hovertext;
		}
	});
	
	/* Blog delete confirm */
	$('li.blog_delete > a').click(function() {
		if (!confirm ("Weet u het zeker dat u dit bericht wilt verwijderen?"))
		{
			return false;
		}
	});
	
	/* RIGHTNOW HACKS */
	path = $(location).attr('pathname');
	if ( path.substr (0, 7) == '/vragen' && path.substr (7, 4) != '/ask' )
	{
		var tmp2 = new RegExp("useFancyBox");
		$('a').each (function () {
			tmp = $(this).attr('href');
			if (tmp && tmp.substr(0, 8) != '/vragen/' && tmp != '#' && !tmp2.test($(this).attr('class')))
			{
				$(this).attr('href', 'javascript:void(0);');
				$(this).attr('onclick', 'document.location.href="' + tmp + '"');
			}
		});
	}
	
	if ( path.substr (0, 7) == '/vragen' )
	{
		$('#helpButton').css ('display', 'none');
		
		var val1 = new RegExp("print");
		var val2 = new RegExp("popup");
		var val3 = new RegExp("search");
		if (val1.test (path) || val2.test (path) || val3.test (path))
		{
			$('body').css ('background', 'none');
			$('body').html ( $('#rn_content').html () );
		}
	}
});

/* Cookie functions from http://www.w3schools.com/JS/js_cookies.asp  */
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

/* FORMAT ERROR MESSAGES TO MAKE JQUERY VALIDATE LOOK AND FEEL LIKE EXTJS */
function formatError(errorMsg)
{
	return "<img class='errorImage' src='/themes/default/images/exclamation.gif' hovertext='" + errorMsg + "' />";
}

/* Password strength checker */
function passwordStrength (strength)
{
	s = '';
	color = '#ffffff';
	document.getElementById('strength_bar').style.width = (10 * strength) + '%';
	switch (strength)
	{
		case '0': color = '#ffffff'; s = 'ongeldig'; break;
		case '1': color = '#ff0000'; s = 'zwak'; break;
		case '2': color = '#fd4141'; s = 'zwak'; break;
		case '3': color = '#ff6000'; s = 'zwak'; break;
		case '4': color = '#e67b3b'; s = 'middel'; break;
		case '5': color = '#e69f3b'; s = 'middel'; break;
		case '6': color = '#ffb400'; s = 'middel'; break;
		case '7': color = '#fcff00'; s = 'middel'; break;
		case '8': color = '#aeff00'; s = 'sterk'; break;
		case '9': color = '#7cee30'; s = 'sterk'; break;
		case '10': color = '#00ff06'; s = 'sterk'; break;
	}
	document.getElementById('strength_bar').style.background = color;
	document.getElementById('strength_word').innerHTML = s;
}
