/**
 * @author 		Serve it V.O.F.
 * @copyright	Copyright (c) Serve it V.O.F. 2010
 */
Ext.ns("Illustratie");
Illustratie.Wisselend = Ext.extend(Ext.util.Observable, {
	pzid: null,
	strAjaxURL: strCurrentURL,

	autoStart: true,
	intFotoDuration: 2000,
	$divFotoHolder: null,
	intFadeDuration: 6500,

	blnLoading: false,

	constructor: function(config) {
		Ext.apply(this, config || {});

		this.$divFotoHolder = $("#zmod_illustratie_afbeelding_" + this.pzid).eq(0);

		Illustratie.Wisselend.superclass.constructor.call(this, config);

		if (this.autoStart)
			this.start();
	},

	start: function() {
		var _this = this;

		setInterval(function() {
			_this.wisselIllustratie.call(_this);
		}, this.intFotoDuration + this.intFadeDuration);
	},

	wisselIllustratie: function() {
		var _this = this;

		if (this.blnLoading) return;
		_this.blnLoading = true;

		$.ajax({
			data: {
				_ajax: true,
				method: "zmod_illustratie_geefRandomAfbeelding",
				pzid: this.pzid
			},
			dataType: "xml",
			success: function(data, textStatus) {
				var $imgOld = _this.$divFotoHolder.find("img");

				$("<img/>")
					.attr({
						src: $("bestandsnaam", data).text() + "?rnd=" + Math.random()
					})
					.hide()
					.load(function() {
						_this.$divFotoHolder.append(this);
						$imgOld.fadeOut(_this.intFadeDuration, function() {
							$(this).remove();
						});
						$(this).fadeIn(_this.intFadeDuration, function() {
							/*setTimeout(function() {
								_this.wisselIllustratie.call(_this);
							}, _this.intFotoDuration);*/
						});

						_this.blnLoading = false;
					});
			},
			type: "POST",
			url: this.strAjaxURL
		})
	}
});
