var today2 = new Date();
var today = new Date();
var current_Day = today.getDate();
var current_Month = today.getMonth();
var current_Year = today.getFullYear();

var scrolling = null;
var height= 0;
var limit = 0;


function scrollStop()
{
	window.clearInterval(scrolling);
}

function scrollUp()
{
	if (parseInt($('#scroller_destination ul li').css('top'))*(-1) > 0)
	{
		$('#scroller_destination ul').children().animate({top: '+='+height+'px'}, 70, 'linear');
	}
}

function scrollDown()
{
	if (parseInt($('#scroller_destination ul li').css('top'))*(-1) < limit)
	{
		$('#scroller_destination ul').children().animate({top: '-='+height+'px'}, 70, 'linear');
	}
}

function bindScrollerOpen()
{
	$('#qs_destinationlistIcon').bind('click', scrollerOpen);
}

function scrollerOpen()
{
	$('#qs_destinationlistIcon').unbind('click');
	
	$('#qs_duration').hide();
	$('#qs_price').hide();
	
	$('#scroller_wrap').slideDown(100);
	$('#scroller_destination').slideDown(100);
	
	$('#qs_destinationlistIcon').bind('click', scrollerClose);
}

function scrollerClose()
{
	$('#qs_destinationlistIcon').unbind('click');
	
	$('#scroller_wrap').slideUp(100);
	$('#scroller_destination').slideUp(100);
	
	$('#qs_duration').show();
	$('#qs_price').show();
	
	// timeout is needed, because event is still active
	window.setTimeout(bindScrollerOpen, 100);
}

$(document).ready(function() 
{
	// ----------------------------------------
	// common settings
	// ----------------------------------------
	
	// clear date input if it has default value
	$('#qs_startdate').focus(function(){
			if ($(this).val() == 'beliebig'){
				$(this).val('');
			}
	});
	$('#qs_enddate').focus(function(){
			if ($(this).val() == 'beliebig'){
				$(this).val('');
			}
	});
	
	// insert default value if field is empty
	$('#qs_startdate').blur(function(){
			if ($(this).val() == ''){
				$(this).val('beliebig');
			}
	});
	$('#qs_enddate').blur(function(){
			if ($(this).val() == ''){
				$(this).val('beliebig');
			}
	});
	
	// ----------------------------------------
	// scroller
	// ----------------------------------------
	
	// height of one row in scroller
	height = $('#scroller_destination ul li').height();
	
	// number of visible rows in scroller
	// --> cannot be computed, because IE always uses complete height with all options
	//numvisible = Math.round( $('#scroller_destination ul').height() / height );
	numvisible = 8;
	
	// maximum value for shifting the list (scrolling effect)
	numoptions = $('#scroller_destination ul').children().length;
	limit = (numoptions - numvisible) * height;
	
	// on load: hide scroller
	$('#scroller_wrap').hide();
	$('#scroller_destination').hide();
	$('#scroller_destination').css('visibility', 'visible');
	
	// hover effect for buttons
	$('.hover_effect').hover(
		function() { $(this).css('background-position', '0 -'+$(this).css('height')); }, 
		function() { $(this).css('background-position', '0 0'); }
	);
	
	// scrolling functionality for buttons
	$('#btnUp').mousedown(function(){
		$(this).css('background-position', '0 -'+$(this).css('height'));
		scrollUp();
		scrolling = window.setInterval(scrollUp, 100); 
	}).mouseup(function() { 
		$(this).css('background-position', '0 0');
		scrollStop();
	});
	
	$('#btnDown').mousedown(function(){ 
		$(this).css('background-position', '0 -'+$(this).css('height'));
		scrollDown();
		scrolling = window.setInterval(scrollDown, 100);
	}).mouseup(function() { 
		$(this).css('background-position', '0 0');
		scrollStop();
	});
	
	
	// highlight list options on hover
	$('#scroller_destination ul li').hover(
		function() { $(this).addClass('scroller_highlight'); }, 
		function() { $(this).removeClass('scroller_highlight'); }
	);
	
	// use value if selected by mouseclick
	$('#scroller_destination ul li').bind('click', function(){
		$('#destinationInput').val( $(this).html() );
		$('#scroller_destination ul li').removeClass('scroller_selected');
		$(this).addClass('scroller_selected');
		
		// remove error if set
		$('#destinationInput').removeClass('error');
		$('#error_destination').hide();
		$('#checkDestination').val('true');
		
		scrollerClose();
	});
	
	// click event for icon to show/hide
	bindScrollerOpen();
	
	// close scroller if clicked anywhere on document
	// except if clicked the close button or an option in the list
	$(document).mousedown(
		function(event){
			var $target = $(event.target);
			var $parents = $target.parents();
			
			// test IE scrollbar
			if (typeof($target[0]) != 'object' || typeof($parents[0]) != 'object'){
				return;
			}

			if ( ($target[0].id != 'btnUp') 
			  && ($target[0].id != 'btnDown') 
			  && ($target[0].id != 'qs_destinationlistIcon') 
			  && ($parents[0].id != 'scroller_destination_list') ){
				scrollerClose();
			}
	});
	
	
	// ----------------------------------------
	// datepicker
	// ----------------------------------------
	
	// SET min and max date for datepicker
	var objDateStart 	= checkDateFormat($('#qs_startdate').val());
	var objDateEnd 		= checkDateFormat($('#qs_enddate').val());
	
	$('#qs_enddate').change(function() {
		if($('#qs_enddate').val() != '') {
			objDateEnd = checkDateFormat($('#qs_enddate').val());
		}
		if ( $('#qs_enddate').val() == 'beliebig' || $('#qs_enddate').val() == '' ) {
			objDateEnd = checkDateFormat(strMaxStartdate); 
		}
	});

	$('#qs_startdate').change(function() {
		if($('#qs_startdate').val() != '') {
			objDateStart = checkDateFormat($('#qs_startdate').val());
		}
		if ( $('#qs_startdate').val() == 'beliebig' || $('#qs_startdate').val() == '' ) {
			objDateStart = new Date(current_Year, current_Month, current_Day); 
		}
	});
	
	
	// DATEPICKER: startdate
	$('#qs_startdate').datepicker({
		
		beforeShow: function() {
			$(this).datepicker("option", { 
				minDate: new Date(current_Year, current_Month, current_Day), // always today
				// maxdate: valid endDate given => enddate -1  ||  otherwise maxDate from component
				//maxDate: ( ( (typeof(objDateEnd) == 'object') && (objDateEnd > today) ) ? new Date(objDateEnd.getFullYear(), objDateEnd.getMonth(), (objDateEnd.getDate() - 1)) : checkDateFormat(strMaxStartdate) )
				// CHANGED: maxdate is always last bookable date (false input allowed -> start after end)
				maxDate: checkDateFormat(strMaxStartdate)
			})
		 },
		 
		showAnim: 'slideDown'
	});
	
	// DATEPICKER: enddate
	$('#qs_enddate').datepicker({
		
		beforeShow: function() {
			$(this).datepicker("option", {
				// mindate: valid startDate given => startdate +1  ||  otherwise today +2
				minDate: ( ( (typeof(objDateStart) == 'object') && (objDateStart > today) ) ? new Date(objDateStart.getFullYear(), objDateStart.getMonth(), (objDateStart.getDate() + 1)) : new Date(current_Year, current_Month, current_Day) ),
				maxDate: checkDateFormat(strMaxStartdate)
			})
		},
		
		showAnim: 'slideDown'
	});
	
	//hover states on the static widgets
	$('ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
});
