/**
 * Custom Javascript
 **/

$(function(){

	/* SLIDER
	-------------------------------------------------- */
	$('.slider ul.scrolling').jcarousel({
    	wrap: "circular",
        auto: 5,
        scroll: 1,
        animation: 1000
	});       

	/* TABS
	-------------------------------------------------- */
	var tab = $('.tab');
	var tabNav = $('ul', tab);
	var tabDivs = $('.tabs', tab);
	
	tabDivs.children('div').hide();
	
	$( $('li.current a', tabNav).attr('href') ).show().addClass('showing');  
	
	$('a', tabNav).click(function(){
		var id = $(this).attr('href');
		var thisLink = $(this);
		
		$('li.current', tabNav).removeClass('current');
		$(this).parent().addClass('current');
		
		$('.showing', tabDivs).fadeOut(200, function(){
			$(this).removeClass('showing');
			$(id).fadeIn(200).addClass('showing');
		});
		
		return false;
	});   

	/* COLOR PICKER
	-------------------------------------------------- */
	
	pickerArrivo();
	pickerPartenza();
	
	function pickerArrivo() {
	
		var today = new Date();
		var start_day = today.getDate() + '/' + (today.getMonth()+1) + '/' + today.getFullYear();
		var end_day = '31/12/' + (today.getFullYear()+5);     
		$('#select-arrivo .date-picker')		
			.datePicker(
				// associate the link with a date picker
				{
					createButton: false,
					startDate: start_day,
					endDate:end_day
				}
			).bind(
				// when the link is clicked display the date picker
				'click',
				function()
				{
					updateSelects($(this).dpGetSelected()[0]);
					$(this).dpDisplay();
					return false;
				}
			).bind(
				// when a date is selected update the SELECTs
				'dateSelected',
				function(e, selectedDate, $td, state)
				{                                     
// 					var datePartenza = selectedDate.getDate() + '/' + (selectedDate.getMonth()+1) + '/' + selectedDate.getFullYear();
// 					$('#select-partenza .date-picker').datePicker({startDate: datePartenza, createButton: false});
					updateSelects(selectedDate);        
  					pickerPartenza(); 
				}
			).bind(
				'dpClosed',
				function(e, selected)
				{                      
					updateSelects(selected[0]);   
				}
			);      
		
		var updateSelects = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);        
			$('#select-arrivo option:selected').removeAttr('selected');
			$('#giorno-arrivo option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#mese-arrivo option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#anno-arrivo option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		// listen for when the selects are changed and update the picker
		$('#giorno-arrivo, #mese-arrivo, #anno-arrivo')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#anno-arrivo').val(),
								$('#mese-arrivo').val()-1,
								$('#giorno-arrivo').val()
							);
					$('#select-arrivo .date-picker').dpSetSelected(d.asString());
				}
			);
		
		// default the position of the selects to today
		updateSelects(today.getTime());
		
		// and update the datePicker to reflect it...
		$('#giorno-arrivo').trigger('change');
	
	}
	
	function pickerPartenza() {
	
		var today = new Date();
		var datePartenza = new Date( $('#anno-partenza').val(), $('#mese-partenza').val()-1, $('#giorno-partenza').val() ); 
		var dateArrivo = new Date( $('#anno-arrivo').val(), $('#mese-arrivo').val()-1, $('#giorno-arrivo').val() );
		var start_day = dateArrivo.getDate() + '/' + (dateArrivo.getMonth()+1) + '/' + dateArrivo.getFullYear();
		var end_day = '31/12/' + (today.getFullYear()+5);  
		 
		$('#select-partenza .date-picker')		
			.datePicker(
				// associate the link with a date picker
				{
					createButton: false,
					startDate: start_day,
					endDate:end_day
				}
			).bind(
				// when the link is clicked display the date picker
				'click',
				function()
				{
					updateSelects($(this).dpGetSelected()[0]);
					$(this).dpDisplay();
					return false;
				}
			).bind(
				// when a date is selected update the SELECTs
				'dateSelected',
				function(e, selectedDate, $td, state)
				{                           
					updateSelects(selectedDate);
				}
			).bind(
				'dpClosed',
				function(e, selected)
				{
					updateSelects(selected[0]);
				}
			);      
		
		var updateSelects = function (selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$('#select-partenza option:selected').removeAttr('selected');
			$('#giorno-partenza option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#mese-partenza option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#anno-partenza option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
		
		if ( datePartenza.getTime() < dateArrivo.getTime() )
			updateSelects(dateArrivo);
		else
			updateSelects(datePartenza);
		
		// listen for when the selects are changed and update the picker
		$('#giorno-partenza, #mese-partenza, #anno-partenza')
			.bind(
				'change',
				function()
				{
					var d = new Date(
								$('#anno-partenza').val(),
								$('#mese-partenza').val()-1,
								$('#giorno-partenza').val()
							);
					$('#select-partenza .date-picker').dpSetSelected(d.asString());
				}
			);
		
		// and update the datePicker to reflect it...
		$('#giorno-partenza').trigger('change');
	
	}
	
});
