/* WEXFORD CIVIL DEFENCE SLIDESHOW */
window.gallery_count = 0;

// Parse gallery data //
function succ_func(id,sec_name,tags) {
	// Return anonymous scoped function for passing arguments to callback //
	return function (data) {
		slideshow_data = data;
		
		// Prepare Tags //
		if (tags && tags.length > 0) {
			tags = tags.replace(/\s+,|,\s+|^\s+|\s+$/g,'');	// Remove excess whitespace //
			tags = tags.replace(/\s\s+/g,' ');
			var temp_tags = tags.split(/,/);
			tags = {};
			for (i = 0; i < temp_tags.length; i++) {tags[temp_tags[i].toLowerCase()] = true;}
		} else {tags = false;}
		
		var select = '#' + id + ' DIV.slide-container';
		var el = $(select);
		var sec_url = '';
		var current_sec_name;
		var collect = false;
		$(data).find('#gallery-data').children().each(function(index,element){
		
			// Find start/end point using section name //
			if (sec_name.length > 0 && $(element).is('H2') && $(element).html() == sec_name) {
				collect = true;
				current_sec_name = sec_name;
				return;
			} else if (sec_name.length > 0 && $(element).is('H2')) {
				collect = false;
				current_sec_name = $(element).html();
				return;
			} else if ($(element).is('H2')) {
				current_sec_name = $(element).html();
				return;
			}
			
			// Search for matching tags //
			var tagged = false;
			if (sec_name.length < 1 && tags && $(element).find('DIV.tags').html()) {
				image_tags = $(element).find('DIV.tags').html().replace(/\s+,|,\s+|^\s+|\s+$/g,'');
				image_tags = image_tags.replace(/\s\s+/g,' ');
				image_tags = image_tags.split(/,/);
				for (i = 0; i < image_tags.length; i++) {
					if (tags[image_tags[i].toLowerCase()]) {
						tagged = true;
						break;
					}
				}
			}
			
			// Collect gallery item data and append to slideshow //
			if (collect || tagged) {
				
				// Get image data //
				var image_title = $(element).find('H3').html();
				$(element).find('DIV.description H3').remove();
				var image_description = $(element).find('DIV.description').html();
				var image_thumb = $(element).find('DIV.image-slide IMG');
				var image_full_url = $(element).find('DIV.image-full IMG').attr('src').replace(/^\/|^file:\/\/\/C:\//i,'');
				image_thumb.attr('src', image_thumb.attr('src').replace(/^\/|^file:\/\/\/C:\//i,''));
				var sec_url = image_thumb.attr('src').replace(/\/[^\/]+$/,'/index.html');	// Deduce set location from image url //
				var sec_dir = image_thumb.attr('src').match(/\/([^\/]+)\/[^\/]+$/,'/index.html')[1];	// Deduce set directory from image url //
				
				// Build Slide
				var slide = $('<a href="' + image_full_url + '" rel="gallery' + gallery_count + '" title="' + image_title + '" class="slide"></a>');
				$(slide).append(image_thumb).append('<h3>' + current_sec_name + '</h3>').append('<div>' + image_title + '</div>');
				$(select).append(slide);
				
				// Store data //
				$(slide).data('gallerydata', {
					'title': image_title,
					'description': image_description,
					'url': sec_url,
					'dir': sec_dir,
					'section': current_sec_name,
					'link': image_full_url,
					'galleryIndex': gallery_count
				})
				
				// Recalculate size and position //
				calcCentre(image_thumb,true,false); // First try (image may not have loaded yet //
				$(image_thumb).load(function(){calcCentre(this,true,false);}); // Second try (cached image may not fire load() event)
			}
		});
		//$('#' + id + ' A.slide').colorbox({maxHeight:"90%"});
		
		// Call next AJAX request //
		window.gallery_count++;
		slideshowQueueAjax();
	}
}


// Queue AJAX request //
var slideshow_ajax_queue = [];
var slideshow_data = '';
function slideshowQueueAjax(arg){
	if (arg) {
		// Add request to queue //
		slideshow_ajax_queue.push(arg);
	} else if (slideshow_ajax_queue.length) {
		if (slideshow_data.length) {
			// Use cached data from first call //
			succ_func(next_call.id,next_call.section,next_call.tags)(slideshow_data);
		} else {
			// Fetch gallery data for first queued request //
			var next_call = slideshow_ajax_queue.shift();
			$.ajax({
				url: 'gallery/sets/index.html',
				dataType: 'html',
				context: document.body,
				success: succ_func(next_call.id,next_call.section,next_call.tags)
			});
		}
	} else {
		// Start slideshow //
		$('A.slide').colorbox({
			maxHeight:"90%",
			onComplete: function(){
				$('#cboxLoadedContent').append('<div class="description">' + $(this).data('gallerydata').description + '</div>');
			}
		});
		$('DIV.slide-container').cycle({
			fx: 'uncover',
			delay: 1000, 
			timeout: 3000, 
			after: function(currSlideElement, nextSlideElement, options, forwardFlag){
				calcCentre($(this).find('IMG'),true,false);
			}
		});
	}
}

// Start AJAX calls //
$(document).ready(function(){slideshowQueueAjax()});

