/*globals alert, $, window, navigationManager, rassegnaManager*/
/*jslint white: true, onevar: true, undef: true, nomen: true, regexp: true, plusplus: true, bitwise: true, newcap: true, maxerr: 50, indent: 4 */


/* Manager della gallery. */
var initGalleriesManager = function () {
	// JSLint strict
	'use strict';

	// That and mine.
	var that = {},
		my = {};


	// Inizializzazione delle galleria.	
	my.initGallery = function (contentDiv) {
		var galleryDiv, c = 0;
		galleryDiv = $('<div>').attr('id', 'gallery').appendTo(contentDiv);
		galleryDiv.galleria({
        width: '100%',
        height: 600,
				data_source: my.gallery,
				thumbnails: false
    });
	};

	my.searchItem = function (itemName) {
		var returnValue;
		$.each(my.data, function (index, item) {
			$.each(item.items, function (index1, item1) {
				if (item1.name === itemName) {
					returnValue = item1;
				}
			});
		});
		return returnValue;
	};
	
	// Apre una galleria.
	my.openItem = function (event) {
		var doOpen, data;
		data = $(this).data('galleryData') ? $(this).data('galleryData') : my.searchItem(event);
		if (event && event.stopPropagation) {
			event.stopPropagation();
		}
		doOpen = function () {
			var contentDiv = navigationManager.createContentDiv();
			contentDiv.append($('<nav>')
					.attr('class','indice_interno')
					.append($('<a>')
					.attr('title', 'Back')
					.attr('href', '/galleries')
					.append('Back')))
				.fadeIn('fast');
			contentDiv.append($('<h1>')
					.append(data.title))
				.fadeIn('fast');
			contentDiv.append($('<h2>')
					.append(data.club))
				.fadeIn('fast');
			contentDiv.append($('<h3 class="evento_data">')
					.append(data.data))
				.fadeIn('fast');
			$.getJSON('ajax/gallery.php?dir=' + data.imagesPath, function (data) {
				my.gallery = data;
				contentDiv.fadeIn('fast');
				my.initGallery(contentDiv);
			});
		}
		navigationManager.fadeOut(doOpen);
		return false;
	};

	// Apertura rassegna stampa.
	my.writeIndex = function() {
		var div, galleryDiv, classSxDx, contatore, contentDiv;
		contatore = 0;
		contentDiv = navigationManager.createContentDiv('Photogallery','../');
		galleryDiv = $('<div>').attr('id', 'gallery_page').appendTo(contentDiv);
		$.each(my.data, function (index, item) {
			$.each(item.items, function (index1, item1) {
				if (contatore%2 == 0){
					classSxDx = 'sx';
				} else {
					classSxDx = 'dx';
				}
				div = $('<div>').addClass("gallery_blocco_" + classSxDx).appendTo(contentDiv);
				div.append($('<h2>').append(item.title));
				var a = $('<a>')
					.click(my.openItem)
					.attr('href', '#')
					.append($("<img>")
						.attr('src', 'img/gallery/' + item1.imageElenco)
						.attr('width', '400')
						.attr('height', '250'))
					.append("<br>")
					.append(my.getTitleFor(item1));
				a.data('galleryData', item1);
				a.attr('title', item1.title);				
				a.appendTo(div);
				div.appendTo(galleryDiv);
				contatore++;
			});
		});
		contentDiv.fadeIn('fast');
	};

	my.getTitleFor = function (data) {
		var title = "";
		if (data.title) {
			title += data.title;
		} else if (data.club) {
			title += data.club;
		}
		if (data.data) {
			title += '<br/><span class=\"gallery_data\">' + data.data + '</span>';
		}
		return title;
	};

	// Load JSON
	my.loadJSON = function (callback) {
		$.getJSON('ajax/galleries.json', function (data) {
			my.initialized = true;
			my.data = data;
			if (callback) {
				callback();
			}
		});
	};

	// Apertura gallerie.
	that.openIndex = function() {
		if (!my.initialized) {
			my.loadJSON(that.openIndex);
		} else {
			navigationManager.fadeOut(function () {
				my.writeIndex();
			});
		}
	};

	// Apertura gallerie.
	that.openItem = function(itemName) {
		if (!my.initialized) {
			my.loadJSON(function () {
				that.openItem(itemName);
			});
		} else {
			navigationManager.fadeOut(function () {
				my.openItem(itemName);
			});
		}
	};

	Galleria.loadTheme('/js/themes/classic/galleria.classic.min.js');
	my.initialized = false;

	return that;
};

