$(window).resize(function() {
	sliderdimension();
});

$(document).ready(function(){
	/* SHUFFLE HOMEPAGE */
	 shuffleHP(true);
	 
	/* TRIPLICA IMMAGINI IN HOME */
	/*var totImg = $(".img-hp").size();
	var lunghezzaRullo = 505 + (totImg-1)*185;
	$(".img-hp").each(function() {
		$(this).hide().addClass('temp');
		$('#photo-arena').append( $(this).clone().show().removeClass('temp') );	
		$(this).removeClass('open')
	});
	$(".img-hp:visible").each(function() {
		$('#photo-arena').append( $(this).clone().hide().addClass('temp').removeClass('open') );		
	});*/
	 
	 
	/* ATTIVA KEYBOARD */
	$(document).bind('keydown',keyboardArrow);

	$('#shuffleHP').click(function() {
		shuffleHP(false);
	});
	
	$(".admin .img-hp").draggable({ stack: '#photo-arena .img-hp' }).click(function() {
		if( $(this).css('width') == '180px' ) {
			var zindex = 0;
			$(".img-hp").each(
				function(intIndex) {
					var thiszindex = parseInt($(this).css('z-index'));
					if(thiszindex > zindex) zindex = thiszindex;
			});
			zindex++;
			
			$(this).animate({ width:'500px'},300,function() {
				$(this).children('.intestazione-foto-box').fadeIn();
			}).addClass('open').css('z-index',zindex); 
			
			$(this).children('.videoPlayGround').addClass('open');
		}
		else {
			$(this).children('.videoPlay').css('display','');
			$(this).children('.closeVideo').css('display','none');
			$(this).children('.intestazione-foto-box').fadeOut().parent().animate({ width:'180px'},300).removeClass('open');
			$(this).children('.videoPlayGround').removeClass('open').html('');
		}
		
	}).mouseenter(function(){
		if( $(this).hasClass('open') ) $(this).children('.intestazione-foto-box').fadeIn();
	}).mouseleave(function(){
		$(this).children('.intestazione-foto-box').fadeOut();
	});
	
	/* HOMEPAGE INTERACT */
	$('.seeMore').click(function(event) {
		event.stopPropagation();
	});
	
	$(".site .img-hp").draggable({ stack: '#photo-arena .img-hp' }).live('click',function() {
		var totImg = $(".img-hp").size();
		
		if( $(this).hasClass('locked') && $(this).hasClass('actual') ) {
			
		}
		else if( $(this).hasClass('actual') ) {
			$(this).children('.videoPlay').css('display','');
			$(this).children('.closeVideo').css('display','none');
			$(this).children('.intestazione-foto-box').fadeOut().parent().removeClass('open');
			$(".img-hp").each(function(index){
				$(this).animate({
					top: $(this).children('img').css('top'),
					left: $(this).children('img').css('left'),
					zIndex: $(this).children('img').css('zIndex'),
					width: '180px'
				},function() {
					if( $(this).hasClass('clone') ) $(this).remove();
				});
				if( $(this).children('img').hasClass('open') ) $(this).addClass('open').animate({ width:'500px' },{ "queue": false });
			}).removeClass('actual');
			
			$('#photo-arena').removeClass('linear');
			
			/* FOOTER MOVE */
			var altezzaBase = 620;
			if( $('#photo-arena').height() > altezzaBase ) $('#photo-arena').animate({ height: altezzaBase },500);
		}
		
		else {
			
			
			var zindex = 0;
			$(".img-hp").each(
				function(intIndex) {
					var thiszindex = parseInt($(this).css('z-index'));
					if(thiszindex > zindex) zindex = thiszindex;
			});
			zindex++;
			
			var indexCliccked = $(this).index();
			var startPointX = 370-parseInt($(this).index()*185);
			var staticstartPointX = startPointX;
			var lunghezzaRullo = 505 + (totImg-1)*185;
			
			/* FOOTER MOVE */
			var altezzaMax = parseInt($('#h-img-box').attr('rel')) + 204;
			if( $('#photo-arena').height() < altezzaMax ) $('#photo-arena').animate({ height: altezzaMax },500);
			
			$(".img-hp").each(function(index){				   
				
				if(index==indexCliccked) {
					$(this).animate({ width:'500px',top:200, left:startPointX }).addClass('open').addClass('actual');	
					startPointX+=505; 
					
				}
				else {
					$(this).animate({ width:'180px', top:350, left:startPointX }).removeClass('open').removeClass('actual');
					startPointX+=185;
					$(this).children('.videoPlay').css('display','');
					$(this).children('.closeVideo').css('display','none');
					$(this).children('.intestazione-foto-box').fadeOut();
					$(this).children('.videoPlayGround').removeClass('open').html('');
					
				}
				
				
			});
			
			if( $('#photo-arena').hasClass('linear') ) {  
				if( $('.img-hp:last').css('left') == "1060px" && parseInt($(this).css('left').replace(/px/,'')) > 370 ) {
					if( $(this).css('left') == "1060px" )  {
						$('#photo-arena').append( $('.img-hp').eq(4).clone().animate({ left:1245 },0).animate({ left:875 },function() {
							var id = $('.img-hp').eq(0).attr('id');
							$('.img-hp').eq(0).remove();
							$('.img-hp#'+id).removeClass('clone');
						}).addClass('clone') );
						
						$('#photo-arena').append( $('.img-hp').eq(5).clone().animate({ left:1430 },0).animate({ left:1060 },function() {
							var id = $('.img-hp').eq(0).attr('id');
							$('.img-hp').eq(0).remove();
							$('.img-hp#'+id).removeClass('clone');
						}).addClass('clone') );
					}
					else  {
						$('#photo-arena').append( $('.img-hp').eq(4).clone().animate({ left:1245 },0).animate({ left:1060 }).addClass('clone') );
						var id = $('.img-hp').eq(0).attr('id');
						$('.img-hp').eq(0).remove();
						$('.img-hp#'+id).removeClass('clone');
					}
				}
				else if( $('.img-hp:first').css('left') == "0px" && parseInt($(this).css('left').replace(/px/,'')) < 370 ) {
					if( $(this).css('left') == "0px" )  {
						$('#photo-arena').prepend( $('.img-hp').eq(-5).clone().animate({ left:-185 },0).animate({ left:185 },function() {
							var id = $('.img-hp:last').attr('id');
							$('.img-hp:last').remove();
							$('.img-hp#'+id).removeClass('clone');																					  
						}).addClass('clone') );
						
						$('#photo-arena').prepend( $('.img-hp').eq(-6).clone().animate({ left:-370 },0).animate({ left:0 },function() { 
							var id = $('.img-hp:last').attr('id');
							$('.img-hp:last').remove();
							$('.img-hp#'+id).removeClass('clone');																				
						}).addClass('clone') );
					}
					else { 
						$('#photo-arena').prepend( $('.img-hp').eq(-5).clone().animate({ left:-185 },0).animate({ left:0 }).addClass('clone') );
						var id = $('.img-hp:last').attr('id');
						$('.img-hp:last').remove();
						$('.img-hp#'+id).removeClass('clone');
					}
				}
				else if( $('.img-hp:first').css('left') == "-185px" && parseInt($(this).css('left').replace(/px/,'')) == 0 ) {
					$('#photo-arena').prepend( $('.img-hp').eq(-5).clone().animate({ left:-370 },0).animate({ left:0 }).addClass('clone') );
					var id = $('.img-hp:last').attr('id');
					$('.img-hp:last').remove();
					$('.img-hp#'+id).removeClass('clone');
				}
				else if( $('.img-hp:last').css('left') == "1245px" && parseInt($(this).css('left').replace(/px/,'')) == 1060 ) {
					$('#photo-arena').append( $('.img-hp').eq(4).clone().animate({ left:1430 },0).animate({ left:1060 }).addClass('clone') );
					var id = $('.img-hp').eq(0).attr('id');
					$('.img-hp').eq(0).remove();
					$('.img-hp#'+id).removeClass('clone');
				}
				
			}
			else {
				$('#photo-arena').prepend( $(".img-hp:last").clone().animate({ width:'180px',left:staticstartPointX-185, top:350 }).addClass('clone').removeClass('open') );
				$('#photo-arena').prepend( $(".img-hp:last").prev().clone().animate({ width:'180px',left:staticstartPointX-370, top:350 }).addClass('clone').removeClass('open') );
				
				$('#photo-arena').append( $(".img-hp:first").next().next().clone().animate({ width:'180px',left:startPointX, top:350 }).addClass('clone').removeClass('open') );
				$('#photo-arena').append( $(".img-hp:first").next().next().next().clone().animate({ width:'180px',left:startPointX+185, top:350 }).addClass('clone').removeClass('open') );
				$('#photo-arena').addClass('linear');
			}

		}
		
	});
	
	$(".site .img-hp.actual").live('mouseenter',function(){
		if( $(this).hasClass('open') ) $(this).children('.intestazione-foto-box').fadeIn();
	}).live('mouseleave',function(){
		$(this).children('.intestazione-foto-box').fadeOut();
	});
	
	
	$('.videoPlayGround').live('click',function(event) {
		if( $(this).hasClass('open') ) {
			event.preventDefault();
			event.stopPropagation();
		}
	}).live('mouseenter',function(event) {
		if( $(this).hasClass('open') ) {
			event.preventDefault();
			event.stopPropagation();
		}
	});
	
	$('.closeVideo').live('click',function(event) {
		event.preventDefault();
		event.stopPropagation();
		$(this).fadeOut().prev().prev('.videoPlayGround').removeClass('open').html('');
		$(this).next().fadeIn();
		$(this).prev().fadeIn();
	});
	
	$('.videoPlay').live('click',function(event) {
		event.preventDefault();
		event.stopPropagation();
		var url = $(this).attr('href');
		$(this).prev().fadeIn().prev().fadeOut();;
		$(this).fadeOut().prev().prev().prev('.videoPlayGround').addClass('open').load(url);
	});
	
	setslider();
	
	
	/* FOTOGRAFI HOME */
	$('#authors-list-header a').click(function() {
		$('#authors-list-header a').removeClass('active');		
		$(this).addClass('active');
		$('.author-menu').hide();
		
		$('.autore-foto-box').show();
		$('#fotografi-menu li').show();
		
		if( $(this).attr('rel') == "adv") {
			$('.autore-foto-box:not(".adv")').hide();
			$('#fotografi-menu li:not(".adv")').hide();
		}
		if( $(this).attr('rel') == "avant") {
			$('.autore-foto-box:not(".avant")').hide();
			$('#fotografi-menu li:not(".avant")').hide();
		}
		if( $(this).attr('rel') == "staff") {
			$('.autore-foto-box:not(".staff")').hide();
			$('#fotografi-menu li:not(".staff")').hide();
		}
		$('#fotografi-menu').fadeIn();
	});
	
	/*$('#adv-list-header a').click(function() {
		if( $(this).hasClass('active') ) return;
		
		$('.author-menu').hide();
		$('#fotografi-menu').show();
		$('.authors-hat').removeClass('active');
		$('.authors-hat[rel="fotografi"]').addClass('active');
		
		$(this).addClass('active');
		$('#authors-list-header').addClass('disabled');
		$('.autore-foto-box:not(".adv")').hide();
		$('#fotografi-menu li:not(".adv")').hide();
	});
	
	$('.authors-hat').click(function() {
		$(this).parent().removeClass('disabled');
		$('#adv-list-header a').removeClass('active');
		
		$('.author-menu').hide();
		var thisRel = $(this).attr('rel');
		$('#'+thisRel+'-menu').fadeIn();
		$('.authors-hat').removeClass('active');
		$(this).addClass('active');
		
		if( $(this).attr('rel') == 'fotografi' ) {
			$('.autore-foto-box').show();
			$('#fotografi-menu li').show();
		}
	});*/
	
	$('.autore-foto-box .autore-dati').click(function() {
		var url = $(this).next('a').attr('href');
		document.location.href=url;
	});
	
	$('.autore-foto-box').mouseenter(function() {
		var href = $(this).children('a').attr('href');	 
		$('#fotografi-menu li a').each(
			function(intIndex) { 
				if( $(this).attr('href') == href ) {
					$(this).addClass('active');
				}
			}
		);
	}).mouseleave(function() {
		$('#fotografi-menu li a').each(
			function(intIndex) { 
				$(this).removeClass('active');
			});
	});
	
	$('.author-menu li a').mouseenter(function() {
		var href = $(this).attr('href');
		$('.autore-foto-box .autore-dati').each(
			function(intIndex) { 
				if( $(this).next('a').attr('href') == href ) {
					$(this).css('display','block');
				}
			}
		);
	});
	
	$('.author-menu li a').mouseleave(function() {
		var href = $(this).attr('href');
		$('.autore-foto-box .autore-dati').each(
			function(intIndex) { 
				if( $(this).next('a').attr('href') == href ) {
					$(this).css('display','');
				}
			}
		);
	});
	
	/*STORIE HOME */
	$('.openfoto-box').mouseenter(function() {
		$(this).children('.info-foto-box').children().fadeIn();
	}).mouseleave(function() {
		$(this).children('.info-foto-box').children().fadeOut();
	});
	
	
	/* GRAFICO MESI STORIE */
	$('.year li').mouseenter(function() {
		if( $(this).children('a').removeClass('active').next('.graph').css('width').replace(/px/,'') > 0 ) {
			$(this).children('a').addClass('active').next('.graph').children('.blue-line').animate({
				width:'100%'														  
			},200);
		}
	});
	
	$(".year li:not('.fixed')").mouseleave(function() {
		$(this).children('a').removeClass('active').next('.graph').children('.blue-line').animate({
			width: 0														  
		},200);
	});
	
	/* OPEN INFO BOX */
	$('.open-info-box').click(function() {
		$('.info-box').slideToggle();
	});
	
	/* RULLO */
	$('.arrow-right').bind('click',rightArrow);	
	$('.arrow-left').bind('click',leftArrow);
	
	
	/* CLICK SULLE FOTO X FAR GIRARE IL RULLO */
	$('#rullino li').live('click',function(event) {
		var numInSel = $(this).index('#rullino li');
		var sizeImg = $('#rullino li').size();
		
		if(numInSel==2 || sizeImg == 1) { return null; }
		
		$('#infofile').fadeOut('fast');
		var newVisible = $(this);
		
		if(numInSel==3) {
			var totScroll = -($(this).prev().children('img').attr('width')+20);
			var giri = 1;
		}
		else if(numInSel>3) {
			var totScroll = $(this).prev().children('img').attr('width')+20;
			var thisImg = $(this);
			for(var i=numInSel;i>3;i--) {
				thisImg = $(thisImg).prev();
				totScroll += $(thisImg).prev().children('img').attr('width')+20;
			}
			totScroll = -totScroll;
			var giri = numInSel-2;
		}
		else if(numInSel==1) { 
			var totScroll = $(this).children('img').attr('width')+20; 
			var giri = 1;
		}
		else if(numInSel==0 && sizeImg !=1) { 
			var totScroll = $(this).children('img').attr('width')+$(this).next().children('img').attr('width')+40; 
			var giri = 2;
		}
		
		$('#rullino li.visible').removeClass('visible');
		var imgtemp = '';
		
		if( totScroll > 0 ) {
			for($x=giri;$x>0;$x--) {
				imgtemp = $('#rullino li:last').clone();
				$('#rullino li:last').remove();
				var imgtemp_w = $(imgtemp).children('img').attr('width') + 20;
				$('#rullino').animate({ left: '-='+imgtemp_w},0).prepend( $(imgtemp) );	
			}
			$('#rullino').animate({
				left: '+='+totScroll+"px"  
			},500,function(){
				$(newVisible).addClass('visible');
				$('#infofile').html($(newVisible).children('img').attr('alt')).fadeIn('slow');
				$('#rullino .videoPlayBig').css('display','').next('.videoBox').html('');
			});
		}
		else {
			for($x=giri;$x>0;$x--) {
				imgtemp = $('#rullino li:first').clone();
				$('#rullino li:first').remove();
				var imgtemp_w = $(imgtemp).children('img').attr('width') + 20;
				$('#rullino').animate({ left: '+='+imgtemp_w},0).append( $(imgtemp) );
			}
			$('#rullino').animate({
				left: '+='+totScroll+"px"  
			},500,function(){
				$(newVisible).addClass('visible');
				$('#infofile').html($(newVisible).children('img').attr('alt')).fadeIn('slow');
				$('#rullino .videoPlayBig').css('display','').next('.videoBox').html('');
			});
		}
	});
	
	$('#rullino li.visible a.videoPlayBig').live('click',function(event) {
			var url = "include/ajax.video_fromtxt.php?videoTxt="+$(this).attr('rel');
			$(this).next('.videoBox').load(url);
			$(this).css('display','none');
			//$(this).next('iframe').css('display','block').prev().css('display','none');
		});
	
	/* INVIO FORM NEWSLETTER */
	$('#newsletter-subscribe').submit(function(event) {
		var campiObb = 0;
		if( $('#newsletter-subscribe input[name="nome"]').val() != "" ) campiObb++;
		if( $('#newsletter-subscribe input[name="cognome"]').val() != "" ) campiObb++;
		if( $('#newsletter-subscribe input[name="societa"]').val() != "" ) campiObb++;
		if( $('#newsletter-subscribe input[name="professione"]').val() != "" ) campiObb++;
		if( $('#newsletter-subscribe input[name="email"]').val() != "" ) campiObb++;
	
		if(campiObb==5) document.newsletter-subscribe.submit();
		else {
			event.preventDefault();
			alert( "The * fields must be filled" );
		}
	});
	
	$(".admin-section .citta-point").draggable({ stack: 'map-canvas' });
	
	$(".citta-point").click(function() {
		$(".map-canvas .author-list-map").slideUp('fast');
		$(this).prev('ul').slideDown('slow');	
		var h_canvas = 616;
		var h_lista = $(this).prev('ul').children('li').size() * 53;
		var h_start = parseInt($(".map-canvas .author-list-map").css('top'));
		
		var h_tot = h_lista + h_start;
		if( h_tot > h_canvas) $('.map-canvas').animate({ height: h_canvas+(h_tot-h_canvas) },500);
		else $('.map-canvas').animate({ height: h_canvas },500);
	});
	
	$('.author-list-map').mouseleave(function() {
		$(this).slideUp('fast');	
		$('.map-canvas').animate({ height:616 },500);
	});
	
	$('.author-list-map li').mouseenter(function() {
		$(this).parent('ul').children('li').removeClass('active');
		$(this).addClass('active');
		/*$('.author-list-map li p.stato').hide();
		$(this).children('p.stato').slideDown();*/
	});
	
	/*$('.author-list-map li a').click(function(event) {
		if( $(this).parent('li').hasClass('active') ) {}
		else event.preventDefault();
	});*/
});

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}


/* RULLO */
var timerId = 0;

function sliderdimension() {
	var widthScreen = $(window).width();
	var widthBanda = Math.round( (widthScreen - 970)/2 );
	$('.sliderroll').width(widthScreen).css('left','-'+widthBanda+'px');
	
	var firstvocerullo = $('#rullino li:first');
	var secondvocerullo = $(firstvocerullo).next();
	
	var startPoint = $(firstvocerullo).width() + $(secondvocerullo).width() - widthBanda + 40;
	//alert(startPoint+" "+$(firstvocerullo).width()+" "+secondvocerullo.width());
	$('#rullino').css('left','-'+startPoint+'px');
}

function setslider() {	
	if( $('#rullino li').size() > 1 ) {
		var lastvocerullo = $('#rullino li:last');
		var prelastvocerullo = $(lastvocerullo).prev();
		//clearTimeout ( timerId );
		
		$('#rullino').prepend($(lastvocerullo).clone()).prepend($(prelastvocerullo).clone());
		$(lastvocerullo).remove();
		$(prelastvocerullo).remove();
		
		sliderdimension();
	}
	
		
	//timerId = setInterval ( "rightroll()", 3000 );
}

function rightroll() {
	$(document).unbind('keydown');
	$('.arrow-right').unbind('click',rightArrow);
	$('#infofile').fadeOut('fast');
	var leftslide = $('#rullino li.visible').width()+20;
	var newVisible = $('#rullino li.visible').next();
	$('#rullino li.visible').removeClass('visible');
	$('#rullino').animate({
		left: '-='+leftslide+"px"  
	},500,function(){
		$(newVisible).addClass('visible');
		var imgtemp = $('#rullino li:first').clone();
		$('#rullino li:first').remove();
		var imgtemp_w = $(imgtemp).children('img').attr('width') + 20;
		$('#rullino').animate({ left: '+='+imgtemp_w},0).append( $(imgtemp) );
		$(document).bind('keydown',keyboardArrow);
		$('.arrow-right').bind('click',rightArrow);
		$('#infofile').html($(newVisible).children('img').attr('alt')).fadeIn('slow');
		$('#rullino .videoPlayBig').css('display','').next('.videoBox').html('');
	});
}

function leftroll() {
	$(document).unbind('keydown');
	$('.arrow-left').unbind('click',leftArrow);
	$('#infofile').fadeOut('fast');
	var rightslide = $('#rullino li.visible').prev().width()+20;
	var newVisible = $('#rullino li.visible').prev();
	$('#rullino li.visible').removeClass('visible');
	$('#rullino').animate({
		left: '+='+rightslide+"px"  
	},500,function(){
		$(newVisible).addClass('visible');
		var imgtemp = $('#rullino li:last').clone();
		$('#rullino li:last').remove();
		var imgtemp_w = $(imgtemp).children('img').attr('width') + 20;
		$('#rullino').animate({ left: '-='+imgtemp_w},0).prepend( $(imgtemp) );		
		$(document).bind('keydown',keyboardArrow);
		$('.arrow-left').bind('click',leftArrow);
		$('#infofile').html($(newVisible).children('img').attr('alt')).fadeIn('slow');
		$('#rullino .videoPlayBig').css('display','').next('.videoBox').html('');
	});
}

function shuffleHP($check) {
	/* SHUFFLE HOMEPAGE */
	$(".img-hp").each(
		function(intIndex) {
			var l = Math.floor(Math.random() * 786);
			var t = Math.floor(Math.random() * 285);
			
			var posizione = $(this).position();
			if( $check ) {
				if( posizione.left ) { }
				else {
					$(this).css("left", l);
					$(this).css("top", t);
				}
			}
			else {
				$(this).animate({ width:'180px'},300).removeClass('open'); 
				$(this).css("left", l);
				$(this).css("top", t);
			}
	});
}

/* FRECCE DA TASTIERA */
var keyboardArrow = function(event) {
	var sizeImg = $('#rullino li').size();
	if(sizeImg == 1) { return null; }
	else {
		if (event.keyCode == '39') {
			rightroll();
		}
		if (event.keyCode == '37') {
			leftroll();
		}
	}
}

var rightArrow = function(event) { rightroll(); }
var leftArrow = function(event) { leftroll(); }
