/********************************************/
/*	core.js for jquery
/*	Author: davide reppucci | kiui
/*  Author URI: http://www.kiui.it
/*	Version 0.2
/*
/*  PLUGIN LOADED
/*	Autogrow
/*	Looped Slider
/*  checkForm
/*	externalLink
/*
/*  latest update: 03/06/2010
/*******************************************/

var core = {
	
	bootstrap : function( _labels, _firstMap ) {
		
		// initializzo il checkForm
		Forms.init(_labels);
		
		// inizializzo il custom form elements
		//Custom.init();
		
		// esternalizzo i link con class external
		var externals = new externalLink();
		
		// se ci sono classi .fadeThis
		if ( jQuery('.fadeThis').length > 0 ) { jQuery('header nav').addClass('noJs'); core.addFadeThis(); }
		
		// se ci sono label inlined
		if ( jQuery('label.inlined + input.input-text').length > 0 ) { core.labelSwap(); }
		
		// se ci sono classi accordion
		if ( jQuery('.accordion').length > 0 ) { core.activateAccordion(); }
		
		// se esiste .ordine
		if ( jQuery('.ordine').length > 0 ) { core.orderDir(); }
		
		// se c'e il portfolio in home
		if ( jQuery('#slider').length > 0 ) { core.activateCreativityGallery(); }
		
		// se c'e lo slider liste home
		if ( jQuery('#lists-slider').length > 0 ) { core.activateListsGallery(); }
		
		// se ci sono classi .data
		if ( jQuery('.data').length > 0 ) { core.chooseBirthday(); }
		
		jQuery('.shadowbox-link').live('submit', function() {
			url = jQuery(this).attr('href');
			jQuery.ajax({ 
				url: url, 
				success: function(data){
					core.showBox(data);
				}
			});
			return false;
		});
		
		// se esiste .infos
		if ( jQuery('.infos').length > 0 ) { core.showInfoTooltip(); }
		
		// se esiste #map		
		if ( jQuery('#map').length > 0 ) {
		
			showmaps();
			showAddress(_firstMap.search, _firstMap.firstBoolean, _firstMap.name, _firstMap.appear, _firstMap.secondBoolean);
			
		}
		
		// se esistono classi .scroll-pane
		if ( jQuery('.scroll-pane').length > 0 ) { core.activateScroller(); }
		
		// invita amici altri
		jQuery('.fi-other').live('click', function() {
			//alert(jQuery(this).attr('href'));
			clickSource = jQuery(this);
			jQuery(clickSource).removeClass('.fi-other')
			jQuery(clickSource).addClass('loading');
			jQuery.ajax({
				url: jQuery(this).attr('href'),
				context: document.body,
				success: function(data) {
					jQuery(clickSource).parent().remove();
					panel = jQuery('#'+ jQuery(clickSource).attr('content') );
        			jQuery(panel).append(data);
        			jQuery(panel).css('height', 424);
        			jQuery(panel).jScrollPane({ scrollbarWidth: 4 });
        			//jQuery(panel)[0].scrollTo( jQuery(panel).data('jScrollPaneMaxScroll') );
				}
			});
			return false;
		});
		// invita amici altri
		jQuery('.fi-all').live('click', function() {
			//alert(jQuery(this).attr('href'));
			clickSource = jQuery(this);
			jQuery(clickSource).addClass('loading');
			jQuery.ajax({
				url: jQuery(this).attr('href'),
				context: document.body,
				success: function(data) {
					jQuery(clickSource).parent().remove();
					panel = jQuery('#'+ jQuery(clickSource).attr('content') );
        			jQuery(panel).html(data);
        			jQuery(panel).css('height', 424);
        			jQuery(panel).jScrollPane({ scrollbarWidth: 4 });
        			//jQuery(panel)[0].scrollTo( jQuery(panel).data('jScrollPaneMaxScroll') );
				}
			});
			return false;
		});
		
		jQuery('.friend-item').find('h3 a').live('click', function() {
		
			// se l'input &#65533; selezionato
			if( jQuery(this).parent().parent().find('input').attr('checked') == true ) {
				jQuery(this).parent().parent().removeClass('active');
				jQuery(this).parent().parent().find('input').attr('checked', false);
			}
			else {
				jQuery(this).parent().parent().addClass('active');
				jQuery(this).parent().parent().find('input').attr('checked', true);
			}
			
			return false;
		});
		jQuery.expr[':'].icontains = function(obj, index, meta, stack){
		return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
		};
		
		jQuery('#amici-cerca-fb').keyup( function() {
			inputSource = jQuery(this);
			if(jQuery('#fi-other-fb').hasClass('fi-other')){
				//jQuery('#fi-other-fb').trigger(e);
				url = jQuery('#fi-other-fb').attr('href');
				target = jQuery('#fi-other-fb').attr('content');
				jQuery('#fi-other-fb').removeClass('fi-other');
				jQuery('#fi-other-fb').addClass('loading');
				jQuery('#fi-other-fb').html('<p>Loading...</p>');
				jQuery.ajax({
					url: url,
					context: document.body,
					success: function(data) {
						panel = jQuery('#'+ target);
	        			jQuery(panel).append(data);
	        			jQuery(panel).css('height', 424);
	        			jQuery(panel).jScrollPane({ scrollbarWidth: 4 });
	        			//jQuery(panel)[0].scrollTo( jQuery(panel).data('jScrollPaneMaxScroll') );
	        			jQuery('#amici-cerca-fb').parent().parent().find('.cerca-fb h3').each( function() {
							if( !jQuery(this).is(':icontains("'+jQuery(inputSource).val()+'")') ) {
								jQuery(this).parent().hide();
							} else {
								jQuery(this).parent().show();
							}
						});
						jQuery('#content-fb')[0].scrollTo(0);
						jQuery('#fi-other-fb').parent().remove();
						}
				});
				return false;
			}else{
				jQuery(this).parent().parent().find('.cerca-fb h3').each( function() {
					if( !jQuery(this).is(':icontains("'+jQuery(inputSource).val()+'")') ) {
						jQuery(this).parent().hide();
					} else {
						jQuery(this).parent().show();
					}
				});
				jQuery('#content-fb')[0].scrollTo(0);
				}
		});
		
		jQuery('#amici-cerca-tvr').keyup( function() {
			inputSource = jQuery(this);
			if(jQuery('#fi-other-tvr').hasClass('fi-other')){
				//jQuery('#fi-other-fb').trigger(e);
				url = jQuery('#fi-other-tvr').attr('href');
				target = jQuery('#fi-other-tvr').attr('content');
				jQuery('#fi-other-tvr').removeClass('fi-other');
				jQuery('#fi-other-tvr').addClass('loading');
				jQuery('#fi-other-tvr').html('<p>Loading...</p>');
				jQuery.ajax({
					url: url,
					context: document.body,
					success: function(data) {
						panel = jQuery('#'+ target);
	        			jQuery(panel).append(data);
	        			jQuery(panel).css('height', 424);
	        			jQuery(panel).jScrollPane({ scrollbarWidth: 4 });
	        			//jQuery(panel)[0].scrollTo( jQuery(panel).data('jScrollPaneMaxScroll') );
	        			jQuery('#amici-cerca-tvr').parent().parent().find('.cerca-tvr h3').each( function() {
							if( !jQuery(this).is(':icontains("'+jQuery(inputSource).val()+'")') ) {
								jQuery(this).parent().hide();
							} else {
								jQuery(this).parent().show();
							}
						});
						jQuery('#content-tvr')[0].scrollTo(0);
						jQuery('#fi-other-tvr').parent().remove();
						}
				});
				return false;
			}else{
				jQuery(this).parent().parent().find('.cerca-tvr h3').each( function() {
					if( !jQuery(this).is(':icontains("'+jQuery(inputSource).val()+'")') ) {
						jQuery(this).parent().hide();
					} else {
						jQuery(this).parent().show();
					}
				});
				jQuery('#content-tvr')[0].scrollTo(0);
				}
		});
		
		jQuery('#box-search').find('form').submit( function() {

			//if( jQuery(this).find('input.valueSwap').attr('value') == jQuery(this).find('input.valueSwap').prev().html() ) { jQuery(this).find('input.valueSwap').val(''); }
		});
		
		/* niko edit */
		jQuery('.pubblica_lista').live('click', function() {
			if( !confirm("Sei sicuro di voler pubblicare la lista?\r\nSi ricorda che una volta pubblicata, la lista non potrà essere più modificata")){
				return false;
			}
		});
	},
	
	addFadeThis : function() {
		
		jQuery(".fadeThis").each(function(i){

		   jQuery(this).append('<span class="hover"></span>').each(function () {
				var spanObject = jQuery('> span.hover', this).css({
					'opacity': 0,
					'display': 'none', // ie6 hack
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'z-index': 1
				});
				jQuery(this).hover(function () {
					jQuery(spanObject).stop().css('display', 'block'); // ie6 hack
					jQuery(spanObject).stop().fadeTo(500, 1);
				}, function () {
					jQuery(spanObject).stop().fadeTo(500, 0, function() {
						jQuery(this).css('display', 'none'); // ie6 hack				
					});
				});
			});
		      
		});	
	
	},
	
	labelSwap : function() {
	
		jQuery("label.inlined + input.input-text").each(function (type) {

			jQuery(this).focus(function () { jQuery(this).prev("label.inlined").addClass("focus"); });
	
			jQuery(this).keyup(function () { jQuery(this).prev("label.inlined").addClass("has-text").removeClass("focus"); });
	
			jQuery(this).blur(function () {
				if(jQuery(this).val() == "") {
					jQuery(this).prev("label.inlined").removeClass("has-text").removeClass("focus");
				}
			});
		});
	
	},
	
	// metodo per attivare l'accordion
	activateAccordion : function() {
		
		jQuery(".accordion").each(function(i){
		
			jQuery('.accordion h2').click(function(e) {
				// exclusive accordion: close all .selected
				jQuery(this).parent().parent().find('h2.ui-state-active').toggleClass('ui-state-active').next().slideUp(400);
				
				// toggle expand/collapse panel
				jQuery(this).toggleClass('ui-state-active').next().slideDown(400);
				
				return false;
			}).next().hide(100);
			jQuery(this).find('h2:first').addClass('ui-state-active').next().slideDown(400);
		   		      
		});	
	
	},
	
	// metodo per attivare il Looped Slider
	activateCreativityGallery : function() {

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

		jQuery('#lists-slider .container').loopedSlider({ pagination: '.pagination', containerClick: false, autoStart: 10000, restart: 10000, fading: false });
		
		jQuery('#lists-slider .pagination').css('width', ( jQuery('#lists-slider .pagination li').length * 21 ) + 29 );
		jQuery('#lists-slider .pagination').css('margin-left', -(jQuery('#lists-slider .pagination').width() / 2) );
	
	},
	// attiva il calendario
	chooseBirthday : function() {
		try {
			today = new Date();
			year = today.getFullYear() + 5;
		} catch (e){
			year = 2015;
		}
		
		jQuery('.data').each( function(i) {
		
			jQuery(this).datepicker( {
				dateFormat: 'dd-mm-yy',
				changeMonth: true,
				changeYear: true,
				dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'],
				monthNamesShort: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
				constrainInput: true,
				firstDay: 1,
				showAnim: 'slideDown',
				yearRange: '1900:'+year
			} );
			
		} );
		
	},
	
	// shadowbox per invitare amici
	inviteFriends : function() {
		
		jQuery('#invite-friends').live('submit', function() {
			Shadowbox.open({
		        content:    'amici-messaggio.php',
		        player:     "iframe",
		        title:      "Invita i tuoi amici alla lista",
		        height:     464,
		        width:      410
		    });
			return false;
			
		} );
		
	},
	
	// shadowbox per invitare amici
	openPaypal : function() {
		
		jQuery('#crea01').live('submit', function() {
			Shadowbox.open({
		        content:    'paypal.php',
		        player:     "html",
		        title:      "Paga in tutta sicurezza con Paypal",
		        height:     464,
		        width:      410
		    });
			return false;
			
		} );
		
	},
	
	// tooltip
	showInfoTooltip : function() {
		
		jQuery('.infos').each( function(i) {
		
			jQuery(this).tooltip({
			    delay: 0,
			    track: true,
			    fade: 250,
			    top: 15, 
				left: 0
			});
			
		} );
		
	},
	
	showBox : function(html) {
		Shadowbox.open({ content: html, player: "html", height: 464, width: 410 });
		return false;
	},
	
	// metodo per attivare gli scroller
	activateScroller : function() {
		
		jQuery(".scroll-pane").each(function(){
		   
		   jQuery(this).jScrollPane({ scrollbarWidth: 4 });
		   		      
		});	
	
	},
	
	// ajax per ordinare elenco liste
	orderDir : function() {
		
		jQuery('.ordine').change( function(i) {
		
			jQuery(this).closest("form").submit();
			
		} );
		
	}
			
};
