//$(document).ready(function(){
//  form_ajax_start('');
//});

function form_ajax_start(sub) {
  $(sub + ' form').each(function() {
    if ($(this).attr('trgt')) {
      $(this).ajaxForm({
        target: $(this).attr('trgt'),
        success: form_response
      });

    }
  });
  $('.button:not(disabled)').button();
  $('.disabled, .selected').button({
    disabled: true
  });
}

function form_response(responseText, statusText, xhr, $form)  {
  $('.'+$form.attr('class')).ajaxForm({
    target: $form.attr('trgt'),
    success: form_response
  });
  $('.button:not(disabled)').button();
  $('.disabled, .selected').button({
    disabled: true
  });
}

function submit_ajax(where,params,target) {
   $.get(where, params, function(data, textStatus) {
      $(target).html(data);
      form_ajax_start(target);
    }, 'html');
    return false;
}
function submit_ajax_post(where,params,target) {
   $.post(where, params, function(data, textStatus) {
      $(target).html(data);
      form_ajax_start(target);
    }, 'html');
    return false;
}

function open_dialog() {
  $('.global_box').remove();
  $('.dialog_box').remove();
  $('.schema').append('<div class="global_box"></div><div class="dialog_box"><img class="wait" src="/public/images/wait.gif" alt="Wait..."/></div>');
}
function close_dialog() {
  $('.global_box').remove();
  $('.dialog_box').remove();
}

function setup_form(c) {
  $('.tip',c).prepend('<img alt="?" src="/public/images/tip.png" onmouseover="tip_show(this);" onmouseout="tip_hide(this);">');
  $('.element > :text',c).focus( function() { element_focus(this);}).blur( function() { element_blur(this);}).each( 
    function () { 
      if ($(this).val() == "" || $(this).val() == '   --   wpisz wartość   --    ') {
        $(this).val('   --   wpisz wartość   --    ').attr('set',0); 
      } else {
        $(this).attr('set',1); 
        if ($(this).hasClass('value')) {
          var x = $(this).val().replace(/[^0-9]/g, "");
          if (x.length > 0) {
            x = x.replace(/[^0-9]/g, "").split('').reverse().join('')
                .match(/\d{3}|\d{1,2}/g)
                .join(' ')
                .split('').reverse().join('');
                $(this).val(x);
          } else {
            $(this).val('   --   wpisz wartość   --    ').attr('set',0);
          }
        }
      }
    } );
}
function tip_show(o) {
  $(o).next().show();
}
function tip_hide(o) {
  $(o).next().hide();
}
function element_focus(o) {
  if ($(o).attr('set') == 0) {
    $(o).val('');
  }
}
function element_blur(o) {
  if ($(o).val() != "") {
    $(o).attr('set',1);
    if ($(o).hasClass('value')) {
      var x = $(o).val().replace(/[^0-9]/g, "");
      if (x.length > 0) {
        x = x.replace(/[^0-9]/g, "").split('').reverse().join('')
            .match(/\d{3}|\d{1,2}/g)
            .join(' ')
            .split('').reverse().join('');
            $(o).val(x);
      } else {
        $(o).val('   --   wpisz wartość   --    ').attr('set',0);
      }
    }
  } else {
    $(o).val('   --   wpisz wartość   --    ').attr('set',0);
  }
}

function showhide(ob,list) {
  if ($(ob).is(':checked')) {
    for(i = 0; i < list.length; i++) {
      $('#'+list[i]).show();
    }
  } else {
    for(i = 0; i < list.length; i++) {
      $('#'+list[i]).hide();
    }
  }
}

function start_hover_left() {
  $('.hover_div1').children().unbind('hover').hover(
  function() {
    $(this).animate({ 'left': '-100px'},'fast')
  },
  function() {
    $(this).animate({ 'left' : '-35px'},'fast');
  }
  );
}
var defmap, defid;
function start_map() {
  defmap = '/public/images/map/mapa.png';
  defid = '';
  $('#kmap').attr('src',defmap);
  $('.map').hover(
  function() {
    $('#kmap').attr('src','/public/images/map/mapa' + $(this).attr('nr') + '.png');
    $('#ad' + defid).hide();
    $('#ad' + $(this).attr('nr')).show();
  }, 
  function() {
    $('#kmap').attr('src',defmap);
    $('#ad' + $(this).attr('nr')).hide();
    $('#ad' + defid).show();
    
  }
  ).click( function() {
    defmap = '/public/images/map/mapa' + $(this).attr('nr') + '.png';
    defid = $(this).attr('nr');
  });
  
}


