  var over;
  var old;
  var cur;
  var timer;
  var timer_out;
  
  jQuery.fn.extend({
    scrollTo : function(speed, easing) {
      return this.each(function() {
        var targetOffset = ($(this).offset().top)-5;
        $('html,body').animate({scrollTop: targetOffset}, speed, easing);
      });
    }
  });
  
  $(function(){

    $tabs=$("#first_level").tabs({ selected: menu, fx: { opacity: 'toggle', duration: 200 }});

    $("#second_level .block").each(
      function(i){
          $(this).mouseover( function() { dropDown(i); } );
          $(this).mouseout( function() { dropUp(); } );
      }
    );
    
    $("#second_level .all_wares").each(
      function(i){
          $(this).attr("id",i);
          $(this).mouseover( function() { steelHere(); } );
          $(this).mouseout( function() { dropUp(); } );
      }
    );

    $("#confstr").tablesorter(); 
    
    $('.onlyjs').show();
    $('.hidden, .manufacturers').hide();
    $('#search_form .arrow').click(function(){if(!$(this).hasClass('active')){showValues(this);} return false;});
    
    $('#search_form .values_layer A').click(function(){ changeVal(this); return false; });
    
    $('#search_form .searchmode_selector A').click(function(){ if(!$(this).hasClass('active')){changeSearchMode(this);}; return false; });
    
    $("#search_form INPUT:not([@type='text'])").click(function(){getResult();});
    $("#search_form INPUT[@type='text']").change(function(){getResult();});
    
    $("#search_form .mf").click(function(){$('#all_manufacturers').attr('checked', false);});

    $("#search_form .mtrx").click(function(){$('#matrix_any').attr('checked', false);});
    $("#search_form .os_input").click(function(){$('#all_os').attr('checked', false);});
    
    $('#footer_wares').scroller({shift: 40});
 
    if(!$.browser.msie){
      $('#search_form A').focus(function(){blur();});
    }
    
    var newsObj=$('#news');
    if(newsObj.length>0){
      newsObj.find('.news_container').width((newsObj.find('.can_hScroll').width())/3);
      newsObj.find('.can_hScroll').scroller();
    }
    
    $('#basket .count_input').keyup(function(){
      if(this.value.length>0){
      if(!/^\d{1,10}$/.test(this.value)){
        this.value='1';
      }
      $('#save_changes').css('visibility', 'visible');
      basketUpdate();
      }
    });
    
    $('#basket .delete').click(function(){
      $('#save_changes').css('visibility', 'visible');
      var count_input=$(this).parent().find('.count_input');
      if(count_input.val()==0){
        count_input.val('1');
      }else{
        count_input.val('0');
      }
      basketUpdate();
      return false;
    });
    
    $('#save_changes').css('visibility', 'hidden');
    
    //if(checkCookie()==false){
    //  $("#addtobasket").text('Купить');
    //}
    
    $('#basket .plus').click(function(){
      basket_count=$(this).parent().parent().find('INPUT');
      basket_count.val(parseInt(basket_count.val())+1);
      basketUpdate();
      $('#save_changes').css('visibility', 'visible');
      return false;
    });
    
    $('#basket .minus').click(function(){
      basket_count=$(this).parent().parent().find('INPUT');
      if(parseInt(basket_count.val())>0){
        basket_count.val(parseInt(basket_count.val())-1);
        basketUpdate();
        $('#save_changes').css('visibility', 'visible');
      }
      return false;
    });
    
    banners_layer=$("#big_banner_layer .banners");
    banners=banners_layer.find('.banner');
    
    
    if(banners.length>1){
      banners_layer.hover(function(){$(this).addClass('hover');} , function(){$(this).removeClass('hover')});
      banner_id=0;
      
      setTimeout("rotateBanner()", 13000);
    }
    
  });
  

  function mousePageXY(e){
    var x = 0, y = 0;

    if (!e) e = window.event;

    if (e.pageX || e.pageY)
    {
      x = e.pageX;
      y = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
      x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
      y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
    }

    return {"x":x, "y":y};
  }

  document.onmousemove = function(e){
    var mCur = mousePageXY(e);
    left=mCur.x;
  }
  
  function dropDown(id){
    over=true;
    timer=setTimeout(function(){
      if(id!=old){
        $('#'+old).fadeOut(200);
        $('#'+id).css('opacity', 0);
        $('#'+id).show();
        lpos=$('#'+id).width();
        lpos=left-(lpos/2);
        if(lpos<20){lpos=20}
        $('#'+id).css('left', lpos);
        old=id;
        $('#'+id).fadeTo(300, 1);
      }
    }, 100);
  }

  function dropUp(){
    over=false;
    clearTimeout(timer);
    clearTimeout(timer_out);
    timer_out=setTimeout(function(){
      if (!over){
        cur=old;
        old=666;
        $('#'+cur).fadeOut(300);
        over=false;
      }
    }, 300);
  }

  function steelHere(){
    over=true;
    clearTimeout(timer);
    clearTimeout(timer_out);
  }

  function popa(elem){
    var lpos = elem.offsetLeft;
    var src = elem.href;
    $("#frame").animate({left: lpos}, "normal");
    $("#main_image").fadeTo('fast', 0, function(){$("#main_image").attr({src: src});});
    //$("#main_image").fadeTo('fast', 1);
  }
  function fadein(elem){
    $(elem).fadeTo('fast', 1);
  }
  
  function changeVal(obj){
    $(obj).parent().parent().children('INPUT').val($(obj).html());
    hideValues();
    getResult();
  }

  function showValues(obj){
    hideValues();
    $(obj).addClass('active').next().slideDown(100, function(){
      $('body').click(function(){
        hideValues();
      });
    });
  }
  
  function hideValues(){
    $('#search_form .arrow').removeClass('active');
    $('#search_form .values_layer').slideUp(100, function(){$('body').click(function(){});});
  }
  
  function changeSearchMode(obj){
    $('#search_form .searchmode_selector A').removeClass('active');
    $(obj).addClass('active');
    $('#search_form .hidden').toggle();
  }
  
  function getResult(){
    //alert($('#search_form FORM').serialize());
    var searchind=$('#search_indicator');
    var selectedData=$('#search_form FORM').serialize();

    searchind.addClass('active');
   $.get('/shop/search?'+selectedData, function(data){
     searchind.html(data).removeClass('active');     
   });
  }

  function showManufacturers(obj){
    $(obj).parent().hide(); 
    $('#search_form .manufacturers').show();
  }

  function basketUpdate(){
    var ware_summ=0;
    $('#basket .basket_ware').each(function(){
      var count=parseInt($(this).find('.count_input').val());
      if(count==0){
        $(this).addClass('deleted');
      }else{
        $(this).removeClass('deleted');
      }
      $(this).find('.ware_summ').text(parseInt($(this).find('.price_val').text())*count);
    });
    $('#basket .ware_summ').each(function(){
      ware_summ=ware_summ+parseInt($(this).text());
    });
    $("#summ .basket_summ").text(ware_summ);
    if(ware_summ==0){
      $('#order_button').attr('disabled', 'true');
    }else{
      $('#order_button').removeAttr('disabled');
    }
  }

  function payment(payment){
    $('#basket .basket_ware .title').each(function(){
      $(this).children('.price').children('.price_val').text($(this).children('.'+payment).text());
      basketUpdate();
    });
  }
  

function rotateBanner(){
  setTimeout("rotateBanner()", 13000);
  if(banners_layer.hasClass('hover')){return false;}
  if(banner_id==banners.length){
    banner_id=0;
  }
  
  
  showBanner($("#big_banner_layer .banner").eq(banner_id));
  banner_id++;
  
}

function showBanner(banner){
  var delay=0;
  resetBanners();
  initBanner(banner);
  banner.css('z-index', banners.length+1).prev().css('z-index', banners.length);
  banner.find("A").find("IMG").each(function(){
    var clip_property = $(this).css('clip');
    
    clip_property = clip_property.split(' ');
    
    var left=parseInt(clip_property[1]);
    var right=parseInt(clip_property[3]);
    var img=$(this);
    delay+=30;
    setTimeout(function(){animateBanner(img, left, right);}, delay);
    

  });
  
}



function animateBanner(img, left, right){
  
  img.animate({'clip':'rect(0px, '+(left+38)+'px, 150px, '+right+')'}, 400, 'linear');
  
}

function initBanner(banner){

  if(!banner.hasClass('first')){
  
    var container = banner.find("A");
    var src = container.find("IMG").eq(0).attr('src');
      
    var content='';
    var offset=38;
    
    
    for(var i=0; i<25; i++){
      content+='<img src="'+src+'" width=950 height=150 style="clip: rect(0px, '+(offset-38)+'px, 150px, '+(offset-38)+'px);" />';
      offset+=38;
    }
  //alert(content);
    container.html(content);
  
  }else{
    banner.removeClass('first');
  }
}

function resetBanners(){
  //var bl=banners.length;
  banners.each(function(i){
    $(this).css('z-index', i);
  });
}
  
/*   function checkCookie() {
    alert(document.cookie.length);
    if (document.cookie.length>0) {
      return true;
    } else {
      return false;
    }
  } */
