//imgRoller control
var p=0;
var posun=130;
var pmax=10000;

function left() {
	if (p+posun>=pmax) p=pmax;
		else p=p+posun;
	//alert(p);
	$("#imgRoller").scrollLeft(p).slow();
}
function right() {
	if (p-posun<=0) p=0;
		else p=p-posun;
	//alert(p);
	$("#imgRoller").scrollLeft(p);
}

//jQuery
$(document).ready(function() {
	$(".conPreview").each(function(index) {
		//alert(index + ': ' + $(this).attr("title"));
		$(this).mouseover(function() {
			var p=$(this).offset();
			$("#conPreview").css({"top":p.top,"left":p.left-255, "border":"1px solid black"}).html("<img src=\"img/con_room_preview_"+$(this).attr("id")+".jpg\" width=\"240\" />").fadeIn("Slow");
		  }).mouseout(function(){
			$("#conPreview").html("").css({"left":"0", "border":"none"});
		  });
	});
	
	$("#saly a.td").click(function(event) {
		event.preventDefault();
		$("input[name='roomNameType']").val($(this).attr("id").substr(1));
		//alert($(this).attr("id"));
		$("#reservation").submit();
	});
	
	$("input[name='capacity']").keyup(function() {
	  if($("#c"+$("input[name='roomNameType']").val()).text()<parseInt($("input[name='capacity']").val())) $("#capacityErr").text("příliš mnoho osob na vybraný sál!! Maximální kapacita je "+$("#c"+$("input[name='roomNameType']").val()).text()+" osob");
		else $("#capacityErr").text("");
	});
	
	$(function() {
		var dates = $( "#from, #to" ).datepicker({
			minDate: '+1d',
			maxDate: '+6m',
			beforeShow: function(input, inst) {
				$(".slider-range").slider({ disabled: true });
			},
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
				
				// if(this.id == "from") {
					// date2 = $.datepicker.parseDate(
						// instance.settings.dateFormat ||
						// $.datepicker._defaults.dateFormat,
						// selectedDate+1, instance.settings );
						// dates.not( "#to" ).datepicker( "option", "maxDate", date2 );
				// }
				// $( ".slider-range" ).show;
				$(".slider-range").slider({ disabled: false });
				$("#reservation").submit();
			}
			
		});
		
		//if($("#from").val()=="") $("#rPotvrdit").attr("disabled","disabled");
		//if($("#to").val()=="") $("#rPotvrdit").attr("disabled","disabled");
		
	});
	
	
	$( ".slider-range" ).each(function() {
		var value=$("input[class~="+$(this).attr("id")+"]").val();
		var t1 = makeValue(value,1);	//parseInt( value, 10 );
		var t2 = makeValue(value,2);	//parseInt( $.trim(value.substr(7)), 10 );
		// alert(t1+" : "+t2+" "+$.trim(value.substr(7)));
		$( this ).empty().slider({
			values: [t1, t2],
			range: true,
			step: .5,
			min: 7,
			max: 23,
			animate: true,
			slide: function( event, ui ) {
				if(ui.values[0]<ui.values[1]) $("input[class~="+$(this).attr("id")+"]").val(makeTime(ui.values[0])+" - " + makeTime(ui.values[1]));
			},
			change: function( event, ui ) {
				$("#reservation").submit();
			}
		});
	});
	
	//lightbox
	$(function() {
		$('#gallery a').lightBox();
		$('#imgs a').lightBox();
	});
	
});

function makeValue(tm,t) {
	if(t==1) {
		v=$.trim(tm.substr(0,5));
		if(v.length==4) {
			c=parseInt(v.substr(0,1), 10 );
			d=parseInt(v.substr(2,2), 10 )/100;
		} else {
			c=parseInt(v.substr(0,2), 10 );
			d=parseInt(v.substr(3,2), 10 )/100;
		}
		d=d>0?.5:0;
		v=c+d;
	} else {
		v=$.trim(tm.substr(7,6));
		if(v.length==4) {
			c=parseInt(v.substr(0,1), 10 );
			d=parseInt(v.substr(2,2), 10 )/100;
		} else {
			c=parseInt(v.substr(0,2), 10 );
			d=parseInt(v.substr(3,2), 10 )/100;
		}
		d=d>0?.5:0;
		v=c+d;
	}
	return v;
}

function makeTime(tm) {
	var hour=parseInt(tm);
	tm-= parseInt(tm); 
	tm*= 60;
	var min=parseInt(tm)+"0";
	
	return hour+":"+min.substr(0,2);
}



