/**
 * @author Tyler Gaw <tylerg@arc90.com>
 *
 * Core Javascript document for the GradeGuru marketing site.
 *
 */


$(document).ready(function()
{
	/**
	 * Initialize FB Connect
	 * @var $FBKey and $FBXD set in <head>
	 */
	 FB.init($FBKey, $FBXD);
	
	/**
	 * Override a couple site-wide colorbox defaults
	 */
	$.fn.colorbox.settings.opacity    = 0.28;
	$.fn.colorbox.settings.transition = 'none';
	$.fn.colorbox.init();
	$('#nav-search-field').smartinput();
	
	PrepareExternalLinks();
	

	/**
	 * Home Page Specific
	 */
	$('#news-ticker ul').innerfade({ speed: 750, timeout: 5000, containerheight: '14px' });
	$('#home-callout-link-more').colorbox({ initialWidth:100, initialHeight:100, transition:'elastic' });
	$('#guru-container em, #guru-container .guru-discipline').truncate({max_length: 32});
	$('#guru-container').transporter({ nextControl: '#arrow-next', prevControl: '#arrow-prev' });
	
	
	/**
	 * Find School Widget
	 */	
	$('#find-school-input').smartinput({ blurAction: ResetFindSchoolList });
	$('#find-school-list li strong').truncate({ max_length: 45 });

	
	/**
	 * url - can either be a string or a function
	 * We're setting it here to the static js file with a query string just for demo purposes
	 *
	 */
	/*$('#find-school-input').filterlist({
		url: function (element) { return 'template4/js/temp-data/temp-find-uni-json.js?q=' + element.val(); },
		resetUrl: 'template4/js/temp-data/temp-find-uni-json-initial-dataset.js',
		dataType: 'json',
		minChars: 3,
		success: function(data) { 
			ParseUniversities(data.universities);
		}
	});*/
	
	/**
	 * Each page can have an info banner, this is it's close button
	 *
	 */
	$('#btn-dismiss').click(function()
	{
		$(this).parent().animate({opacity:0}, 200).slideUp(200);
	});
	
	/**
	 * Guru Statuses balloon
	 * To display add class="guru-balloon-trigger" to any element
	 *
	 */
	$('.guru-balloon-trigger').attr('href', 'jsp/guruStatusesContent.jsp')
	.cluetip({cluetipClass: 'statuses', width: 550, attribute: 'href', dropShadow: false, topOffset: 100, leftOffset: 4, arrows: true });
});


/**
 * Create html from the universities JSON object
 *
 */
var ParseUniversities = function(universities)
{ 
	var uniHtml = '';
	$.each(universities, function(i, item)
	{
		/**
		 * We only want to display 5
		 * and stop the loop since it could be pretty large
		 */
		if(i < 5)
		{
			uniHtml += "<li><a href=" + item.url + "><strong>" + item.name + "</strong> <em>" + item.noteCount + "</em></a></li>";
		}
		else
		{
			return false;
		}
	});
	$('#find-school-list').html(uniHtml);
	$('#find-school-list li strong').truncate({ max_length: 45 });
};


/**
 * Function overloaded by Sukhpal in order to pass the unread messages count
 */
/**
 * Create html from the universities JSON object
 *
 */
var ParseUniversities = function(universities, unreadMessagesCount)
{ 
	var uniHtml = '';
	$.each(universities, function(i, item)
	{
		/**
		 * We only want to display 5
		 * and stop the loop since it could be pretty large
		 */
		if(i < 5)
		{
			uniHtml += "<li><a href=" + item.url + "><strong>" + item.name + "</strong> <em>" + item.noteCount + "</em></a></li>";
		}
		else
		{
			return false;
		}
	});
	$('#find-school-list').html(uniHtml);
	$('#find-school-list li strong').truncate({ max_length: 45 });
};



/**
 * Set the find school list back to the initial list
 *
 */
var ResetFindSchoolList = function()
{
	$("#find-school-input").trigger("filterlist.clear");
};


/**
 * Links with a rel=external will open in a new window
 *
 */
var PrepareExternalLinks = function()
{
	$("a[rel='external']").click(
		function()
		{
			window.open($(this).attr('href'));
			return false;
		}
	);
};



/**
 * Create html from the universities JSON object
 *
 * @param JSON universities
 * @param INT  startPoint
 *
 */
var ParseUniversities = function (universities, startPoint)
{
	var uniHtml, totalUnis, countDisplay, countLastDisplay, displayMax, i, item, controls, nextStartPoint, prevStartPoint, zeroCounter;
	
	uniHtml      = '';
		
	if(universities!=null && universities.length>0) {

		totalUnis    = universities.length;
		startPoint   = parseInt(startPoint, 10) || 0;
		displayMax   = 5;
		countDisplay = startPoint + 1;
		i = null;
		zeroCounter = 0;

		for (i = startPoint; i <= totalUnis; i++)
		{
			item = universities[i];

			if (totalUnis > displayMax)
			{
				if (zeroCounter < (displayMax - 1) && item)
				{
					uniHtml += "<li><a href=" + item.url + "><strong>" + item.name + "</strong> <em>" + item.noteCount + "</em></a></li>";
				}
				else
				{
					controls = '';
					if (startPoint > 0)
					{
						prevStartPoint = startPoint - (displayMax - 1);
						controls += "<a href='" + prevStartPoint + "' rel='" + prevStartPoint + "'>prev</a>";
					}

					if (totalUnis > (countDisplay + displayMax - 2))
					{
						nextStartPoint = startPoint + (displayMax - 1);
						controls += "<a href=" + nextStartPoint + " rel='" + nextStartPoint + "'>next</a>";
					}

					countLastDisplay = (countDisplay + displayMax - 2);
					countLastDisplay = (countLastDisplay < totalUnis) ? countLastDisplay : totalUnis;

					uniHtml += "<li id='find-school-paging'>Displaying " + countDisplay + "-" +  countLastDisplay + " of " + totalUnis + controls + "</li>";
					break;
				}
			}
			else
			{
				if (item)
				{
					uniHtml += "<li><a href=" + item.url + "><strong>" + item.name + "</strong> <em>" + item.noteCount + "</em></a></li>";
				}
			}

			zeroCounter++;
		};

		$('#find-school-list').html(uniHtml);
		$('#find-school-list li strong').truncate({ max_length: 45 });

		$('#find-school-paging a').click(
			function ()
			{
				/**
				 * A: Why not use the href attr?
				 * Q: Because IE is a big doofus.
				 *
				 */
				ParseUniversities(universities, $(this).attr('rel'));
				return false;
			}
		);
	} else {
		uniHtml = "<li><strong><center>&nbsp;</center></strong></li>";
		uniHtml = uniHtml + "<li><strong><center>&nbsp;</center></strong></li>";
		uniHtml = uniHtml + "<li><strong><center><font color='White'>No " + $("#studyCenter").val() + " found</font></center></strong></li>";
		$('#find-school-list').html(uniHtml);
	}
};


/**
 * Set the find school list back to the initial list
 *
 */
var ResetFindSchoolList = function ()
{
	$("#find-school-input").trigger("filterlist.clear");
};


/**
 * Links with a rel=external will open in a new window
 *
 */
var PrepareExternalLinks = function ()
{
	$("a[rel='external']").click(
		function ()
		{
			window.open($(this).attr('href'));
			return false;
		}
	);
};


/**
 * Utility function to capitalize the first letter of a word
 * @author http://kevin.vanzonneveld.net
 *
 */
var ucwords = function(str) 
{
    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
};