/********************************************/
/*	core.js for jquery
/*	Author: davide reppucci | nascar
/*  Author URI: http://www.nascar.it
/*
/*	PLUGIN LOADED:
/*	Superfish
/*	Autogrow
/*	Loopedslider
/*	checkForm
/*	Editable
/*	Autogrow
/*
/*  latest update: 15/03/2010
/*******************************************/

var core = {

	bootstrap : function( _labels ) {

		//start fancybox
		if ( jQuery('.fancybox').length > 0 ) { core.activateFancyBox(); }

		// slideviewerpro
		if ( jQuery('#slideshow').length > 0 ) { core.activateSlideshow(); }
		
		// borderFX
		//if ( jQuery('.border-fx').length > 0 ) { core.activateBorderFX(); }
	
		// nav menu
		if ( jQuery('#nav').length > 0 ) { core.activateDropDown(); }
	
		// cart
		if ( jQuery('#cart').length > 0 ) { core.activateCartSummary(); }
		
		// se esiste #slider-product
		if ( !jQuery('#videoplayer') || jQuery('#videoplayer').html() == null ) {
			if ( jQuery('#slider-product').length > 0 ) { core.activateProductHomeGallery(); }
		}
		
		// se ci sono classi .container
		if ( jQuery('#body .container').length > 0 ) { core.activateLoopedSlider(); }
		
		if(jQuery('ul.categorie select.style').length > 0 ) { core.activateUniform(); }
		
		// se ci sono textarea con classe expanding
		if ( jQuery('textarea.expanding').length > 0 ) { core.expandTextarea(); }
		
		// se esistono classi .ui-tabs
		if ( jQuery('.ui-tabs').length > 0 ) { core.activateTabs(); }
		
		// se esistono classi .editable
		if ( jQuery('.editable').length > 0 ) {

			jQuery('.editable').each(function(i){
			
				jQuery(this).editable("post.php", { saving:"<img src='images/main-450-editable-loading.gif'>", delayOnBlur: 1 });
				
			});
		
		}
		
		// se esistono classi .infoAlert
		if ( jQuery('.infoAlert').length > 0 ) { core.activateInfoTooltip(); }
		
		// se esistono classi .infoAlertReg
		if ( jQuery('.infoAlertReg').length > 0 ) { core.activateInfoTooltip2(); }
		
		// se esistono classi .pager
		if ( jQuery('.pager').length > 0 ) {

			jQuery('.pager').each(function(i){
			
				jQuery(this).css('width', 145 + ( jQuery(this).find('.p-link').length * 7) + ( (jQuery(this).find('.p-link').length - 1) * 8) );
				
			});
		
		}
		
		// se esiste #bpcb-friend a
		if ( jQuery('#bpcb-friend a').length > 0 ) {

			jQuery('#bpcb-friend a').click(function() {
			
				jQuery('#bpcbf-send').fadeIn();
				return false;
			
			});
		
		}
		
		// se esiste #bpcbfs-close a
		if ( jQuery('#bpcbfs-close a').length > 0 ) {

			jQuery('#bpcbfs-close a').click(function() {
			
				jQuery('#bpcbf-send').fadeOut();
				return false;
			
			});
		
		}
		
		// se esiste #bpcb-add a
		if ( jQuery('#bpcb-add a').length > 0 ) {

			jQuery('#bpcb-add a').click(function() {
			
				jQuery('#bpcba-share').fadeIn();
				return false;
			
			});
		
		}
		
		// se esiste #bpcbas-close a
		if ( jQuery('#bpcbas-close a').length > 0 ) {

			jQuery('#bpcbas-close a').click(function() {
			
				jQuery('#bpcba-share').fadeOut();
				return false;
			
			});
		
		}
						
		// 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 attivare il megadropdown
	activateDropDown : function() {
		
		/*
		 * DANIELE MURRONI: ho riattivato il click sulla categoria, nel menu.
		 
			jQuery('#menu li a').click( function() {
				
				//if( jQuery(this).attr('rel') != '') { return false; }
				
			} );
		*/
		
		jQuery('#menu li a').hover( function() {
			
			if( jQuery(this).attr('rel') != '') {
				
				jQuery('#nav').find('#'+ jQuery(this).attr('rel') ).css('z-index', 998);
				//jQuery('#nav').find('#'+ jQuery(this).attr('rel') ).fadeIn(300);
				jQuery('#nav').find('#'+ jQuery(this).attr('rel') ).show();
				jQuery('#nav').find('#'+ jQuery(this).attr('rel') ).stopTime();
				jQuery(this).addClass('hover');
				return false;
			
			}
			
		}, function () {
			
			if( jQuery(this).attr('rel') != '') {
			
				jQuery('#nav').find('#'+ jQuery(this).attr('rel') ).oneTime( 1, 'hideDrop', function() {
					
					jQuery(this).css('z-index', 995);
					//jQuery(this).fadeOut(300);
					jQuery(this).hide();
					jQuery('#menu a[rel='+jQuery(this).attr('id')+']').removeClass('hover');
				
				} );
				
				return false;
			
			}
			
		} );
		
		jQuery('#nav div.dropdown').hover( function() {
			
			jQuery(this).stopTime();
			return false;
			
		}, function () {

			jQuery(this).oneTime( 200, 'hideMyself', function() {
				
				jQuery(this).css('z-index', 995);
				jQuery(this).fadeOut(300);
				jQuery('#menu a[rel='+jQuery(this).attr('id')+']').removeClass('hover');
			
			} );
			
			return false;
			
		} );
	
	},
	
	/** FANCYBOX *************************************************************/
		activateUniform : function() {
		
			jQuery('ul.categorie select.style').uniform();
		
		},
	
		
		activateFancyBox : function() {
		
			jQuery('.fancybox').fancybox();
		    	
		}, // end .activateFancyBox
	
	// metodo per attivare il riepilogo carrello
	activateCartSummary : function() {
	
		jQuery('#cart #cs-carrello').click( function() {
		
			if( !jQuery(this).hasClass('opened') ) {
			
				jQuery('#cart #cart-list').fadeIn(300);
				jQuery(this).addClass('opened');
			
			}
		
		} );
		
		jQuery('#cl-carrello').click( function() {
			
			jQuery('#cart #cart-list').fadeOut(300);
			jQuery('#cart #cs-carrello').removeClass('opened');
		
		} );
		
		jQuery('#cl-switch a').click( function() {

			jQuery('#cart #cart-list').fadeOut(300);
			jQuery('#cart #cs-carrello').removeClass('opened');
			jQuery('#cart #wish-list').fadeIn(300);
			jQuery('#cart #cs-wish').addClass('opened');
		
		} );		
		
		jQuery('#cart #cs-wish').click( function() {
		
			if( !jQuery(this).hasClass('opened') ) {
			
				jQuery('#cart #wish-list').fadeIn(300);
				jQuery(this).addClass('opened');
			
			}
		
		} );
		
		jQuery('#wl-carrello').click( function() {
			
			jQuery('#cart #wish-list').fadeOut(300);
			jQuery('#cart #cs-wish').removeClass('opened');
		
		} );
		
		jQuery('#wl-switch a').click( function() {

			jQuery('#cart #wish-list').fadeOut(300);
			jQuery('#cart #cs-wish').removeClass('opened');
			jQuery('#cart #cart-list').fadeIn(300);
			jQuery('#cart #cs-carrello').addClass('opened');
		
		} );
	
	},
	
	// metodo per attivare il Looped Slider
	activateProductHomeGallery : function() {

		jQuery('#slider-product .container').loopedSlider({ pagination: '.pagination', containerClick: false, autoStart: 10000, restart: 10000, fading: true });
	
	},
	
	// metodo per attivare il Looped Slider
	activateLoopedSlider : function() {

		jQuery('#body .container').loopedSlider({ pagination: '.pagination', containerClick: false, autoStart: false, restart: 10000 });
		
		jQuery('#body .container').each( function() {
			
			jQuery(this).find('.pagination').css('width', ( jQuery(this).find('.pagination li').length * 15)+50 );
			jQuery(this).find('.pagination').css('left', 110-(((jQuery(this).find('.pagination li').length * 19)+30)/2) );
			
		} );
	
	},
	
	// 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 i tabs
	activateTabs : function() {

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

			jQuery(this).tabs({ remote: true, fx: { opacity: 'toggle', duration: 200 } });
		   		      
		});	
	
	},
	
	// metodo per attivare i tooltip info
	activateInfoTooltip : function() {
		
		jQuery(".infoAlert").each(function(i){
		   
		   jQuery(this).tooltip({
			    delay: 0,
			    track: true,
			    fade: 250,
			    top: -40, 
				left: 5,
				showBody: " - "
			});
		   		      
		});	
	
	},
	
	// metodo per attivare i tooltip info reg
	activateInfoTooltip2 : function() {
		
		jQuery(".infoAlertReg").each(function(i){
		   
		   jQuery(this).tooltip({
			    delay: 0,
			    track: false,
			    fade: 250,
			    top: -40, 
				left: 48,
				showBody: " - "
			});
		   		      
		});
	
	},
		
	// metodo per attivare il borderFX
	activateBorderFX : function() {
		     
	   jQuery(".border-fx").insetBorder({
		  speed          : 300,           // 1000 = 1 second, default 250
		  borderColor    : "#333",    // Default white (#ffffff)
		  inset          : 5,             // Border width, default 10px
		  borderType     : "solid",        // Border type, e.g. dashed
		  outerClass     : "ibe_outer",    // Class name of outer wrap
		  innerClass     : "ibe_inner"     // Class name of inner border
		});
	
	},
	
	// metodo per attivare slideviewerpro
	activateSlideshow : function() {
		   
	   jQuery('#slideshow').before('<ul id="nav-focus">').cycle({
			slideExpr: 'a img', 
		    fx:     'fade', 
		    speed:  500, 
		    timeout: 5000,
		    prev:    '#prev-thumb',
        	next:    '#next-thumb', 
		    pager:  '#nav-focus',
		    pauseOnPagerHover: 0, 
		     
		    //callback fn that creates a thumbnail to use as pager anchor 
		    pagerAnchorBuilder: function(idx, slide) { 
		        return '<li><a href="#"><img src="' + slide.src + '" /></a></li>'; 
		    } 
		});
		
		// se presente una sola immagine non visualizzare frecce
		if ( jQuery('#slideshow img').length < 2 ) { jQuery('#prev-thumb,#next-thumb').remove(); }
		
		// conteggio thumbs
		var itemsNav = (jQuery('#nav-focus li').length)-1;
		
		// centro la navigazione dello slideshow
		jQuery('#nav-focus').css({
		
		"width" : ((jQuery('#nav-focus li:last').outerWidth(true))*itemsNav)+(jQuery('#nav-focus li.activeSlide').outerWidth(true)),
		"left"  : (((jQuery('#slideshow').width())/2)-((jQuery('#nav-focus').outerWidth(true))/2))
		
		});
		
		// posizioni le frecce rispetto alla navigazione
		jQuery('#prev-thumb').css({
		
		"left"  : (((jQuery('#slideshow').width())/2)-((jQuery('#nav-focus').width())/2)) - (jQuery('#prev-thumb').width())
		
		});
		
		jQuery('#next-thumb').css({
		
		"right"  : (((jQuery('#slideshow').width())/2)-((jQuery('#nav-focus').width())/2)) - (jQuery('#next-thumb').width())
		
		}); 
	
	},

	/** FANCYBOX *************************************************************/
		
	activateFancyBox : function() {
	
		jQuery('.fancybox').fancybox();
	    	
	} // end .activateFancyBox
	
	

};

