/********************************************/
/*	core.js for jquery
/*	Author: davide reppucci | nascar
/*  Author URI: http://www.nascar.it
/*	Version 0.2
/*
/*  PLUGIN LOADED
/*	Autogrow
/*	Slide Viewer Pro
/*	Inner Fade
/*	Looped Slider
/*  checkForm
/*	externalLink
/*
/*  latest update: 07/12/09
/*******************************************/

/* ATTIVAZIONE ACCORDION PER MOOTOOLS */
var myAccordion = new Accordion();
window.addEvent('domready', function() {

	//create our Accordion instance
	myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
		trigger: 'mouseover',
		opacity: true,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#41464D');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#528CE0');
		}
	});

	$$('.toggler').addEvent('mouseenter', function() { this.fireEvent('click'); });
});



var core = {

	bootstrap : function( _labels, _firstMap ) {


		// jQuery.prettySociable();

		// se ci sono textarea con classe expanding
		if ( jQuery('textarea.expanding').length > 0 ) {

			core.expandTextarea();

		}
		// se ci sono classi ui-tabs
		if ( jQuery('.ui-tabs').length > 0 ) {

			core.activateTabs();

		}

		// se ci sono classi accordion
		if ( jQuery('.accordion').length > 0 ) { core.activateAccordion(); }

		// se ci sono classi accordion al click
		if ( jQuery('.accordionclick').length > 0 ) { core.activateClickAccordion(); }

		// se esiste #newsletter
		//if ( jQuery('#newsletter').length > 0 ) { core.activateBoxNewsletter(); }

		// se esiste #wishist
		if ( jQuery('#wishlist').length > 0 ) { core.activateBoxWishlist(); }

		// se esiste #wishist
		if ( jQuery('#condividi').length > 0 ) { core.activateBoxCondividi(); }

		// se esiste #condividi product
		// if ( jQuery('.condividiProduct').length > 0 ) { core.activateBoxCondividiProduct(); }

		// se esiste #carrello
		if ( jQuery('#carrello-content').length > 0 ) { core.activateBoxcarrello(); }

		// se esiste #pannello
		if ( jQuery('#pannello').length > 0 ) { core.deactivatePannelloProdotto(); }

		// se esiste #apri-info-prodotto
		if ( jQuery('#apri-info-prodotto').length > 0 ) { core.activatePannelloProdotto(); }

		// se ci sono classi .slides
		if ( jQuery('.slides').length > 0 ) { core.activateLoopedGallery(); }

		// se ci sono textarea con classe expanding
		if ( jQuery('textarea.expanding').length > 0 ) { core.expandTextarea(); }

		// pallini slide
        if ( jQuery('.container .slides').length > 0 ) {

              jQuery(".container").each(function(i){

                       jQuery(this).find('.pagination').css('width', ( jQuery(this).find('.pagination li').length * 24 )  );

                       jQuery(this).find('.pagination').css('margin-left', -( jQuery(this).find('.pagination').width() / 2 ) );

              });

        }

		jQuery.fn.slideFadeToggle = function(speed, easing, callback) {

       		return jQuery(".toggle").animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
    	};

    	//jQuery("#riepilogo h3").click(function () {	 jQuery(".lista").toggle(); });

		jQuery("#pagamento").click(function () {
			var attivo = parseInt(jQuery('#pagamento input:radio:checked').val());
			jQuery('.listapagamenti').accordion({active: attivo,'fillSpace': true,'clearStyle': true});
		});
		jQuery("#spedizione").click(function () {
			var attivo = parseInt(jQuery('#spedizione input:radio:checked').val());
			jQuery('.listaspedizioni').accordion({active: attivo,'fillSpace': true,'clearStyle': true});
		});

		jQuery(".apriricerca").click(function () {

											   if(jQuery(".nascondiricerca").hasClass("hidden")){
													jQuery(".mostraricerca").addClass("hidden");
													jQuery(".nascondiricerca").removeClass("hidden");
													jQuery(".toggle").fadeIn("slow");
											   } else {
												   jQuery(".nascondiricerca").addClass("hidden");
												   jQuery(".mostraricerca").removeClass("hidden");
												   jQuery(".toggle").fadeOut("slow").slideUp("slow");
											   }

											});

		jQuery("#diffaddress").click(function () {
			if (jQuery('#diffaddress').attr('checked')){
				jQuery("#shipping_address_box").fadeIn("slow");
			} else {
				jQuery("#shipping_address_box").fadeOut("slow").slideUp("slow");
		    }
		});

		// initializzo il checkForm
		Forms.init(_labels);

		// inizializzo il custom form elements
		//Custom.init();

		// esternalizzo i link con class external
		var externals = new externalLink();

	},

	// metodo per espandere le textarea
	expandTextarea : function() {

		jQuery("textarea.expanding").each(function(i){

		   jQuery(this).autogrow({ maxHeight: 500, minHeight: 100, lineHeight: 23 });

		});

	},

	// metodo per attivare l'accordion
	activateAccordion : function() {

		jQuery(".accordion").each(function(i){
			jQuery(this).accordion({ event: 'mouseover', alwaysOpen: false, autoheight: false, header: 'h3', animated: 'easeslide', 'fillSpace': false,'clearStyle': false });
		});

	},

	// metodo per attivare l'accordion al click
	activateClickAccordion : function() {

		jQuery(".accordionclick").each(function(i){
			//jQuery(this).accordion({ event: 'click', alwaysOpen: false, autoheight: false, header: 'h3', animated: 'slide','fillSpace': false,'clearStyle': true });
			jQuery(this).accordion({header: 'h3', autoHeight: false});
		});

	},

	// metodo per attivare i tabs
	activateTabs : function() {

		jQuery(".ui-tabs").each(function(i){

		   jQuery(this).tabs({ remote: true, fx: { opacity: 'toggle', duration: 200 } });

		});

	},

	// metodo per attivare la newsletter
	activateBoxcarrello : function() {

		jQuery('#open-carrello').click(function() {
			if ( jQuery('#carrello > .header').hasClass('header-on')){
				jQuery('#carrello-content').fadeOut('fast');
				jQuery('#carrello > .header').removeClass('header-on');
			}else{
				jQuery('#carrello-content').fadeIn('fast');
				jQuery('#carrello > .header').addClass('header-on');
			}
			return false;
		});

		jQuery("#carrello a.close-box").click(function() {
			jQuery('#carrello-content').fadeOut('fast');
			jQuery('#carrello > .header').removeClass('header-on');
		    return false;
		});
	},


	// metodo per disattivare il pannello prodotto
	deactivatePannelloProdotto : function() {

		jQuery('#immagineprodotto').mouseover(function() {
	        if( jQuery('#pannello').hasClass('opened') ) {
	      	  jQuery('#pannello').fadeOut('slow');
	      	  jQuery('#pannello').removeClass("opened");
	      	}
			return false;
		});

	},

	// metodo per attivare il pannello prodotto
	activatePannelloProdotto : function() {

		jQuery('#apri-info-prodotto').mouseover(function() {
			jQuery('#pannello').fadeIn('slow');
	      	jQuery('#pannello').addClass("opened");
			return false;
		});

	},

	// metodo per attivare la newsletter

	activateBoxNewsletter : function() {

		jQuery('#open-newsletter').click(function() {
			if( jQuery('#newsletter > .header').hasClass('header-on') ) {
				jQuery('#newsletterform').fadeOut('fast');
				jQuery('#newsletter > .header').removeClass('header-on');
			}else{
				jQuery('#newsletterform').fadeIn('fast');
				jQuery('#newsletter > .header').addClass('header-on');
			}
			return false;
		});

		jQuery("#newsletterform a.close-box").click(function() {
			jQuery('#newsletterform').fadeOut('slow');
			return false;
		});

		jQuery("#nw-close").click(function() {

			jQuery('#n-close').show();
			jQuery('#n-warning').removeClass('here');
			jQuery('#nw-message').html();

			jQuery('#n-email').val( jQuery('#n-email').attr('default') );
			jQuery('#n-name').val( jQuery('#n-name').attr('default') );
			jQuery('#n-age').val( jQuery('#n-age').attr('default') );
			jQuery('#n-sesso').val('');
			jQuery('#selectn-sesso').html( jQuery('#n-sesso option:first').text() );
			jQuery('#n-cap').val( jQuery('#n-cap').attr('default') );

			return false;

		});


	},


	// metodo per attivare la wishlist
	activateBoxWishlist : function() {

		jQuery('#open-wishlist').click(function() {

			if( jQuery('#wishlist > .header').hasClass('header-on') ) {
				jQuery('#wishlistcontent').fadeOut('fast');
				jQuery('#wishlist > .header').removeClass('header-on');
			}else{
				jQuery('#wishlistcontent').fadeIn('fast');
				jQuery('#wishlist > .header').addClass('header-on');
			}
			return false;

		});

		jQuery("#wishlist a.close-box").click(function() {
			jQuery('#wishlistcontent').fadeOut('slow');
			return false;
		});
	},


	// metodo per attivare il box condividi
	activateBoxCondividi : function() {

		jQuery('#open-condividi').click(function() {

			if( jQuery('#condividi > .header').hasClass('header-on') ) {
				jQuery('#condividi > .shareMenu').fadeOut('fast');
				jQuery('#condividi > .header').removeClass('header-on');
			}else{
				jQuery('#condividi > .shareMenu').fadeIn('fast');
				jQuery('#condividi > .header').addClass('header-on');
			}
			return false;

		});

		jQuery("#condividi a.close-box").click(function() {
			jQuery('#condividi > .shareMenu').fadeOut('slow');
			return false;
		});

	},

	/*
	// metodo per attivare il box condividi
	activateBoxCondividiProduct : function() {

		jQuery('#open-condividi-product').click(function() {

	        if( jQuery('.condividiProduct').hasClass('opened') ) {

	        	  jQuery('.condividiProduct').fadeOut('slow');

	        	} else {
	        	jQuery('.condividiProduct').fadeIn('slow');

	        	//CARRELLO
	        	jQuery('#carrello-open').fadeOut('slow');
				//NEWSLETTER
	        	jQuery('#newsletter').fadeOut('slow');
				//WISHLIST
	        	jQuery('#wishlist').fadeOut('slow');
	        	}

			return false;

		});

		jQuery("a.chiudiShare").click(function() {

			jQuery('#condividi_product').fadeOut('slow');
			return false;

		});

	},
	*/


	// metodo per attivare il Looped Slider
	activateLoopedGallery : function() {

		jQuery('.container').each(function(i){

		   jQuery(this).loopedSlider({ pagination: '.pagination', containerClick: false, autoStart: 5000, fading: true });

		});

	}

};

