$(function(){
	if($.browser.msie && $.browser.version < 9)
	{

		$('.rounded, .r_wrapper span a').each(function(){
			var borders = {
				top: parseInt($(this).css('borderTopWidth')),
				right: parseInt($(this).css('borderRightWidth')),
				bottom: parseInt($(this).css('borderBottomWidth')),
				left: parseInt($(this).css('borderLeftWidth'))
			};
			var margin = {
				top: parseInt($(this).css('marginTop')),
				right: parseInt($(this).css('marginRight')),
				bottom: parseInt($(this).css('marginBottom')),
				left: parseInt($(this).css('marginLeft'))
			};
			var dims = {
				width: parseInt($(this).outerWidth())-borders.right-borders.left,
				height: parseInt($(this).outerHeight()-borders.top-borders.bottom)
			};
			if(borders.top+borders.right+borders.bottom+borders.left > 0){
				var elem = $('<span>').addClass('padd_rounded').css({
					backgroundColor : $(this).css('borderBottomColor'),
					'position':'relative',
					'display':'block',
					'marginTop' : margin.top,
					'marginRight' : margin.right,
					'marginBottom' : margin.bottom,
					'marginLeft' : margin.left,
					'width' : dims.width,
					'height': dims.height
				//'float': $(this).css('float')
				});
				if(borders.top>0) elem.css('paddingTop',borders.top);
				if(borders.right>0) elem.css('paddingRight',borders.right);
				if(borders.bottom>0) elem.css('paddingBottom',borders.bottom);
				if(borders.left>0) elem.css('paddingLeft',borders.left);
				$(this).css({
					'border': '0 none',
					'margin':0
				});
				elem.append($(this).clone(true));
				$(this).replaceWith(elem);
			}
		//console.log($(this).attr('class')+' -> border: '+borders.top+' '+borders.right+' '+borders.bottom+' '+borders.left);
		});
	}

	if($('.attached').length){
		if($('.attached .list .item').length>4){
			$(".attached").jCarouselLite({
				btnNext: ".attached-wrapper .right_btn",
				btnPrev: ".attached-wrapper .left_btn",
				visible:4
			});
		}
		else{
			$('.attached-wrapper .left_btn, .attached-wrapper .right_btn').hide();
		}
	}

});

var showloginform = 0;
var showaccountlinks = 0;

$(document).ready(function(){
													 
	$('#info_progetti_2011 a').click(function(event) {
		event.preventDefault();
		$('#info_progetti_2011').hide();
		$('#info_progetti_2010').show();
	});
	
	$('#info_progetti_2010 a').click(function(event) {
		event.preventDefault();
		$('#info_progetti_2010').hide();
		$('#info_progetti_2011').show();
	});
	
	$('#btn_login a').click(function(event) {
		event.preventDefault();
		if (showloginform == 0) {
			$('#login_form').show();
			showloginform = 1;
		} else {
			$('#login_form').hide();
			showloginform = 0;
		}
	});

	$('#logged .account-info .account-btn-wrapper .account-btn').click(function(event) {
		event.preventDefault();
		if (showaccountlinks == 0) {
			$('.account-links').show();
			showaccountlinks = 1;
		} else {
			$('.account-links').hide();
			showaccountlinks = 0;
		}
	});

	if( $('#last_info_tabs').length ){
		hidetabs();
		$('#last_info_tabs .widget:first').show();
		$('#last_info_tabs .tabs-titles .tab-title').click(function(event){
			event.preventDefault();
			var id = $(this).attr('href');
			id = id.replace('#', '');
			hidetabs();
			if(id == 'recent-posts-3') $('#last_info_tabs .tabs-titles').removeClass('tabs-titles-2').addClass('tabs-titles-1');
			else $('#last_info_tabs .tabs-titles').removeClass('tabs-titles-1').addClass('tabs-titles-2');
			$('#'+id).show();
		});
	}
	function hidetabs(){
		$('#last_info_tabs .widget').each(function(){
			$(this).hide();
		});
	}
	function hidefutureeventstabs(){
		$('.event-body .event-section').each(function(){
			$(this).hide();
		});
	}
	var event_tab_selected;
	if( $('.event-section-titles').length ){
		hidefutureeventstabs();
		
		
		if(window.location.href.search('eventsubscribe-link') > 0)
		{
			event_tab_selected = 'eventsubscribe';
			$('#'+event_tab_selected).show();
		}
		else{
			$('.event-body .event-section:first').show();
			event_tab_selected = ($('.event-section-titles .event-section-title a:first').attr('href')).replace('#', '');
		}

		/* $('.event-section-titles').addClass(event_tab_selected+'-active'); */
		$('.event-section-titles a').click(function(event){
			event.preventDefault();
			var id = $(this).attr('href');
			id = id.replace('#', '');
			hidefutureeventstabs();
			$('.event-section-title').removeClass('event-tab-active');
			$(this).parent().addClass('event-tab-active');
			event_tab_selected = id;
			$('#'+id).show();
		});
	}

/* animazione hp */

	var tempo = setInterval(function () { go_next() }, 15000);
	
	$('#slide_up').click(function(event){
		event.preventDefault();
		if(curr_step!=5) {
			go_next();
			clearInterval(tempo);
			tempo = setInterval(function () { go_next() }, 15000);
		}
	});
	
	$('#slide_down').click(function(event){
		event.preventDefault();
		if(curr_step!=1) {
			go_prev();
			clearInterval(tempo);
			tempo = setInterval(function () { go_next() }, 15000);
		}
	});

	$('#videotrigger').click(function(event){
		event.preventDefault();
		if(is_video_on==0) {
			$('.youtube_video_container').show();
			is_video_on = 1;
			clearInterval(tempo);
		} else {
			$('.youtube_video_container').hide();
			if ($.browser.msie) {
				remove_video();
			}
			is_video_on = 0;
		}
	});
	
	$('#videotrigger_2').click(function(event){
		event.preventDefault();
		if(is_video_on_2==0) {
			$('.youtube_video_container_2').show();
			is_video_on_2 = 1;
			clearInterval(tempo);
		} else {
			$('.youtube_video_container_2').hide();
			if ($.browser.msie) {
				remove_video();
			}
			is_video_on_2 = 0;
		}
	});

/* fine animazione hp */

	if($('#login_username').val()!="") {
		$('#login_username').addClass('nobg');
	}
	
	if($('#login_password').val()!="") {
		$('#login_password').addClass('nobg');
	}
	
	$('#login_form input').focus(function(event) {
		$(this).addClass('nobg');
	});
	
	$('#login_form input').blur(function(event) {
		if ($(this).val()=="") {
			$(this).removeClass('nobg');
		}
	});

	/*
	 * Scritta cerca su form di ricerca
	 */

	if(jQuery('#searchform').length)
	{
		if( jQuery.trim(jQuery('#searchform #s').val()) == ''	){
				jQuery('#searchform #s').val('CERCA');
			}
		jQuery('#searchform #s').bind({
			'focus' : function(){
				if(
						jQuery.trim(jQuery(this).val()) == 'CERCA'
						|| jQuery.trim(jQuery(this).val()) == ''
					){
					jQuery(this).val('');
				}
			},
			'blur' : function(){
				if( jQuery.trim(jQuery(this).val()) == ''	){
					jQuery(this).val('CERCA');
				}
			}
		});
	}

	jQuery('.social_links a').each(function(){
		jQuery(this).attr('target', 'blank');
	});

	/* scrolling eventi futuri */
	
	$('#next_events_pallini a').eq(0).css({'opacity':'1'});
	
	$('#next_events_pallini a').click(function(event) {
		event.preventDefault();
		$('#next_events_pallini a').css({'opacity':'0.2'});
		$(this).css({'opacity':'1'});
		$('#next_events_container ul').animate({'margin-left': ($(this).index())*(-580) + 'px'});
	});
	
	/* scrolling eventi passati */
	
	$('#past_events_pallini a').eq(0).css({'opacity':'1'});
	
	$('#past_events_pallini a').click(function(event) {
		event.preventDefault();
		$('#past_events_pallini a').css({'opacity':'0.2'});
		$(this).css({'opacity':'1'});
		$('#past_events_container ul').animate({'margin-left': ($(this).index())*(-591) + 'px'});
	});
	
	/* scrolling rullo sidebar */
	
	/*
	var tempo_rullo = setInterval(function () { go_next_li() }, 6000);
	
	$('#rullo_home_pallini a').eq(0).css({'opacity':'1'});
	
	$('#rullo_home_pallini a').click(function(event) {
		event.preventDefault();
		clearInterval(tempo_rullo);
		tempo_rullo = setInterval(function () { go_next_li() }, 6000);
		$('#rullo_home_pallini a').css({'opacity':'0.2'});
		$(this).css({'opacity':'1'});
		curr_li = $(this).index();
		$('#rullo_home ul').animate({'left': curr_li*(-298) + 'px'});
	});
	*/
	
});

(function( $ ){

	$.fn.animateFromPos = function( options, hidemefirst ) {

		var hide = (hidemefirst) ? hidemefirst : false;
		if($.browser.msie) hide = false;
		var settings = {
			top		:	'auto',
			right	:	'auto',
			bottom:	'auto',
			left	:	'auto',
			duration:	300,
			callback: function(){}
		};
		var posit = {};
		var animateOpt = {};

		return this.each(function() {
			if ( options ) {
				$.extend( settings, options );
			}

			var posit = {};

			if(settings.top != 'auto')	posit.top = parseInt($(this).css('top'));
			if(settings.right != 'auto')	posit.right = parseInt($(this).css('right'));
			if(settings.bottom != 'auto')	posit.bottom = parseInt($(this).css('bottom'));
			if(settings.left != 'auto') posit.left = parseInt($(this).css('left'));
			if(hide === true)	posit.opacity = 1;
			
			if(hide===true) $(this).css({opacity:0});
			
			animateOpt = {};
			$.each(settings, function(){
				if(settings.top != 'auto') animateOpt.top = settings.top;
				if(settings.right != 'auto') animateOpt.right = settings.right;
				if(settings.bottom != 'auto') animateOpt.bottom = settings.bottom;
				if(settings.left != 'auto') animateOpt.left = settings.left;
				if(hide === true)	animateOpt.opacity = 0;
				animateOpt.visibility = 'visible';
			});

			$(this)
					.css(animateOpt)
					.animate(posit,settings.duration,'easeOutQuint',settings.callback);
		});

	};
})( jQuery );

var curr_step = 1;
var step = 276;
var curr_pos = -1104;
var is_video_on = 0;
var is_video_on_2 = 0;

function remove_video() {
	$('.youtube_video_container object').remove();
	$('.youtube_video_container').append('<object width="382" height="242"><param name="movie" value="http://www.youtube.com/v/eKb-qeEQ4R0?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/eKb-qeEQ4R0?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="382" height="242" wmode="transparent"></embed></object>');
	
	$('.youtube_video_container_2 object').remove();
	$('.youtube_video_container_2').append('<object width="382" height="242"><param name="movie" value="http://www.youtube.com/v/hVMHZPZoJv8?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/hVMHZPZoJv8?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="382" height="242" wmode="transparent"></embed></object>');
	
}

function go_next() {
	if(curr_step!=5) {
		curr_pos = curr_pos + step;
		$("#i_lanci").animate({"top": curr_pos + "px"},240);
		$("#slide_down").css({"opacity":"1"});
		curr_step ++;
		if(curr_step==5) {
			$("#slide_up").css({"opacity":"0.3"});
		}
	} else {
		curr_pos = -1104;
		$("#i_lanci").animate({"top": curr_pos + "px"},240);
		$("#slide_down").css({"opacity":"0.3"});
		$("#slide_up").css({"opacity":"1"});
		curr_step = 1;
	}
	$('.youtube_video_container').hide();
	$('.youtube_video_container_2').hide();
	if ($.browser.msie) {
		remove_video();
	}
	is_video_on = 0;
	is_video_on_2 = 0;
}

function go_prev() {
	curr_pos = curr_pos - step;
	$("#i_lanci").animate({"top": curr_pos + "px"},240);
	$("#slide_up").css({"opacity":"1"});
	curr_step --;
	if(curr_step==1) {
		$("#slide_down").css({"opacity":"0.3"});
	}
	$('.youtube_video_container').hide();
	$('.youtube_video_container_2').hide();
	if ($.browser.msie) {
		remove_video();
	}
	is_video_on = 0;
	is_video_on_2 = 0;
}


/* animazione rullo come funziona */

var curr_li = 0;
var tot_li = 5;

function go_next_li() {
	$('#rullo_home_pallini a').css({'opacity':'0.2'});	
	$('#rullo_home_pallini a').eq(curr_li).css({'opacity':'1'});
	$('#rullo_home ul').animate({'left': curr_li*(-298) + 'px'},function() {
			if (curr_li==tot_li+1) {
				$('#rullo_home ul').css({'left':'0px'});
				$('#rullo_home_pallini a').eq(0).css({'opacity':'1'});
				curr_li = 0;
			}
	});
	curr_li++;
}
