function log(e){
    //return false;
    if (typeof(console) == 'object') 
        console.log(e);
    //else alert(e);  
};

var rememberValue = {
    config: {
        rememberEl: '.rememberValue',
        values: new Array()
    },
    init: function(){
        $(rememberValue.config.rememberEl).focus(function(){
            rememberValue.name = $(this).attr('name');
            if (rememberValue.config.values[rememberValue.name] == null || $(this).val() == rememberValue.config.values[rememberValue.name]) {
                rememberValue.config.values[rememberValue.name] = $(this).val();
                $(this).val('').addClass('filled');
            }
        }), $(rememberValue.config.rememberEl).blur(function(){
            rememberValue.name = $(this).attr('name');
            if ($(this).val() == '') {
                $(this).val(rememberValue.config.values[rememberValue.name]).removeClass('filled');
            }
        })
    }
};

function dropDownSelectCallback(el){
    el.parent().parent().children().each(function(){
        if ($(this).find('input').is(':checked')) {
            $(this).addClass('ui-dropdownchecklist-item-selected');
        }
        else {
            $(this).removeClass('ui-dropdownchecklist-item-selected');
        }
        $(this).filter(function(){
            return $(this).is(':last-child');
        }).addClass('lastitem');
    });
}

//--------------------------------------------------------------------------

$(document).ready(function () {

  /**
  * EXTERNAL LINKS
  */
  $('#content a[href^="http"]:not([href*=' + window.location.host + ']),#content a[href^="https"]:not([href*=' + window.location.host + '])').filter(function () {
    return !$(this).find('img').length;
  }).addClass('external');

  /**
  * INPUT IMAGE CLICK FIX
  */
  $('#search *:image').click(function () {
    $(this).parents('form').submit();
    return false;
  });

  // REMEBER INPUT VALUE
  rememberValue.init();

  /**
  * IE6 MENU HOVER
  */
  $('#menu>li').hover(function () {
    $(this).addClass('hover');
  }, function () {
    $(this).removeClass('hover');
  });

  /**
  * INDEXES ROTATION
  */
  if ($('#indexesBar .inner ul li').size() > 0) {
    $('#indexesBar').addClass('carousel').find('.inner').scrollable({
      circular: true
    }).autoscroll({
      interval: 7000
    });
  }
  /**
  * BOX TABS
  */
  $(".box .tabs a").click(function () {
    var targetId = $(this).blur().attr('href');
    if (targetId && targetId.match(/\#.+/)) {
      $(this).addClass('selected').siblings().removeClass('selected');
      $(targetId).siblings('.content').hide();
      $(targetId).show();
      return false;
    }
  });
  $(".box .tabs a:first").click();

  /**
  * BOX CONTENT EQUAL HEIGHT
  */
  $(".box").each(function () {
    maxheight = 0;
    if ($(this).find('.content').length > 1) {
      $(this).find('.content').each(function () {
        if ($(this).height() > maxheight) {
          maxheight = $(this).height();
        }
      })
      $(this).find('.content').height(maxheight);
    }
  });

  /**
  * OL TRANSFORMATIONS
  */
  $('ol').each(function () {
    $(this).addClass('transformed').children('li').each(function (i) {
      $(this).addClass('cleaned').wrapInner('<div class="ct"></div>').prepend('<span class="no">' + (i + 1) + '.</span>');
    });
  });

  /**
  * Calendars
  */
  // Common config
  var calConfigBase = {
    onSelect: function (dateText, inst) {
      inst.input.attr('value', dateText);
    },
    beforeShowDay: $.datepicker.noWeekends,
    minDate: new Date(2007, 7 - 1, 17),
    maxDate: 0
  };
  // CZ config
  if (typeof LANG != 'undefined' && LANG == 'cz') {
    var calConfigLocal = {
      closeText: 'Zavřít',
      prevText: '&#x3c;Dříve',
      nextText: 'Později&#x3e;',
      currentText: 'Nyní',
      monthNames: ['leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen', 'září', 'říjen', 'listopad', 'prosinec'],
      monthNamesShort: ['led', 'úno', 'bře', 'dub', 'kvě', 'čer', 'čvc', 'srp', 'zář', 'říj', 'lis', 'pro'],
      dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
      dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
      dayNamesMin: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
      weekHeader: 'Týd',
      dateFormat: 'dd.mm.yy',
      firstDay: 1,
      isRTL: false,
      showMonthAfterYear: false,
      yearSuffix: ''
    };
  }
  // EN config
  else {
    var calConfigLocal = {};
  }
  // merge configs
  var calConfig = $.extend(calConfigBase, calConfigLocal);
  // init datepicker, append button
  var dtp = [];
  $('.dateSelector .date').each(function (i) {
    dtp[i] = $(this).append('<span class="cico"><!-- --></span>').find('input').datepicker(calConfig);
    // show datepicker on button click
    $(this).find('.cico').click(function () {
      dtp[i].datepicker("show");
    });
  });

  /**
  * TABLE CONTROLS
  */
  $('.tableControls .show').click(function () {
    $('tr.toggle,th.toggle,td.toggle').show();
    $('.tableControls .show').hide();
    $('.tableControls .hide').show();
    $.cookie('tableState', 'visible');
    return false;
  });
  $('.tableControls .hide').click(function () {
    $('tr.toggle,th.toggle,td.toggle').hide();
    $('.tableControls .show').show();
    $('.tableControls .hide').hide();
    $.cookie('tableState', 'hidden');
    return false;
  });
  if ($.cookie('tableState') != 'visible') {
    $('.tableControls .hide').hide();
    $('tr.toggle,th.toggle,td.toggle').hide();
  }
  else {
    $('.tableControls .show').hide();
    $('tr.toggle,th.toggle,td.toggle').show();
  }

  /**
  * TABLE ODD/EVEN
  */
  $('tr:even').addClass('even');

  /**
  * FILTERS
  */
  $('.select').addClass('selectActive');
  $('.select select').each(function () {
    var sel = $(this);
    $(this).wrap('<span class="selectWrap"></span>');
    // submit button when multiple
    if ($(this).attr('multiple')) {
      var config = {
        // submit ob dropdown close
        onComplete: function (selector) {
          $(selector).parents('form').submit();
        }
      };
      $(this).parent().addClass('multiselect');
    }
    // simple, submit on change
    else {
      var config = {
        closeRadioOnClick: true,
        // submit ob dropdown close
        onComplete: function (selector) {
          //$(selector).parents('form').submit();
        }
      };
      // submit immediately after select
      $(this).change(function () {
        $(this).parents('form').submit();
      });
    }
    // init dropdown
    $(this).dropdownchecklist(config/* see http://dropdown-check-list.googlecode.com/svn/trunk/doc/dropdownchecklist.html */);
    // set selected
    $('.ui-dropdownchecklist-item input').each(function () {
      dropDownSelectCallback($(this));
    });
  });





});

