if(!window.YUKU) window.YUKU = {};
window.YUKU.slideshow = {
 radius: 300,
 preloadOffset: 6,
 MaxTicSpeed: 8000,
 minTicSpeed: 1100,
 ticSpeed: 4000,


 init: function(slidesHolderId, slidesControlsId, playbackControlsId, currentSlideId, speedIndicatorId, loadingIndicatorId) {
    this.speedIndicatorId = speedIndicatorId;
    this.currentSlideId = currentSlideId;
    this.loadingIndicatorId = loadingIndicatorId;
    var slidesHolder = get_by_id(slidesHolderId);
    this.addTempSlides(slidesHolder);
    this.initScroller(slidesHolder);
    this.initSlider(speedIndicatorId);
    var currentSlide = get_by_id(currentSlideId);
    var el = get_by_tag('IMG',currentSlide)[0];
    currentSlide.style.height = el.style.height = '1px';
    currentSlide.style.width = el.style.width = '1px';
    el.style.visibility = 'hidden';
    addEvent(get_by_id(slidesControlsId),'click',this.controlsClick);
    addEvent(get_by_id(playbackControlsId),'click',this.controlsClick);
    YUKU.slideshow.startTicing(1);
  },


 showLoader: function(){
   get_by_id(this. loadingIndicatorId).style.display = 'block';
  },


 hideLoader: function() {
   get_by_id(this. loadingIndicatorId).style.display = 'none';
  },


 addTempSlides: function(holder) {
    kill_children(holder);
    var holderImgSrc = this.holderImage;
    for(var i = 0, a, item, items = this.slides;
	item = items[i];i++) {
      a = make_el('A',{href: item.location,
	    itemIndex: i,
	    title: item.name},holder);
      make_el('IMG',{src:holderImgSrc}, a);
    }
  },


 initScroller: function(holder) {
    holder.className = 'thumbslider';
    this.scrl3D = new Scroller3d(holder.id, this.radius, this.minTicSpeed-100, this.loadSlide);
    this.scrl3D.draw(this.currentSlide);
  },


 showNewSpeed: function() {
    var speedIndicator = get_by_id(this.speedIndicatorId);
    speedIndicator.innerHTML = Math.round(this.ticSpeed/1000) + ' sec';
  },


 updateSpeed: function(speedOffset) {
    var sl = YUKU.slideshow;
    var newSpeed = Math.round(sl.MaxTicSpeed*speedOffset);
    if(newSpeed < sl.minSpeed) newSpeed = sl.minSpeed;
    sl.ticSpeed = newSpeed;
    if(sl.isTicing) {
      sl.stopTicing();
      sl.startTicing();
    }
    sl.showNewSpeed();
  },


 initSlider: function(id) {
    var holder = get_by_id(id).parentNode;
    var sliderBar = new SliderBar(this.updateSpeed);
    holder.appendChild(sliderBar.getSlider());
    sliderBar.setValue(0.5);
    this.showNewSpeed();
  },


 loadSlide: function(item) {
    var sl = YUKU.slideshow;
    sl.showLoader();
    sl.wasTicing = sl.isTicing;
    sl.stopTicing();
    sl.preloadThumbs(item.itemIndex);
    var currentSlide = get_by_id(sl.currentSlideId);
    var img = get_by_tag('IMG',currentSlide)[0];
    currentSlide.style.width=img.width+'px';
    currentSlide.style.height=img.height+'px';
    sl.fadeSlide(getId(img),100,1);
    img = make_el('IMG',currentSlide);
    img.style.display = 'none';
    img.onload = sl.switchSlides;
    img.src = item.href;
    currentSlide.href = item.href;
 },


 fadeSlide: function(id,from,to) {
    an = new Animator();
    var fade = function(val) {
      var el = get_by_id(id);
      if(el) {
	el.style.display = '';
	set_opacity(el,val);
      }
      else an.stop();
    };
    an.start(fade,from,to,'out',200);
  },


 switchSlides: function() {
    var sl = YUKU.slideshow;
    var currentSlide = get_by_id(sl.currentSlideId);
    remove_el(get_by_tag('IMG',currentSlide)[0]);
    var fromX = currentSlide.clientWidth, fromY = currentSlide.clientHeight;
    this.style.marginLeft = '-50000px';
    this.style.display = '';
    var Xoffset = this.width-fromX,Yoffset = this.height-fromY;
    this.style.display = 'none';
    this.style.marginLeft = '';
    img = currentSlide = null;
    var an = new Animator();
    var resize = function(val) {
      var currentSlide = get_by_id(sl.currentSlideId);
      currentSlide.style.width = fromX + Math.round(val/100*Xoffset) + 'px';
      currentSlide.style.height = fromY + Math.round(val/100*Yoffset) + 'px';
      if(val==100) {
	an.stop();
	curentSlide = get_by_tag('IMG',currentSlide)[0];
	sl.hideLoader();
	sl.fadeSlide(getId(curentSlide),0,100);
	if(sl.wasTicing) {
	  sl.startTicing();
	  sl.wasTicing = false;
	}
      }
    };
    an.start(resize,0,100,'out',200);
  },


 preloadThumbs: function(indx) {
    var low = indx-this.preloadOffset, high = indx+this.preloadOffset;
    var items = this.slides;
    if(low < 0) low = 0;
    if(high > items.length-1) high = items.length-1;
    while(low <= high) {
      this.loadThumb(items[low].thumb,low++);
      this.loadThumb(items[high].thumb,high--);
    }
  },


 loadThumb: function(src, indx) {
    var img = get_by_tag('IMG',this.scrl3D.ring.getItems()[indx])[0];
    if(img.src != src)
      img.src = src;
  },


 controlsClick: function(e) {
    var srcEl = get_srcEl(e);
    while(srcEl.tagName != 'A'
	  && (srcEl = srcEl.parentNode));
    if(srcEl) {
      preventDefault(e);
      var wasTicing =  false;
      var sl = YUKU.slideshow;
      if(className.test(srcEl, 'previous')) {
	wasTicing = sl.stopTicing();
	sl.scrollByNSlides(-1);
	if(wasTicing)sl.startTicing();
      }
      else if(className.test(srcEl, 'next')) {
	wasTicing = sl.stopTicing();
      	sl.scrollByNSlides(1);
	if(wasTicing)sl.startTicing();
      }
      else if(className.test(srcEl, 'close'))
      	window.history(-1);
      else if(className.test(srcEl, 'play')) {
      	sl.startTicing(1);
      	sl.tic();
      }
      else if(className.test(srcEl, 'playbackward')) {
      	sl.startTicing(-1);
      	sl.tic();
      }
      else if(className.test(srcEl, 'pause'))
      	sl.stopTicing();
    }
  },


 scrollByNSlides: function(indx) {
    var curIndx = this.scrl3D.ring.currentIndex;
    indx = (Math.round(curIndx) - curIndx)+indx;
    this.scrl3D.scrollBy(indx);
  },


 startTicing: function(by) {
    if(by)this.ticBy = by;
    this.stopTicing();
    this.ticId = setInterval(function(){
	YUKU.slideshow.tic();}, this.ticSpeed);
    this.isTicing = true;
  },


 tic: function() {
    this.scrollByNSlides(this.ticBy);
  },


 stopTicing: function() {
    clearInterval(this.ticId);
    var wasTicing = this.isTicing;
    this.isTicing = false;
    return wasTicing;
  }
}
