$(document).ready(function() {


	$('.js, .nojs').show();
	
	
	$('#lastposts a').focus(function() {
		$(this).blur();
	});


	$('#blog-latest').jcarousel({
        // Configuration goes here
        scroll: 1,
        auto: 5,
        initCallback: carousel_links,
        itemVisibleInCallback: light_bullet,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    
    // Al click vado a vedere quale indice ha il link che ho premuto.
    // Quindi vado a scorrere
    video_slider_container = $('#video_carousel');
    
    // Calcolo la sua larghezza totale.
    li_width = video_slider_container.find('li').outerWidth(true);
    total_width = video_slider_container.find('li').size() * li_width;
    video_slider_container.width( total_width );
    
    $('#video_carousel_bullets a').bind('click',function() {
			var index = $(this).parent().index();
			var width = video_slider_container.find('li').outerWidth(true);
			var right = index * width;
			
			$('#video_carousel_bullets a').removeClass('selected');
			video_slider_container.animate({
				'right':right
			},1000);
			
			$(this).addClass('selected');
			
			return false;
	});
		
	// Impostiamo il timer per lo scroll
	scroll_video_interval = setInterval( function(){
			next_a = null;
			$('#video_carousel_bullets a').each(function(index, node){
				if ( $(this).hasClass('selected') ){
					next_a = $('#video_carousel_bullets a:eq(' + (index + 1) +')');
					
				}
			});
			next_a.trigger('click');
			
			

	},5000 );


// EFfetto di rollover alle immagini cliccabili
    $('img.rollover').hover(function() {
        $(this).attr('src', $(this).attr('src').replace('.png', '-hover.png'));
    }, function() {
        $(this).attr('src', $(this).attr('src').replace('-hover.png', '.png'));
    });

    jQuery('#members-list li,#groups-list li').hover(function(){
    	jQuery(this).attr('style','background-color:#f1f8fb');
    },function(){
    	jQuery(this).removeAttr('style');
    });
    
    jQuery('#new-topic-button').bind('change click',function(){
    	if(jQuery('#new-topic-post:visible').size() > 0)
    		jQuery('#new-topic-button').removeClass('open');
    	else
    		jQuery('#new-topic-button').addClass('open');
    });
    	

    $('#search-terms').focus(function() {
        if (this.value == 'Search') {
            this.value = '';
        }
    });
	jQuery('a:not(.flickr)').each(function(){
		url=jQuery(this).attr('href');
		if(url!=undefined&&url.indexOf('http')!=-1&&url.indexOf(location.host)==-1){
			jQuery(this).attr({'title':'External Site, a new window will be open'});
			jQuery(this).addClass('external');
			}
	});
	jQuery('a.external').click(function(){
		new_win=window.open(this.href,'new_win');new_win.focus();return false;});
	
	jq('.show-hide-members').click(function(){
		jQuery(this).parent().next().toggle('fast');
		return false;		
	});
	
	jQuery('.hide-show').hide();
	
});


function light_bullet(carousel, li_item, index) {
	jQuery('#lastposts a').removeClass('selected');
	index = index -2;
    jQuery('#lastposts a:eq(' + index + ')').addClass('selected');
    
    
    // Imposta il link "Read more"
    selected_post = jQuery('#blog-latest > li:eq(' + index + ') h1 a');
    post_link = jQuery(selected_post).attr('href');
    jQuery('#readmore a').attr('href', post_link);
}


function carousel_links(carousel) {
    jQuery('#lastposts a').click(function() {
        goto1 = parseInt($(this).attr('id').substring(5));
        carousel.scroll(goto1);
        
        jQuery('#lastposts a').removeClass('selected');
        jQuery(this).addClass('selected');
        

        
        return false;
    });

}


 function mostraProfilo() {
 	$('#item-nav').show('slow');
 	$('#item-body').show('slow');
 }



