var colors = ['#C6E166', '#9AA963', '#7A9221', '#DBF08D', '#E0F0A8', "#006eab", "#009512", "#00acd6", "#5bac26",
	      "#62b5cf", "#79ad1c", "#7d003f", "#83cde2", "#97bf0d", "#aedad9", "#bbae78", "#c50049", "#ce7104",
	      "#d9d3c2", "#dbede2", "#dc002e", "#e85555", "#e95e40", "#eb6e08", "#ebe571", "#f4b2c2", "#f8b358",
	      "#f8e7f1", "#fce0ce", "#ffe91a", '#4C7199', '#475C73', '#193D63', '#7FA4CC', '#93AFCC', '#EBB76A',
	      '#B09368', '#996923', '#F5CD90', '#F5D7AB', '#A64B97', '#7C4974', '#6C185E', '#D27CC5', '#D293C8',
	      '#EBD56A', '#B0A468', '#998423', '#F5E490', '#F5E8AB', '#EB6A6A', '#B06868', '#992323', '#F59090', '#F5ABAB'];

var shuffle = function(o) { for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; };
var change_colors = function() {shuffle(colors); $(".square").each(function(index, element){$(element).css("background-color", colors[index]);});};
var open_link = function(square) {
	var target = "#" + $(square).find(".links").attr("href").split("!")[1];
	$(".content").not(target).hide();
	$(".close").not($(square).find(".close")).hide();
	$(".links").not($(square).find(".links")).show();
	$(target).toggle("fold", {}, 2000, function(){$(square).find(".close, .links").toggle();});
	$(".second_level_link, .galleries").hide();
	_gaq.push(['_trackPageview', '/' + target]);
};

Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');

function pause_galleries() {
	var all_galleries = Galleria.get() || [];
	for (var i=0; i < all_galleries.length; i++) {
		all_galleries[i].pause();
	}
}

function show_gallery_for_album(item) {
	pause_galleries();
	$("#" + item.name).show();
	item.gallery = $("#" + item.name).galleria({ width: 580, height: 580, autoplay: 3000, lightbox: true, showInfo: true, imageTimeout: 900000, debug: true,
		picasa:'useralbum:105272290235099918088/' + item.gallery_name,
		picasaOptions: {
			sort: 'date-posted-asc',
			max: 100,
		}
	});

	$(".galleries").not("#" + item.name).hide();
	_gaq.push(['_trackPageview', '/' + item.name]);
}

function create_and_attach_gallery(item) {
	$(item.container).append("<div class='galleries content' id="+ item.name +"></div>");
	$(item.container).find(".second_level_link").click(function() {
		show_gallery_for_album(item);
		if ($("#open_gallery").is(":visible"))
			$("#open_gallery, #square_6 .close").toggle();
	});
}

function hide_subgalleries() {
	$(".galleries").hide();
	$("#open_gallery, #square_6 .close").toggle();
}

function show_subgalleries() {
	$(".close, .content").hide();
	$(".links").not(".second_level_link").show();
	$("#open_gallery, #square_6 .close").toggle();

	var items = [{name: "anatolia", container: "#square_1", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiAnatolici"},
	{name: "persia", container: "#square_13", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiPersiani"},
	{name: "caucaso", container: "#square_8", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiCaucaso"},
	{name: "tibet", container: "#square_15", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiTibetani"},
	{name: "africa", container: "#square_3", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiAfricani"},
	{name: "turkmeni", container: "#square_20", target: "#!gallery", gallery: null, gallery_name: "TappetiAntichiTurkmeni"}];

	if ($(".second_level_link").size() === 0) {
		for (var i=0; i < items.length; i++) {
			$(items[i].container).addClass("square_link").append('<a class="links second_level_link" style="display: none" href="' + items[i].target + '">' + items[i].name + '</a>');
			create_and_attach_gallery(items[i]);
		}
	}

	$(".second_level_link").each(function(i,element){
		$(element).delay(i*200).fadeIn();
	});

}

function hide_ceramics() {
	$(".galleries").hide();
	$("#open_ceramics, #square_18 .close").toggle();
}

function show_ceramics() {
	$(".close, .content").hide();
	$(".links").not(".second_level_link").show();
	var item = {name: "maiolica", container: "#square_18", target: "#!gallery", gallery: null, gallery_name: "Maioliche"};
	$(item.container).append("<div class='galleries content' id="+ item.name +"></div>");
	show_gallery_for_album(item);
	$("#open_ceramics, #square_18 .close").toggle();
	_gaq.push(['_trackPageview', '/ceramics']);
}

jQuery(document).ready(function($) {
	change_colors();
	setInterval("change_colors()", 10000);
	$(".square_link").click(function(){ open_link(this) });
	$("#gallery").click(function(event){ event.stopPropagation() });
	//$("#square_6").click(show_second_level);
	$("#open_gallery").click(show_subgalleries);
	$("#square_6 .close").click(hide_subgalleries);
	$("#open_ceramics").click(show_ceramics);
	$("#square_18 .close").click(hide_ceramics);
	$("#open_"+location.hash.substring(2)).click();
});

