
// Plugin scroll to
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);


// Open jQuery document ready
// Open jQuery document ready
// Open jQuery document ready
jQuery(document).ready(function() {
// Open jQuery document ready
// Open jQuery document ready
// Open jQuery document ready


  // Implemento le Funzioni per le classi first last e la numerazione dei LI
  
  jQuery('ul li').each(function(item) {
    jQuery(this).addClass('item-' + item ); 
  });

  jQuery("li:first-child").addClass("first");
  jQuery("li:last-child").addClass("last");
  
  // Implemento le Funzioni per le classi first last per gli articoli
  
  jQuery("article:first-child").addClass("first");
  jQuery("article:last-child").addClass("last");
  
  // Implemento scroll nelle pagine
  jQuery('.home #slidecounter').click(function(){
	jQuery.scrollTo( "#content", 500);
  return false;
  });

  jQuery('.top').click(function(){
	jQuery.scrollTo( 0, 500);
  return false;
  });
  
  
  jQuery('.show_share').click(function(){
	jQuery(this).next(".addthis_toolbox").slideToggle(300);
  });
   
  jQuery('<div class="last_three"></div>').insertBefore('.template-wp-framework_taxonomy-php .sm article:eq(2), .page-template-press .sm article:eq(2), .page-template-blog .sm article:eq(2)');
  jQuery('.sm article:eq(2)').appendTo('.last_three');
  jQuery('.sm article:eq(3)').appendTo('.last_three');
  jQuery('.sm article:eq(4)').appendTo('.last_three');
  
  
  
    jQuery(".page-template-press .sm .post-3 .background").prependTo('.page-template-press .sm .post-3 .entry-summary');
	jQuery(".page-template-press .sm .post-3 .wrap-img").prependTo('.page-template-press .sm .post-3 .entry-summary');
    jQuery(".page-template-press .sm .post-4 .background").prependTo('.page-template-press .sm .post-4 .entry-summary');
	jQuery(".page-template-press .sm .post-4 .wrap-img").prependTo('.page-template-press .sm .post-4 .entry-summary');
    jQuery(".page-template-press .sm .post-5 .background").prependTo('.page-template-press .sm .post-5 .entry-summary');
	jQuery(".page-template-press .sm .post-5 .wrap-img").prependTo('.page-template-press .sm .post-5 .entry-summary');
  
    jQuery(".page-template-blog .sm .post-3 .background").prependTo('.page-template-blog .sm .post-3 .entry-summary');
	jQuery(".page-template-blog .sm .post-3 .wrap-img").prependTo('.page-template-blog .sm .post-3 .entry-summary');
    jQuery(".page-template-blog .sm .post-4 .background").prependTo('.page-template-blog .sm .post-4 .entry-summary');
	jQuery(".page-template-blog .sm .post-4 .wrap-img").prependTo('.page-template-blog .sm .post-4 .entry-summary');
    jQuery(".page-template-blog .sm .post-5 .background").prependTo('.page-template-blog .sm .post-5 .entry-summary');
	jQuery(".page-template-blog .sm .post-5 .wrap-img").prependTo('.page-template-blog .sm .post-5 .entry-summary');
	
	jQuery(".template-wp-framework_taxonomy-php .sm .post-3 .background").prependTo('.template-wp-framework_taxonomy-php .sm .post-3 .entry-summary');
	jQuery(".template-wp-framework_taxonomy-php .sm .post-3 .wrap-img").prependTo('.template-wp-framework_taxonomy-php .sm .post-3 .entry-summary');
    jQuery(".template-wp-framework_taxonomy-php .sm .post-4 .background").prependTo('.template-wp-framework_taxonomy-php .sm .post-4 .entry-summary');
	jQuery(".template-wp-framework_taxonomy-php .sm .post-4 .wrap-img").prependTo('.template-wp-framework_taxonomy-php .sm .post-4 .entry-summary');
    jQuery(".template-wp-framework_taxonomy-php .sm .post-5 .background").prependTo('.template-wp-framework_taxonomy-php .sm .post-5 .entry-summary');
	jQuery(".template-wp-framework_taxonomy-php .sm .post-5 .wrap-img").prependTo('.template-wp-framework_taxonomy-php .sm .post-5 .entry-summary');
  	
jQuery("#recaptcha_widget_div").prepend('<div class="clear">&nbsp;</div>');

  
  // Impelmento la funzione per allineare l'altezza del contenuto con la sidebar
  
  if ( jQuery('#content').height() < jQuery('#sidebar-right').height() ){
    jQuery('#content').height(jQuery('#sidebar-right').height());
  }
  
  // Implemento la funzione per la paginazione
  
  jQuery('.page-numbers.current,.page-numbers.dots').remove();
  
  jQuery('.page-numbers').each(function() {
    if(jQuery(this).hasClass('prev') == false){
	  if(jQuery(this).hasClass('next') == false){
	    jQuery(this).remove();
	  }
	}
  });
  
  var pattOld = /\?page=[0-9][0-9][0-9]|\?page=[0-9][0-9]|\?page=[0-9]/gi
  
  var patt=/[0-9][0-9][0-9]|[0-9][0-9]|[0-9]/gi;
  
  if(jQuery('.page-numbers.prev').length > 0) {
	
	var urlPrev= jQuery('.page-numbers.prev').attr('href');
  
    var urlPrevOld = urlPrev.match(pattOld);
	
	var urlPrevClear = urlPrev.replace( urlPrevOld , "");
  
    var pagePrev = urlPrevClear.match(patt);
  
    var newUrlPrev = urlPrevClear.replace("/page/" + pagePrev , "?page=" + pagePrev);
  
    jQuery('.page-numbers.prev').attr('href', newUrlPrev);
  
  }
	
  if(jQuery('.page-numbers.next').length > 0){
	
	var urlNext= jQuery('.page-numbers.next').attr('href');
  
    var urlNextOld = urlNext.match(pattOld);
	
	var urlNextClear = urlNext.replace( urlNextOld , "");
  
    var pageNext = urlNextClear.match(patt);
  
    var newUrlNext = urlNextClear.replace("/page/" + pageNext , "?page=" + pageNext);
  
    jQuery('.page-numbers.next').attr('href', newUrlNext);
  
  }
  
  // Implemento la funzione per il replace del campo newsletter
  
  var valInput = jQuery('#oxikk-oxikk').val();
  
  jQuery('#oxikk-oxikk').click(function() {
        if (jQuery(this).val() == valInput )
        {
            jQuery(this).val('');
        }
    });
    
    jQuery('#oxikk-oxikk').blur(function(){
        if (jQuery(this).val() == '' )
        {
            jQuery(this).val( valInput );
        }    
    });
	
  jQuery('#widget_sp_image-3').append('<div class="link" />');
  jQuery('#widget_sp_image-3 .link').load('http://www.magnoliaristorante.it/ristorante/la-cantina .link');
  jQuery(window).load(function() {
    jQuery('#widget_sp_image-3 > a').attr('href', jQuery('.link a').attr('href'));
	jQuery('#widget_sp_image-3 .link').remove();
  });
  
  // Implemento la funzione per visualizzare il pop-up della newsletter-------------------------------------------------------------------------------------------------------------------------------
	
	jQuery('#widget_sp_image-6 a').addClass('pop-up-contatti');
	jQuery('#widget_sp_image-7 a').addClass('pop-up-newsletter');
	jQuery('#widget_sp_image-5 a').addClass('pop-up-newsletter');
	
	jQuery('#pop-up-background').click(function() {
		jQuery('#pop-up-background').css({ 'display' : 'none', 'z-index' : '-99999' });
		jQuery('#pop-up').css({ 'display' : 'none', 'z-index' : '-99999' });
		jQuery('#news-form').css('display', 'none');
		jQuery('#contact-form').css('display', 'none');
	});
	
	jQuery('a.pop-up-newsletter, a.pop-up-contatti').attr('href', 'javascript:void(0)');
	
	var windowHeight = jQuery(window).height();
	var windowWidth = jQuery(window).width();
	
	jQuery('a.pop-up-newsletter').click(function() {
		jQuery('#pop-up-background').css({ 'top' : (windowHeight / 2) - 100, 'left' : (windowWidth / 2) - 100, 'position' : 'fixed', 'z-index' : '99999999', 'width' : '100px', 'height' : '100px' });
		jQuery('#pop-up-background').fadeIn(400).delay(200).animate({ width: windowWidth, left: 0  }, 1000 )
											   .animate({ height: windowHeight, top:0 }, 1000, function() {
													jQuery('#pop-up').delay(400).fadeIn(800);
												} );

		jQuery('#pop-up').css({ 'z-index' : '999999999'});
		jQuery('#news-form').css('display', 'block');
	});
	
	jQuery('a.pop-up-contatti').click(function() {
		jQuery('#pop-up-background').css({ 'top' : (windowHeight / 2) - 100, 'left' : (windowWidth / 2) - 100, 'position' : 'fixed', 'z-index' : '99999999', 'width' : '100px', 'height' : '100px' });
		jQuery('#pop-up-background').fadeIn(400).delay(200).animate({ width: windowWidth, left: 0  }, 1000 )
											   .animate({ height: windowHeight, top:0 }, 1000, function() {
													jQuery('#pop-up').delay(400).fadeIn(800);
												} );

		jQuery('#pop-up').css({ 'z-index' : '999999999'});
		jQuery('#contact-form').css('display', 'block');
	});
	
	
	jQuery(window).resize(function() {
		jQuery('#pop-up-background').css({ 'width' : jQuery(window).width(), 'height' : jQuery(window).height() });
	});
	jQuery('.close_button').click(function() {
		jQuery('#pop-up-background').css({ 'display' : 'none', 'z-index' : '-99999' });
		jQuery('#done').css({ 'display' : 'none' });
		jQuery('#pop-up').fadeOut('slow');
		jQuery('#pop-up').css({ 'display' : 'none', 'z-index' : '-99999' });
		jQuery('#news-form').css('display', 'none');
		jQuery('#contact-form').css('display', 'none');
	});
	
  // Implemento le funzioni per i link dei banner carta dei vini e menu-------------------------------------------------------------------------------------------------------------------------------


	if(window.location.pathname == "/ristorante/cucina"){
	
		jQuery('#widget_sp_image-4 a').attr('href', jQuery('.link.pdf').attr('href'));
	
	}
	
	if(window.location.pathname == "/ristorante/la-cantina" ){
	
		jQuery('#widget_sp_image-3 a').attr('href', jQuery('.link.pdf').attr('href'));
	
	}


// Close jQuery document ready
// Close jQuery document ready
// Close jQuery document ready
});
// Close jQuery document ready
// Close jQuery document ready
// Close jQuery document ready

jQuery(document).ready(function() {
	
	
	/*
	 * Implemento il codice per il form newsletter
	 */
	 
	 //disabled all the text fields
	jQuery('#submit-news').attr('disabled','true');
	jQuery('#submit-news').addClass('disable');
	
	// Implemento la funzione per il replace del campo newsletter
	
	jQuery('#news-form .input').each(function() {
			var valInput = jQuery(this).val();
			jQuery(this).addClass('null');
			
			jQuery(this).click(function() {
				if (jQuery(this).val() == valInput )
				{
					jQuery(this).val('');
				}
			});
			
			jQuery(this).blur(function(){
			
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( valInput );
					jQuery(this).addClass('null');
				}
				
				if( (jQuery(this).val() != '') && (jQuery(this).val() != valInput) ){
					jQuery(this).removeClass('null');
				}
				
				if (!jQuery('#news-form .email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)){
					jQuery('#news-form .email').addClass('null');
				}
				
			});
			
			jQuery(this).keyup(function(){
			
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( valInput );
					jQuery(this).addClass('null');
				}
				
				if( (jQuery(this).val() != '') && (jQuery(this).val() != valInput) ){
					jQuery(this).removeClass('null');
				}
				
				if (!jQuery('#news-form .email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)){
					jQuery('#news-form .email').addClass('null');
				}
				
			});
	});
	
	
	function checkValid2(){
	
		var validation = true;
				
		jQuery('#news-form .input').each(function() {
				
			if(jQuery(this).hasClass('null')){
					  
				validation = false;
					  
			}
				
		});
		
		if(validation == true){
			jQuery('#submit-news').removeAttr('disabled');
			jQuery('#submit-news').removeClass('disable');
		} 
		if(validation == false){
			jQuery('#submit-news').attr('disabled','true');
			jQuery('#submit-news').addClass('disable');
		}
	
	}
	
	
	jQuery('#news-form .input').keyup(function() {
		
		checkValid2();
		
	});
	
	/*
	 * Implemento il codice per il form contatti
	 */
	
	//disabled all the text fields
	jQuery('#submit').attr('disabled','true');
	jQuery('#submit').addClass('disable');
	
	// Implemento la funzione per il replace del campo newsletter
	
	jQuery('#contact-form .input, #contact-form .textarea').each(function() {
			var valInput = jQuery(this).val();
			var text = jQuery(this).text();
			jQuery(this).addClass('null');
			
			jQuery(this).click(function() {
				if (jQuery(this).val() == valInput )
				{
					jQuery(this).val('');
				}
				if (jQuery(this).val() == text )
				{
					jQuery(this).val('');
				}
			});
			
			jQuery(this).blur(function(){
			
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( valInput );
					jQuery(this).addClass('null');
				}
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( text );
					jQuery(this).addClass('null');
				}
				
				if( (jQuery(this).val() != '' || jQuery(this).text() != '') && (jQuery(this).val() != valInput || jQuery(this).text() != text) ){
					jQuery(this).removeClass('null');
				}
				
				if (!jQuery('#contact-form .email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)){
					jQuery('#contact-form .email').addClass('null');
				}
				
			});
			
			jQuery(this).keyup(function(){
			
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( valInput );
					jQuery(this).addClass('null');
				}
				if (jQuery(this).val() == '' )
				{
					jQuery(this).val( text );
					jQuery(this).addClass('null');
				}
				
				if( (jQuery(this).val() != '' || jQuery(this).text() != '') && (jQuery(this).val() != valInput || jQuery(this).text() != text) ){
					jQuery(this).removeClass('null');
				}
				
				if (!jQuery('#contact-form .email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)){
					jQuery('#contact-form .email').addClass('null');
				}
				
			});
	});
	
	
	function checkValid(){
	
		var validation = true;
				
		jQuery('#contact-form .input,#contact-form  .textarea').each(function() {
				
			if(jQuery(this).hasClass('null')){
					  
				validation = false;
					  
			}
				
		});
		
		if(validation == true){
			jQuery('#contact-form #submit').removeAttr('disabled');
			jQuery('#contact-form #submit').removeClass('disable');
		} 
		if(validation == false){
			jQuery('#contact-form #submit').attr('disabled','true');
			jQuery('#contact-form #submit').addClass('disable');
		}
	
	}
	
	
	jQuery('#contact-form .input, #contact-form .textarea').keyup(function() {
		
		checkValid();
		
	});
	
	
	//if submit button is clicked
	
	jQuery('#contact-form #submit').click(function () {		
		
		//Get the data from all the fields
		var nome = jQuery('input[name=nome]');
		var cognome = jQuery('input[name=cognome]');
		var email = jQuery('input[name=e-mail]');
		var message = jQuery('textarea[name=messaggio]');
		
		//organize the data properly
		var data = 'nome=' + nome.val() + '&cognome=' + cognome.val() + '&email='
		+ email.val() + '&message='  + encodeURIComponent(message.val());
		
		//show the loading sign
		jQuery('.loading').show();
		
		//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: "http://www.magnoliaristorante.it/wp-content/plugins/smarticomunicazione/ajaxform/ajaxform.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html=="1") {					
					//hide the form
					jQuery('#contact-form').fadeOut('fast');					
					
					//show the success message
					jQuery('#done').fadeIn('fast');
					
					jQuery('#contact-form input').each(function() {
						switch(jQuery(this).attr('name')){
						
							case 'nome' :
							  jQuery(this).val('Nome*');
							break;
							case 'cognome' :
							  jQuery(this).val('Cognome*');
							break;
							case 'e-mail' :
							  jQuery(this).val('E-mail*');
							break;
						
						}
					});
					
					jQuery('#contact-form textarea').val('Messaggio*');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Siamo spiacenti ma non &egrave; stato possibile inviare i dati, Vi preghiamo di riprovare');				
			}		
		});
		
		//show the loading sign
		jQuery('.loading').hide();
		
		//cancel the submit button default behaviours
		return false;
	});	
});	

