var $J = jQuery.noConflict();

$J(
   function() {
      FixBackground();
      FixProductsMenu();
      FixContactsForm();
      FixSidebarHeight();
      FixBackground();
      FixSidebarHeight();
      FixPagerLinks();
   }
);

function FixProductsMenu() {
   $J('#top_right ul li:eq(0)').removeClass().addClass('first');

   $J('#top_right ul li:not(:eq(0))').each(
      function(i,obj){
         var active = false;

         if($J(obj).hasClass('active')){
            active = true;
         }
         $J(obj).removeClass().addClass('wide');

         if(active){
            $J(obj).addClass('active');
         }
      }
   );

   $J('#top_right ul li').each(
      function(i,obj) {
         if($J(obj).hasClass('active')){
            $J('#top_right').css('background','url('+TMPL_IMAGES_PATH+'right_top_menu_bckg_'+i+'.gif) no-repeat left bottom')
         }
      }
   );
}

function FixSidebarHeight() {
   if($J('.bgfix').length) {
      var dh = $J('.bgfix').get(0).offsetHeight;

      if( parseInt(dh) > '535'){
         $J('#sidebar').css('height',dh);
      } else {
         $J('#center').css('height','482px'); // ?!?!?!?
      }
   }
}

function FixContactsForm() {
   var o = $J('#chrono_verification');

   if(o.length){
      o.val('Enter the code shown below');
   }
}

function FixBackground() {
   var mright  = $J('#wrapper').get(0).offsetLeft;

   if($J.browser.msie && parseInt($J.browser.version) == 7) {
      // Fix IE7 horizontal scroll
      mright -= 1;
   }

   var hheight = parseInt($J('#header').get(0).offsetHeight + $J('#hornav').get(0).offsetHeight);
   var cheight =
      parseInt(
         $J('.bgfix').length ?
            (hheight + $J('#content_wrapper').get(0).offsetHeight + $J('#bottom').get(0).offsetHeight - 32)
            :
            (hheight + $J('#content_wrapper').get(0).offsetHeight + $J('#bottom').get(0).offsetHeight)
      );

   $J('.mainbody').css('height', cheight + 'px');
   $J('.rightbckg').css('height', (cheight - hheight) + 'px').css('width', mright + 'px');

   // fix min width
   if($J('.bottombckg').get(0).offsetWidth < 950) {
      $J('.bottombckg').css('width','950px');
   }
}

function FixPagerLinks() {
   if($J('.title_wrapper').length && $J('#prev_tmpl').length && $J('#next_tmpl').length){
      var prev_url = $J($J('#prev_tmpl').val()).find('a').length ? $J($J('#prev_tmpl').val()).find('a').attr('href') : 'javascript:void(0);';
      var next_url = $J($J('#next_tmpl').val()).find('a').length ? $J($J('#next_tmpl').val()).find('a').attr('href') : 'javascript:void(0);';

      $J('<a class="aleft"  href="'+prev_url+'"></a>').mouseover(
         function(){$J(this).css('background','url('+TMPL_IMAGES_PATH+'l_arrow_over.gif) no-repeat')}
      ).mouseout(
         function(){$J(this).css('background','url('+TMPL_IMAGES_PATH+'l_arrow.gif) no-repeat')}
      ).insertBefore('#sidebar .content .title_wrapper .contentheading');
      $J('<a class="aright" href="'+next_url+'"></a>').mouseover(
         function(){$J(this).css('background','url('+TMPL_IMAGES_PATH+'r_arrow_over.gif) no-repeat')}
      ).mouseout(
         function(){$J(this).css('background','url('+TMPL_IMAGES_PATH+'r_arrow.gif) no-repeat')}
      ).insertAfter('#sidebar  .content .title_wrapper .contentheading');
   }
}

