function down_time(){
  hours = $('.hours').text();
  minutes = $('.minutes').text();
  seconds = $('.seconds').text();
  sec = setInterval(function(){
    seconds--;
    if (seconds < 10) seconds = '0' + seconds;
    if (seconds >= 0){
      $('.seconds').text(seconds);
    } else {
      seconds = 59;
      minutes--;
      if (minutes < 10) minutes = '0' + minutes;
      if (minutes >= 0){
        $('.seconds').text(seconds);
        $('.minutes').text(minutes);
      } else {
        minutes = 59;
        hours--;
        if (hours < 10) hours = '0' + hours;
        if (hours >= 0){
          $('.seconds').text(seconds);
          $('.minutes').text(minutes);
          $('.hours').text(hours);
        } else {
          $('.popupBox').remove();
          $('.shadow').remove();
          clearInterval(sec)
        }
      }
    }
  }, 1000)
}

function open_popup(href){
  $('.popupBox').remove();
  $('.shadow').remove();
  $('body').append('<div class="shadow"></div><div class="popupBox"><div class="popupClose">&nbsp;</div><div class="popupShadowTopRight"><div class="popupShadowTopLeft">&nbsp;</div></div><div class="popupShadowRight"><div class="popupShadowLeft"><div class="contentTopRight"><div class="contentTopLeft">&nbsp;</div></div><div class="popupRight"><div class="popupLeft"><div class="popupCenter"><div class="popupSomeBg"></div></div></div></div><div class="contentBottomRight"><div class="contentBottomLeft">&nbsp;</div></div></div></div><div class="popupShadowBottomRight"><div class="popupShadowBottomLeft">&nbsp;</div></div></div>');
  $('.popupClose').click(function(){
    $('.popupBox').remove();
    $('.shadow').remove();
    clearInterval(sec)
  })
  ppLeft = ($('body').width()-$('.popupBox').width())/2;
  ppTop = $('body').scrollTop() + 100 + $('html').scrollTop();
  $('.popupBox').css({'left':ppLeft, 'top':ppTop});
  jQuery.get(href, {popup: true}, function(data){
    $('.popupSomeBg').html(data);
    down_time();
  });
  
  var moveBox = true;
  $('.popupBox').mousedown(function(e){
    dX = ppLeft - e.pageX;
    dY = ppTop - e.pageY;
    moveBox = true;
    $('body').mousemove(function(pos){
      if (moveBox == true){
        ppLeft = pos.pageX + dX;
        ppTop = pos.pageY + dY;
        $('.popupBox').css({'left':ppLeft, 'top':ppTop});
      }        
    });
  })
  $('.popupBox').mouseup(function(e){
    moveBox = false
  })
}

jQuery(document).ready(function(){
  $('a').click(function () { leave = false; });
  $('form').submit(function () { leave = false; });
  
  
});




  
  
  $(function(){
    $('#nav_head ul:first').show();
    height = $('#nav_head ul:first li:first').height();
    width = $('#nav_head ul:first li:first').width();
    $('#nav_head ul:first').hide();
    var col = 0;
    
    $('#nav_head ul').each(function(){
      itemsLi = $('> li', this).length;
      if (height!=0){
        itemIn = Math.sqrt(height * width * itemsLi) / height / 1.3;
      }
      itemIn = itemIn.toFixed(0);
      while ((itemsLi % itemIn < itemIn / 2) && (itemsLi % itemIn != 0)){
        itemIn++
      }
      colItem = 0;
      ul = $(this)
      $(this).find('> li').each(function(i) {
        if (i == 0 || i == itemIn || i == itemIn * 2 || i == itemIn * 3 || i == itemIn * 4){
          ul.append('<div class="tmc-col tmc-col-'+colItem+' tmc-col-global-'+col+'"></div>');
          col++;
          colItem++;
        }
        col_1 = col - 1;
        jQuery(this).clone().appendTo('.tmc-col-global-'+col_1);
        jQuery(this).remove();
      })
      ul.css({'width': colItem*width+'px'});
    })
    
    /*$('#nav_head ul ul').each(function(){
      itemsLi = $('> li', this).length;
      if (height!=0){
        itemIn = Math.sqrt(height * width * itemsLi) / height / 1.1;
      }
      itemIn = itemIn.toFixed(0);
      while ((itemsLi % itemIn < itemIn / 2) && (itemsLi % itemIn != 0)){
        itemIn++
      }
      colItem = 0;
      ul = $(this)
      $(this).find('> li').each(function(i) {
        if (i == 0 || i == itemIn || i == itemIn * 2 || i == itemIn * 3 || i == itemIn * 4){
          ul.append('<div class="tmc-col tmc-col-'+colItem+' tmc-col-global-'+col+'"></div>');
          col++;
          colItem++;
        }
        col_1 = col - 1;
        jQuery(this).clone().appendTo('.tmc-col-global-'+col_1);
        jQuery(this).remove();
      })
      ul.css({'width': colItem*width+'px'});
    })
    
    win = ($('body').width() - 190 ) / 2;
    $('#nav_head li').hover(function(){
      pos = $(this).offset();
      if (pos.left > win){
        $(this).addClass('tmc-right')
      }
    })
    
    $('#nav_head li').hover(function(){
      $(this).css('z-index', '2')
    }, function(){
      $(this).css('z-index', '1')
    })*/
    
  });
