// JavaScript Document


$(function() {
	//$('.ac_links').hide();
	$('#search').jInputLabel({str:'Keywords'});
	$('#formBtnG').click(function() { 
		var valSearch = $("#search").val();
		if (valSearch == 'Keywords' || valSearch == '') {
			alert("Please enter one or more keywords");
			return false;
		} else {
			document.forms['cse-search-box'].submit();
		}
		
		//subscribe('<?php echo $base_url;?>', $('#email').val(), 'fr'); 
	});
	
	
	$('ul.accordionBlock ul').hide();
	$('ul.accordionBlock h5 a').click(function(e) {
		e.preventDefault();
		
		var index = $('ul.accordionBlock h5 a').index(this);

		$('ul.accordionBlock h5 a').each(function(i) {
			if (index == i)
			{
				
				var $ul = $(this).parent().next('ul:first')
				var $li = $ul.parent();
				if ($ul.is(':visible'))
				{
					$ul.slideUp('slow');
					$li.removeClass('open');
				}
				else
				{
					$ul.slideDown('slow');
					$li.addClass('open');
				}
			}
			else
			{
				var $ul = $(this).parent().next('ul:first')
				var $li = $ul.parent();
				if ($ul.is(':visible'))
				{
					$ul.slideUp('slow');
					$li.removeClass('open');
				}
			}
		});
	});
});

// Function for printing the page
function printPage() { print(document); }

// Subscribe a user to the newsletter
function subscribe(base_url, email, lang)
{
	$.post(base_url+'ajax/newsletter_subscribe', {email:email, lang:lang}, function(data) {
		if (data == '0')
		{
			$('#subscribeBox').html('<p>Merci. Votre courriel a été ajouté à la liste d\'envoi.</p>');
		}
		else
		{
			alert(data);	
		}
	});
}

// JS for the project / intitiatives pages
function update_highlite(year)
{
	if (year == 0)
	{
		$('#afficheToutBtn').css('visibility', 'hidden');
		// Unhighlite all columns
		$('.selected').removeClass('selected');
		//$('#rightCol .textSegment').show(); // Use if sorting by year
		$('ul.projectLinksList li').show(); // Use if sorting by title
		return false;
	}

	// Highlite the button
	$('#yearLinks .subColumnSmall a').each(function() {
		if ($(this).attr('id') == 'highliteBtn_'+year)
		{
			$(this).parent().addClass('selected');
		}
		else
		{
			$(this).parent().removeClass('selected');
		}
	});

	$('#afficheToutBtn').css('visibility', 'visible');

	// Highlite the column
	$('#projectBoxes .subColumnSmall').each(function() {
		if ($(this).attr('id') == 'column_'+year)
		{
			$(this).addClass('selected');
		}
		else
		{
			$(this).removeClass('selected');
		}
	});

	// Show the result
	// use this if we are sorting by year
/*	$('#rightCol .textSegment').each(function() {
		if ($(this).attr('id') == 'textSegment_'+year)
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}
	});*/

	// use this if we are sorting by title
	$('ul.projectLinksList li').each(function() {
		if ($(this).hasClass('projectYear_'+year))
		{
			//$(this).show();
			$(this).addClass('selected');
		}
		else
		{
			//$(this).hide();
			$(this).removeClass('selected');
		}												
	});
	
	
	//$('a.leftArrow_'+year).addclass('selected');
	$('#yearLinks a.navRightArrow, #yearLinks a.navLeftArrow').each(function() {
		if ($(this).hasClass('arrow_'+year))
		{
			$(this).addClass('selected');
		}
		else
		{
			$(this).removeClass('selected');
		}
	});
	
}

// ID - Class reference
/*highliteBtn_ /
textSegment_ / textSegment
column_ / subColumnSmall*/