$(document).ready(function(){
  function preloadPhotos(photos) {
      for (var i=0;i<photos.length;i++){
          preloaded[i] = new Image();
          preloaded[i].setAttribute('src',photos[i]);
    }
    timer = window.setInterval(checkPhotos, 100);
  }
  function checkPhotos() {
    var uncompleted = 0;
    for (var i = 0; i < preloaded.length; i++) {
      if (!preloaded[i].complete) {
        uncompleted++;
      }
    }
    if (uncompleted == 0) {
      var ScrollHeight = 0;
      var ScrollContainer = $('#scroller');
      var Products = ScrollContainer.find('div.rc_blocks');
      var AllProducts = ScrollContainer.find('div');
      var i = 0;
      var till = 0;
      AllProducts.each(
        function() {
          if ($(this).attr('id') == "sel_prod") {
            till = i;
          }
          i++;
        });
      i = 0;
      if (till != 0) {
        Products.each(
          function() {
            if (i < till) {
              var h = parseInt($(this).height());
              ScrollHeight += h;
            }
            i++;
          });
        ScrollHeight = ScrollHeight + till*31;
        var ScrollerHeight = parseInt($('#scroller').height());
        var ScrollWindow = parseInt($('#scroller_container').height());
        if (ScrollerHeight - ScrollHeight < ScrollWindow) {
          if (ScrollerHeight > ScrollWindow) {
            jscroller_scroller.css({position: 'absolute', left: 0, top: ScrollWindow - ScrollerHeight});
          }
          else {
            jscroller_scroller.css({position: 'absolute', left: 0, top: 0});
          }
        }
        else {
          jscroller_scroller.css({position: 'absolute', left: 0, top: -ScrollHeight});
        }
      }
      else {
        jscroller_scroller.css({position: 'absolute', left: 0, top: 0});
      }
      jscroller_init();
      window.clearInterval(timer);
    }
  }
  this.defaults = {
    scroller: {
      interval: 0,
      refresh: 10,
      direction: "up",
      speed: 1,
      id: "#scroller",
      cont_id: "#scroller_container"
    }
  }
  var jscroller_config = $.extend(this.defaults), jscroller_scroller = $(jscroller_config.scroller.id), jscroller_scroller_cont = $(jscroller_config.scroller.cont_id);
  function jscroller_startScroll() {
    if(!jscroller_config.scroller.interval) {
      jscroller_config.scroller.interval=window.setInterval(jscroller_doScroll,jscroller_config.scroller.refresh);
    }
  }
  function jscroller_stopScroll() {
    if (jscroller_config.scroller.interval) {
      window.clearInterval(jscroller_config.scroller.interval);
      jscroller_config.scroller.interval=0;
    }
  }
  function jscroller_init() {
    jscroller_scroller_cont.css('overflow','hidden');
    if(!jscroller_config.scroller.interval) {
      if (window.attachEvent) {
        window.attachEvent("onfocus", jscroller_startScroll);
        window.attachEvent("onblur", jscroller_stopScroll);
        window.attachEvent("onresize", jscroller_startScroll);
        window.attachEvent("onscroll", jscroller_startScroll);
      }
      else if (window.addEventListener) {
        window.addEventListener("focus", jscroller_startScroll, false);
        window.addEventListener("blur",	jscroller_stopScroll, false);
        window.addEventListener("resize", jscroller_startScroll, false);
        window.addEventListener("scroll", jscroller_startScroll, false);
      }
      jscroller_startScroll();
      if ($.browser.msie) window.focus();
    }
  }
  function jscroller_getElem(Elem) {
    return (typeof Elem == "string" && document.getElementById)? document.getElementById(Elem) : Elem;
  }
  $('#up').hover(
    function() {
      if(!jscroller_config.scroller.interval && jscroller_config.scroller.direction == "down") {
        jscroller_startScroll();
      }
      jscroller_config.scroller.direction = "up";
      jscroller_config.scroller.speed = 4;
    },
    function() {
      jscroller_config.scroller.speed = 1;
    }
  );
  $('#down').hover(
    function() {
      if(!jscroller_config.scroller.interval && jscroller_config.scroller.direction == "up") {
        jscroller_startScroll();
      }
      jscroller_config.scroller.direction = "down";
      jscroller_config.scroller.speed = 4;
    },
    function() {
      jscroller_config.scroller.speed = 1;
    }
  );
  $('#scroller').hover(
    function() {
      jscroller_config.scroller.speed = 0;
    },
    function() {
      jscroller_config.scroller.speed = 1;
    }
  );
  function jscroller_doScroll() {
    if (scroller_dom = jscroller_getElem(jscroller_scroller.attr("id"))) {
      var	p_top= Number((/[0-9-,.]+/.exec(jscroller_scroller.css('top'))||0)), p_left=Number((/[0-9-,.]+/.exec(jscroller_scroller.css('left'))||0)), min_height=jscroller_scroller_cont.height(), min_width=jscroller_scroller_cont.width(),	speed=jscroller_config.scroller.speed, p_height=scroller_dom.offsetHeight, p_width=scroller_dom.offsetWidth, direction=jscroller_config.scroller.direction,	jscroller=jscroller_scroller;
      switch(direction) {
      case 'up':
        if (p_top <= min_height-p_height) {jscroller_stopScroll();}
        jscroller.css('top',p_top-speed+'px');
        break;
      case 'right':
        if (p_left >= min_width) {p_left=-1*p_width;}
        jscroller.css('left',p_left+speed+'px');
        break;
      case 'left':
        if (p_left <= -1*p_width) {p_left=min_width;}
        jscroller.css('left',p_left-speed+'px');
        break;
      case 'down':
        if (p_top >= 0) {p_top=-speed;jscroller_stopScroll();}
        jscroller.css('top',p_top+speed+'px');
        break;
      }
    }
  }
  preloadPhotos(preload_photos);
});
