﻿;(function ($) {
	'use strict';
	var methods = {
		showHideScorecard: function (params) {
			/*	Show / hide the scorecard on course pages
				this = the CourseTemplate control */
			return this.each(function () {
				var scorecardCount,
					iIndex,
					thisScorecard,
					settings = { scorecardIds: [] },
					toggleScorecard;

				if (params) { $.extend(settings, params); }

				scorecardCount = settings.scorecardIds.length;

				toggleScorecard = function (event) {
					event.preventDefault();
					var $this = $(this),
						scorecardId = $this.data('scorecardId'),
						scorecard = $('#' + scorecardId);

					if (scorecard.is(':hidden')) {
						scorecard.slideDown();
						$this.text('[Hide Scorecard]');
						_gaq.push(['_trackEvent', 'Venue courses - View scorecard (' + scorecardId + ')', 'Click']);
					} else {
						scorecard.slideUp();
						$this.text('[Show Scorecard]');
					}
				};

				for (iIndex = 0; iIndex < scorecardCount; iIndex += 1) {
					thisScorecard = $('#' + settings.scorecardIds[iIndex]);

					thisScorecard
						.hide()
						.after($('<a/>', {
							'class': 'closePanel',
							click: toggleScorecard,
							data: { 'scorecardId': settings.scorecardIds[iIndex] },
							'text': '[Show Scorecard]'
						}));
				}
			});
		},
		faqs: function (params) {
			/*	Show / hide FAQs
				this = the Definition List */
			return this.each(function () {
				var definitionList = $(this),
					allTerms = definitionList.find('dt'),
					allDefinitions = definitionList.find('dd');

				allDefinitions
					.append($('<a/>', { 
						'href': '#',
						'class': 'currentlyShown',
						'text': '[Close]',
						'click': function(event) {
							event.preventDefault();
							allDefinitions.hide();
							allTerms
								.addClass('currentlyHidden')
								.parents('div.zone').toggleClass('ie8fix');
							}
						}))
					.hide();

				allTerms
					.addClass('currentlyHidden')
					.click(function() {
						allDefinitions.hide();
						allTerms.addClass('currentlyHidden');
						$(this)
							.toggleClass()
							.next('dd').show()
								.parents('div.zone').toggleClass('ie8fix');
					});
			});
		}
	};

	$.fn.generalFunctions = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.generalFunctions');
		}
	};

}(jQuery));
