// default language
var LANG_OPEN_SEARCH_OPTIONS = 'Open search options';
var LANG_CLOSE_SEARCH_OPTIONS = 'Close search options';
var LANG_OPEN_JOBS = 'Open all jobs';
var LANG_CLOSE_JOBS = 'Close all jobs';
var LANG_OPEN_JOB = 'Open job';
var LANG_CLOSE_JOB = 'Close job';

// bootstrap onload
$(document).ready(function ()
{
	var job = $('#jobs-open');
	var jobs = $('#page .job');
	var allToggle = false;

	if (job && jobs)
	{
		// global toggle
		job.click(function ()
		{
			allToggle = !allToggle;
			job.html(allToggle ? LANG_CLOSE_JOBS : LANG_OPEN_JOBS);
			jobs.each(function (i)
			{
				(allToggle
					? $(this).children('.animate').slideDown()
					: $(this).children('.animate').slideUp()
				);

				(allToggle
					? $(this).children('.footer').children('.toggle').html(LANG_CLOSE_JOB)
					: $(this).children('.footer').children('.toggle').html(LANG_OPEN_JOB)
				);
			});

			return false;
		});

		// toggling for job items
	    jobs.each(function (i)
	    {
		    // toggle description
	    	$(this).children('h4').click(function()
	    	{
	    		$(this).parent().children('.footer').children('.toggle').html(
	    			$(this).parent().children('.footer').children('.toggle').html() == LANG_CLOSE_JOB
	    				? LANG_OPEN_JOB
	    				: LANG_CLOSE_JOB
	    		);

	    		$(this).parent().children('.animate').slideToggle();
	    		return false;
	    	});

	    	$(this).children('.footer').click(function()
	    	{
	    		$(this).children('.toggle').html(
	    			$(this).children('.toggle').html() == LANG_CLOSE_JOB
	    				? LANG_OPEN_JOB
	    				: LANG_CLOSE_JOB
	    		);

	    		$(this).parent().children('.animate').slideToggle();
	    		return false;
	    	});
	    });
	}

    $('#page .faq').each(function (i)
    {
    	$(this).click(function()
    	{
    		// close all open entries
    		$('#page .faq p').each(function (i)
    		{
    			$(this).slideUp();
    		});

    		// open clicked entry
    		$(this).children('p').slideToggle();

    		// stop event propagation
    		return false;
    	});
    });

    // toggling for search form
    $('#search .toggle').each(function (i)
    {
    	$(this).click(function()
    	{
    		var v = $('#search .extended').is(':visible');
    		var d = v ? '-=300' : '+=300';
    		$('#sidebar>.bg').animate({ height: d}, 400);
    		$('#search .extended').slideToggle();
			$(this).html(v ? LANG_OPEN_SEARCH_OPTIONS : LANG_CLOSE_SEARCH_OPTIONS);

    		/*, function ()
    		{
    			if ($(this).is(':visible'))
    			{
    				$('#sidebar>.bg').height($('#sidebar>.bg').height() + $(this).height());
    			}
    			else
    			{
    				$('#sidebar>.bg').height($('#sidebar>.bg').height() - $(this).height());
    			}
       		});
       		*/
    		return false;
    	});
    });

    // open by default if user searched
    if ($('#search .open').length > 0)
    {
		$('#search .extended').css({ display: 'block'});
		$('#sidebar>.bg').height($('#sidebar>.bg').height() + 300);
	}
    
    $('a.external').each(function (idx, elem) {
    	$(elem).attr('target', '_blank');
    });
});

