$(function() {
  var top = 0;
  var li = $("#showcase_list li.active").get(0);
  if (li) {
     top = parseInt($("#showcase_list li.active").position().top) + 'px';
     $("#showcase .arrow").css('top', top).show();
  }
  $("#showcase .left img").each(function() {
     if (!$(this).hasClass('active')) $(this).hide();
  });
  
  
  $("#showcase_list a.head").hover(
     function() {
        $("#showcase_list li").removeClass('active');
        $("#showcase .arrow").hide();
        $(this).parent().addClass('active');
        top = parseInt($("#showcase_list li.active").position().top) + 'px';
        $("#showcase .arrow").css('top', top).show();

        var id = $(this).attr('id').match(/head([0-9]+)$/)[1];
        if (!$("#showcase .left img#imghead"+id).hasClass('active')) {
           $("#showcase .left img.active").fadeOut('normal').removeClass('active');
           $("#showcase .left img#imghead"+id).fadeIn('slow').addClass('active');
        }
     },
     function() {
     }
  );
  
  $("#topmenu ul.menu li.toplink_cont").hover(
    function() {
       $("a.toplink", this).css("color", "black");
       $(".ddimage", this).css("background-position", "0 -4px");
       var nwidth = $(this).outerWidth() + 40;
       $drop = $(".dropdown",this);
       $drop.css('z-index', '1001');
       //alert($drop.width());
       //alert(nwidth);
       if ($drop.width() < nwidth) {
          $drop.css('width', nwidth+'px');
       }
       $drop.show();
       //alert($drop.css('width'));
       //alert($drop.width());
       //alert($("table", $drop.get(0)).width());
       
       if (!$(this).hasClass('active')) 
          $(this).css("background", "url(/image/layout/top_menu_push.gif) top left repeat-x");
       
       $(this).css("z-index", "121");
       
       if ($(this).hasClass('first')) {
          $("#topmenu li.lcorner").css("background", "url(/image/layout/ddmenu/p_top_menu_select_left.gif) 0 0 no-repeat");
          $drop.css("left", '-24px');
          $("ul.sub", $drop.get(0)).css("margin-left", "12px");
       }
    },
    function() {
       $(".dropdown", this).hide();
       $(this).css("z-index", "101");
       $(".ddimage", this).css("background-position", "0 0");
       $("a.toplink", this).css('color', '#4c515a');
       if (!$(this).hasClass('active')) $(this).css("background", "none");
       if ($(this).hasClass('first')) {
          if ($(this).hasClass('active')) {
             $("#topmenu li.lcorner").css("background", "url(/image/layout/top_menu_push_left.gif) 0 0 no-repeat");
          } else { 
             $("#topmenu li.lcorner").css("background", "none")
          }
       };
    }
  );
  
  $("#gallery .item a").lightBox({
     imageLoading: '/gal/ajax-loader.gif',
     imageBtnClose: '/gal/lightbox-btn-close.gif',
     imageBtnPrev: '/gal/lightbox-btn-prev.png',
     imageBtnNext: '/gal/lightbox-btn-next.png',
     imageBlank:  '/gal/lightbox-blank.gif',
     txtImage: 'Изображение',
     txtOf: 'из'
  });
  
  $("a[rel=lightbox]").lightBox({
     imageLoading: '/gal/ajax-loader.gif',
     imageBtnClose: '/gal/lightbox-btn-close.gif',
     imageBtnPrev: '/gal/lightbox-btn-prev.png',
     imageBtnNext: '/gal/lightbox-btn-next.png',
     imageBlank:  '/gal/lightbox-blank.gif',
     txtImage: 'Изображение',
     txtOf: 'из'
  });
  
  if (typeof u_dsc_rn == 'function') u_dsc_rn();
  
  $("table.general").each(function() {
     $("tr", this).removeClass("lastrow");
     $("tr:first", this).addClass('firstrow');
     $("td", this).removeClass("lastcolumn");
     $("td", this).removeClass("firstcolumn");
     $("tr:last", this).addClass("lastrow");
     $("tr", this).each(function() {
        $("td:first", this).addClass('firstcolumn');
        $("td:last", this).addClass('lastcolumn');
     });
  });
  
  $("a[rel=gallerybox]").click(function() {
     var href = $(this).attr('href');
     $.get(
        href,
        {"ajax": 1},
        function(data) {
           if (data.length > 0) {
              $.facebox.settings.opacity = '0.8';
              $.facebox.settings.overlay = true;
              //$.facebox({opacity:0.5, overlay: true});
              $.facebox(data);
              updateGalleryCont(href);
           }
        }
     );
     return false;
  });
  
});
  
function updateGalleryCont(href) {
   $("#facebox #photogallery_cont .prev, #facebox #photogallery_cont .next").css("height", parseInt($("#facebox #photogallery_cont .photo").outerHeight()) + 'px');
   $("#facebox #photogallery_cont .prev, #facebox #photogallery_cont .next").click(function() {
      var photoid = $(this).attr('photoid');
      var h = $("#facebox #photogallery_cont .photo img").height();
      var w = $("#facebox #photogallery_cont .photo img").width();
      $("#facebox #photogallery_cont .photo").append('<div class="ajax_loader" style="position: absolute; width:'+ w +'px; height: '+h+'px; top: 1px; left: 50px; background: #DDD url(/gal/ajax-loader.gif) center center no-repeat;"></div>');
      $.get(
         href + photoid + '/',
         {"ajax": 1},
         function(data) {
            $("#facebox #photogallery_cont").replaceWith(data);
            updateGalleryCont(href);
         }
      );
   });
}

