$(document).ready(function(){	
	$('div.scrolled-content').each(function(){
		var newWidth = 0;
		$(this).children().each(function(){
			if ($(this).height()) {
				var thisWidth = $(this).position().left + $(this).outerWidth(true);
				if (thisWidth > newWidth) newWidth = thisWidth;
			}
		});
		$(this).width(newWidth+"px");
		if (this.offsetWidth < this.parentNode.offsetWidth) {
			$('div.scrollbar').remove();
		}
	});
	
	var handleScrollDrag = function(e,ui){
		var $ui = ui.helper;
		var $content = $('.scrolled-content');
		var $holder = $content.parent();
		
		var contentWidth = $content.width() - $holder.width();
		var maxRange = $ui.parent().width() - $ui.width();
		
		var newLeft = Math.round(contentWidth * ($ui.position().left / maxRange));
		$content.css('left',-newLeft+"px");
	};
	
	$('div.scrollbar .handle').draggable({
		containment:'parent',
		axis:'x',
		drag:handleScrollDrag
	});
	
	$('div.scrollbar .arrow').click(function(e){
		var step = 0.1;
		
		e.preventDefault();
		
		var $handle = $('div.scrollbar .handle');
		
		if ($(this).hasClass('arrow-left')) {
			var dir = -1;
		} else dir = 1;
		
		var maxRange = $handle.parent().width() - $handle.width();
		var newLeft = $handle.position().left + (maxRange * step * dir);
		newLeft = (newLeft < 0) ? 0 : (newLeft > maxRange) ? maxRange : newLeft;
		
		$handle.css('left',newLeft+"px");
		handleScrollDrag(null,{helper:$handle});
	});
	
	function ajaxGetCities(countryId) {
		function handleError() {
			$('select#city').attr('disabled',true).children().not(':first').remove();
			$('select#country').get(0).selectedIndex = 0;
			
			alert('Wystąpił błąd przy pobieraniu listy miast!');
		}
		
		function handleSuccess(data) {
			if (data == 'ERROR') handleError();
			else {
				cities = eval('('+data+')');
				var $select = $('select#city');
				$select.attr('disabled',false).children().not(':first').remove();
				for (i in cities) { $select.append('<option value="'+cities[i].id+'">'+cities[i].name+'</option>'); }
			}
		}
		
		var ajaxOptions = {
			url:$url_prefix+'/ajax/getCities/',
			type:'get',
			data:'id='+countryId,
			dataType:'text',
			error:handleError,
			success:handleSuccess
		};
		
		$.ajax(ajaxOptions);
	}
	
	$('select#country').change(function(){
		if (this.value == -1) $('select#city').attr('disabled',true).children().not(':first').remove();
		else {
			ajaxGetCities(this.value);
		}
	});
	
	var datepickerDefaults = {
		showMonthAfterYear: false,
		showAnim: 'slideDown',
		duration:300,
		beforeShow: function(input, inst)
		{
			// centrowanie wzg. inputa
			setTimeout(function(){inst.dpDiv.css({marginLeft: (-(inst.dpDiv.get(0).offsetWidth-input.offsetWidth)/2)+2 + 'px'});},50);
		}
	};	
	
	$.datepicker.setDefaults($.extend(datepickerDefaults, $.datepicker.regional['pl']));
	
	$('input.calendar').datepicker();
	
	function dateBound(obj) {
		try {
			var milis = $(obj).datepicker('getDate').getTime() + 1000*60*60*24;
			var nextDay = new Date();
			nextDay.setTime(milis);
			$("#departure_date").datepicker('option','minDate',nextDay);
		} catch (ex) { }
	}
	$("#arrive_date").datepicker('option','minDate',new Date()).datepicker('option','onSelect',function(){
		dateBound(this);
	}).each(function(){
		dateBound(this);
	});
});
