var WMSlideshow = Class.create();

/**
 * WMSlideshow is the javascript class for slideshows in hollandfit (copied from stemat on Wed Apr 20 2011)
 *
 * ==Changelog==
 *
 * Reyo Stallenberg - Thu Mar 27 2008
 * ------------------------------
 * - Totally refactored, can now handle multiple images with only one instance
 *
 * @since unknown
 * @author unknown
 **/
WMSlideshow.prototype = {
	/**
	 * initialize
	 *
	 * Initialize a new WMSlideshow
	 *
	 * @since unknown
	 * @access public
	 * @param integer speed
	 * @param integer duration
	 * @param integer pause
	 * @return void
	 **/
	initialize: function(windmillid, speed, duration, pause) {
		this.windmillid = windmillid;
		this.speed = speed;
		this.duration = duration;
		this.pause = pause;
		this.slideshows = {};
	},

	/**
	 * start
	 *
	 * Does some cleanup on this.slideshows and creates an observer for the pageload
	 *
	 * @since unknown
	 * @access public
	 * @return void
	 **/
	start: function() {
		//first do some cleanup
		var slideshows = [];
		$H(this.slideshows).each(function(slideshow) {
			var cid = slideshow[0];
			var slideshow = slideshow[1];
			slideshow["cid"] = cid;
			slideshow["current"] = 0;
			slideshows.push(slideshow);
		}.bind(this) );
		this.slideshows = slideshows;

		//then we'll wait
		Event.observe(window, "load", this.slide.bindAsEventListener(this) );
	},

	changed: function() {
		document.location.reload();
	},

	/**
	 * doSlide
	 *
	 * Calls the opacity change and checks time
	 *
	 * @since unknown
	 * @access public
	 * @return void
	 **/
	doSlide: function() {
		this.time += this.speed;
		var firstopacity = Math.cos((this.time / this.duration) * (Math.PI/2));
		var secondopacity = Math.pow((this.time / this.duration), 8);

		this.slideshows.each(function(slideshow){
			this.setOpacity($(slideshow.cid + "_first_" + this.windmillid), firstopacity);
			this.setOpacity($(slideshow.cid + "_second_" + this.windmillid), secondopacity);
		}.bind(this) );

		if (this.time < this.duration) {
			this.doSlide.bind(this).delay(this.speed / 1000);
		}
		else { 
			this.slideshows.each(function(slideshow){
				$(slideshow.cid + "_first_" + this.windmillid).innerHTML = $(slideshow.cid + "_second_" + this.windmillid).innerHTML;
				this.setOpacity($(slideshow.cid + "_first_" + this.windmillid), 1);
				this.setOpacity($(slideshow.cid + "_second_" + this.windmillid), 0);
			}.bind(this) );
			this.slide.bind(this).delay(this.pause / 1000);
		}
	},

	/**
	 * setOpacity
	 *
	 * Does the opacity change
	 *
	 * @since unknown
	 * @access public
	 * @param element element
	 * @param float opacity
	 * @return void
	 **/
	setOpacity: function(element, opacity) {
		if (element.filters && element.filters.alpha) {
			element.filters.alpha.opacity = Math.round(opacity * 100);
		}
		else {
			element.style.opacity = opacity;
		}
	},

	/**
	 * slide
	 *
	 * Sets stuff up initial to the real slide and the calls the real slide
	 *
	 * @since unknown
	 * @access public
	 * @return void
	 **/
	slide: function() {
		this.time = 0;

		this.slideshows.each(function(slideshow){
			this.setOpacity($(slideshow.cid + "_first_" + this.windmillid), 1);
			this.setOpacity($(slideshow.cid + "_second_" + this.windmillid), 0);

			if ($(slideshow.cid + "_first_" + this.windmillid).innerHTML == "") {
				$(slideshow.cid + "_first_" + this.windmillid).innerHTML = slideshow.images[0].html;
		}		

			slideshow["next"] = slideshow.current + 1;
		
			if (slideshow.next == slideshow.images.length) {
				slideshow.next = 0;
		}
		
			if (slideshow.next == slideshow.current) {
				return;
			}
		
			var nextimage = slideshow.images[slideshow.next];
			$(slideshow.cid + "_second_" + this.windmillid).innerHTML = nextimage.html;
		
			slideshow.current = slideshow.next;

		}.bind(this));

		this.doSlide();
	}
};

var WMImage = Class.create();
/**
 * WMImage is the javascript class for images in slideshows in shipmate
 *
 * @since unknown
 * @author unknown
 **/
WMImage.prototype = {
	/**
	 * initialize
	 *
	 * Initialize a new WMImage
	 *
	 * @since unknown
	 * @access public
	 * @param string url
	 * @param integer width
	 * @param integer height
	 * @return void
	 **/
	initialize: function(url) {
		this.html = this.getHtml(url);
	},

	/**
	 * getHtml
	 *
	 * Creates html string for an image and returns it
	 *
	 * @since unknown
	 * @access public
	 * @return string
	 **/
	getHtml: function(url) {
		var image = new Image();
		Event.observe(image, "error", this.imageError.bindAsEventListener(this), false);
		image.src = url;
		return "<img alt='' src='" + url + "'/>";
	},

	imageError: function() {
		this.html = "<img alt='' src='/images/empty.gif'/>";
	}
};

