(function(a){
  a.fn.example=function(e,c){
    var d=a.isFunction(e);
    var b=a.extend({},c,{
      example:e
    });
    return this.each(function(){
      var f=a(this);
      var g;
      if(a.metadata){
        g=a.extend({},a.fn.example.defaults,f.metadata(),b)
      }else{
        g=a.extend({},a.fn.example.defaults,b)
      }
      if(!a.fn.example.boundClassNames[g.className]){
        a(window).unload(function(){
          a("."+g.className).val("")
        });
        a("form").submit(function(){
          a(this).find("."+g.className).val("")
        });
        a.fn.example.boundClassNames[g.className]=true
      }
      if(a.browser.msie&&!f.attr("defaultValue")&&(d||f.val()==g.example)){
        f.val("")
      }
      if(f.val()==""&&this!=document.activeElement){
        f.addClass(g.className);
        f.val(d?g.example.call(this):g.example)
      }
      f.focus(function(){
        if(a(this).is("."+g.className)){
          a(this).val("");
          a(this).removeClass(g.className)
        }
      });
      f.change(function(){
        if(a(this).is("."+g.className)){
          a(this).removeClass(g.className)
        }
      });
      f.blur(function(){
        if(a(this).val()==""){
          a(this).addClass(g.className);
          a(this).val(d?g.example.call(this):g.example)
        }
      })
    })
  };

  a.fn.example.defaults={
    className:"example"
  };

  a.fn.example.boundClassNames=[]
})(jQuery);

/***************************************************************************************************
/* mapa
 **/
var Mapki = {
  map: null,
  geocoder: null,
  infoWindow: null,
  directionsService: null,
  directionsDisplay: null,
  marker: null,
  markers: [],
  locations: [],
  cluster: null,
  type: 'place',
  tram: false,
  divPlace: "mapPlaceBox",
  divRoute: "mapRouteBox",
  myplace: "Kraków, Polska"
};
/**************************************************************************************************/

function centerPager()
{
  if (typeof(document.getElementById('pager')) != 'undefined') {
    document.getElementById('pagerBody').style.margin = '0px 0px 0px ' + ((document.getElementById('pager').offsetWidth - document.getElementById('pagerBody').offsetWidth)/2) + 'px';
  }
}
function flipArrow(value) {
  $('#frontBoxArrow').css('display', value);
}


/* 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() {
  /*************************************************************************************************
   * tooltip
   */
  $('.tip').tooltip({
    showURL: false
  });
  /************************************************************************************************/
  $('#searchInput').focus();
  $("a.window").click(function(){
    window.open( $(this).attr('href'), "sociable", "width=800, height=600, toolbar=0, location=0, directories=0, resizable=0");
    return false;
  });
  /*************************************************************************************************
	 * wyszukiwarka
	 */
  $("div.searchMenu a.menu").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();
        });
      }
    });
  /************************************************************************************************/
  $("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');
  /*************************************************************************************************
	 * wyszukiwarka wydarzeń
	 */
  $('#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
  });
  /*************************************************************************************************
	 * wyszukiwarka firm
	 */
  var searchLoaded = false;
  $('#companySearch .searchbranches').click( function() {
    if(searchLoaded == false){
      $.ajax({
        url   : '/api/proxyGet/client/wkrakowie_pl_firmy/name/categoriesForSearch',
        success: function(response) {
          $("#categoriesListing").html(response);
          searchLoaded = true;
        }
      });
    }
    $("#categoriesListing").dialog({
      width: 750,
      height: 550,
      modal: true,
      resizable: false,
      close: function(){$("#categoriesListing").dialog('destroy');}
    });
  });
  /*************************************************************************************************
	 * wyszukiwarka na mapie
	 */
  $("#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(){
    if(Mapki.type !== 'place'){
      Mapki.type = 'place';
      $("#mapRouteBy").fadeOut(100);
      $("#mapRouteWhere").fadeOut(100,function(){
        $("#mapPlaceWhere").fadeIn(100)
      });
      $("#mapRouteTime").fadeOut(100);
    }
  });
  $("#rw2").click(function(){
    if(Mapki.type !== 'route'){
      Mapki.type = 'route';
      $("#mapPlaceWhere").fadeOut(100,function(){
        $("#mapRouteWhere").fadeIn(100,function(){
          $("#mapRouteBy").fadeIn(100);
        });
      });
      if(Mapki.tram == true){
        $("#mapRouteTime").fadeIn(100);
      }
    }
  });
  $("#rb1,#rb2,#rb3").click(function(){
    if(Mapki.tram == true){
      Mapki.tram = false;
      $("#mapRouteTime").fadeOut(100);
    }
  });
  $("#rb4").click(function(){
    if(Mapki.tram == false){
      Mapki.tram = true;
      $("#mapRouteTime").fadeIn(100);
    }
  });
  $("#mapPlaceSearch").submit(function(){
    if(Mapki.type == 'place'){
      Mapki.findPlace($("#placeName").val());
      return false;
    }else{
      $("#departFrom").attr("disabled",false);
      $("#arriveTo").attr("disabled",false);
      return true;
    }
  });
  $("#mapRouteSearch").submit(function(){
    if(Mapki.type == 'route'){
      Mapki.calcRoute();
      return false;
    }else{
      $("#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
  });
  /************************************************************************************************/
  $('#contactFormContainer').hide();
  $('a#contactFormToggle').click(function() {
    $('#contactFormContainer').slideToggle(400);
    return false;
  });
  /*************************************************************************************************
	/* images
	 */
  $('#imageGallery a[rel=foto]').fancybox({});
  $('a.fancybox').fancybox({
    'transitionIn':'elastic',
    'transitionOut':'elastic'
  });
  /*************************************************************************************************
	* font size
	*/
  $('.aaa a').click(function(){
    var oldSize = $(this).parent().find('a.active').attr('rel');
    var newSize = $(this).attr('rel');
    if(oldSize>newSize){
      size = oldSize - newSize;
      $('.main *,.content *').animate({
        fontSize:"-="+size+"px"
      },10);
    }else if(oldSize<newSize){
      size = newSize - oldSize;
      $('.main *,.content *').animate({
        fontSize:"+="+size+"px"
      },10);
    }
    $(this).parent().find('a').removeClass('active');
    $(this).addClass('active');
  });
});

/* ado caption */
function addCaption() {
    var element = '#'+this.orgId+'_nfo';
    $(function(){
	if(element){
	    $(element).addClass("addinfo").html("- reklama -");
	}
    });
}
function noCaption() {
    var element = this.id;
/*console.log('ado not loaded to #'+element);*/
}

