/*jQuery.noConflict();*/
jQuery(document).ready(function(){
	
	
	/* credits Window - START */
	if(jQuery('a.credits_link[rel]').length>0) {
		jQuery('a.credits_link[rel]').click(function(){
			link_ajax=jQuery(this).attr('rel').replace(/____/g,'/');
			var link_top=jQuery(this).offset().top-0;
			var link_left=jQuery(this).offset().left-0;
			var link_width=jQuery(this).width();
			var win_h=jQuery('body').height()-0;
			var win_w=jQuery('body').width()-0;
			jQuery('body').append('<div class="window_credits_layer" style="position: absolute; top: 0; left: 0; width: '+win_w+'px; height: '+win_h+'px;">&nbsp;</div><div class="window_credits visibility_hidden"><div class="window_credits_padding"><div class="window_credits_border"><div class="window_credits_header"><a href="#" class="close_credits"><span class="accesskey">chiudi</span></a><div class="credits_title"><h2 class="accesskey">Credits</h2></div><div class="clear">&nbsp;</div></div><div class="clear">&nbsp;</div><div class="credits_ajax"></div><div class="clear">&nbsp;</div></div></div></div>');
			jQuery('.credits_ajax').load(link_ajax, function(response,status,xhr){
				n=0
				waitingForResult = setInterval(function(){
					if(status=='success'){
						clearInterval(waitingForResult);
						var mgl=0;
						var mgt=0;
						var mg_w=jQuery('.window_credits').width()-0;
						var mg_h=jQuery('.window_credits').height()-0;
						/* mgl=win_w-mg_w-10; */
						mgl=Math.floor(link_left+(link_width/2)-(mg_w/2));
						if ((mgl+mg_w)>win_w){mgl=win_w-mg_w-5;}
						if (mgl<0){mgl=5;}
						/*if (mgl>link_left) { mgl=link_left;}*/
						mgt=win_h-mg_h-0;
						if (link_top<mg_h){mgt=link_top;}
						else {mgt=link_top-mg_h;}
						jQuery('.window_credits').css({'top': mgt+'px','left': mgl+'px' });
						jQuery('.window_credits').toggleClass('visibility_hidden');
						/* modifica target blank */
						jQuery('a.nuovafinestra').click(function(){
								jQuery(this).attr('target','_blank');
						});
						jQuery('a.nuovafinestra_inline').click(function(){
								jQuery(this).attr('target','_blank');
						});
						/* fine target blank */
						if (jQuery('.window_credits').length>0)
						{
							jQuery(window).resize(function(){
								jQuery('.window_credits').remove();
								jQuery('.window_credits_layer').remove();
							});
							jQuery('.close_credits, .window_credits_layer').click(function(){
								jQuery('.window_credits').remove();
								jQuery('.window_credits_layer').remove();
								return false;
							});
						}
					}
				}, 500);
			});
			return false;
		});
	}
	/* credits Window - END */

	
	/* calendario eventi */
	// datepicker
	jQuery('.datepicker').datepicker();

	// scelta periodo
	jQuery('.ric_label.choice').each(function(){
		var sub_label=jQuery(this).children('label').text();
		var sub_value=jQuery(this).next('.ric_radio').children('input').attr('value');
		jQuery(this).parent().append('<div class="ric_link"><a href="#" alt="'+sub_label+'" value="'+sub_value+'" class="choice_link">'+sub_label+'</a></div>');
		jQuery(this).next('.ric_radio').remove();
		jQuery(this).remove();
	});
	jQuery('.ric_choice a').click(function(){
		 var sub_value=jQuery(this).attr('value');
		 jQuery(this).parent().append('<input type="hidden" name="choice" value="'+sub_value+'">');
		 jQuery(this).parents('form').submit();
	}); 
	
	/* paginazione banner box_vetrina */
	if (jQuery('.box_vetrina').length>0) {
		jQuery('.banner_home_navigazione_sx,.banner_home_navigazione_dx').each(function(i){
			jQuery(this).click(function(){
				var attuale = jQuery(this).closest('.box_vetrina').find('.ogni_tre:not(.nascosto)');
				attuale.addClass('nascosto');
				var next;
				if (i==0) {
					next = attuale.prev('.ogni_tre');
				}
				else {
					next = attuale.next('.ogni_tre');
				}
				if (next.length>0) {
					next.removeClass('nascosto');
				}
				else {
					if (i==0) {
						attuale.parent().find('.ogni_tre:last').removeClass('nascosto');
					}
					else {
						attuale.parent().find('.ogni_tre:first').removeClass('nascosto');
					}
				}
			});
		});
	}	

/* Gestione collocazioni - START */
	/* jQuery('.admin_collocazioni strong').replaceWith('<a  class="toggle_tendina" href="#"><strong>Collocazioni</strong></a>');
	jQuery('.admin_collocazioni ul').addClass('nascosto');
	jQuery('.admin_collocazioni a.toggle_tendina').click(function(){
		if (jQuery('.admin_collocazioni ul').hasClass('nascosto'))
			jQuery('.admin_collocazioni ul').removeClass('nascosto');
		else
			jQuery('.admin_collocazioni ul').addClass('nascosto');
		return false;
	});
	*/
	if (jQuery('.window_collocazioni').length>0)
	{
		jQuery('#odwt-icon-collocazioni').click(function(){
			if (jQuery('.window_collocazioni').hasClass('nascosto'))
				jQuery('.window_collocazioni').removeClass('nascosto');
			else
				jQuery('.window_collocazioni').addClass('nascosto');
			return false;
		});
	}
	else
	{
		jQuery('#odwt-icon-collocazioni').hide();
	}
/* Gestione collocazioni - END */

	/* target blank start */
	
	// title target blank
	jQuery('a.nuovafinestra').each(function(){
	        var title=jQuery(this).attr('title');
	        if (title!='') {title=title+' - ';}
	jQuery(this).attr('title',title+'Il link si apre in una nuova finestra');
	});
	jQuery('a.nuovafinestra_inline').each(function(){
	        var title=jQuery(this).attr('title');
	        if (title!='') {title=title+' - ';}
	        jQuery(this).attr('title',title+'Il link si apre in una nuova finestra');
	});
	
	// modifica target blank
	jQuery('a.nuovafinestra').click(function(){
	        jQuery(this).attr('target','_blank');
	});
	jQuery('a.nuovafinestra_inline').click(function(){
	        jQuery(this).attr('target','_blank');
	});
	
	/* target blank end */	
	
	/* menu */
	
	jQuery('a.open_close').click(function(){
		jQuery(this).parents('.menu_sub').find('.ul3liv').slideToggle().toggleClass('hide');
		jQuery(this).parents('.menu_sub').prevAll().each(function(){
			
			if (!jQuery(this).find('.ul3liv').hasClass('hide')){
				jQuery(this).find('.ul3liv').slideToggle().addClass('hide');
			}
		});
		jQuery(this).parents('.menu_sub').nextAll().each(function(){
			
			if (!jQuery(this).find('.ul3liv').hasClass('hide')){
				jQuery(this).find('.ul3liv').slideToggle().addClass('hide');
			}
		});
	return false;
	});

});
