var debug = false;

function log(string){
	if(debug){
		console.info(string);
	}
}

numbervideo = 0;

/* Function Site */

$(function(){

	$('.overlay').css('height',$('body').height());
	
	/* prelevo data corrente per countdown */
	
	dateFuture = new Date(2011,9,27,20,30,00);
	
	function GetCount(ddate){

		dateNow = new Date();
		amount = ddate.getTime() - dateNow.getTime();
		days = 0;
		hours = 0;
		mins = 0;
		secs = 0;
		out = '';
	
		if(amount > 0){
	
			amount = Math.floor(amount/1000);
	
			days=Math.floor(amount/86400);//days
			amount=amount%86400;
	
			hours=Math.floor(amount/3600);//hours
			amount=amount%3600;
	
			mins=Math.floor(amount/60);//minutes
			amount=amount%60;
	
			secs=Math.floor(amount);//seconds
		
			
			out += days;
			out += hours;
			out += mins;
			out += secs;
			
			
			out = out.substr(0,out.length-2);
			
			
			
			if( days <= 9 ){ days = '0'+days; }
			if( secs <= 9 ){ secs = '0'+secs; }
			if( mins <= 9 ){ mins = '0'+mins; }
			if( hours <= 9 ){ hours = '0'+hours; }
			
			
		}
	}
	
	var date = GetCount(dateFuture);
	
	$('#counter').countdown({
		image: 'images/digits.png',
		startTime: days + ':' + hours + ':' + mins + ':' + secs
	});
	
	$('.prenota, .openvideo a').click(function(){
		$('#video').show();
		$('.youtubevideo').html('<iframe width="640" height="390" src="http://www.youtube.com/embed/LXWtvewXdMQ?autoplay=1&amp;rel=0" frameborder="0" allowfullscreen></iframe>');
		
		$('.overlay').show();
		$('.closevideo').show();
	});
	
	$('.overlay, .closevideo').click(function(){
		//jwplayer().stop();
		$('.youtubevideo').html('');
		$('#video').hide();
		$('.closevideo').hide();
		$('.overlay').hide();
		$('.galleryvideo').show();
	});
	
	$('li a').hover(function(){
	
		var submenu = $(this).attr('title');
		var select = $('.men_' + submenu);
		
		if( $(this).hasClass('hover') ){
		
			closesubmenu( submenu );
			
			select.show();
			select.mouseleave(function(){
				select.hide();
			});

		} else {
			
			$('.submenu').hide();
			
		}
		
	});
	
	//url = 'jsoncast.php';
	


var CastPage = {
	
	init : function( url ){ 
		/*
$('li.face').hover(function(){
			$(this).css({'opacity' : '1'});
			if( !$(this).hasClass('active') ){
				$(this).children().show();
			}
		
			$(this).click(function(){
			$('ul.content').addClass('view');
			$('.castpage li').each(function(){
				$(this).removeClass('active')
			});
			$(this).addClass('active');
			$('li.face').each(function(){
				$(this).css({
					'opacity' : '.2'
				})
			});
			id = $('.descr', $(this)).attr('id');
			id = id.replace('id_','');
			
			$(this).css({ 
				'opacity' : '1'
			})
			
			$.get( url, function( data ){
			
				$('.divbio').hide();
				if( id > -1 && id < 4 ){ 
					$('#uid_1').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_1').show();
				}
				else if( id > 3 && id < 8 ){ 
					$('#uid_2').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_2').show();
				}
				
				else if( id > 7 && id < 12 ){ 
					$('#uid_3').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_3').show();
				}
				
				else if( id > 11 && id < 16 ){ 
					$('#uid_4').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_4').show();
				}
				
				else if( id > 10 && id < 20 ){ 
					$('#uid_5').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_5').show();
				}
				
				else if( id > 19 && id < 24 ){ 
					$('#uid_6').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_6').show();
				}
				
				else if( id > 23 && id < 28 ){ 
					$('#uid_7').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_7').show();
				}
				
				else if( id > 27 && id < 32 ){ 
					$('#uid_8').html('<span class="close" onclick="closeBio()"></span><span class="arrows" style="left:'+data[id].x+'px;"></span><h2>' + data[id].nome + '</h2><h3>' + data[id].title + '</h3><p>' + data[id].descr + '</p>' );
					$('.divbio#uid_8').show();
				}
				
			}, 'json');				
			
		});
		
	}, function(){
		
		if( $('ul.content').hasClass('view') ){
			if( !$(this).hasClass('active') ){
				$(this).css({'opacity' : '.2'});
			} else {
				$(this).css({'opacity' : '1'});
			}
		} else {
			$(this).css({'opacity' : '1'});
		}
		$(this).children('.descr').hide();		
	});
*/
	
	}
	
}

	
	Form.populatePr();
	
	teamslide( 'musiche' );
	teamslide( 'liriche' );
	teamslide( 'testo' );
	
	giuriaslide( 'carlinebrouwer' );
	giuriaslide( 'chiaranoschese' );
	giuriaslide( 'federicobellone' );
	giuriaslide( 'franceschiappetta' );
	giuriaslide( 'janverveer' );
	giuriaslide( 'michaelkosarin' );
	giuriaslide( 'simonemanfredini' );
	
	registrazioneslide( 'privato' );
	registrazioneslide( 'azienda' );
	
	clearInput( 'mailnewsletter', 'mail' );
	Form.cleartypeInput( 'ondemand-nome-mit' );
	Form.cleartypeInput( 'ondemand-mail' );
	Form.cleartypeInput( 'ondemand-comune-mit' );
	Form.cleartypeInput( 'ondemand-cap' );
	Form.cleartypeInput( 'ondemand-cognome-mit' );
	Form.cleartypeInput( 'ondemand-indirizzo-mit' );
	Form.cleartypeInput( 'ondemand-civico' );
	Form.cleartypeInput( 'ondemand-data' );
	Form.cleartypeInput( 'ondemand-tel' );
	Form.cleartypeInput( 'ondemand-nome-dest' );
	Form.cleartypeInput( 'ondemand-indirizzo-dest' );
	Form.cleartypeInput( 'ondemand-cognome-dest' );
	Form.cleartypeInput( 'ondemand-comune-dest' );
	Form.cleartypeTextarea( 'ondemand-message' );
	Form.controllCounter( 'ondemand-message' );
	
	Form.submitForm();
	
	$('.ondemand-provincia-mit').click(function(){
		$('.submenuprov').show();	
	});
	
	$('#si').click(function(){
		Form.setSi();
	});
	
	$('#no').click(function(){
		Form.setNo();
	});
	
	
	
	//$('.second').hide();
	
	$('#tabs .numberfirst').click(function(){
		$('.second').hide();
		$('.first').show();
		$('#first').addClass('active');
		$('#second').removeClass('active');
	});
	
	$('#tabs .numbersecond').click(function(){
		$('.first').hide();
		$('.second').show();
		$('#second').addClass('active');
		$('#first').removeClass('active');
	});
	
	
	//
	
	$.ajax({
		url: "http://gdata.youtube.com/feeds/api/users/stageentitalia/uploads?alt=json",
		dataType: "jsonp",
		success: function( json ){
			
			numbervideo = json.feed.entry.length;
			
			for( i=0; i<json.feed.entry.length; i++ ){
			
				var url = json.feed.entry[i].media$group.media$thumbnail[0].url;
				var title = json.feed.entry[i].title.$t;
				var videohref = json.feed.entry[i].media$group.media$content[0].url;
				    videohref = videohref.replace("?f=user_uploads&app=youtube_gdata", "?autoplay=1&amp;rel=0");				
				
				$('.galleryvideo').append('<li id="id_' + i + '"><a href="javascript:;" class="relvideo_' + i + '" onclick="clickvideo(\''+i+'\',\''+videohref+'\');"><img src="' + url + '" alt="' + title + '" width="290" /></a><p><br />' + title + '</p></li>');
					
			}
			
			$('#id_1').hide();
			$('#id_2').hide();
			
		}
		
	});
	
	// scroll notizie
	
	var speed = 1000;
	var elementHeight = 29;
	var countElements = $('#news-navigation ul li');
	var numberElements = countElements.length;
	var totalHeight = numberElements * elementHeight;
	var containerHeight = 456;
		
	var currentPlace = elementHeight * 7;
	var currentDifference = 0;
	
	$("#up").addClass("nogo");
    
    $("#down").click(function(event) {
    
    	$("#down").addClass("nogo");
    	$("#up").removeClass("nogo");
    	event.preventDefault();
 
		$("#news-navigation ul").animate({ top: "-458px"}, speed );
			
    });
    
    $("#up").click(function(event) {
    	
    	$("#down").removeClass("nogo");
    	$("#up").addClass("nogo");
    	event.preventDefault();
 
		$("#news-navigation ul").animate({ top: "0px"}, speed );
    	
    });
	
	
	//onloadfancy();
	
	$('a[rel=group]').fancybox({
		'titlePosition' : 'inside',
		'titleFormat': '<p></p>'
	});

	
	$('.ingrandisci').fancybox();
	$('.terminicondizioni').fancybox();
	$('.privacy').fancybox();
	
	
	$('.homefancy').fancybox({
		'titleShow' : 'false',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: '.7',
		'width'				: 877,
		'height'			: 384,
		'scrolling'			: 'no',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	
	// fotogallery
	var instanceOne = new ImageFlow();
		instanceOne.init({ ImageFlowID:'fotogallery', onClick: function() { clickfancy( $(this) ); } });
	
	$('#giorno, #mese, #anno').change(function(){
		create_data();
	});
	
	/*$('.slideshow').cycle({
		fx: 'scrollLeft', 
		timeout:  0, 
		speed:    "2000", 
		prev:    '#prev', 
		next:    '#next'
	});*/
	
	$('.slideshow').cycle({
		fx: 'scrollHorz',
		autostop: 0,
		autostopCount: 0,
		pause : 1,
		timeout: 5000,
		prev:    '#prev', 
		next:    '#next'
	});

	$('.slide').cycle({
		fx: 'scrollLeft', 
		timeout:  0, 
		speed:    "2000", 
		prev:    '#prev-exclusivepage', 
		next:    '#next-exclusivepage'
	});
	
	/*loadvideo('duca', 1);
	loadvideo('elisabetta', 2);
	loadvideo('francesca_ture', 3);
	loadvideo('bob_messini', 4);*/
	
	
	$('#next, #prev').hide();
	$('.slideshow').hover(function(){
		$('#next, #prev').fadeIn();
	});
	
	$('#next, #prev').mouseleave(function(){
		$('#next, #prev').fadeOut();
	});

	
	
	
});

function clickvideo( e, url ){
	
	
		$('#video').show();
		$('.youtubevideo').html('<iframe width="640" height="390" src="' + url + '" frameborder="0" allowfullscreen></iframe>');
		$('.overlay').show();
		$('.closevideo').show();
		$('.galleryvideo').hide();
	
	
}

function create_data(){

	giorno = $('#giorno').val();							
	mese = $('#mese').val();							
	anno = $('#anno').val();							

	data = anno + '-' + mese + '-' + giorno;
	
	$('#birthdate').val( data );

}

function clickfancy( obj ){
	
	var link = $( obj ).attr('id');
	$('.photofancy #'+ link ).click();
		
}

function closesubmenu( sub ){

	if( sub == 'musical' ){
		$('.men_gallery').hide();
		$('.men_casting').hide();
		$('.men_biglietti').hide();
	}

	else if ( sub == 'gallery' ){
		$('.men_musical').hide();
		$('.men_casting').hide();
		$('.men_biglietti').hide();
	}
	
	else if ( sub == 'casting' ){
		$('.men_musical').hide();
		$('.men_gallery').hide();
		$('.men_biglietti').hide();
	}
	
	else if ( sub == 'biglietti' ){
		$('.men_musical').hide();
		$('.men_gallery').hide();
		$('.men_casting').hide();
	}
	
}

function registrazioneslide( div ){

	$('.' + div +' h3').click(function(){
		
		/* Reset */
		$('.registrazionepage h3').css('background','url(images/arrows_team.jpg) top left no-repeat');
		$('.descr').hide();
		
		/* Imposta */
		$('.' + div +' .descr').slideToggle('slow');
		$(this).css('background','url(images/arrows_team_open.jpg) top left no-repeat');
		
	})
	
}

function teamslide( div ){

	$('.' + div +' h3').click(function(){
		
		/* Reset */
		$('.teampage h3').css('background','url(images/arrows_team.jpg) top left no-repeat');
		$('.descr').hide();
		
		/* Imposta */
		$('.' + div +' .descr').slideToggle('slow');
		$(this).css('background','url(images/arrows_team_open.jpg) top left no-repeat');
		
	})
	
}

function giuriaslide( div ){

	$('.' + div +' h3').click(function(){
		
		/* Reset */
		$('.giuriapage h3').css('background','url(images/arrows_team.jpg) top left no-repeat');
		$('.descr').hide();
		
		/* Imposta */
		$('.' + div +' .descr').slideToggle('slow');
		$(this).css('background','url(images/arrows_team_open.jpg) top left no-repeat');
		
	})
	
}

function clearInput( classinput, type ){
	
	var typedefault;
	
	if( type == 'username' ){ typedefault = 'username'; }
	else if( type == 'mail' ){ typedefault = 'Inserisci la tua mail'; }
	
	$('.' + classinput ).click(function(){
		
		inputval = $(this).val();
		
		if( inputval == typedefault ){ $(this).val(''); }
			
	});
	
	$('.' + classinput ).blur(function(){
	
		inputval = $(this).val();

		if( inputval == '' ){ $(this).val( typedefault ); }
		
	});
	
}

	var page = 0;

function verticalslide( classe ){
	
	var top;
	var removeclass;

	if( classe == 'up' && page > 0 ){
		
		page--;
    	top = 0-426*page;
    	
		$('.down' ).removeClass('disactive');
	
	}
	
	else if( classe == 'down' && page < 3 ) {
	
		page++;	
		top = -426*page;
		 
    	$('.up' ).removeClass('disactive');
    	

    }
	
	if( !$('.' + classe ).hasClass('disactive') ){
		
		$('.news ul').animate({
			marginTop : top
		}, 1000, function(){
		
			if( page == 3 ){
				$('.down' ).addClass('disactive');
			}
			
			if( page == 0 ){
				$('.up' ).addClass('disactive');
			}
		
		});
		
	}
	
}


var Form = {

	submitActive : false,
	check : false,

	cleartypeInput : function( value ){
	
		var message = new Array;
		var inputval = new Array;
	
		var placeholder = $(':input').attr('placeholder');
		
		if( !placeholder ){
		
			$('.' + value + ':input').each(function( index ){
			
				message[index] = $(this).val();
				 
				$(this).focus(function(){
				
					inputval[index] = $(this).val();
					
					if( inputval[index] == message[index] ){
						$(this).val('');
					}
				
				});
				
				$(this).blur(function(){
				
					inputval[index] = $(this).val();
					
					if( inputval[index] === '' ){
						$(this).val( message[index] );
					}
				
				});
			
			});	
	
		} else { return; }
		
	},
	
	cleartypeTextarea : function( value ){
		
		var prefVal = 'Il tuo messaggio';
		
		var placeholder = $('textarea.' + value).attr('placeholder');
		
		if( !placeholder ){
		
			$('textarea.' + value).focus(function(){
				
				inputval = $(this).val();
				
				if( inputval == prefVal ){
					$(this).val('');
				}
				
			});
			
			$('textarea.' + value).blur(function(){
				
				inputval = $(this).val();
				
				if( inputval == '' ){
					$(this).val( prefVal );
				}
				
			});
		
		}
		
	},
	
	controllCounter : function( value ){
	
		defaultNum = -300;
		
		$('textarea.' + value).keyup(function(){
			num = $(this).val().length;
			
			html = defaultNum + num;
			$('#num').html( html );
			
			if( num > 300 ){
				$('#num').addClass('maxNum');
				Form.submitActive = false;
			} else {
				$('#num').removeClass('maxNum');
				Form.submitActive = true;
			}
			
		});
		
	},
	
	setCheck : function(){
	
		if( Form.check ){
			Form.check = false;
			$('#sibox').removeClass('active');
			$('#privacy').removeAttr('checked');
		} else {
			Form.check = true;
			$('#sibox').addClass('active');
			$('#privacy').attr('checked','checked');
			$('#privacy').val('1');
		}
			
	},
	
	populatePr : function(){
		
		var url = 'getCitiesList.php'
		
		$.get(url, function(data){
			
			for( var i=0; i<data.cities.city.length; i++ ){
			
				$('#province').append('<option>' + data.cities.city[i].city + '</option>');
				
			}
			
			for( var i=0; i<data.cities.city.length; i++ ){
			
				$('.submenuprov').append('<li><a href="javascript:;" onclick="Form.setPr(\'' + data.cities.city[i].city + '\')">' + data.cities.city[i].city + '</a></li>');
				
			}
			
		}, 'json');
		
	},
	
	setPr : function( val ){
		
		$('#province').val( val );
		$('.submenuprov').hide();
		$('.ondemand-provincia-mit').val( val )
				
	},
	
	submitForm : function(){
		
		
		$("#formsisterondemand").submit(function(event) {
	
			event.preventDefault();
	
		var $form = $( this ),
			sender_message = $form.find( 'textarea[name="sender_message"]' ).val(),
			sender_firstname = $form.find( 'input[name="sender_firstname"]' ).val(),
			sender_email = $form.find( 'input[name="sender_email"]' ).val(),
			sender_city = $form.find( 'select[name="sender_city"]' ).val(),
			sender_prov = $form.find( 'input[name="sender_prov"]' ).val(),
			sender_zip = $form.find( 'input[name="sender_zip"]' ).val(),
			sender_lastname = $form.find( 'input[name="sender_lastname"]' ).val(),
			sender_address = $form.find( 'input[name="sender_address"]' ).val(),
			sender_address_num = $form.find( 'input[name="sender_address_num"]' ).val(),
			sender_birthdate = $form.find( 'input[name="sender_birthdate"]' ).val(),
			sender_phone = $form.find( 'input[name="sender_phone"]' ).val(),
			recipient_firstname = $form.find( 'input[name="recipient_firstname"]' ).val(),
			recipient_prov = $form.find( 'input[name="recipient_lastname"]' ).val(),
			recipient_city = $form.find( 'input[name="recipient_city"]' ).val(),
			sender_privacy = $form.find( 'input[name="sender_privacy"]' ).val(),
			url = $form.attr( 'action' )
			
			if( sender_message == 'Il tuo messaggio' ){ $('.feedback').html('Messaggio non valido'); sender_message = ''; } 
			else if( sender_firstname == 'Nome' ){ $('.feedback').html('Nome Mittente non valido'); sender_firstname = ''; }
			else if( sender_email == 'Email' ){ $('.feedback').html('Email non valida'); sender_email = ''; }
			else if( sender_city == 'Comune' ){ $('.feedback').html('Citt&agrave; Mittente non valida'); sender_city = ''; }
			else if( sender_prov == 'Provincia' ){ $('.feedback').html('Provincia non valida'); sender_prov = ''; }
			else if( sender_zip == 'Cap' ){ $('.feedback').html('Cap non valido'); sender_zip = ''; }
			else if( sender_lastname == 'Cognome' ){ $('.feedback').html('Cognome non valido'); sender_lastname = ''; }
			else if( sender_address == 'Indirizzo' ){ $('.feedback').html('Indirizzo non valido'); sender_address = ''; }
			else if( sender_address_num == 'Civico' ){ $('.feedback').html('Numero civico non valido'); sender_address_num = ''; }
			else if( sender_birthdate == 'Data di Nascita' ){ $('.feedback').html('Data di Nascita non valida'); sender_birthdate = '0000-00-00'; }
			else if( sender_phone == 'Telefono' ){ $('.feedback').html('Telefono non valido'); sender_phone = ''; }
			else if( recipient_firstname == 'Nome' ){ $('.feedback').html('Nome Destinatario non valido'); recipient_firstname = ''; }
			else if( recipient_city == 'Comune' ){ $('.feedback').html('Citt&agrave; Destinatario non valida'); recipient_city = ''; }
			else if( sender_privacy == '0' ){ $('.feedback').html('Devi accettare l&rsquo;informativa sulla privacy'); sender_privacy = ''; }
			else {
				$('.feedback').html('');
			}
		
			$.post( url, {	sender_message: sender_message,
							sender_firstname: sender_firstname,
							sender_email : sender_email,
							sender_city : sender_city,
							sender_prov : 'MI',
							sender_zip : sender_zip,
							sender_lastname : sender_lastname,
							sender_address : sender_address,
							sender_address_num : sender_address_num,
							sender_birthdate : sender_birthdate,
							sender_phone : sender_phone,
							recipient_firstname : recipient_firstname,
							recipient_prov : 'Milano',
							recipient_city : recipient_city,
							sender_privacy : sender_privacy } , function( json ) {
	
							if( json.dataSaved ){
								$('.feedback').html('Messaggio inviato correttamente!');
								reset_field();
							} else if ( json.errorsFound && json.message ==  'invalidEmail'){
								$('.feedback').html('Email non valida');
							}				
		} , 'json' );
	
	});
		
	}

}

function reset_field(){

	$form = $("#formsisterondemand");
	
	$form.find( 'textarea[name="sender_message"]' ).val('Il tuo messaggio');
	$form.find( 'input[name="sender_firstname"]' ).val('Nome');
	$form.find( 'input[name="sender_email"]' ).val('Email');
	$form.find( 'input[name="sender_zip"]' ).val('Cap');
	$form.find( 'input[name="sender_lastname"]' ).val('Cognome');
	$form.find( 'input[name="sender_address"]' ).val('Indirizzo');
	$form.find( 'input[name="sender_address_num"]' ).val('Civico');
	$form.find( 'input[name="sender_birthdate"]' ).val('Data di Nascita');
	$form.find( 'input[name="sender_phone"]' ).val('Telefono');
	$form.find( 'input[name="recipient_firstname"]' ).val('Nome');
	$form.find( 'input[name="recipient_lastname"]' ).val('Comune');
}

function closeBio(){
	$('.divbio').hide();
	$('ul.content').removeClass('view');
					
	$('.castpage li').each(function(){
		$(this).removeClass('active').css({'opacity' : '1'});
	});
}
