// JavaScript Document
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function() {
	setInterval("slideSwitch()", 5000 );

	/* Text Variables */
	txtDetails = (lang == 'fr' ? 'D&eacute;tails' : 'Details') + ' [+]';
	txtReduire = (lang == 'fr' ? 'R&eacute;duire' : 'Close') + ' [&minus;]';

	if ( $('.offer').length ) {

		/* If browser's JavaScript is deactivated or not supported, do not display dynamic elements */
		$('.offer .js').removeClass('js').addClass('accordion');

		/* Job offer hiding/revealing mechanism */
		$('.offer .entry').hide();

		$('.offer .accordion').toggle(
			function(){
				var e = $(this).parent().parent().children('.entry');
				$(this).html(txtReduire);
				$(e).slideDown();
			},
			function(){
				var e = $(this).parent().parent().children('.entry');
				$(this).html(txtDetails);
				$(e).slideUp('fast');
			}
		);

		if ( $('.highlight').length ) {
			var offset = $('.highlight').offset();
			$('.highlight .accordion').click().animate({ scrollTop: offset.top }, 500);
			$('html,body').animate({ scrollTop: offset.top }, 500);
		}

		$('select#job_position').change(function(){
			$('select option:selected').each(function(){
				var v = $(this).val();

				if (v == '0') {
					$('.offer .entry:visible').slideUp(function(){
						$('.offer .entry:hidden').prevAll('.options').children('.accordion').html(txtDetails);
					});
				} else {
					if ( $('.offer .entry:visible').length )
					{
						$('.offer:not([id="offer-'+v+'"]) .entry:visible').slideUp(function(){
							$('.offer .entry:hidden').prevAll('.options').children('.accordion').html(txtDetails);
							$('.offer[id="offer-'+v+'"] .accordion').html(txtReduire);
							$('.offer[id="offer-'+v+'"] .entry:hidden').slideDown();
						});
					} else {
						$('.offer[id="offer-'+v+'"] .accordion').html(txtReduire);
						$('.offer[id="offer-'+v+'"] .entry').slideDown();
					}
				}
			});
		}).change();

		$('a.select').click(function(){
			var id = $(this).attr('href').split('=');
			$('select#job_position').val(id[1]);
			return false;
		});

	}
});