var screenH = 0;
var screenW = 0;
var marginleft = 0;

$(window).resize(function () {
	$('body.splash #content').css('display','none');
	
	screenH = $(window).height();	
	screenW = $(window).width();
	marginleft = (screenW-980)/2;
	
	$('body.splash #splash').css('height',screenH).css('width',screenW);
	$('body.splash #bigcanvas').css('width',screenW+980).css('height',screenH);
	
	$('body.splash #content').css('display','block');
});

$(window).load(function () {
	screenH = $(window).height();	
	screenW = $(window).width();
	marginleft = (screenW-980)/2;
	
	$('body.splash #content').css('display','block');
	
	$('body.splash #splash').css('height',screenH).css('width',screenW);
	$('body.splash #bigcanvas').css('width',screenW+980).css('height',screenH);
	
	var tipologiarullo = 'rullino';
	
	$('.languagemenu a').click(function() {
		slidesplash();
	});
	
	function slidesplash(silence) {
		$('#splash').animate({ left: '-='+(screenW-marginleft)+'px'},1000);	
		$('#content').animate({ left: '-='+(screenW-marginleft)+'px'},1000,function() {
			$('body').removeClass('splash');
			$('#content').animate({ left: '+='+(screenW-marginleft)+'px'},0);
			$('#bigcanvas').css('width','auto').css('height','auto');
		});
	}
	
	$('#changecolor').click(function(event) {
		event.preventDefault();
		$('body').toggleClass('black').toggleClass('white');
		var newColor = $('body').attr('class');
		$.get('include/session.php?color='+newColor);
	});
	
	$('.languagemenu a').click(function(event) {
		event.preventDefault();
		var newlang = $(this).attr('class');
		if(newlang == 'it') {
			$('#fotografi').html('FOTOGRAFI');	
			$('#registi').html('REGISTI');	
			$('#team').html('TEAM');	
			$('#contatti').html('CONTATTI');
		}
		if(newlang == 'en') {
			$('#fotografi').html('PHOTOGRAPHERS');	
			$('#registi').html('DIRECTORS');	
			$('#team').html('TEAM');	
			$('#contatti').html('CONTACT');	
		}
		//alert($('#cambialingua').attr("class"));
		$('#cambialingua').addClass(newlang);
		$.get('include/session.php?lang='+newlang);
		//alert($('#cambialingua').attr("class"));
		
	});
	
	$('#gridview').click(function(event) {
		event.preventDefault();
		tipologiarullo = 'rullino';
		$('.rullinobig').css('display','none');
	});
	
	$('#slideview').click(function(event) {
		event.preventDefault();
		tipologiarullo = 'rullinobig';
		$('.rullinobig').css('display','block');
	});
	
	//////////////////////////////////////
	
	//////////////////////////////////////
	
	$('.arrownav').each(function() { if (!$(this).hasClass("invisible")) $(this).css("visibility","visible"); } );
	$('.loader').css("display","none");
	$('.photogrid').fadeTo("slow",1);

	$('.arrownav').click(function(event) {
		event.preventDefault();
		if( $(this).hasClass('rightarrow') ) {
			slideright(tipologiarullo);
		}
		if( $(this).hasClass('leftarrow') )	{
			slideleft(tipologiarullo);
		}
		
	});
	
	function setslider(tiporullo) {
		var lastvocerullo = $('.'+tiporullo+' .slideroll:last');
		$('.'+tiporullo).prepend($(lastvocerullo).clone()).css('left','-599px');
	}
	
	function slideright(tipologiarullo,silence) {
		var firstvocerullo = $('.'+tipologiarullo+' .slideroll:first').next().clone();
		$('.'+tipologiarullo+' .slideroll:first').remove();
		$('.'+tipologiarullo).append( $(firstvocerullo) ).animate({ left: '+=599px'},0);	
		if(silence == 1) $('.'+tipologiarullo).animate({ left: '-=599px'},0);	 
		else $('.'+tipologiarullo).animate({ left: '-=599px'});	  
	}
	
	function slideleft(tipologiarullo,silence) {
		var lastvocerullo = $('.'+tipologiarullo+' .slideroll:last').prev().clone();
		$('.'+tipologiarullo+' .slideroll:last').remove();
		$('.'+tipologiarullo).prepend( $(lastvocerullo) ).animate({ left: '-=599px'},0);
		if(silence == 1) $('.'+tipologiarullo).animate({ left: '+=599px'},0);
		else $('.'+tipologiarullo).animate({ left: '+=599px'});
	}
		
	
	$('.boximg a, .boxvideo a').live('click',function(event) {
		event.preventDefault();
		
		tipologiarullo = 'rullinobig';
		$('.rullinobig').css('display','block');
		
		var partenza = parseInt( $('.rullinobig div:first').next('div').children().children().attr('class') );
		var arrivo = parseInt( $(this).attr('class') );
		
		if(partenza < arrivo) { for(var i = partenza; i < arrivo; i++)  { slideright(tipologiarullo,1); } }
		else { for(var i = arrivo; i < partenza; i++)  { slideleft(tipologiarullo,1); } }
		
	});
	
	setslider('rullino');
	setslider('rullinobig');

});
