// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Slideshow = Class.create({
	initialize: function(placeholder, imgs, opt_random_order) {
		this.placeholder = $(placeholder);
		this.imgs = imgs;
		this.random_order = opt_random_order;
		this.curImgNum = 0;
		this.curImg = null;
	},
	
	open_link: function(imgnum) {
          if (this.imgs[this.curImgNum].link) {
	    //location.href = this.imgs[this.curImgNum].link;		
            window.open(this.imgs[this.curImgNum].link,"_blank",""); 
          }
	},

	show: function() {
          if (this.imgs[this.curImgNum].link) {
            var imgEl = new Element('img', {src: this.imgs[this.curImgNum].src, id: "coverFlowImageId", 'class': "linkata" });
          } else {
            var imgEl = new Element('img', {src: this.imgs[this.curImgNum].src, id: "coverFlowImageId" });
          }

          var a = new Element('div', { style: ""});
	  a.appendChild(imgEl);
	  this.placeholder.update(a);
          Event.observe(a, 'click', this.open_link.bind(this, this.curImgNum));
	  //cvi_reflex.add(document.getElementById("coverFlowImageId"), { tilt: "right", distance: 5, border: 10, transparency: 80,color: "#ffffff" });
	  Effect.Appear(this.placeholder, {duration: 0.5});
	  setTimeout(this.load_next_image.bind(this),5000);	  
	},
	
	fade_and_show: function() {
	  Effect.Fade(this.placeholder, { duration: 0.5, afterFinish: this.show.bind(this)});
	},
	
	load_next_image: function() {
	  if (this.random_order) {
		this.curImgNum = Math.floor(Math.random()*this.imgs.length);
    } else {
      this.curImgNum += 1;
      if (this.curImgNum == this.imgs.length) {
        this.curImgNum = 0;
      }
    }
	  this.curImg = new Image ;
	  this.curImg.onload = this.fade_and_show.bind(this);
	  this.curImg.src = this.imgs[this.curImgNum].src;
	},
	
	start: function() {
		this.load_next_image();
	}
});

