/* engine v.1.3
 * by IonDen
 * rev 12
 * 19.01.2010
 */

function updateKorzina(){
    $.ajax({
        cache: false,
        url: '/ajax/order/status/',
        success: function(data){
            $('#topCart').replaceWith('<div id="topCart">'+data+'</div>');
        }
    });
}

$(document).ready(function(){
   // главное меню
   var currentMenu = 0;
   var currentWidth = 0;
   var currentOff;

   var hideCD;
   var hideTimer = 0;

   var $links = $('td.bm a');

   $links.click(function(event) {
      event.preventDefault();
   });

   function _mwidth($menu) {
      var w = 0;

      var count = $links.length;
      if($menu.children(".cols").children("DIV").length) count = $menu.children(".cols").children("DIV").length;
      for (var i = 0; i < count; i++) {
         w += $links.eq(i).parent().outerWidth();
      }

      return w-3;
   }

   function _mc($menu) {
      var $colsw = $menu.find('.cols'),
         $cols = $colsw.children('div').css('height',  'auto');
      $menu.find('.sep-n').remove();
      var cH = $colsw.height(), h = 0;

      for (var j = 0; j < $cols.length; j++) {
         var dH = cH - $cols.eq(j).outerHeight(),
            c = Math.ceil(dH / 31);
         if ((dH == 0) && (!h)) {
            h = $cols.eq(j).height()
         }
         var s = '';
         for (var k = 0; k < c; k++) {
            s += '<li class="sep sep-n"></li>';
         }
         $cols.eq(j).find('ul').append(s);
      }
      $cols.height(h);
   }
   $(window).resize(function() {
      $('.menu').data('resize-flag', false);
   });

   $links.mouseenter(function() {
      clearInterval(hideCD);
      $("div.menu").hide();
      $("#menuArr").hide();
      currentMenu = $(this).attr("id").slice(2);
      currentWidth = $(this).width();
      currentOff = $(this).offset();

      var $menu = $("#menu" + currentMenu).show();
      if (!$menu.data('resize-flag')) {
         var m_w = _mwidth($menu);
         var count = $links.length;
          if($menu.children(".cols").children("DIV").length) count = $menu.children(".cols").children("DIV").length;

        $menu.children(".cols").children("DIV").css("width",m_w/count);
         $menu. width(m_w);
         _mc($menu);
         $menu.data('resize-flag', true);
      }
      $("#menuArr").show().css("left", currentOff.left + (currentWidth / 2) - 8);

      var localWidth = $("#menu" + currentMenu).width();
      var localLeft = 10;
      $("#menu" + currentMenu).css("left",localLeft);
   }).mouseleave(function() {
      hideTimer = 0;
      hideCD = setInterval(omgHide, 50);
   });

   $("div.menu").mouseenter(function() {
      clearInterval(hideCD);
   });
   $("div.menu").mouseleave(function() {
      hideTimer = 0;
      hideCD = setInterval(omgHide, 50);
   });


   function omgHide() {
      if(hideTimer < 4) {
         hideTimer++;
      } else {
         hideTimer = 0;
         clearInterval(hideCD);
         $("div.menu").hide();
         $("#menuArr").hide();
      }
   };


   // подсветка таблиц
   $("table.list tr").hover(
      function(){
         $(this).addClass("over");
      },
      function(){
         $(this).removeClass("over");
      }
   );

   $("table.char tr").hover(
      function(){
         $(this).addClass("over");
      },
      function(){
         $(this).removeClass("over");
      }
   );

   // выпадающие менюхи
   var num = $("a.aj").length;
   var current = 0;
   var curOffset = 0;
   var curWidth = 0;
   var minWidth = 0;
   var insideNum = 0;
   var insideCurrent = 0;
   var insideText;
   var allow = true;

   for(var i = 0; i < num; i++) {
      $("#sel" + i).click(
         function(){
            $(this).focus();
            current = parseInt(this.id.slice(3));
            curOffset = $("#sel" + current).offset();
            curWidth = $("#sel" + current).width() + 12;
            emulateSelect();
         }
      );
      $("#sel" + i).blur(
         function(){
            hideSelect();
         }
      );
   };

   function emulateSelect() {
      $("#s" + current).show().css("left",curOffset.left - 7).css("top",curOffset.top - 3).width(curWidth);
      insideNum = $("#s" + current + " a").length;

      $("#s" + current).hover(
         function(){
            allow = false;
         },
         function(){
            allow = true;
         }
      );

      for(var i = 0; i < insideNum; i++) {
         $("#s" + current + "p" + i).click(
            function(){
               insideCurrent = this.id;
               insideText = $(this).text();
               allow = true;
               $("#sel" + current).text(insideText);
               hideSelect();
            }
         );
      }
   }

   function hideSelect() {
      if(allow === true) {
         $("#s" + current).hide();
      }
   }

   // рекламные попапы
   var popX = 0;
   var popY = 0;
   var popCurrent = 0;

    $(document).bind("mousemove",function(e){
        popX = e.pageX;
        popY = e.pageY;
    });

   $("a.advLink").click(
      function(){
         $(this).blur(0);
         popCurrent = parseInt(this.id.slice(7));
         $(".popup").hide();
         $("#popup" + popCurrent).show().css("top",popY - 20).css("left",popX + 30);
      }
   );
   $("a.close").click(
      function(){
         $(".popup").hide();
      }
   );

   // редактирование кол-ва в корзине
    var currentID;
    var currentNum = 0;

    $("div.number b").click(
        function(){
            currentID = $(this).parent().attr("id");
            currentNum = parseInt($("#" + currentID + " input").attr("value"));
            currentNum++;
            $("#" + currentID + " input").attr("value",currentNum);

            $.ajax({
                cache: false,
                url: '/ajax/order/edit/'+ currentID + '/' + currentNum + '/',
                success: function(data){
                    $('#itogo').replaceWith('<b id="itogo">' + data + '</b>');
                    updateKorzina();
                }
            });
        }
    );

    $("div.number i").click(
        function(){
            currentID = $(this).parent().attr("id");
            currentNum = parseInt($("#" + currentID + " input").attr("value"));
            if(currentNum > 1) {
                currentNum--;
            }
            $("#" + currentID + " input").attr("value",currentNum);

            $.ajax({
                cache: false,
                url: '/ajax/order/edit/'+ currentID + '/' + currentNum + '/',
                success: function(data){
                    $('#itogo').replaceWith('<b id="itogo">' + data + '</b>');
                    updateKorzina();
                }
            });
        }
    );

    //Paginator
    var nextlink = $('#nextlink').attr('href');
    var prevlink = $('#prevlink').attr('href');

       document.onkeydown=function(e){
          if(e.which == 17) isCtrl=true;

          if(nextlink != undefined && e.which == 39 && isCtrl == true) {
             location.href=nextlink;
          }
          if(prevlink != undefined && e.which == 37 && isCtrl == true) {
             location.href=prevlink;
          }
       }

    //Раскрывающеся меню
    $(".leftmenu .root b").click(function(){
       var $div = $(this).parent();
       if($div.hasClass('open'))
       {
          $div.removeClass("open").addClass("cls");
       }
       else
       {
          $div.removeClass("cls").addClass("open");
       }
    });



});
