/* event search function */
var evDate;
function eventShowHideDates()
{
  if($('#evWhen').attr('checked') == 1)
  {
    evDate = $('#evDate').val();
    $('#evDate').val('').removeClass('ui-state-active').addClass('ui-state-disabled');
    $('input[name=eda]').val('active');
  }
  else
  {
    $('#evDate').val(evDate).addClass('ui-state-active').removeClass('ui-state-disabled');
    $('input[name=eda]').val('inactive');
  }
}
function eventUpdateDates(){
  var eds = new Date( $("#evDate").datepicker( "getDate" ) );
  $('#eventSearch input[name=eds]').val(eds.getTime()/1000);
}
/* end */

$(function(){
	$("div.view label").addClass("ui-button-text-only");
	$("div.view span").attr("style","float:left;");
	$("div.view .ui-button-icon-primary").attr("style","float:left;margin-top:4px;");
	$('#eventSearch div.radios').buttonset();
	$('#eventSearch div.all').buttonset();
	$('#articleSearch .text input[type=text], #eventSearch .text input[type=text],#eventSearch div.kiedy input[type=text]').addClass('ui-button ui-widget ui-state-active');
	$('#eventSearch').submit(
		function(){
			eventUpdateDates();
		});
	$('#eventSearch input.kiedy').click(
		function()
		{
			if($('#evWhen').attr('checked') == 1)
			{
				$('#evWhen').attr('checked', false).removeClass('ui-state-active').addClass('ui-state-disabled').button( "refresh" );
				eventShowHideDates();
			}
		});
	$('#evWhen').click(function(){
		eventShowHideDates();
	});
	if($('#evWhen').attr('checked') == 1)
	{
		eventShowHideDates();
	}
  $( "#evDate" ).datepicker({
    showAnim:'fadeIn',
    showOn:'focus',
    dateFormat:'dd.mm.yy',
    defaultDate:"+1w",
    changeMonth:false,
    changeYear:true,
    numberOfMonths:2,
    showButtonPanel: true,
    showOtherMonths: true,
    selectOtherMonths: true
  });
	$("div.searchMenu a").click(
		function()
		{
			var last = $(this).parent().parent().find("a.selected");
      var href = $(this).attr('rel');
      if(last.index() !== $(this).index()){
        $(this).parent().find("a").removeClass('selected');
        $(this).addClass('selected');
        $(this).parent().parent().find("div.searchTabs:eq("+last.index()+")").hide(0,function()
        {
          $("#"+href+"").show();
        });
      }
		});

	$("#routeSwap").button({
		icons:{
			primary:"ui-icon-transferthick-e-w"
		},
		text:false
	}).click(function(){
		var from = $("#departFrom").val();
		var to = $("#arriveTo").val();
		$("#departFrom").val(to);
		$("#arriveTo").val(from);
	});
	$("#rw1").click(function(){
		$("#mapRouteBy").fadeOut(100);
		$("#mapRouteWhere").fadeOut(100,function(){
			$("#mapPlaceWhere").fadeIn(100)
		});
		$("#mapRouteTime").fadeOut(100);
	});
	$("#rw2").click(function(){
		$("#mapRouteBy").fadeIn(100);
		$("#mapPlaceWhere").fadeOut(100,function(){
			$("#mapRouteWhere").fadeIn(100)
		});
	});
	$("#rb1,#rb2,#rb3").click(function(){
		$("#mapRouteTime").fadeOut(100);
	});
	$("#rb4").click(function(){
		$("#mapRouteTime").fadeIn(100);
	});
	$("#mapPlaceSearch").submit(function(){
		$("#departFrom").attr("disabled",false);
		$("#arriveTo").attr("disabled",false);
		return true;
	});
	$("#mapRouteSearch").submit(function(){
		$("#departFrom").attr("disabled",false);
		$("#arriveTo").attr("disabled",false);
		return true;
	});
	$("#mapGoSearch").submit(function(){
		$("#departFrom").attr("disabled",false);
		$("#arriveTo").attr("disabled",false);
		return true;
	});
	$("#routeTime").change(function(){
		switch($(this).val()){
			default:
				$("#routeDate").fadeIn();
				break;
			case 'now':
				$("#routeDate").fadeOut();
				break;
		}
	});
	$("#routeDate").datepicker({
		showTime:true,
		time24h:true,
		stepMinutes: 5
	});

	$('a.fancyoferta').fancybox({
		'imageScale': false,
		'centerOnScroll': false
	});
});
