// 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, messageholder, flickr_users_url, imgs, opt_random_order) {
		this.placeholder = $(placeholder);
		this.messageholder = $(messageholder);
		this.flickr_users_url = flickr_users_url;
		this.imgs = imgs;
		this.random_order = opt_random_order;
		this.curImgNum = 0;
		this.curImg = null;
	},
	
	open_user: function(imgnum) {
	  this.messageholder.innerHTML = "Loading photosets from the requested user..."
	  location.href = this.flickr_users_url + this.imgs[this.curImgNum].owner;		
	},
	
	show: function() {
	  var imgEl = new Element('img', {src: this.imgs[this.curImgNum].src, id: "coverFlowImageId" });
	  var a = new Element('a', { style: "cursor:pointer"});
	  a.appendChild(imgEl);
	  this.placeholder.update(a);
	  Event.observe(a, 'click', this.open_user.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();
	}
});