$(document).ready(function() {

	// Gestione target blank links con doctype strict
	// This is how it works:
	// <a href="http://www.tripwiremagazine.com/" rel="external">tripwiremagazine.com</a>
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});

	// gestione IMG HOVER
	$(".mHover").each(function() {
		var src = $(this).attr('src');
		var extension = src.substring(src.lastIndexOf('.'), src.length);
		$(this).mouseover(function() {
			$(this).attr('src', src.replace(extension,'-on' + extension));
		}).mouseout(function() {
			$(this).attr('src', src);
		});
	});
	
	checkingContent();
	
	startHPAnimation();
	
	scopriSpritz_bubble.init();
	fancyBox();
	//formDove();
	
	fixLangLink();
	
	
	//loginAreaRiservata();
	
	
});

var fixLangLink = function(){
	$('.menu.classMenu > .level1:last-child').addClass('menu-lang-link '+ $('.menu.classMenu > .level1:last-child > a').html());
	$('.menu.classMenu > .level1:last-child > a').html('');
};

var fancyBox = function(){
	
	$('.immagini.imagelist').fancybox({
		'titlePosition':'inside'
		
	});
	
	$('.fancyPopup').fancybox();
	
	$('.immagini.videolist').click(function () {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		    : 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {'wmode' : 'transparent', 'allowfullscreen' : 'true'}
		});

		return false;
	});
	
};


var formDove = function(){
	
	if ($('#tipo_mercato').length > 0) {
	
		if ($('#tipo_mercato').val().toLowerCase() == 'italia') {
			$('#stato').val('Italia');
			$('#stato').attr('disabled','disabled');
		}
		
		$('#tipo_mercato').change( function(){
			
			if ( $(this).val().toLowerCase() == 'italia' || $(this).val() == '' ){
				
				$('#stato').val('Italia');
				$('#stato').attr('disabled','disabled');
				
			} else {
				
				$('#stato').attr('disabled','');
			}
			
		});
		
		$('#doveTroviSpritz').find('#submit').click( function(){
			$('#stato').attr('disabled','');		
		});
	
	}
};

var checkingContent = function (){
	var contenuto = $('.content .textarea').html();
	if ( contenuto ) {
		//contenuto = contenuto.replace(/ispritz/g, '<span class="ispritz">iSpritz</span>');
		//contenuto = contenuto.replace(/iSpritz/g, '<span class="ispritz">iSpritz</span>');
		
		
		   /* var re = '(^|>| )(iSpritz|ispritz)(?![^<]*(?:<\/script|>))( |<|$)',
		      regExp = new RegExp(re, 'i'),
		      sTag = '<span class="ispritz">',
		      eTag = "</span>";
		    
		    contenuto = contenuto.replace(regExp, '$1'+ sTag +'$2'+ eTag+'$3');
		
		*/
		
		
		//controllo se nel testo c'è anche l'ancora di googlemap, in quel caso sostituisco con 
		//il div e carico la libreria
		
		
		if ( contenuto.indexOf('{{#mappa}}') != -1 ) {
			
			contenuto = contenuto.replace('{{#mappa}}','<div id="googlemap"></div>');
			
			
			var script = document.createElement("script");
			  script.type = "text/javascript";
			  script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=appendGoogleMap";
			  document.body.appendChild(script);
			  
			  
			  
			
		}
		
		$('.content .textarea').html(contenuto);
	
	}
};

var appendGoogleMap = function (){
	var myLatlng = new google.maps.LatLng(45.415202,11.243649);
	  var myOptions = {
	    zoom: 9,
	    center: myLatlng,
	    mapTypeId: google.maps.MapTypeId.HYBRID,
	    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
	    }
	  };
	  
	  
	  var map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
	  var image = '/skin/website/img/logo-map.png';
	  var marker = new google.maps.Marker({
	      position: myLatlng,
	      title:"Hello World!",
	      icon: image
	  });
	  
	  // To add the marker to the map, call setMap();
	  marker.setMap(map);  
	
};


var homePage_bubble = function (){

	$("#bubbles .bubble2").fadeIn(500, function () {
        $("#bubbles .bubble5").fadeIn(500, function () {
            $("#bubbles .bubble3").fadeIn(500, function () {
                $("#bubbles .bubble1").fadeIn(500, function () {
                    $("#bubbles .bubble4").fadeIn(400);
                });
            });
        });
    });
	
	
	$('#bubbles .bubble .close').click(function(){
		$("#bubbles .bubble4").fadeOut(200, function () {
	        $("#bubbles .bubble5").fadeOut(200, function () {
	            $("#bubbles .bubble3").fadeOut(200, function () {
	                $("#bubbles .bubble2").fadeOut(200, function () {
	                    $("#bubbles .bubble1").fadeOut(100);
	                });
	            });
	        });
	    });
	});
};


var startHPAnimation = function (){
	if($('#home-flash-content').length!=0){
		
		  
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		attributes.id = "flash";
		attributes.align = "right";
		swfobject.embedSWF("video/03.swf", "home-flash", "426", "726", "9.0.0", false, {}, params, attributes,function(){});	
		var playerVersion = swfobject.getFlashPlayerVersion();
		if ( playerVersion.release == 0 ) var android = true;
		else var android = false;
		
		
		
		var ua = navigator.userAgent;
		var checker = {
		  iphone: ua.match(/(iPhone|iPod|iPad)/),
		  blackberry: ua.match(/BlackBerry/)
		};
		
		if (checker.iphone || checker.blackberry || android) {
			$('#home-video-handler').show();
			setTimeout( 'homePage_bubble()', 3700 );
		}
		
		$('#home-video-handler').click(function(){
			$('#home-video-handler').remove();
			$('#video').show();
			document.getElementById('video').load();
			document.getElementById('video').play();
			
		});
		
		
		
		setTimeout( 'showHiddenLayer()', 3500 );
		
		
		
		
		
		
		
	}
	
};

var showHiddenLayer = function (){
	$('#payoff').fadeIn();
	$('#menu').fadeIn();
	$('#orange_bar .centralBg .content').fadeIn();	
};
var scopriSpritz_bubble = {
	init : function(){
		if ( $('#round_bubbles').length!=0 ) {
		
			//effetto di ingrandimento delle immagini all'hover
			$('#round_bubbles .grow').hover(
					function(){
						var position = $(this).position();
						
							 $(this).css({ 
								width: parseInt($(this).width())+40+"px",
								height: parseInt($(this).height())+40+"px",
								top : parseInt(position.top)-10+'px',
								left : parseInt(position.left)-10+'px'
							  }, 100 );
						 
					},
					function(){
						
							var position = $(this).position();
							 $(this).css({ 
								 width: parseInt($(this).width())-40+"px",
								 height: parseInt($(this).height())-40+"px",
								 top : parseInt(position.top)+10+'px',
								 left : parseInt(position.left)+10+'px'
							  }, 100 );
						
					}
				);
		
			//inizializzo le variabili per i movimenti delle bubble
			this.param1;
			this.param2;
			this.param3;
			this.contatore = 0;
			
			this.switchParam();
			setInterval('scopriSpritz_bubble.switchParam()',3000);
		
		
		}
	},
	switchParam : function(){
		
		this.setParams(this.contatore);
		this.contatore++;
		if(this.contatore == 3){
			this.contatore = 0;
		}
	},
	moveBubbles : function(){
		//muovo le bubbles
		$($('#round_bubbles .move')[0]).animate({path : new $.path.bezier(this.param1)},3000);
		$($('#round_bubbles .move')[1]).animate({path : new $.path.bezier(this.param2)},3000);
		$($('#round_bubbles .move')[2]).animate({path : new $.path.bezier(this.param3)},3000);
		
	},
	setParams : function(i){
			//setto le variabili per i movimenti delle bubble
			if(i==0){
				this.param1 = {
					start: { 
					  y: 209, 
					  x: 200, 
					  angle: 10
					},  
					end: { 
					  y: 90,
					  x: 344, 
					  angle: -10, 
					  length: 0.85
					}
				  }
				this.param2 = {
					start: { 
					  y: 270, 
					  x: 140, 
					  angle: 10
					},  
					end: { 
					  y: 128,
					  x: 180, 
					  angle: 60, 
					  length: 1.25
					}
				  }
				this.param3 = {
					start: { 
					  y: 250, 
					  x: 740, 
					  angle: 10
					},  
					end: { 
					  y: 145,
					  x: 585, 
					  angle: -90, 
					  length: 1.25
					}
				  }
			}
			else if(i==1) {
				this.param1 = {
					start: { 
						y: 90,
						x: 344,
						angle: -10 
					},  
					end: { 
					  y: 178,
					  x: 429, 
					  angle: -60, 
					  length: 0.85
					}
				  };
				this.param2 = {
					start: { 
						y: 128,
						x: 180, 
						angle: 60
					},  
					end: { 
					  y: 13,
					  x: 254, 
					  angle: 20, 
					  length: 0.25
					}
				  };
				this.param3 = {
					start: { 
						y: 145,
						x: 585,  
					 	angle: -90
					},  
					end: { 
					  y: 263,
					  x: 454, 
					  angle: -40, 
					  length: 1.25
					}
				  };
			
			}
			else if(i==2) {
				this.param1 = {
					start: { 
						y: 178,
						  x: 429, 
						  angle: -60
					},  
					end: { 
						y: 209, 
						  x: 200, 
						  angle: 10,
					  length: 0.85
					}
				  };
				this.param2 = {
					start: { 
						y: 13,
						  x: 254, 
						  angle: 20
					},  
					end: { 
						y: 270, 
						  x: 140, 
						  angle: 10,
					  length: 1.25
					}
				  };
				this.param3 = {
					start: { 
						y: 263,
						  x: 454, 
						  angle: -40
					},  
					end: { 
						y: 250, 
						  x: 740, 
						  angle: 10, 
					  length: 1.25
					}
				  };
			

		}
			this.moveBubbles();
		
		
	}
};


var loginAreaRiservata = function(){
	
	$('#dLogin').click (function(){
		$.post('/login', $('#loginForm > form').serialize(), function(data) {
			
				if ( data == 'si' ) {
					
					document.location.href = $('#loginForm').find('input[name=page]').val();
				} 
				else
				{
					$('#loginForm').find('.notice').html($('#loginForm').find('input[name=errormsg]').val());			
					
				}
			});
		return false;
	});
	
	
	
};

$(window).load(function() {
	if ($('#home_overlay').length > 0) {
		$.fancybox({
			content : $('#home_overlay').html(),
			overlayColor: '#111',
			onStart : function() {
				$('#fancybox-wrap').addClass('home_overlay');
			},
			onClosed : function() {
				$('#fancybox-wrap').removeClass('home_overlay');
			}
		});
	}
});

