$(document).ready(function()
{
  $("#query_contractor_type_1").bind('click', function() {
    companyNameShowHide();
  });
  $("#query_contractor_type_2").bind('click', function() {
    companyNameShowHide();
  });

  $("#queryForm input, #queryForm select").bind('click', function() {
    $.ga.load('UA-628880-2', function(pageTracker) {
      pageTracker._trackPageview('/form_click');
    });
  });

  if(!$("#query_contractor_type_1:checked").val() == 1 && !$("#query_contractor_type_2:checked").val() == 1)
    $("#query_contractor_type_1").attr('checked', true);

  $("#query_expected_date").datepicker({minDate: +1, showAnim: 'fold'}, $.datepicker.regional['pl']);

  $("#fileInput").uploadify({
    'uploader'       : '/js/uploadify.swf',
    'script'         : '/upload',
    'folder'         : false,
    'cancelImg'      : '/images/action_stop.gif',
    'hideButton'     : true,
    'wmode'          : 'transparent',
    'width'          : 70,
    'height'         : 27,
    'queueID'        : 'fileQueue',
    'scriptData'     : {"PHPSESSID" : "2pcfg759ekfu920h829d7ru6p2", "token" : $("#query_token").val()},
    'auto'           : true,
    'multi'          : true
  });   

  $.ajax
  ({
    type:   'POST',
    url:    '/query/refreshLanguageFromSelect',
    data:   'sid=1&selected='+$("#query_language_from").val(),
    success: function(result){$("#query_language_from").html(result);}
  });
  $.ajax
  ({
    type:   'POST',
    url:    '/query/refreshLanguageToSelect',
    data:   'id='+$("#query_language_from").val()+'&sid='+$("#query_service").val(),
    success: function(result)
    {
      $("#unassociated_query_language_to").html(result);
      $("#query_language_to > option").each(
         function()
         {
           var id = $(this).attr('value');
           $(this).remove();
           sfDoubleList.moveone('unassociated_query_language_to', 'query_language_to', id);
         });
      emptyCheck('query_language_to');
      emptyCheck('unassociated_query_language_to');
    }
  });
  $("#unassociated_query_language_to").bind("dblclick", function(){
    sfDoubleList.moveone('unassociated_query_language_to', 'query_language_to', $(this).attr("value"));
  });
  $("#query_language_to").bind("dblclick", function(){
    sfDoubleList.moveone('query_language_to', 'unassociated_query_language_to', $(this).attr("value"));
  });

  $("#www_tr").hide();

  $("#query_expected_format").qtip
  ({
    position: 
    {
      corner: 
      {
        target: 'topRight',
        tooltip: 'middleLeft'
      },
      adjust:
      {
        x: 10
      }
    },
    style:
    {
      border: 
      {
        width: 1,
        radius: 4,
        color: '#D19405'
      },
      background: '#FECE2F',
      color: 'black',
      padding: 4, 
      textAlign: 'center',
      title:
      {
        font: '11px'
      }
    },
    show: 
    {
      delay:  0,
      when:   'click',
      effect: 'slide'
    },
    hide: 
    {
      when:   {event: 'unfocus'},
      effect: 'slide'
    }
  });
});

function emptyCheck(srcId)
{
  var empty = 1;
  $("#"+srcId+" > option").each(
   function()
   {
     var id = $(this).attr('value');
     if(id > 0)
     {
       empty = 0;
       return false;
     }
   });
  if(empty == 1)
    $("#"+srcId).html("<option value=\"0\" disabled=\"disabled\">brak</option>");
}

function companyNameShowHide()
{
  if($("input[@name='query_contractor_type']:checked").val() == 1)
    $("#company_name_tr").show();
  else
    $("#company_name_tr").hide();
}