/********************************************/
/*	core.js for jquery
/*	Author: davide reppucci | nascar
/*  Author URI: http://www.nascar.it
/*  Version: 0.2
/*
/*  It includes: autogrow, accordion,
/*  ui-tabs, slider, searchform, checkForm
/*  cufon, externalLink
/*
/*  latest update: 11/06/09
/*******************************************/

var core = {
	
	
	bootstrap : function( _labels ) {
		
		// se ci sono textarea con classe expanding
		if ( $('textarea.expanding').length > 0 ) {

			core.expandTextarea();
		
		}
		
		if ( $('#map').length > 0 ) {
		
			showmaps();
			
			//showAddress(_firstMap.search, _firstMap.firstBoolean, _firstMap.name, _firstMap.appear, _firstMap.secondBoolean);
			
		}

		
		// se ci sono classi accordion
		if ( $('.accordion').length > 0 ) {

			core.accordionElement();
		
		}
		
		// se ci sono classi ui-tabs
		if ( $('.ui-tabs').length > 0 ) {

			core.activateTabs();
		
		}
		
		// se ci sono classi .slider
		if ( $('.slider').length > 0 ) {

			core.slideGallery();
		
		}
		
		// se ci sono classi .fade
		if ( $('.fade').length > 0 ) {

			core.fadeGallery();
		
		}
		
		// se c'è il form di ricerca
		if ( $('#advanced').lenght > 0 ) {

			core.toggleSearchForm();
		
		}
		
		// se ci sono classi .fade
		if ( $('.cycle-slider').length > 0 ) {
			
			core.cycleSlider();
		
		}
		
		// se ci sono classi accordion
		if ($('#news-home .titolo a').length > 0 && $('#nh-adv').length > 0 ) {

			$('#news-home').mouseenter(function() {
			
				$('#nh-adv').fadeIn();
				return false;
			
			});
			
			$('#nh-adv').mouseleave(function() {
			
				$('#nh-adv').fadeOut();
				return false;
			
			});
		}
		
		// initializzo il form validator
		Forms.init(_labels);
		
		// inizializzo il custom form elements
		Custom.init();
	
		// esternalizzo i link con class external
		var externals = new externalLink();
		
		if ( $('#newsletter').length > 0 ) {
		
			core.activateBoxNewsletter();
		
		}
		
		// Form contatti
		if ( $('#bottone-si').length > 0 ) {

			$('#bottone-si').click(function() {
				$('#risposta-si').slideDown();
				$('#risposta-no').slideUp("fast").hide();

				if(!$('#bottone-si').hasClass('selected')) {
					$('#bottone-si').addClass('selected');
				}
				
				if($('#bottone-no').hasClass('selected')) {
					$('#bottone-no').removeClass('selected');
				}
				
				return false;
			});
			
			$('#bottone-no').click(function() {
				$('#risposta-no').slideDown();
				$('#risposta-si').slideUp("fast").hide();

				if(!$('#bottone-no').hasClass('selected')) {
					$('#bottone-no').addClass('selected');
				}
				
				if($('#bottone-si').hasClass('selected')) {
					$('#bottone-si').removeClass('selected');
				}
				
				return false;
			});
		}
		
		jQuery('.dropdown').bgiframe();
		
		if($('body.home').length > 0 && $('#shadowbox-content').length > 0) { 
			Shadowbox.open({
		        content:    $('#shadowbox-content').html(),
		        player:     "html",
		        title:      "",
		        height:     374,
		        width:      956
		    });
		} 
		
		if($('div.toselect').length > 0) {
			
			$('div.toselect').each(function() {
				
				// Genero p che dovrà ospitare select
				var p = $('<p/>');
				p.attr('class', $(this).attr('class'))
				//alert(p);

				//var select = $('<select />');
				
				// Appendo al p la label nel div
				label = $(this).children('label');
				p.append(label);
				
				// Genero select
				var select = $('<select />');
				select.attr('class', 'lunga-c');
				select.change(function() {
					if(this.selectedIndex > 0) location.href = $(this).val();
				})
				
				// Prima option vuota
				select.append('<option value="">-</option>');
				
				// Ciclo su elementi a del div
				$(this).find('a').each(function() {
					option = $('<option />');
					option.attr('value', $(this).attr('href'));
					option.html($(this).html());

					if($(this).hasClass('selected')) {
						option.attr('selected', 'selected');
					}
					
					select.append(option);
				});
				
				// Appendo al p la select
				p.append(select);
				
				// Aggiungo p dopo div
				$(this).after(p);
				
				// Nascondo div
				$(this).hide();
			});
		}
	},
	
	// metodo per espandere le textarea
	expandTextarea : function() {
		
		$("textarea.expanding").each(function(i){

		   $(this).autogrow({ maxHeight: 500, minHeight: 100, lineHeight: 23 });
		      
		});	
	
	},
	
	// metodo per attivare l'accordion
	accordionElement : function() {
		
		$(".accordion").each(function(i){

		   $(this).accordion({ animated: 'slide', autoHeight: false });
		   		      
		});	
	
	},
	
	// metodo per attivare i tabs
	activateTabs : function() {
		
		$(".ui-tabs").each(function(i){
		   
		   $(this).tabs({ remote: true, fx: { opacity: 'toggle', duration: 200 } });
		   		      
		});	
        
        if(('.ui-tabs a[href^=#contenuto_]').length > 0 ) {
            $(".ui-tabs a[href^=#contenuto_]").each(function(i){
            	itab = $(this).attr('href');
            	htabs = $(".ui-tabs div.testo-tabs a[href$=" + itab + "]");
            	if(htabs.length > 0 ) {
	            	htabs.each(function(j){
	            		$(this).attr('rel', itab)
	            		$(this).click(function() {
		                       $('.ui-tabs').tabs('select', $(this).attr('rel') );
		                       return false;
		                });
	            	});
            	}
            });       
        }
	
	},
	
	slideGallery : function() {
	
		$(".slider").each(function(i){
		   
		   $(this).easySlider({
				controlsBefore:	'<p id="controls">',
				controlsAfter:	'</p>',
				auto: true, 
				continuous: true
			});
		   		      
		});
	
	},
	
	cycleSlider : function() {
		
		$(".cycle-slider").each(function(i){
		   
		   $(this).cycle({
		   		
		   		fx:'fade',
		   		pager:'.cycle-pager',		   
		   		speed:700
		   
		   });
		   		      
		});
	
	},
	
	// metodo per mostrare/nascondere il form di ricerca
	toggleSearchForm : function() {
	
		$('#advanced').hide();
        
        $('#search_button').click(function () {
        	
        	if( $(this).hasClass('aggiungi') ) {
        	
				$(this).addClass('rimuovi').removeClass('aggiungi');
				$('#advanced').slideDown("slow");
				
			} else {
			
				$(this).addClass('aggiungi').removeClass('rimuovi');
				$('#advanced').slideUp("slow");
				
			}	
					      
	      
	    });
	
	},
	
	// fade gallery
	fadeGallery : function() {
	
		$(".fade ul").each(function(i){
		   
		   $(this).innerfade({
				speed: 'slow',
				timeout: 6000,
				type: 'sequence'
			});
		   		      
		});
	
	},
	
	// metodo per attivare il box newsletter
	activateBoxNewsletter : function() {
		
		$(".open-newsletter").each(function(i){
		
			$(this).click(function() {
			
				if( $('#newsletter').hasClass('opened') ) {
			   
				   	$('#newsletter').fadeOut('slow');
			   
				} else {
				
					$('#newsletter').fadeIn('slow');
					   
				}
				return false;
				
			});
				  
		});
		
		$("#bn-close").click(function() {
		
			$('#newsletter').fadeOut('slow');
			return false;
		
		});	
	
	}
	
};

