/*
 * Created by Giuseppe Migliozzi.
 * User: gmigliozzi
 * Date: 13/04/11
 * Time: 15.29
 * To change this template use File | Settings | File Templates.
 */
// UTILS GENERALE //

// loading //

$(document).ready(function(){
	
	$('body').addClass('white_bg')
	$('#loader').stop().css('display','block');
	 

});

$(window).load(function() {
	$('body').removeClass('white_bg')
	$('#wrap').css('display','block');
	$('#loader').stop().css('display','none');
	
	
	
	
	

	
	$('#barra_logo').css('width',($(window).width()-(parseInt($('#logo img').css('width'))+$('#logo img').offset().left))+'px');
	
	$('#crediti').css('width',$(window).width()+'px'); // prima al massimo in modo da poter settare il testo
	$('#crediti p').css('left',(-$('#crediti p').width()-20)+'px');  // settaggio del testo
	$('#crediti').css('width',0+'px'); // impostazione reale iniziale
	$('#crediti').css('left',(-$('#content').offset().left)+'px');
	
	
	
	Cufon.replace('#footer ul li a, #copy,#credits',{ fontFamily: 'HelveticaLT' ,  hover: true });
	
	var path = window.location.toString();
	if(path.search("=en") != -1) {
		$('.inglese').addClass('active');
		
	} else {
		
		$('.italiano').addClass('active');	
	}
	
	$(".play").click(function () {
	     var sound;
	        sound = $('.jp-controls').attr('sound');
	        if(sound==1){
	            $('.jp-controls').attr('sound',0);
	            sound = 0;
	        } else if(sound==0){
	            $('.jp-controls').attr('sound',1);
	            sound = 1;
	        }


	        $.ajax({
			type:	"GET",
			url:	"/default/index/sound/",
			data:	"sound="+sound,
			complete: function (data, stato) {

	            }
	        });
	});

	sound();


	var credits_open = false;
	$('#credits a').toggle(function(){
		credits_open = true;
		$('#crediti').animate({
	    width: $(window).width()
    	}, 750, 'easeInOutQuart');
		
		$('#crediti p').oneTime(100, function(){$('#crediti p').animate({
			  left:$('#copy').offset().left+15 
			    
			  }, 1050,'easeInOutQuart');});
		
		return false;
		}, 
		
		function (){
			credits_open = false;
			$('#crediti p').animate({
				  left:(-$('#crediti p').width())
				    
				  }, 600,'easeInOutQuart')
			
			$('#crediti').animate({
			    width: 0
		    	}, 750, 'easeInOutQuart');
			return false;
		}
	)
	
	if($.browser.msie && jQuery.browser.version == "6.0" || $.browser.msie && jQuery.browser.version == "7.0" || $.browser.msie && jQuery.browser.version == "8.0"){

		$('#advice').css('display','block');	
	} else {
		if(window.location.pathname == "/"){
			$('#menuFooter li').css('opacity','0');
			$('.flags').css('opacity','0');
			$('#copy').css('opacity','0');
			$('#credits').css('opacity','0');
			$('#player').css('opacity','0');
			$('#menuFooter li').each(function(index) {
			$(this).oneTime(3000+(100*index), function(){$(this).animate({
			    opacity: 1
	    		}, 800);});
			});
		
			$('.flags').oneTime(3500, function(){$('.flags').animate({
				opacity: 1
			}, 800);});
		
			$('#copy').oneTime(3600, function(){$('#copy').animate({
				opacity: 1
			}, 800);});
		
			$('#player').oneTime(3700, function(){$('#player').animate({
				opacity: 1
			}, 800);});
			
			$('#credits').oneTime(3800, function(){$('#credits').animate({
				opacity: 1
			}, 800);});
		}
	}
	
	
	$(window).resize(function (){
		
	
		
		
	
		
		$('#crediti').css('width',$(window).width()+'px'); // prima al massimo in modo da poter settare il testo
		$('#crediti p').css('left',(-$('#crediti p').width()-20)+'px'); // settaggio del testo
		$('#crediti').css('width',0+'px'); // impostazione reale iniziale
		$('#crediti').css('left',(-$('#content').offset().left)+'px');
		
		if(credits_open){
			$('#crediti').css('left',(-$('#content').offset().left)+'px');
			$('#crediti p').css('left',($('#copy').offset().left+10)+'px');
			$('#crediti').css('width',$(window).width()+'px');}
	});
	
	
});

var changing = false;


function gallery_init() {
	 $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
	 foto_loading = 0;
	
	 
	var current_image = 1;
	var slide_length = $("#itemDet img" ).length;
    
	
    $('#itemDet').css('opacity','0');
	$('#slider_nav').css('display','none');
	$('#content_gallery .destra').css('display','none');
	
	$('#content').oneTime(1, function(){$('#content_gallery').fadeIn(1);});
	
	if($('#itemDet').children('.wrap_element').length < 2){
		$('.slide_pointer').hide();
		$('.slide_position').hide();
		$('#slider_nav').css('background','none');
		
	} else {
		  $('#slider_nav .slide_position').oneTime(700, function(){
		    	$('#slider_nav .slide_position').animate({
			    width: (580/slide_length)*current_image
		    	}, 500, 'easeInOutQuart');
		    
		    	$('#slider_nav .slide_pointer').animate({
			    left: ((580/slide_length)*current_image)-5 
		    	}, 500, 'easeInOutQuart');
		    	
		    });
	}
	
	$('#content').oneTime(1, function(){$('#slider_nav').fadeIn(800);});
	$('#loading_zoom').fadeIn(300);
	$('#loading').fadeIn(300);
	$('#content').oneTime(100, function(){$('.destra').fadeIn(600);});
	$('#content').oneTime(200, function(){$('#itemDet').animate({
	   opacity: 1
	    
	  }, 600);});
	
    $('#slider_nav .slide_position').css('width','0px');
    $('#slider_nav .slide_pointer').css('left','0px');
    
  
    
    
    
  
  /* $('#zoom_obj').css('left',-$('#content').offset().left);
   $('#zoom_obj').css('width',$('#content').offset().left+620);*/
   
 
    
	$('#itemDet').nivoSlider({
		effect:'slideInLeft',
		animSpeed:350, 
        directionNav:true,
        directionNavHide:false,
        controlNav:false,
        pauseTime:6000,
        manualAdvance:true,
        prevText: '',
        nextText: '',	
    });
	
	$('#itemDet').data('nivoslider').start();
	
	
	$('.nivo-prevNav').click(function(){
		   
		if (!changing) {
			changing = true;
		current_image--;
		if(current_image == 0){
			current_image=slide_length;
		}
		if(more_opened){
			
			$('#morinfo').trigger('click');	
		}
		
		$('.mousetrap').css('display','none');
		$('.mousetrap:eq('+(current_image-1)+')').css('display','block');
	
	}	
		
	});
	$('.nivo-nextNav').click(function(){
		 
		if (!changing) {
			changing = true;
		current_image++;
		if(current_image == slide_length+1){
			current_image=1;
		}
			if(more_opened){
			
			$('#morinfo').trigger('click');	
		}
		
		$('.mousetrap').css('display','none');
		$('.mousetrap:eq('+(current_image-1)+')').css('display','block');
		
		}
		
		
	});
	
	var more_opened = false;
	
	$('#morinfo').toggle(function(){
		$('.nivo-caption p span').fadeIn(200);
		$('#morinfo h3').text('-');
		more_opened = true;
		},
		
		function (){
			more_opened = false;
			$('#morinfo h3').text('+');
			$('.nivo-caption p span').fadeOut(200);	
		}
	);
	
	if($('#itemDet').children('.wrap_element').length < 2){
		$('.nivo-nextNav').hide();
		$('.nivo-prevNav').hide();
		
	}
	
	var caption_title = $('.nivo-caption  h4').text().replace(/[-]/g," ");
	$('.nivo-caption  h4').text(caption_title);
	$('.nivo-html-caption  h4').text(caption_title);
	
	if($('.nivo-caption  span').text() == "IN-ALLESTIMENTO") {
		$('.nivo-caption  h4').text("IN ALLESTIMENTO");
		$('.nivo-html-caption  h4').text("IN ALLESTIMENTO");
		$('#morinfo').hide();
	} else {
		$('#morinfo').show();
		
	}
	
}

function regolamento() {
	
   // $('#itemDet').css('opacity','0');
  
	
	$('#content').oneTime(490, function(){  $('#content_gallery').fadeIn(500);$('#content').hide();});
	
	
	
	
	
    
}

function form() {
	
    $('#itemDet').css('opacity','0');
	
	
	$('#content').oneTime(1, function(){$('#content_form').fadeIn(1);});
	
	
	
	
	
    
}

var foto_loading = 0;

function muovi_lente(e){
	
	$('#zoom_lens').stop().animate({
	    left: (e.pageX-$('#content_gallery').offset().left+20),
	    top:  e.pageY
	    
	  }, 700, 'easeOutQuart');
	
	
	
	
}




