
var intervalObj = null;
var fadeInterval = 4000;
var fadeSpeed = 400;

$(function(){      
   
   $('input[type=button]').button();
   
   // efekty novinek
   if ($('#templatemo_promo .item').length > 1) {
      intervalObj = setInterval(fadeNews, fadeInterval);
   }
   
   // tlacitka novinek
   if ($('#items_wrapper .item').length > 1) {
      var box = $('<div id="newsbox" style="position: absolute; bottom: 10px; right: 100px; text-align: center; width: 200px"></div>');
      
      var cnt = $('#templatemo_promo .item').length;
      for (var i = 0; i < cnt; i++) {
         var mini = $('<div style="position: relative; top: 4px; display: inline-block; width: 20px; height: 20px; margin-right: 10px; background:url(/images/dot-0.png); cursor: pointer"></div>');
         var i1 = $('<img src="/images/dot-1.png" class="on" style="position: absolute; top:0; left: 0;" />');
         
         if ($($('#templatemo_promo .item').get(i)).filter(':visible').length == 0) {
            i1.hide();
         }                  
         
         mini.append(i1);         
         box.append(mini);
      }
      
      $('#items_wrapper').append(box);
      
      // prepinani kliknutim
      $('#newsbox > div').click(function(){
         var el = $(this);
         if (el.find('img').filter(':visible').length > 0)
            return;

         var index = el.index();
         
         var speed = fadeSpeed;
         var news = $('#templatemo_promo .item');
         var active = news.filter(':visible');
         var next = $(news.get(index));

         var activeBox = $( $('#newsbox > div').get( news.index(active) ) );
         var nextBox = $( $('#newsbox > div').get(index) );

         active.fadeOut(speed, function() {next.fadeIn(speed)});
         activeBox.find('img').fadeOut(speed, function() {nextBox.find('img').fadeIn(speed)});         
         
         clearInterval(intervalObj);
         intervalObj = setInterval(fadeNews, fadeInterval);
      });
   }
   
   
   // lightbox
   $("a[rel^='lightbox']").fancybox();
   
   // krasne tlacitka   
   $('.button').livequery(function() {$(this).button();});
   
   
   // roztazeni obsahu
   var footerH = parseInt( $('#templatemo_footer_wrapper').css('margin-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_footer_wrapper').css('margin-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_footer_wrapper').css('padding-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_footer_wrapper').css('padding-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_footer_wrapper').css('height').replace(/px$/, '') );
   var headerH = parseInt( $('#templatemo_header_wrapper').css('margin-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_header_wrapper').css('margin-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_header_wrapper').css('padding-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_header_wrapper').css('padding-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_header_wrapper').css('height').replace(/px$/, '') );                  
   var promoH = parseInt( $('#templatemo_promo_wrapper').css('margin-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_promo_wrapper').css('margin-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_promo_wrapper').css('padding-top').replace(/px$/, '') )
                  + parseInt( $('#templatemo_promo_wrapper').css('padding-bottom').replace(/px$/, '') )
                  + parseInt( $('#templatemo_promo_wrapper').css('height').replace(/px$/, '') );
                  
   $('#templatemo_content_wrapper').css('min-height', ($(document).height() - footerH - headerH - promoH) + 'px');
   
   
   // hide flashes after a while
   $("div.flash").each(function () {
      var el = $(this);
      setTimeout(function () {
            el.animate({"opacity": 0}, 2000);
            el.slideUp();
      }, 7000);
   });
         
   // ajax
   $("a.ajax").live("click", function (event) {
      event.preventDefault();
      $.get(this.href);
   });
});


function fadeNews() {
   var speed = fadeSpeed;
   var news = $('#templatemo_promo .item');
   var active = news.filter(':visible');
   var next = active.next('.item').length == 0 ? news.first() : active.next('.item');
   
   var activeBox = $( $('#newsbox > div').get( news.index(active) ) );
   var nextBox = $( $('#newsbox > div').get( news.index(next) ) );
   
   if (active.filter(':hover').length == 0 && $('#newsbox').filter(':hover').length == 0) {
      active.fadeOut(speed, function() {next.fadeIn(speed)});
      activeBox.find('img').fadeOut(speed, function() {nextBox.find('img').fadeIn(speed)});      
   }   
}



function formatDateTimeString(day, month, year, hours, minutes) {   
   var d = ''; 
   if (typeof(year) != undefined && year != null)
      d = day + '.' + month + '.' + year;
   else
      d = day + '.' + month + '.';
   
   var t = '';
   /*
   if (typeof(hours) != 'undefined') {
      if (hours < 10)
         hours = '0' + hours;
      t = ' ' + hours + ':';
      
      if (typeof(minutes) != 'undefined') {
         if (minutes < 10)
            minutes = '0' + minutes;
         
         t += minutes;
      }
   }
   */
      
   return d + t;
}


$.fn.extend({
  insertAtCaret: function(myValue){
  var obj;
  if( typeof this[0].name !='undefined' ) obj = this[0];
  else obj = this;

  if ($.browser.msie) {
    obj.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    obj.focus();
    }
  else if ($.browser.mozilla || $.browser.webkit) {
    var startPos = obj.selectionStart;
    var endPos = obj.selectionEnd;
    var scrollTop = obj.scrollTop;
    obj.value = obj.value.substring(0, startPos)+myValue+obj.value.substring(endPos,obj.value.length);
    obj.focus();
    obj.selectionStart = startPos + myValue.length;
    obj.selectionEnd = startPos + myValue.length;
    obj.scrollTop = scrollTop;
  } else {
    obj.value += myValue;
    obj.focus();
   }
 }
});
