$(document).ready(function(){ siteInit();});
$(window).bind('load', function(){GUnload();});  

// funzione per inizializzare il layout del sito e 
// gli observer
//------------------------------------------------
function siteInit(){

	// sostituisco gli eventuali indirizzi email
	//------------------------------------------------	
	$('.indirizzoEmail').each(function (i){
		var address = $(this).html();
		addressArray = address.split(" ");
		var text = indirizzoEmail(addressArray);
		$(this).html(text);
	});
	

	$('.annoSelezionabile').click(function(event) {
		aggiornaEventi(event);
	});

	
	$('.apri').click(function(event) {
		if ($('#' + $(this).attr('rel')).is(':visible')) {
			$('#' + $(this).attr('rel')).slideUp('slow');
	    	$(this).html('Continua a leggere...');	    
		} else {
			$('#' + $(this).attr('rel')).slideDown('slow');
	    	$(this).html('Chiudi');	    
		}
	});
	

	// scroll nei link interni nel vademecum
	//------------------------------------------------	
	if ($('.scrollabile').length) {
		$('.scrollabile').localScroll();
	}

	
	if ($('#sliderCapodanno').length) {	
		setInterval( "slideSwitch()", 4000 );
	}
	

	// observer per nascondere i submenu non relativi
	// alla pagina corrente
	//------------------------------------------------
	$(".sottoMenu:not('.attuale')").each(function(i)
		{
			$(this).hide();
		});



	if ($('#slider').length)
	{
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			pause: 4000,
			controlsShow: false,
			speed: 1200
		});

		// caption sulle immagini dello slider
		//------------------------------------------------					
		$('.boxgrid.captionfull').hover(function(){
			$(".cover", this).stop().animate({top:'150px'},{queue:false,duration:160});
		}, function() {
			$(".cover", this).stop().animate({top:'200px'},{queue:false,duration:160});
		});

	}
	

	if ($('.prevendita').length) {
		$('.prevendita').click(function (event) { 									
			
			var strClassi = $(event.target).attr('class');
			var classiSplitted = strClassi.split(' ');
			var eventoId = '';
			
			$.each(classiSplitted, function(){				
				if (this.indexOf('eid_') >= 0) {
					eventoId = this.substring(4);					
					src = 'http://www.eventbrite.com/tickets-external?ref=etckt&eid=' + eventoId;
					$('#framePrevendita').attr('src', src);						
					$("#dialog").dialog({
						bgiframe: true,
						height: 330,
						width: '90%',
						modal: true,
						resizable: false,
						title: 'Prevendita accredito all\'evento'
					});	

					$("#dialog").dialog('open');    	

				}
			});
			
		});		
	}
	

	// mappa google nei contatti
	//------------------------------------------------	
	if ($('#map').length ) 
	{
		var map = new GMap2(document.getElementById("map"));
		var center = new GLatLng(45.417890,10.9854161);
		map.setCenter(center, 15);

		var marker = new GMarker(center, {draggable: true});
		var description = "<strong>INTERZONA Associazione Culturale</strong><br />Via Scuderlando, 4<br />37135 Verona (VR)<br />Italia";
	
		map.addControl(new GLargeMapControl());
		
		var point = new GLatLng(45.417890,10.9854161);
		var mark = new GMarker(point);
		map.addOverlay(mark);
										
		mark.openInfoWindowHtml(description);

		GEvent.addListener(mark, "click", function() {
				mark.openInfoWindowHtml(description);
		});
	}


    $('.linkImmagine').hover(function(){
        id = $(this).attr('rel');
        $('#' + id + 'h').show();                                     
    }, function(){
        id = $(this).attr('rel');
        $('#' + id + 'h').hide();                                     
    });


    
    
    
    
    jQuery('.linkImmagine').click(function(event)
    {
    	
    	$.fancybox.open('<h1>FUFU</h1>');
    	$.fancybox('<h1>GIGI</h1>');
    	
        opzioni = {'id' : $(this).attr('rel')};
              
        jQuery.ajax({
        url: '/galleria/dettaglio-album',
        type: "POST",
        data: (opzioni),
        dataType: "json",
        success: function(risposta){
        	
        	jQuery('#galleria').galleria({
    		    data_source: risposta,
    	        width:500,
    	        height:500,
    		});
        	//mostraFoto(risposta);        	
/*
        	if (risposta.descrizione) {
                $('#descrizioneProgetto').html((risposta.descrizione).replace(/\n/g, "<br />"));
            }       
            if (risposta.foto) {        
                foto = '';
                $.each(risposta.foto, function(indice, valore) { 
                    foto += '<li>';
                    foto += '<a class="thumb" href="' + valore.foto + '" title="' + risposta.titolo + '">';
                    foto += '<img src="' + valore.anteprima + '" alt="' + risposta.titolo + '" />';
                    foto += '</a>';
                    foto += '<div class="caption">';
                    foto += '<div class="image-title"></div>';
                    foto += '</div>';
                    foto += '</li>';
                });
                
                $('ul.thumbs').html(foto);
                
                
            };
            
            //var gallery = $('#thumbs').galleriffic(opzioniGalleria);
            console.info('ecco');
            
            
            //$('#contenutoSecondario').slideDown('slow');
            //$('#contenutoSecondario').show();
            
           	*/
    
        },
        error: function(){
        }
     });                
       
        
    });
	
        
	
}


function mostraFoto(risposta) 
{

		$('#galleria').galleria({
		    data_source: risposta,
	        width:500,
	        height:500
		});
	
	
}



// Funzione per comporre via script l'indirizzo 
// email, questo per evitare gli agenti spammer
//------------------------------------------------
function indirizzoEmail(addressArray){
	
	var domain = "izona";
	var atsign = "&#64;";
	var dotsign = ".";

	var username = addressArray[0];
	var extension = addressArray[addressArray.length - 1];
	var domain = addressArray[addressArray.length - 3];

	var addr = username + atsign + domain + dotsign + extension;
	var text = username + atsign + domain + dotsign + extension;

	var address = '<' + 'a' + ' ' + 'href="' + 'mail' + 'to:' + addr + '">' + text + '<\/' + 'a' + '>';

	return address;
 
}




function aggiornaEventi(evento) {
	
	$('.annoAttivo').each(function() {
		$(this).removeClass('annoAttivo');
	});
		
	anno = $(evento.target).html();
	
	$('li[name=' + anno +']').addClass('annoAttivo');
	
	jQuery.ajax({
        url: "/archivio/aggiorna-eventi",
        type: "POST",
        data: 'anno=' + anno,
        dataType: "html",
        success: function(risposta){
			$('#elencoDateArchivio').html(risposta);
    		//console.info('SUCCESSO');
    	},
	    error: function(){
    		console.info('ERRORE');
	    }
    });
}   


// TODO: solo per capodanno
function slideSwitch() {
	
    var $active = $('#sliderCapodanno img.active');

    if ($active.length == 0) {
    	$active = $('#sliderCapodanno img:last');
    }

    var $next =  $active.next().length ? $active.next() : $('#sliderCapodanno img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}






