var $j = jQuery.noConflict();

$j(document).ready(function(){

  /*$j('body, table, div').each(function() {
    if ($j(this).width() > 1280) {$j(this).css('width', '1280px');}
  });*/
	
	/*$j('.wrapper').each(function() {
		$wrapper = $j(this);
		if ($j.browser.ie) {
			function resizeHandler() {
				var wWidth = $j(window).width();
				if (wWidth > 1280) {
					wWidth = 1280;
				} else if (wWidth < 980) {
					wWidth = 980;
				} else {
					wWidth = 'auto';
				}
				$wrapper.width(wWidth);
			}
			$j(window).resize(resizeHandler);
			resizeHandler();
			//$j('body').css('max-width','1280px');
		} else {
			$wrapper.css({
				'max-width': '1280px',
				'min-width': '980px',
				'margin': '0 auto'
			});
		}
	});*/

  $j("a.gallery").fancybox();
  /*jQuery('.first-and-second-carousel').jcarousel({
  scroll:1,visible:5,wrap: 'circular',
  initCallback:function(self,t){
  if(self.options.size <= 5){
  if(self.options.size == 1)
  {
  self.buttonNext.css('display', 'none');
  self.buttonPrev.css('display', 'none');
  }
  self.options.visible = self.options.size;
  }
  }
  });*/

  jQuery('.first-and-second-carousel').each(function(){
    var s = $j(this).find('li').size();
    if(s > 5) s = 5;
    else $j(this).css({'width':74*s});

    $j(this).jcarousel({
      scroll:1,visible:s,wrap: 'circular',
      initCallback:function(self,t){
        if(self.options.size == 1){
          self.buttonNext.css('display', 'none');
          self.buttonPrev.css('display', 'none');
        }
        if(self.options.size < 5){
          self.container.css({'width':74*self.options.size + 100});
          self.clip.css({'width':74*self.options.size + 100});
          var li = self.list.children('li');
          var di = self.options.visible !== null ? Math.ceil(self.clipping() / self.options.visible) : null;
          if (li.size() > 0) {
            var wh = 0, j = self.options.offset;
            li.each(function() {
              self.format(this, j++);
              wh += self.dimension(this, di);
            });

            self.list.css(self.wh, (wh + 100) + 'px');
          }
        }
      }
    })


  });

  /*$j('.gTop').click(function(){
    if($j(this).parent().parent().next().find('ul.first-and-second-carousel li:first a').size())
    {
      $j(this).parent().parent().next().find('ul.first-and-second-carousel li:first a').click();
      return false;
    }
  })*/


  $j('#topSlide div').each(function() {
    var images = $j('img', this);
    var current = 0;
    function slideF(){
      images.eq(current % images.length).fadeOut(2000, function(){
        images.eq(++current % images.length)
        .fadeIn(2000, function(){setTimeout(slideF, 2000);});
      });
    }
    setTimeout(slideF, 2000);
  });

  var stack = new Array();
  var inMenu = false;
  var intervalId = 0;

  var config = {sensitivity: 1,interval: 100,over: makeTall,timeout: 100,out: makeShort};
  function makeTall() {
    inMenu = true;
    var p = $j(this).find('p.parent');
    p.removeClass('close');
    p.next().slideDown(270);
  }
  function makeShort(){
    var p = $j(this).find('p.parent');
    stack.push(p);
    inMenu = false;
  }

  function makeAllShort(){
    if (!inMenu){
      while (p = stack.pop()){
        p.addClass('closed');
        p.next().slideUp(270,function(){
          $j('.contentBlockTableTdLeft p.cat.closed').removeClass('closed').addClass('close');
        });
      }
      //  		clearInterval(intervalId);
      inMenu = false;
    }
  }

  intervalId = setInterval(makeAllShort, 2500);

  $j('.contentBlockTableTdLeft p:not(.fix)').next('.menuSubCat:not(.fix)').slideUp(0);
  $j('.contentBlockTableTdLeft p:not(.fix)').parent().hoverIntent(config);

  $j('#sendZ').click(function(){
    yaCounter525977.reachGoal('basket_send_click');
    _gaq.push(['_trackPageview', '/virtual/basket/send_click.html']);
  });

  $j('#oznrss').click(function(){
    yaCounter525977.reachGoal('pallet_send');
    _gaq.push(['_trackPageview', '/virtual/pallet/send.html']);
  });

  $j('#oznrss2').click(function(){
    yaCounter525977.reachGoal('drivein_send');
    _gaq.push(['_trackPageview', '/virtual/drivein/send.html']);
  });

  $j('#oznrss3').click(function(){
    yaCounter525977.reachGoal('mezonine_send');
    _gaq.push(['_trackPageview', '/virtual/mezonine/send.html']);
  });

  $j('#spnsvp').click(function(){
    yaCounter525977.reachGoal('pallet_manual');
    _gaq.push(['_trackPageview', '/virtual/pallet/manual.html']);
  });

  $j('.tab1, .tab2, .tab3').click(function(){
    var tb = $j(this).parent().parent();
    tb.find('td.openTab img').attr('src','/images/m2_l.gif');
    tb.find('td.active').attr('class','');
    tb.find('td.closeTab img').attr('src','/images/m2_slash.gif');
    tb.find('td.closeTabLast img').attr('src','/images/m2_r.gif');
    $j(this).parent().addClass('active');
    $j(this).parent().next().hasClass('closeTabLast')
    ? $j(this).parent().next().find('img').attr('src','/images/m2_r2.gif')
    : $j(this).parent().next().find('img').attr('src','/images/m2_slash1.gif');

    $j(this).parent().prev().hasClass('openTab')
    ? $j(this).parent().prev().find('img').attr('src','/images/m2_l1.gif')
    : $j(this).parent().prev().find('img').attr('src','/images/m2_slash2.gif');

    $j(this).parent().parent().parent().find('div.mtab').css({'display':'none'});
    $j(this).parent().parent().parent().find('div.m'+$j(this).attr('class')).css({'display':'block'});
  })

  //	$j('.smallImage').click(function(){
  //		$j(this).parent().parent().find('.bigImage').attr('src',$j(this).attr('src'));
  //	})

});

