/******************************************************************************************************************/
// TABS HANDLE
/******************************************************************************************************************/

function handleTabs(){
	$('.tabs .tab').hide();
	
	$('.tabs .sel').each(function(){
		initid = $(this).find('a').attr('href');
		$(initid).show();
	});
	
	$('.tabs .labels a').click(function(){
		$(this).parents('.tabs').find('.labels li').removeClass('sel');
		$(this).parent().addClass('sel');
			
		if ($(this).hasClass('ajax')) {
			mylink = $(this).attr('href');
			
			$(this).parents('.tabs').find('.tab').hide();			
			myajaxtab = $(this).parents('.tabs').find('.tab-ajax');
						
			myajaxtab.show();
			
			myajaxtab.find('ul').load(mylink);
			
		}
		else {
			myid = $(this).attr('href');
			$(this).parents('.tabs').find('.tab').hide();
			$(myid).show();
		}
		
		return false;
	});
}

/******************************************************************************************************************/
// FORM HANDLE
/******************************************************************************************************************/

function handleForm(){
	// datepicker
	$('.datepick').datepicker({ 
		showOn: 'both',
		buttonImage: siteUrl+'img/icon-calendar.gif',
		buttonImageOnly: true,
		monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
		dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'],
		dateFormat: 'dd/mm/yy',
		currentText: 'Oggi'
	});

	//init
	//$('#frm-partenza option:eq(0)').attr('selected','selected');
	//$('#frm-arrivo option:eq(0)').attr('selected','selected');
	//$('#frm-arrivo').attr('disabled','disabled');
/*	
	// check on select change
	$('#frm-partenza').change(function(){
		mycode = $(this).find('option:selected').attr('value');
		$('#results').hide();
		$('#results ul').hide();
		
		if (mycode == "FA" || mycode == "ANN" || mycode == "ANS") {
			//$('#frm-arrivo').removeAttr('disabled');
			//$('#frm-arrivo option:eq(1)').removeAttr('selected').hide();
			//$('#frm-arrivo option:eq(0)').attr('selected','selected');
			$('#frm-arrivo-sn').hide();
			$('#frm-arrivo-ns').show();
			
			$('#frm-arrivo-sn').removeAttr( "name" );
			$('#frm-arrivo-ns').attr( "name", "arrivo" );
		}
		/*else if (mycode == "-1") {
			$('#results').hide();
			$('#results ul').hide();
			$('#frm-partenza option:eq(0)').attr('selected','selected');
			$('#frm-arrivo option:eq(0)').attr('selected','selected');
			$('#frm-arrivo').attr('disabled','disabled');
		}* /
		else {
			//$('#frm-arrivo').attr('disabled','disabled');
			//$('#frm-arrivo option:eq(1)').show().attr('selected','selected');
			
			$('#frm-arrivo-ns').hide();
			$('#frm-arrivo-sn').show();
			
			$('#frm-arrivo-ns').removeAttr( "name" );
			$('#frm-arrivo-sn').attr( "name", "arrivo" );
			
			// mostra gli orari di andata disponibili		
			//$('#results').show();
			//$('#andata').show();
			
			//$('#andata li').hide();
			//$('#a-'+mycode).show();
		}
		
		// mostra gli orari di andata disponibili		
		$('#results').show();
		$('#andata').show();
			
		$('#andata li').hide();
		$('#a-'+mycode).show();
		
	});
	
	$('#frm-arrivo-sn, #frm-arrivo-ns').change(function(){
		mycode = $(this).find('option:selected').attr('value');	
		$('#results').hide();
		$('#results ul').hide();
		
		if (mycode == "-1") {
			$('#results').hide();
			$('#results ul').hide();
		}
		else {		
			// mostra gli orari di ritorno disponibili		
			$('#results').show();
			$('#ritorno').show();
				
			$('#ritorno li').hide();
			$('#r-'+mycode).show();		
		}
	});
	
	$('#frm-arrivo, #frm-partenza-2').change(function(){
		mycode = $('#frm-partenza-2').find('option:selected').attr('value');		
		$('#results').hide();
		$('#results ul').hide();
		
		if (mycode == "-1") {
			$('#results').hide();
			$('#results ul').hide();
		}
		else {		
			// mostra gli orari di ritorno disponibili		
			$('#results').show();
			$('#ritorno').show();
				
			$('#ritorno li').hide();
			$('#r-'+mycode).show();		
		}
	});
*/	
	$('#frm-partenza').change(function(){
		mycode = $(this).find('option:selected').attr('value');
		$('#results').hide();
		$('#results ul').hide();
		
		if (mycode == "FA" || mycode == "ANN" || mycode == "ANS") {
			$('#frm-arrivo-sn').hide();
			$('#frm-arrivo-ns').show();
			
			$('#frm-arrivo-sn').removeAttr( "name" );
			$('#frm-arrivo-ns').attr( "name", "arrivo" );
		}
		else {
			$('#frm-arrivo-ns').hide();
			$('#frm-arrivo-sn').show();
			
			$('#frm-arrivo-ns').removeAttr( "name" );
			$('#frm-arrivo-sn').attr( "name", "arrivo" );
		}
		
		// mostra gli orari di andata disponibili		
		$('#results').show();
		$('#andata').show();
			
		$('#andata li').hide();
		$('#a-'+mycode).show();	
	});
	
	$('#frm-arrivo-ns').change(function(){
		gmycode = $('#frm-partenza').find('option:selected').attr('value');
		mycode = $(this).find('option:selected').attr('value');
		$('#andata li .price').hide();
		$('#a-'+gmycode+' .ar-'+mycode).show();
		
	});
	
	$('.prenota #frm-partenza, .prenota #frm-arrivo-sn, .prenota #frm-arrivo-ns').change(function(){
		var partenza_code = $('#frm-partenza').find('option:selected').attr('value');
		
		if( $('#frm-arrivo-ns').is(':visible') ) {
			// it's visible, do something
			var arrivo_code = $('#frm-arrivo-ns').find('option:selected').attr('value');
		}
		else {
			// it's not visible so do something else
			var arrivo_code = $('#frm-arrivo-sn').find('option:selected').attr('value');
		}
		
		
		if (partenza_code == "FA" || arrivo_code == "FA" ) {
			var ul_list = 'disc_orizzontale_falconara';
			$('.disc_orizzontale_falconara').show();
			$('.disc_orizzontale_ancona').hide();
		}
		else if(partenza_code == "ANN" || partenza_code == "ANS" || arrivo_code == "ANN" || arrivo_code == "ANS"){
			var ul_list = 'disc_orizzontale_ancona'; 
			$('.disc_orizzontale_ancona').show();
			$('.disc_orizzontale_falconara').hide();
		}
		
		$('.' + ul_list + ' #a-' + partenza_code).show();
		//console.log('.' + ul_list + ' #a-' + partenza_code);
		
	});
	
	
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	// innerfade
	$('#photo').innerfade({ speed: 750, timeout: 2500, type: 'sequence', containerheight: '226px' }); 
	
	// tabs
	handleTabs();
	
	// form
	handleForm();
	
	// fancybox
	$('.fancybox').fancybox({
		type: 'iframe',
		width: 600,
		height: 500
	});
	
});
