// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/
(function($){

    // labelize skelin input
    if($.fn.labelizor)
    {
      $('div.pgfoot #l_kt, div.pgfoot #l_pw').labelizor();
      //$('').labelizor();
    }
    
    $('.pgmain, .pgextra2').equalizeHeights();

    //remove flicker trick n' kick
    $('#noflickerCSS').remove();
    $('body').addClass('js-active');

    if (!window.EPLICA_loggedin) {

      // pngfix for IE6
      if ($.fn.ifixpng && $.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      };


      //zebra tables
      $('table:not(.nozebra)').each(function() {
    	  $(this).find('tr:even:not(.populated):not(.hidden_field):not(.clear)').addClass('alt');
      });


      //skelin login slider
      $('div.enav a.skelin:not(.loggedin)')
          .bind('click', function() {
              $('#loginform').slideToggle(350, 'easeInOut');
              return false;
            });


      //fieldset collapser in wizard
      var collapseFields = $('div.wizard fieldset.collapsing');
      collapseFields
          .find('div.checkbar input:checkbox')
              .each(function() {
                  $(this)
                      .bind('click', function (e) {
                          $(this).parents('fieldset.collapsing').eq(0)[this.checked ? 'addClass' : 'removeClass']('collapse-open').find('div.fields').slideToggle(350, 'easeInOut');
                        })
                      .filter(':not(:checked)').parents('fieldset.collapsing').find('div.fields').hide().end().end().end()
                      .filter(':checked').parents('fieldset.collapsing').eq(0).addClass('collapse-open').find('div.fields').slideDown(350, 'easeOut');
                });
            



      if (window.datePicker)
      {
        datePicker.defaultCssFile = '/js/eplicacommon/datepicker/1.0/dp.css';
        datePicker.defaults.dateFormat = '%dd.%mm.%yyyy';
        datePicker.openOnFieldClick=false;
        datePicker.init();
      }



      //popup blocks
      $('a.popup:not(.popup-active)').popup();


      //selectbox datachanger
      $('div.datachanger').each(function(){
          var datachanger = this;
          $('select', this)
              .bind('change', function(){
                  var i = this.selectedIndex;
                  $('thead th:gt(0), td', datachanger).addClass('hidden');
                  $('thead th:eq('+(i+1)+')', datachanger).removeClass('hidden');
                  $('tbody tr', datachanger).each(function(){
                      $('td:eq('+i+')', this).removeClass('hidden');
                    }); 
                })
              .trigger('change');
        });



      //enable click on table rows
      $('div.popupblock table tbody, table.clickablecells tbody')
          .bind('click', function (e) {
              var tr = $(e.target).closest('tr');
              if (tr.length)
              {
            	processIndicator.show();
      			setTimeout(function(){window.location=tr.find('td a').attr('href');},500);
                return false;
              }
            });



      //hover class for basket
      $('div.karfan div.boxbody > div')
          .bind('mouseenter', function() {
              $(this).addClass('hover');
            })
          .bind('mouseleave', function() {
              $(this).removeClass('hover');
            });


      //Article collapser
      var collapsearticle = $('.collapse');
      $('h4', collapsearticle).each(function() {
          var heading = $(this);
          heading.wrapInner('<a href="#" />');
          heading.add( heading.nextUntil('h2, h4') ).wrapAll('<div class="collapsebox" />');
        });
      $('.collapsebox', collapsearticle)
          .each(function() {
              var collapsebox = $(this);
              collapsebox.find('> *:not(h4)').wrapAll('<div class="collapsecontent" />').parent().hide();
              collapsebox
                  .find('h4 a').bind('click', function() {
                      collapsebox.find('.collapsecontent').slideToggle(100);
                      collapsebox.toggleClass('collapse-open');
                      return false;
                    });
            });


    }; // end notloggedin
    
    
        //Styling checkboxes
    $('body').addClass('js-active');
    
    var label = $('.home .fi_chk label'),
          chk = $('.home .fi_chk input');
          
      
        $('.home .fi_chk input:checked').each(function(){
            $(this)
              .next()
                .css('background-position', '0 -68px');
          });

    chk.focus(function(){
        if (!$(this).is(':checked')) {
          $(this)
            .next()
              .css('background-position', '0 -34px');
        } else {
          $(this)
            .next()
              .css('background-position', '0 -102px');
        }
      });
      
    chk.blur(function(){
        if (!$(this).is(':checked')) {
          $(this)
            .next()
              .css('background-position', '0 0');
        } else {
          $(this)
            .next()
              .css('background-position', '0 -68px');
        }
      });
      
    chk.bind('click', function(){
        if ($(this).is(':checked')) {
          $(this)
            .next()
              .css('background-position', '0 -102px');
        } else {
              $(this)
                .next()
                  .css('background-position', '0 -34px');
            }
      });

    /*
    
    //select box pretty
    var selectbox = $('.selectbox');
           
    $('.fi_sel').prepend('<span class="selectbox"></span>')
    var sbheight = $('.selectbox').height(),
           sbwidth = $('.selectbox').width();
    $('.fi_sel select').change(function(){
        var seltext = "";
          $('select option:selected').each(function(){
              seltext += $(this).text();
            });
            $('.selectbox').text(seltext);
      })
        .change()
          .css({
            width: sbwidth,
            height: sbheight,
            margin: '-'+sbheight+'px 0 0',
            opacity: '.001'
            });*/


    processIndicator.defaults.is="";
    processIndicator.defaults.en="";
    
  //tablerowcollapse
	if(window.tablerowcollapse)
	{
		tablerowcollapse.init();
	}
	
	// Focus first element
	$.fn.focus_first = function() {
    //$('input:visible, select:visible, textarea:visible').eq(0).focus(); ?
    
	  var elem = $('input:visible', this).get(0);
	  var select = $('select:visible', this).get(0);
	  if (select && elem) {
	    if (select.offsetTop < elem.offsetTop) {
	      elem = select;
	    }
	  }
	  var textarea = $('textarea:visible', this).get(0);
	  if (textarea && elem) {
	    if (textarea.offsetTop < elem.offsetTop) {
	      elem = textarea;
	    }
	  }
	  
	  if (elem) {
	    elem.focus();
	  }
	  return this;
	};

})(jQuery);

