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

var navigationManager, galleriesManager, rassegnaManager, homepageManager;

// Questo oggetto funzione gestisce la navigazione, non le varie pagine!
var initNavigationManager = function () {
	// JSLint strict
	'use strict';

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



	// Apertura gallerie.
	my.openGalleries = function () {
		that.fadeOut(function () {
			$('<div class="galleries-content">')
				.appendTo(that.contentBody).hide().append("Galleries").fadeIn('fast');
		});
	};

	// Fade out.
	that.fadeOut = function (callback) {
		// Se ci sono elementi visibili, li mando in fade out.

		// HMMM, intanto così, va.
		that.contentBody.children().remove();

		$(".tooltip").fadeOut('fast');
		if (that.contentBody.find(' > *:visible').size()) {
			// Fade out, poi mi richiamo.
			that.contentBody.find(' > *:visible').fadeOut('fast', function (event) {
				that.fadeOut(callback);
			});
		} else {
			that.contentBody.children().remove();
			callback();
		}
	};

	// Apertura pagina statica.
	that.openStaticPage = function (href, callback, doContent) {
		if (doContent === undefined) {
			doContent = true;
		}
		that.fadeOut(function () {
			$.ajax({url: href, success: function (data, textStatus, jqXHR) {
				if (doContent) {
					that.createContentDiv().append(data).fadeIn('fast', callback);
				} else {
					that.contentBody.append(data);
					if (callback) {
						callback();
					}
				}
			}
		});
		});
	};

	// Manage menu link.
	my.manageMenuLink = function (event) {
		if (!$(this).attr('href')) {
			return false;
		}
		homepageManager.stopTimer();
		if ($(this).attr('href').match(/\/pages\/[\w-\/]*\.html$/)) {
			// E' una pagina html?
			event.stopPropagation();
			that.openStaticPage($(this).attr('href'));
			return false;
		} else if ($(this).attr('href') === '/rassegna') {
			// E' la rassegna stampa?
			event.stopPropagation();
			rassegnaManager.openIndex();
			return false;
		} else if ($(this).attr('href') === '/galleries') {
			// E' la rassegna stampa?
			event.stopPropagation();
			galleriesManager.openIndex();
			return false;
		} else if ($(this).attr('href') === '/') {
			// E' la rassegna stampa?
			event.stopPropagation();
			homepageManager.open();
			return false;
		}
		return true;
	};

	// Crea il div del contenuto. Non tutte le pagine le hanno.
	that.createContentDiv = function (title, link_back) {
		var returnValue = $("<div>").attr("id", "contentDiv");
		if ($('#contentDiv').size()) {
			$('#contentDiv').remove();
		}
		if (link_back) {
			returnValue.append($('<nav>')
					   .attr('class','indice_interno')
					   .append($('<a>')
					   .attr('title', 'Back')
					   .attr('href', link_back)
					   .append('Back')))
				       .fadeIn('fast');
		}
		if (title) {
			returnValue.append($("<h1>").append(title));
		}
		return returnValue.hide().appendTo(that.contentBody);
	};

	// Init
	my.init = function () {
		$("a").live('click', my.manageMenuLink);
	};

	// Tutto parte da qui.
	that.contentBody = $('#contentBody');
	my.init();

	return that;
};

$(function () {
	'use strict';
	galleriesManager = initGalleriesManager();
	rassegnaManager = initRassegnaManager();
	homepageManager = initHomepageManager();
	navigationManager = initNavigationManager();
	homepageManager.open();
});


