/* ----------------------------------------------------------------------------
   These are common utilities used in other functions.
---------------------------------------------------------------------------- */
jQuery.fn.fadeHide = function(speed, easing, callback) {
	return this.animate({opacity: 'hide'}, speed, easing, callback); 
};

jQuery.fn.fadeShow = function(speed, easing, callback) {
  	return this.animate({opacity: 'show'}, speed, easing, callback); 
};

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i;
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}

/* ----------------------------------------------------------------------------
   rollover();
   This function is for rollover on Learn More link on results page;
   needed it to work on IE6. 
   Pass this on the ajax methods.
---------------------------------------------------------------------------- */
function rollover()
{
	$("#results-set a.rollover").each(function() 
	{			
		rollsrc = $(this).children("img").attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_over.gif");
		$("<img>").attr("src", rollON);
	});				
	$("#results-set a.rollover").mouseover(function()
	{
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/_over/);
		if (!matches)
		{
			imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
			$(this).parent("div").css({backgroundColor:"#f5f5f5"});
		}
	});
	$("#results-set a.rollover").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
		$(this).parent("div").css({backgroundColor:"transparent"});
	});
}
		
/* ----------------------------------------------------------------------------
   randomImage();
   This function is to rotate main images  on load of the page.
---------------------------------------------------------------------------- */
function randomImage()
{
	var randomImages = ["colleagues-talking","two-men-talking","woman-laughing","young-couple-smiling","young-man-reading"];
	var randomNum = Math.floor(Math.random() * randomImages.length);
	$("#header h1").css({background: "url(images/header/" + randomImages[randomNum] + ".jpg)"});
}


/* ----------------------------------------------------------------------------
   highlightWords();
   This section is to get glossary terms from PHP script, then put that array 
   into the term to 'highlight' those terms onload of the page.
---------------------------------------------------------------------------- */
// variable to pass from one method to another
var termsList;
// helper method which passes string from PHP to JS
function getGlossaryTerms(val)
{
	termsList = val;
	return termsList;
}
// main function
function highlightWords()
{
	//alert(termsList);
	//var glossary = new Array('AEI','Alberta works','AMA','APEGGA','BVC','CAMRT','CARNA','CBE','CCIS','CEAS','CGA','CIC','CIWA','CLB','CLBA','CRNE','CSSD','EI','ELT','ENFORM','Enform','ESL','Essential Skills','ICT','Immigrant Bridging Program','Integrated Training','IQAS','Job Placement Services','LINC','MCCEE','NPPE','OSCE','PEBC','Placement','SAIT','Work Experience/Practicum');
	var glossary = new Array();
	glossary = termsList.split(",");	
	for(i=0;i < glossary.length;i++)
	{
		if(glossary[i]!="") // need to fix, there is an empty item in the array, fix array or leave this
		{
			$('#search-results').html($('#search-results').html().replace(glossary[i]+" ","<a href='glossary.php?term="+glossary[i]+"' id='glossary-item-"+glossary[i]+"' rel='glossary.php?term="+glossary[i]+"' class='custom-width'><span class='base-link-1'>"+glossary[i]+"</span></a> "));
			$('#search-results').html($('#search-results').html().replace(glossary[i]+")","<a href='glossary.php?term="+glossary[i]+"' id='glossary-item-"+glossary[i]+"' rel='glossary.php?term="+glossary[i]+"' class='custom-width'><span class='base-link-1'>"+glossary[i]+"</span></a>)"));
		}
	}
}


/* ----------------------------------------------------------------------------
   togglePage();
   This section is highlight the page number in the pagination area
   of the results page.
---------------------------------------------------------------------------- */
// need this global variable to help set correct page number and programs
// on the return results page
var globalPageNumber = 1;
// used on the results page
function togglePage(pageNumber,resultsNumber,numberPerPage)
{	
	var thisNumber;
	for(i=1;i < resultsNumber + 1;i++)
	{
		$("#result-"+i+"").hide();
		$("#toggleResults"+i).css({ fontWeight:"normal", fontSize:"11px" });
	}
	for(j=1; j < numberPerPage + 1; j++)
	{
		thisNumber = (pageNumber * numberPerPage - numberPerPage) + j;
		$("#result-"+thisNumber+"").show();
	}
	$("#toggleResults"+pageNumber).css({ fontWeight:"bolder", fontSize:"12px" });
	
	globalPageNumber = pageNumber;
}
// used on details page to return back to results
function togglePageReturn(resultsNumber,numberPerPage)
{	
	var thatNumber;
	for(i=0;i < resultsNumber;i++)
	{
		$("#result-"+i+"").hide();
		$("#toggleResults"+i).css({ fontWeight:"normal", fontSize:"11px" });
	}
	for(j=0; j < numberPerPage; j++)
	{
		thatNumber = (globalPageNumber * numberPerPage - numberPerPage) + j + 1;
		$("#result-"+thatNumber+"").show();
	}
	$("#toggleResults"+globalPageNumber).css({ fontWeight:"bolder", fontSize:"14px" });
}
/* ----------------------------------------------------------------------------
   This section is for the ajax methods:
   1. Getting results set - getResults();
   2. Getting details of a specific program - getDetails();
   3. Returning to results page from the details page - returnResults();
---------------------------------------------------------------------------- */
// Loading image to show while ajax call is made
var loading = "<div style='text-align:center;width:660px;height:400px;padding:100px 0;'><img src='images/common/ajax-loading.gif' alt='loading' /></div>";
//
// 1. Getting results set 
//
function getResults(field,placement)
{
	globalPageNumber = 1;
	var fields = document.getElementById('search-programs').field_of_work;
	var fieldsChosen = getSelectedRadio(document.getElementById('search-programs').field_of_work);
	var fieldsValue = fields[fieldsChosen].value;
	var placements = document.getElementById('search-programs').program_work_placement;
	var placementsChosen = getSelectedRadio(document.getElementById('search-programs').program_work_placement);
	var placementsValue = placements[placementsChosen].value;
	$('#search-results').html(loading); 
	$.post('results.php', {field_of_work: fieldsValue, program_work_placement: placementsValue}, function(data) 
	{ 
		if(data.length > 0)
		{
			$("#search-results").html(data).fadeShow("slow");
			rollover();
		}
	});
}
//
// 2. Getting details of a specific program
//
function getDetails(program,field,placement,resultsnum,numberperpage)
{
	$('#search-results').html(loading); 
	$.post('details.php', {program_id: program, field_of_work: field, program_work_placement: placement, results_number: resultsnum, number_per_page: numberperpage}, function(data) 
	{
		if(data.length > 0)
		{							
			$('#search-results').html(data);					
			highlightWords();
			$('a.custom-width').cluetip({
				width:'334px', 
				showTitle: false,
				dropShadow: false, 
				positionBy: 'auto',
				arrows: false,
				topOffset: 15,
				leftOffset: 15
			});
			window.scrollTo(0,0);
			$('#search-results').show("slide", { direction: "left" });
		}
	});
	//
}
//
// 3. Returning to results page from the details page
//
function returnResults(field,placement,resultsnum,numberperpage)
{
	$('#search-results').html(loading); 
	//$('#search-results').hide("slide", { direction: "right" });
	$.post('results.php', {field_of_work: field, program_work_placement: placement}, function(data)
	{ 
		if(data.length > 0)
		{		
			$("#search-results").html(data); 			
			rollover();
			togglePageReturn(resultsnum,numberperpage);
			$('#search-results').show("slide", { direction: "left" });
		}
	});
}
// not used currently; in testing
function loadContent()
{
    $('#search-results').hide('fast',loadContent);  
    function loadContent() {  
        getResults();
		showNewContent();
    }  
    function showNewContent() {  
        $('#search-results').fadeIn('slow', hideLoader());  
    }  
    function hideLoader() {  
        $('#load').fadeOut('slow');  
    }  
    return false;  	
}
