if (typeof Rouge === "undefined") {
  Rouge = {};
}

/*
* The imageGallery module is reponsible for dynamically loading
* the images (via ajax) for the gallery from folders located in the /images directory.
*/
Rouge.imageGallery = (function () {
	//private
	var igOptions = {},
		galleriaOptions = {
			height: '500',
			preload: 3, 
			transition: 'fade',
			image_margin: 0,
			image_crop: true,
			carousel: false,
			height: 'auto',
			image_margin: 0,
			max_scale_ratio: 1, 
			// for thumbnails
			thumb_crop: 'height',
			thumb_fit: false,
			thumb_margin: '0',
			thumb_image_insertbefore: '<span></span>'
		};
	
	initGalleryNavEventListeners = function () {
		var galleryNavCnt = $("#" + igOptions.galleryNavElementID);
		var galleryNavLinks = $("a", galleryNavCnt);
		
		$(galleryNavLinks).live("click", function(e) {
			e.preventDefault();
			galleryNavLinks.removeClass("selected");
			$(this).addClass("selected");
			initSelectedGallery();
		});
	};
	
	initThumbnailEventListeners = function() {
		var thumbMouseOverQueue = [],
			thumbMouseOutQueue = [],
			animationSpeed = 30; //ms

		$("#galleria .galleria-image span").live("mouseover mouseout", function(event) {
			if (event.type == 'mouseover') {
				thumbMouseOverQueue = [];
				thumbMouseOverQueue.push({s: this,p: [{backgroundPosition: "0 -88px"}, animationSpeed]});
				thumbMouseOverQueue.push({s: this,p: [{backgroundPosition: "0 -176px"}, animationSpeed]});
				thumbMouseOverQueue.push({s: this,p: [{backgroundPosition: "0 -264px"}, animationSpeed]});
				thumbMouseOverQueue.push({s: this,p: [{backgroundPosition: "0 -352px"}, animationSpeed]});
				$.fxQueue.start(thumbMouseOverQueue);
			} else {
				thumbMouseOutQueue = [];
				thumbMouseOutQueue.push({s: this,p: [{backgroundPosition: "0 -264px"}, animationSpeed]});
				thumbMouseOutQueue.push({s: this,p: [{backgroundPosition: "0 -176px"}, animationSpeed]});
				thumbMouseOutQueue.push({s: this,p: [{backgroundPosition: "0 -88px"}, animationSpeed]});
				thumbMouseOutQueue.push({s: this,p: [{backgroundPosition: "0 0"}, animationSpeed]});
				$.fxQueue.start(thumbMouseOutQueue);
			}
		});
	};
	
	desaturateActiveThumbnail = function() {
		var activeThumbImg = $(".galleria-thumbnails div.active img:first");
		//TODO: Find plugin that works with reduced size image, or apply desat before we resize with galleria
	};
	
	aJaxRequest = function(config) {
		$.ajax({
			type: 'POST',
			url: igOptions.galleryAjaxURL,
			data: config.data,
			success: config.success,
			dataType: "html"
		});
	};
	
	initDefaultGalleryTheme = function() {
		// Load the classic theme
		Galleria.loadTheme('/themes/classic/galleria.classic.js');
	};
	
	initGalleriaPlugin = function() {
		var galleriaCnt = $("#" + igOptions.galleryImageContainerID);
		galleriaCnt.galleria(galleriaOptions);
	};
	
	initSelectedGallery = function() {
		var selectedGalleryNavLink = $("#" + igOptions.galleryNavElementID + " a.selected");
		aJaxRequest({
			data: {galleryName: selectedGalleryNavLink.attr("data-id")},
			success: function(data) {
				var galleryCnt = $("#" + igOptions.galleryImageContainerID);
				galleryCnt.empty().html(data);
				initGalleriaPlugin();
			}
		});
	};
	
	bindToGalleriaEvents = function() {
		$(document).bind(Galleria.IMAGE, function(e) {
			desaturateActiveThumbnail();
		});
	};
	
	//public
	return {
    	init: function(galleryOptions) {
    		igOptions = galleryOptions;
    		initGalleryNavEventListeners();
    		// initThumbnailEventListeners();
    		bindToGalleriaEvents();
    		initDefaultGalleryTheme();
    		initSelectedGallery();
    	}
  	};
}());
