var ImagesImporter = Class.create();
/**
 * ImagesImporter - grab images from remote server and build images list
 */
ImagesImporter.prototype = {

	parentElementId: '',
	imgClass: '',
	imgOutlineClass: '',
	layout: 'float',
	layTabRows: 4,
	targetUrl: 'readimages.php',
	imgRelativePath: 'images',
	imgThumbRelativePath: 'images/thumbs',
	imgBaseUrl: '',
	maxThumbSize: 0,
	response: '',

	/**
	 * Initialize
	 * @param {Object} cfgLocal		configuration object (local data)
	 * @param {Object} cfgRemote	configuration object (remote data)
	 */
	initialize :function(cfgLocal, cfgRemote) {

		// reprint parameters
		this.parentElementId = cfgLocal.target || '';
		this.layout = cfgLocal.layout || this.layout;
		this.layTabRows = cfgLocal.layTabRows || this.layTabRows;
		this.imgClass = cfgLocal.imgClass || this.parentElementStyle;
		this.imgOutlineClass = cfgLocal.imgOutlineClass || this.imgOutlineClass;

		this.targetUrl = cfgRemote.targetUrl || this.targetUrl;
		this.imgRelativePath = cfgRemote.imgPath || this.imgRelativePath;
		this.imgThumbRelativePath = cfgRemote.imgThumbsPath || this.imgThumbRelativePath;

		this.maxThumbSize = cfgRemote.maxThumbSize || this.maxThumbSize;

		// send request for images listing
		this.sendRequest();
	},

	/**
	 * Send request (Ajax or direct using <script src>)
	 */
	sendRequest :function() {

		var url = this.targetUrl +
			'?imgDir=' +
			encodeURIComponent(this.imgRelativePath) +
			(this.maxThumbSize > 0 ?
			'&maxThumbSize='+this.maxThumbSize :
			'&imgThumbDir='+encodeURIComponent(this.imgThumbRelativePath));
/*
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: (function(transport){
		 	this.processResponse(transport);
		 }).bind(this)
		});
*/
		var sc = document.createElement('script');
		sc.type = 'text/javascript';
		sc.src = url;
		document.body.appendChild(sc);
	},

	/**
	 * process JSON reply form 'readimages.php'
	 * @param {Object} transport
	 */
	processResponse :function(transport) {

		if (transport.responseJSON != undefined) {

			// handle ajax request transport
			this.response = transport.responseJSON;
		}
		else {

			// handle direct JSON
			this.response = transport;
		}

		if (this.response != null) {

			if (this.layout == 'horizontal' ||
				this.layout == 'vertical' ||
				this.layout == 'table') {

				// vertical, horizontal, table - put images on table
				var tab = Builder.node('table', { cellpadding: 0,
							cellspacing: 1, style: 'float:left;'});
				$(this.parentElementId).appendChild(tab);

				var tbody = Builder.node('tbody');
				tab.appendChild(tbody);

				if (this.layout == 'horizontal') {

					// horizontal - put on one line
					var tr = Builder.node('tr');
					tbody.appendChild(tr);
					el = tr;
				}
				else {

					// vertical - ech image new row
					el = tbody;
				}
			}
			else {

				// float mode - append directly
				el = $(this.parentElementId);
			}

			var col = 1;
			for (var i = 0; i < this.response.images.length; i++) {

				switch (this.layout) {

					case 'float':

						var div = Builder.node('div', {
							className: this.imgOutlineClass,
							style: 'float:left;'
						});
						el.appendChild(div);

						div.appendChild(this.constructItem(i));
					break;

					case 'horizontal':

						var td = Builder.node('td', {
							align: 'center',
							valign: 'middle',
							className: this.imgOutlineClass
						});
						td.appendChild(this.constructItem(i));

						el.appendChild(td);
					break;

					case 'vertical':

						var td = Builder.node('td', {
							align: 'center',
							valign: 'middle',
							className: this.imgOutlineClass
						});
						var tr = Builder.node('tr');
						td.appendChild(this.constructItem(i));
						tr.appendChild(td);
						el.appendChild(tr);
					break;

					case 'table':

					var td = Builder.node('td', {
						align: 'center',
						valign: 'middle',
						className: this.imgOutlineClass
					});
					td.appendChild(this.constructItem(i));

					if (col == this.layTabRows+1 ||
						tr == undefined) {

						// add new row
						var tr = Builder.node('tr');
						tr.appendChild(td);
						el.appendChild(tr);

						col = 1;
					}
					else {

						tr.appendChild(td);
					}

					col++;
					break;
				}
			}	// end of loop
		}
	},

	/**
	 * Construct single image item (img+a for Lightbox)
	 * @param {Object} i	images data offset
	 */
	constructItem :function(i) {

		var r = this.response;

		var a = Builder.node('a', {
			href: r.baseImgUrl + r.images[i],
			rel: "lightbox[" + this.parentElementId + "]"
		});

		var img = Builder.node('img', {
			src: r.baseImgThumbUrl + r.thumbs[i],
			className: this.imgClass
		});

		a.appendChild(img);

		return a;
	}
}

//document.observe('dom:loaded', function(){});
