jQuery(function() {
	if(jQuery('#galleria').length) {
		Galleria.loadTheme("/includes/galleria/themes/khp/galleria.khp.js");
		jQuery("#galleria").galleria({
			clicknext: true
		}).css({visibility: 'visible'});
	}
	// Park Locations page
	if(jQuery('.activity_photos').length) {
		// Hide everything to start with
		jQuery('.activity_photos').hide();
		// Show Auckland by default
		activitySlide("auckland");
	}
});
function displayMultiplePark(city) {
	// Hide all cities first
	var allPageTags = new Array(); 
	var allPageTags=document.getElementsByTagName("*");
	for (i=0; i<allPageTags.length; i++) if (allPageTags[i].className == 'map_multiple_cities') allPageTags[i].style.display = 'none';
	document.getElementById('map_' + city).style.display = 'block';
}

current_city_slide = '';
function activitySlide(city) {
	if(current_city_slide && document.getElementById('activity_' + current_city_slide)) document.getElementById('activity_' + current_city_slide).style.display = 'none';
	if (document.getElementById('activity_' + city))document.getElementById('activity_' + city).style.display = 'block';
	current_city_slide = city;
}

var annimationSpeed = 300;
$(function() {
	if(document.getElementById('activity-search')) {
		$('#activity-search .selection a').click(function() {
			var id = $(this).attr('id');
			var name = $(this).html();
			if($(this).hasClass('selected')) {
				$('#s' + id).remove();
				$(this).removeClass('selected');
				refreshResults();
			} else {
				$('<div id="s' + id + '"><span>' + name + '</span><a onclick="$(\'#' + id + '\').trigger(\'click\');" rel="' + id + '">x</a></div>').appendTo('#search-criteria .criteria');
				$(this).addClass('selected');
				refreshResults();
			}
		});
		// Load results from cookie if available
		loadResults();
	}
});

function showPopular(type, showPopular) {
	if(showPopular) {
		$('#activity-search .' + type + ' a.not').css({display: 'inline-block'});
		$('#activity-search div.popular-' + type + ' a.popular').removeClass('selected');
		$('#activity-search div.popular-' + type + ' a.all').addClass('selected');
	} else {
		$('#activity-search .' + type + ' a.not').hide();
		$('#activity-search div.popular-' + type + ' a.popular').addClass('selected');
		$('#activity-search div.popular-' + type + ' a.all').removeClass('selected');
	}
}
function refreshCriteriaSymbols() {
	$('#search-criteria .criteria .symbol').remove();
	$('#search-criteria .criteria div').each(function(index) {
		var symbol = 'plus';
		if(index == 0) {
			symbol = 'start';
		}
		$('<div class="symbol symbol-' + symbol + '"></div>').prependTo(this);
	});
}

function refreshResults() {
	refreshCriteriaSymbols();
	// Get all criteria to search on
	var criteria = '';
	var html = '';
	var valid = Array();
	$('#search-criteria .criteria a').each(function() {
		criteria += $(this).attr('rel') + ',';
	});
	if(!criteria) {
		$('.results-header').fadeOut(annimationSpeed);
		$('#search-results').fadeOut(annimationSpeed); 
	} else {
		// Save criteria in a cookie so that when we come back we can load it again
		$.cookie('khpActivitySearch', criteria, { expires: 1 });
		// Fade everything in
		$('.results-header').fadeIn(annimationSpeed);
		$('#search-results').fadeIn(annimationSpeed); 
		$('#search-results .no-results').fadeOut();
		$.getJSON('/includes/ajax.php?f=activitySearch&criteria=' + criteria, function(data) {
			if(data) {
				$.each(data, function(key, val) {
					html = '<div id="r' + val.id + '" class="result" style="display: none;">';
						html += '<div class="image"><img src="/images/activities/thumb_' + val.image + '" width="100" height="75" alt="" /></div>';
						html += '<div class="detail">';
							html += '<div class="name">' + val.name + '</div>';
							html += '<div class="city">' + val.city + ' (' + val.type + ')</div>';
							html += '<div class="description">' + val.description + ' <a href="/activities.php?activityid=' + val.id + '">Read more...</a></div>';
						html += '</div>';
					html += '</div>';
					// Only display if not already there
					if(!$('#r' + val.id).length) {
						$(html).appendTo('#search-results');
						$('#r' + val.id).fadeIn(annimationSpeed);
					}
					valid[valid.length] = val.id;
				});
			}
			// Loop through all displayed results and if they didn't apepar in the latest search then remove them
			$('#search-results .result').each(function() {
				var found = false;
				for(var a in valid) {
					if('r' + valid[a] == $(this).attr('id')) {
						found = true;
					}
				}
				if(!found) {
					$(this).fadeOut(annimationSpeed, function() {
						$(this).remove();
						$('#search-results .result .image').removeClass('image-last');
						$('#search-results .result div.image:last').addClass('image-last');
					});
				}
			});
			if(!valid.length) $('#search-results .no-results').fadeIn();
			// Need to tidy up spacing between each one
			$('#search-results .result .image').removeClass('image-last');
			$('#search-results .result div.image:last').addClass('image-last');
			// Do one final check to hide the no results found as it can display when loaded quickly via the cookie
			setTimeout(function() {
				if($('#search-results .result').length) {
					$('#search-results .no-results').hide();
				}
			}, annimationSpeed);
		});
	}
}

function loadResults() {
	var criteria = $.cookie('khpActivitySearch');
	if(criteria) {
		var options = criteria.split(',');
		for(var a in options) {
			var id = options[a];
			if(id) {
				$('#' + id).trigger('click');
			}
		}
	}
}
