$(document).ready(function(){
	
	toggleText('txtIndirizzo1', 'Il tuo indirizzo');
	
	$(".genericForm").validate({
		rules: {
			nameField: {
				required: true
			},
			emailField: {
				required: true,
				email: true
			},
			privacy: {
				required: true
			},
            motivo: {
				required: true
			}
			
		}
	});



	$(".newsletterForm").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
            privacy: {
				required: true
			}
			}
	});

	//$('.boxAux').wrap('<span id="quote" />'); 
    
	//$('.newsletterForm').ajaxForm(function() {
	//$("#quote").load("http://www.magicworld.it/sito/grazie.php");
	//});



   $('.auto-submit-star').rating({
	callback: function(value, link){
    var id= $(this).attr('id');
	if(value>0){rateImg(value,id);}
	}
	});









	$('#controls li.page a').each(
		function( intIndex ){
			$(this).addClass("number"+(intIndex + 1).toString());
			$(this).bind("click", function(e){
				e.preventDefault();
				if ($(this).is(".active")){  } else {
					clearInterval(timings);
					var element = $(this);
					$('#introHome li.number'+(intIndex + 1).toString()).addClass('last-active').css({opacity: 1});
					$('#introHome li.active').animate({opacity: 0}, 500, function(){
						$('#introHome li.active').removeClass('active');
						$('#introHome li.number'+(intIndex + 1).toString()).removeClass('last-active').addClass('active');
						element.addClass('active');
						element.parent().siblings().children().removeClass('active');
					});
				}
			});
		}
	);
	
	$('#introHome li').each(
		function( intIndex ){
			$(this).addClass("number"+(intIndex + 1).toString());
		}
	);
	
	$('#controls li.next a').bind("click", function(e){
		e.preventDefault();
		clearInterval(timings);
		var $active = $('#introHomeWrapper ul li.active');
		if ( $active.length == 0 ) $active = $('#introHomeWrapper ul li:last');
		var $next =  $active.next().length ? $active.next() : $('#introHomeWrapper ul li:first');
		$active.addClass('last-active');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {
			$('#controls ul .active').removeClass('active');
			$active.removeClass('active last-active');
			var newActive = ($(this).attr('class').substring(0,7));
			$('#controls ul .'+newActive).addClass('active');
		});
	});
	
	$('#controls li.prev a').bind("click", function(e){
		e.preventDefault();
		clearInterval(timings);
		var $active = $('#introHomeWrapper ul li.active');
		if ( $active.length == 0 ) $active = $('#introHomeWrapper ul li:first');
		var $prev =  $active.prev().length ? $active.prev() : $('#introHomeWrapper ul li:last');
		$active.addClass('last-active');
		$prev.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {
			$('#controls ul .active').removeClass('active');
			$active.removeClass('active last-active');
			var newActive = ($(this).attr('class').substring(0,7));
			$('#controls ul .'+newActive).addClass('active');
		});
	});
	
	$(function() {
		timings = setInterval( "slideSwitch()", 7000 );
	});
	
	$('#footer a[href="#"]').bind("click", function(e){
		e.preventDefault();
		alert("presto online");
	});
	
	intercetta();

	$("#giostraContent #carouselContent").carousel({
		dispItems: 8,
		btnsPosition: "outside",
		loop: true
		// animSpeed: 800
		// autoSlideInterval: 5000,
		// delayAutoSlide: 2000,
		// animSpeed: 500,
		// loop: false
	});
	
	
	
	$("#dettaglioFoto-2 .carousel, #dettaglioVideo-2 .carousel").carousel({
		dispItems: 1,
		btnsPosition: "outside",
		pagination: true,
		loop: true,
		prevBtn: "<a>«</a>",
		nextBtn: "<a>»</a>"
	});
	
	$('#dettaglioFoto-2 a.carousel-control.next').appendTo($('#dettaglioFoto-2 .carousel-pagination p'));
	$('#dettaglioFoto-2 a.carousel-control.previous').prependTo($('#dettaglioFoto-2 .carousel-pagination p'));
	$('#dettaglioVideo-2 a.carousel-control.next').appendTo($('#dettaglioVideo-2 .carousel-pagination p'));
	$('#dettaglioVideo-2 a.carousel-control.previous').prependTo($('#dettaglioVideo-2 .carousel-pagination p'));
	
	$("#giostraImages ul li a").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	if ($("#tutte a.active, #sballo a.active, #famiglia a.active, #relax a.active").length > 0){
		var active = $('#tutte a.active, #sballo a.active, #famiglia a.active, #relax a.active').parent().attr('id');
		if(active == 'tutte'){
		} else {
			$('#aquaticContent .content-1 .single').hide();
			$('#aquaticContent .content-1 .single.'+active).show();
		}
	}
	
	$('#tutte a, #sballo a, #famiglia a, #relax a').bind('click', function(e) {
		e.preventDefault();
		$(this).parent().siblings().children().removeClass('active');
		$(this).addClass('active');
		var id = $(this).parent().attr('id');
		if(id == 'tutte'){
			$('#aquaticContent .content-1 .single').fadeIn();
		} else {
			$('#aquaticContent .content-1 .single').fadeOut();
			$('#aquaticContent .content-1 .single.'+id).fadeIn();
		}
	});
	
	$('#giornata ul li a').bind('click', function(e) {
		e.preventDefault();
		$(this).parent().siblings().children().removeClass('active');
		$(this).addClass('active');
		var type = $(this).parent().attr('class');
		if(type == 'parcoAquatico'){
			$('#parcoDivertimenti').fadeOut(function(){
				$('#parcoAquatico').fadeIn();
			});
		} else {
			$('#parcoAquatico').fadeOut(function(){
				$('#parcoDivertimenti').fadeIn();
			});
		}
	});

window.fbAsyncInit = function() {
        FB.init({appId: '133521176678371', status: true, cookie: true,
                 xfbml: true});
      };
      (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
          '//connect.facebook.net/it_IT/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());


});

/* DISATTIVO PER BLOCCARE CAROSELLO BANNER HOME
function slideSwitch() {
	var $active = $('#introHomeWrapper ul li.active');
	if ( $active.length == 0 ) $active = $('#introHomeWrapper ul li:last');
	var $next =  $active.next().length ? $active.next() : $('#introHomeWrapper ul li:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 800, function() {
		$('#controls ul .active').removeClass('active');
		$active.removeClass('active last-active');
		var newActive = ($(this).attr('class').substring(0,7));
		$('#controls ul .'+newActive).addClass('active');
	});
}
*/

function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
	try {
		// Mozilla / Safari
		this._xh = new XMLHttpRequest();
	} catch (e) {
		// Explorer
		var _ieModelos = new Array(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i=0;i < _ieModelos.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(_ieModelos[i]);
				success = true;
			} catch (e) {
				// Implementar manejo de excepciones
			}
		}
		if ( !success ) {
			// Implementar manejo de excepciones, mientras alerta.
			return false;
		}
		return true;
	}
}

datosServidor.prototype.ocupado = function() {
	estadoActual = this._xh.readyState;
	return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.procesado = true;
	}
}

datosServidor.prototype.enviar = function(urlget,datos) {
	if (!this._xh) {
		this.iniciar();
	}
	if (!this.ocupado()) {
		this._xh.open("GET",urlget,false);
		this._xh.send(datos);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseText;
		}
		
	}
	return false;
}



function rateImg(rating,imgId )  {
	    //alert("Test");
		remotos = new datosServidor;
		nt = remotos.enviar(path_assoluta+'update.php?rating='+rating+'&imgId='+imgId);
		//rating = (rating * 25) - 8;
		//document.getElementById('current-rating').style.width = rating+'px';
		document.getElementById('rating').innerHTML = '<p><strong>Grazie per il tuo voto!</strong></p>';
}




   function intercetta() {
                    for (var i=0; i<document.links.length; i++)
                    if(document.links[i].rel=="blank"){
                      document.links[i].target="_blank";
                    }
                  }


function toggleText(p1, p2){
    $('input'+'#'+p1).focus(function () {
        if (this.value == p2) {this.value = '';}
    });
    $('input'+'#'+p1).blur(function () {
        if (this.value == '') {this.value = p2;}
    });
}























