(function($){
	$.fn.extend({
		//pass the options variable to the function
		clioFAQ: function(options) {
			// Set the default values, use comma to separate the settings, example:
			var defaults = {
				enableFoldAnim: true,
				scrollInView: true,
				scrollToActive: false,
				elementId: null
			}
			var options =  $.extend(defaults, options);
			if (options.elementId == null) return;

			// Read a page's GET URL variables and return them as an associative array.
			function getUrlVars() {
				var vars = [], hash;
				var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
				for(var i = 0; i < hashes.length; i++) {
					hash = hashes[i].split('=');
					vars.push(hash[0]);
					vars[hash[0]] = hash[1];
				}
				return vars;
			}

			var res = this.each(function() {
				$('dl.tx-cliofaq-foldout',$(this)).each(function() {

					var foldout = $(this);

					// Bind clickevent to like and dislike buttons
					$(foldout).find('a.tx-cliofaq-like, a.tx-cliofaq-dislike').each(function() {
						$(this).bind('click', function(event) {
							event.preventDefault();
							var atagParent = $(this).parent();
							$.ajax({
								url: $(this).attr('href'),
								type: "GET",
								dataType: 'json',
								success: function(data) {
									//console.log(data);
									$('span.tx-cliofaq-like-result',atagParent).text(data.positive);
									$('span.tx-cliofaq-dislike-result',atagParent).text(data.negative);
								},
								error: function(jqXHR, textStatus, errorThrown) {
								}
							});
						})
					})

					// Add events for folding/unfolding each FAQ item
					$('dt span',foldout).bind('click', function() {
						if (!$(foldout).hasClass("active")){
							if (options.enableFoldAnim) {
								// Fold any open FAQs
								$(options.elementId).find("dl.active > dt span").trigger('click');
							}
							// Unfold the selected FAQ
							$(foldout).addClass("active");
							$(foldout).children("dd").slideDown('slow', function() {

								var docViewTop = $(window).scrollTop();
								var docViewBottom = docViewTop + $(window).height();

								var elemTop = $(this).offset().top;
								var elemBottom = elemTop + $(this).height();

								var fitInsideView = ($(window).height()-$(this).height())>0;
								var topVisible = (elemTop >= docViewTop)

								if (options.scrollToActive || (options.scrollInView && !topVisible)) {
									$('html, body').animate({
										scrollTop: $(foldout).offset().top
									}, 'fast');
								}
							});
						}
						else {
							// Fold the selected FAQ
							$(foldout).removeClass("active");
							$(foldout).children("dd").slideUp('fast');
						}
						return false;
					}).bind('mouseover', function() {
						$(this).css('textDecoration', 'underline');
					}).bind('mouseout', function() {
						$(this).css('textDecoration','none');
					});
				});
			});

			var getvar = getUrlVars();
			if (getvar['foldout']) {
				//alert($('#tx-cliofaq-item-'+getvar['foldout']).text());
				$('#tx-cliofaq-item-'+getvar['foldout']+' span').trigger('click');
			}

			return res;
		}
	});

})(jQuery);

