var mapsdir = "maps";

var modelspath = "gm/models.php";
var typespath = "gm/types.php";
var offsetspath = "gm/offsets.php";
var comparisonspath = "gm/comparisons.php";

var refreshimg = "img/refresh.gif";

// STARTUP
$(function(){
	init();
	initModelsMenu();
});
// ---

// Inizializzazione generale
function init(){
	var content = $('.content');
	content.attr('top', content.css('top')).css('top','0px');
}

// Inizializza il menu models.
function initModelsMenu(){
	var models = $('.models');
	var blocks = models.find('.block');
	blocks.slice(0,2).lock(true).end().slice(2).lock(false).end().manageLocking();
	blocks.find('.model a').manageClick().manageModelLink();
	models.manageModelsRefresh();
}

// Gestisce l'aggiornamento del menu dei modelli
$.fn.manageModelsRefresh = function(){
	var $this = this
	this.find('.refresh a').click( function(){
		$this.find('.blocks').refreshModelsBlocks();
		return false;
	});
}

// Aggiorna i blocchi del menu dei modelli
$.fn.refreshModelsBlocks = function(){
	var blocks = this.find('.block');
	var lockedBlocksIds = $.map(blocks.locked(), "$(a).attr('id')");
	var activeLinksIds = $.map(blocks.find('.model a').activeLinks(), "$(a).attr('id')");
	var $this = this;
	$.post(modelspath, {dir: mapsdir}, function(page){
		$this.empty().html(page);
		newblocks = $this.find('.block');
		newblocks.lock(false).filter(
				function(){return lockedBlocksIds.has($(this).attr('id'));}
			).lock().end().manageLocking();
		newlinks = newblocks.find('.model a');
		newlinks.filter(
				function(){return activeLinksIds.has($(this).attr('id'));}
			).selectAmong(newlinks).end().manageClick().manageModelLink();
		$('.panels').refreshPanels();
	});
}

// Gestisce il locking/unlocking di ogni blocco e il suo comportamento rispetto agli eventi click e hover.
$.fn.manageLocking = function(){
	this.hover(
		function(){
			$(this).find('.items').show();
		} ,
		function(){
			$(this).nonLocked().find('.items').hide();
		}
	).find('.date a').click(
		function(){
			var block = $(this).parents('.block');
			if(!(block.find('.model a').activeLinks().length)){
				block.toggleLock();
			}
			return false;
		}
	);
	this.find('.model a').click(
		function(){
			$(this).parents('.block').lock();
			return false;
		}
	);
	return this;
}

$.fn.manageModelLink = function(){
	this.click( function(){
		$('.panels').initPanels().find('.types').loadTypes($(this).attr('href'));
	});
	return this;
}

$.fn.manageTypeLink = function(){
	this.click( function(){
		$('.offsets').loadOffsets($(this).attr('href'));
		return false;
	});
	return this;
}

$.fn.manageOffsetLink = function(){
	this.click( function(){
		var href = $(this).attr('href');
		$('.content').loadPanel(href);
		$('.comparisons').loadComparisons(href, $(this).attr('id'));
		return false;
	});
	return this;
}

$.fn.manageComparisonLink = function(){
	this.click( function(){
		href = $(this).attr('href');
		$('.content').loadPanel(href);
		return false;
	});
	return this;
}

$.fn.loadTypes = function(href, memo, propagate){
	if(memo==undefined){memo=true;}
	if(propagate==undefined){propagate=true;}
	var old_href = this.attr('href');
	var $this = this;
	var current = this.find('a').activeLinks().attr('id');
	$.post(typespath, {href: href}, function(page){
			if($this.attr('href') != old_href){return;}
			$this.empty().html(page).attr('href', href);
			var typelinks = $this.find('a');
			typelinks.manageClick().manageTypeLink();
			if(memo){var tosel = typelinks.filter('#' + current);}
			if(!memo || !tosel.length){var tosel = typelinks.slice(0,1);}
			if(propagate){tosel.trigger('click');}else{typelinks.select(tosel);}
		});
	return this;
}

$.fn.loadOffsets = function(href, memo, propagate){
	if(memo==undefined){memo=true;}
	if(propagate==undefined){propagate=true;}
	var old_href = this.attr('href');
	var $this = this;
	var current = this.find('a').activeLinks().attr('id');
	$.post(offsetspath, {href: href}, function(page){
			if($this.attr('href') != old_href){return;}
			$this.empty().html(page).attr('href', href);
			var offsetlinks = $this.find('a');
			offsetlinks.manageClick().manageOffsetLink();
			if(memo){var tosel = offsetlinks.filter('#' + current);}
			if(!memo || !tosel.length){var tosel = offsetlinks.slice(0,1);}
			if(propagate){tosel.trigger('click');}else{offsetlinks.select(tosel);}
		});
	return this;
}

$.fn.loadComparisons = function(href, offset){
	var old_href = this.attr('href');
	var $this = this;
	$.post(comparisonspath, {href: href, offset:offset}, function(page){
			if($this.attr('href') != old_href){return;}
			$this.empty().html(page).attr('href', href);
			var comparisonlinks = $this.find('a');
			comparisonlinks.manageClick().manageComparisonLink();
			var tosel = comparisonlinks.filter('#' + offset);
			if(!tosel.length){tosel = comparisonlinks.slice(0,1);}
			comparisonlinks.select(tosel);
		});
	return this;
}

$.fn.loadPanel = function(href){
	return this.initContent().find('.view').html("<img src='" + href +"'>");
}

// Inizializza il contenuto di .panels
$.fn.initPanels = function(){
	var $this = this;
	$('.content').initContentPos();
	this.find('.refresh').not(":has(a)").html("<a href='#'><img src='" + refreshimg + "'></a>");
	this.find('.refresh a').click( function(){
			$this.refreshPanels();
			return false;
		});
	return this;
}

// Aggiorna il contenuto di .panels
$.fn.refreshPanels = function(){
	var types = this.find('.types');
	var offsets = this.find('.offsets');
	types.loadTypes(types.attr('href'), true, false);
	offsets.loadOffsets(offsets.attr('href'), true, false);
}

// Inizializza la posizione del contenuto centrale
$.fn.initContentPos = function(){
	return this.not("[top='auto']").css('top', this.attr('top')).attr('top', 'auto');
}

// Inizializza il contenuto centrale
$.fn.initContent = function(){
	var commands_html = "<a href='#'>&lt;&lt;</a><a href='#'>&lt;</a><a href='#'>&gt;</a><a href='#'>&gt;&gt;</a>";
	this.not(":has(.view)").html("<div class='commands'>" + commands_html + "</div><div class='view'><img></div>")
		.find('.commands a').manageCommandsLinks();
	return this;
}

$.fn.manageCommandsLinks = function(){
	this.filter(':eq(0)').click( function(){ $('.offsets a').filter(':first').trigger('click'); } );
	this.filter(':eq(1)').click( function(){ $('.offsets a').activeLinks().prev().trigger('click'); } );
	this.filter(':eq(2)').click( function(){ $('.offsets a').activeLinks().next().trigger('click'); } );
	this.filter(':eq(3)').click( function(){ $('.offsets a').filter(':last').trigger('click'); } );
}

// Filtra i blocchi eliminando quelli locked.
$.fn.nonLocked = function(){
	return this.not("[locked='1']");
}

// Filtra i blocchi eliminando quelli non locked.
$.fn.locked = function(){
	return this.filter("[locked='1']");
}

// Locka o unlocka un blocco del menu models.
$.fn.lock = function(locked){
	if(locked==undefined){locked=true;}
	var items = this.find('.items');
	if(locked){items.show();}else{items.hide();}
	return this.attr('locked', (locked ? '1' : '0'));
}

// Inverte lo stato locked/unlocked di un blocco del menu models.
$.fn.toggleLock = function(){
	return this.lock(this.nonLocked().length);
}
