/* ------------------------------ */
/* Initiate JS				*/
/* ------------------------------ */

//Add this button variables
var addthis_offset_top = -50;
var addthis_offset_left = 4;

function init(){

	//Initialise the h1 tag line
	$('h1').addClass('banner' + Math.ceil(Math.random() * 5));

	//Form overlabels
	$("label[for='searchfield']").overlabel();

	//Add this button
	$('.share-this')
		.click(function(){
			return addthis_sendto();
		})
		.mouseover(function() {
			return addthis_open(this, '', '[URL]', '[TITLE]');
		})
		.mouseout(function() {
			addthis_close();
		});

	// Video transcripts
	jQuery.each($('.transcript'), function() {

		var transcript = $(this);

		// Create toggle link
		var link = $('<a class="view-transcript" href="#">View transcript</a>')
			.click(function() {
				if (transcript.css('display') == 'none') {
					transcript.css('display', 'block');
				} else {
					transcript.css('display', 'none');
				}
				return false;
			});

		// Hide transcript and add link
		transcript
			.css('display', 'none')
			.before(link);

	});

	/****************************************************************
	 * Calendar month-selector
	 ****************************************************************
	 */
	var oMonthSelector = document.createElement('select');
	var oCurrentDate = new Date;
	var iCurrentYear = null;
	var asMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

	// Extract the base reference from the first calender link entry
	var sEventsHRef = $('.monthLink:first').attr('href').toString();

	// Note the current year/month; take care that the CMS month is 1-based, and JavaScript is 0-based
	var iSelectedYear  = parseInt(sEventsHRef.replace(new RegExp('^.*/0?([0-9]*)/[0-9]*/?$'), '$1'));
	var iSelectedMonth = parseInt(sEventsHRef.replace(new RegExp('^.*/[0-9]*/0?([0-9]*)/?$'), '$1')) - 1;

	// Now strip the year/month from the href
	sEventsHRef = sEventsHRef.replace(new RegExp('/[0-9]*/[0-9]*/?$'), '');

	var oMonthSelectorGroup = null;
	function vAddMonth(bAddOptGroup, bIsSelected, iYear, iMonth, iOffset) {

		// Add a new, special optgroup
		if(bAddOptGroup) {
			oMonthSelectorGroup = document.createElement('optgroup');
			$(oMonthSelectorGroup).attr('label', iYear);
			$(oMonthSelector).append(oMonthSelectorGroup);
		}

		// Create a new <option/> element for the month; take care that the CMS month is 1-based, and JavaScript is 0-based
		var oMonth = document.createElement('option');
		$(oMonth).attr('value', sEventsHRef +'/'+ iYear +'/'+ (iMonth + 1) +'?custom:calendar&calendar_offset='+ iOffset );
		$(oMonth).text( asMonths[ iMonth ] );

		// Mark the current date if selected
		if(bIsSelected) {
			$(oMonth).attr('selected', 'selected');
		}

		// Append the new element
		$(oMonthSelectorGroup).append(oMonth);
	}

	// Handle the case that the year presented is before the current scope
	if( iSelectedYear  < oCurrentDate.getFullYear() ||
	   (iSelectedYear == oCurrentDate.getFullYear() &&
	    iSelectedMonth < oCurrentDate.getMonth())) {

		vAddMonth(true, true, iSelectedYear, iSelectedMonth, 0);
		iCurrentYear = iSelectedYear;
	}

	// Build the new options list
	for(var iCount = 0; iCount < 12; iCount++) {

		vAddMonth(	oCurrentDate.getFullYear() != iCurrentYear,
					oCurrentDate.getFullYear() == iSelectedYear && oCurrentDate.getMonth() == iSelectedMonth,
					oCurrentDate.getFullYear(),
					oCurrentDate.getMonth(),
					iCount);

		// Note the year and increment the month
		iCurrentYear = oCurrentDate.getFullYear();
		oCurrentDate.setMonth( oCurrentDate.getMonth() + 1 );
	}

	// Handle the case that the year presented is beyond the current scope
	if( iSelectedYear  > oCurrentDate.getFullYear() ||
	   (iSelectedYear  == oCurrentDate.getFullYear() &&
	    iSelectedMonth > oCurrentDate.getMonth())) {

		vAddMonth(	iSelectedYear != iCurrentYear,
					true,
					iSelectedYear,
					iSelectedMonth,
					0);
	}

	// Bind an on-change handler
	$(oMonthSelector)
		.change(function() {
					// Redirect to the page selected
					document.location = $(this).filter('* option:selected').attr('value');
				});

	// Replace all calendar entries
	$('.monthLink')
		.replaceWith(oMonthSelector);

}

/* ------------------------------ */
/* Form overlabels			*/
/* ------------------------------ */
jQuery.fn.overlabel = function() {
		this.each(function(index) {
				var label = $(this); var field;
				var id = this.htmlFor || label.attr('for');
				if (id && (field = document.getElementById(id))) {
						var control = $(field);
						label.addClass("overlabel-apply");
						if (field.value !== '') {
								label.fadeOut(100);
						}
						control.focus(function () {label.fadeOut(100);}).blur(function () {
								if (this.value === '') {
										label.fadeIn(100);
								}
						});
						label.click(function() {
								var label = $(this); var field;
								var id = this.htmlFor || label.attr('for');
								if (id && (field = document.getElementById(id))) {
										field.focus();
								}
						});
				}
		});
};

/* ------------------------------ */
/* END Form overlabels */
/* ------------------------------ */
