$(document).ready(function() 
{
	positionHomepageUserContent();
	setNavBehaviour();
	duplicateVacanciesPagebrowser();
	setTafDisplay();
});

function duplicateVacanciesPagebrowser()
{
	if ( $('div.dmmjobcontrol_pagebrowser').length > 0 )	
	{
		$('dl.dmmjobcontrol_dl').after('<div class="dmmjobcontrol_pagebrowser">' 
			+ $('div.dmmjobcontrol_pagebrowser').html() + '</div>');
	}
}

function positionHomepageUserContent()
{
	if ( $('#usercontent').length > 0 )
	{
		var newContent	= '';
		var watchUrl	= '';
		var image		= '';
		var imageFound	= '';
		$('p.bodytext').each(function (i) 	// loop trough all p.bodytext elements
		{
			if ( $(this).find('img').attr('src') ) 
			{
				// When the element contains an image, retrieve hyperlink and image source, paste into dt-element
				imageFound	= true;
				image		= '';
				if ( $(this).find('a').attr('href') )
				{
					watchUrl	= '<a class="more" href="' + $(this).find('a').attr('href') 
										+ '">Watch movie <span>&raquo;</span></a>';
					image		= '<a href="' + $(this).find('a').attr('href') + '">' 
										+ '<img src="/fileadmin/new-abroad-experience.com/img/ico/watch-movie.png"' 
										+ ' alt="" /></a>';
				}

				newContent += '<dt><div style="background-image: url(' 
					+ $(this).find('img').attr('src') + ')">' + image + '</div></dt>';
			}
			else
			{
				// when no image found, it probably has to be plain text, retrieve text and paste into dd-element
				if ( watchUrl == '' )
				{	// when image has not been linked, try to find a link in plain text and use anchor for "watch movie" hypertext
					if ( $(this).find('a').attr('href') )
						watchUrl = '<a class="more" href="' + $(this).find('a').attr('href') + '">Read more <span>&raquo;</span></a>';
				}
			
				// concatenate string, reset variables
				newContent += "\r\n\t\t" + '<dd' + ( imageFound ? ' class="margin"': '' ) + '>' + watchUrl + '' + $(this).html() + '</dd>';
				watchUrl 	= '';
				imageFound	= false;
			}
		});

		if ( newContent != '' )
		{	// show definition list when created
			$('#usercontent').before('<dl>' + newContent + '</dl>');
			$('#usercontent').remove(); //.css('display', 'none');
		}
	}
}

function setNavBehaviour()
{
	$('#navigation > li').hover(
		function () 
		{
			$(this).find('ul').show();
		}, 
		function () 
		{
			$(this).find('ul').hide();
		}
    );
}

function setTafDisplay()
{
	if ( $('form[name=tell_a_friend]').length > 0 && gup('action') != 'tell_a_friend' )
		toggleTaf();
}

function toggleTaf()
{
	var tafDisplay	= $('form[name=tell_a_friend]').css('display') != 'none' ? 'none': 'block';
	$('form[name=tell_a_friend]').css('display', tafDisplay);
}

function gup( name )
{
	// retrieve url paramteres (after ?)
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}