var Archive = Class.create();
Archive.prototype = {
	dateBar: null,
	cameraId: -1,
	imageElements: null,
	firstId: 0,
	lastId: 0,
	hasNext: false,
	hasPrevious: true,
	loadingFinishedListener: null,
	date: null,
	loading: false,
	preset: -1,
	count: -1,
	initialize: function(dateBar, cameraId, firstId, lastId) {
		this.dateBar = dateBar;
		this.dateBar.setDateChangeListener(
			this.dateChangeListener.bind(this));
		this.cameraId = cameraId;
		
		// get all image elements in the container
		this.imageElements = $$('#archive_table img');
		this.count = this.imageElements.length;
		
		this.firstId = firstId;
		this.lastId = lastId;
		
		Event.observe('archive_next', 'click', 
			this.nextImages.bindAsEventListener(this));
		Event.observe('archive_prev', 'click',
			this.previousImages.bindAsEventListener(this));
		Event.observe('archive_positions_select', 'change',
			this.positionChanged.bindAsEventListener(this));
		
	},
	setLoadingFinishedListener: function(listener) {
		this.loadingFinishedListener = listener;
	},
	setPreset: function(preset) {
		if (this.loading)
			return;
			
		this.loading = true;
		this.preset = preset;
		this.date = null;
		// retrieve the last images taken from the new preset position
		SiteCam.apiCall(
			'Archive',
			this.cameraId,
			'set_preset',
			$H({before: (this.lastId+1), preset: this.preset, count: this.count}),
			this.updateImages.bind(this)
		);
	},
	nextImages: function() {
		if (this.loading)
			return;
		this.loading = true;
		this.date = null;
		if (this.hasNext) {
			SiteCam.apiCall(
				'Archive',
				this.cameraId,
				'get_next',
				$H({after: this.lastId, preset: this.preset, count: this.count}),
				this.updateImages.bind(this)
			);
		}
		else {
			this.loading = false;
		}
	},
	previousImages: function() {
		if (this.loading)
			return;
		this.loading = true;
		this.date = null;
		if (this.hasPrevious) {
			SiteCam.apiCall(
				'Archive',
				this.cameraId,
				'get_previous',
				$H({before: this.firstId, preset: this.preset, count: this.count}),
				this.updateImages.bind(this)
			);
		}
		else {
			this.loading = false;
		}
	},
	positionChanged: function(e) {
		var preset_id = Event.element(e).value;
		this.setPreset(preset_id);
	},
	dateChangeListener: function(date) {
		if (this.loading)
			return;
		this.loading = true;
		this.date = date;
		var fromTimeStamp = date.toUnixTimestamp();
		SiteCam.apiCall(
			'Archive',
			this.cameraId,
			'get_from',
			$H({from: fromTimeStamp, preset: this.preset, count: this.count}),
			this.updateImages.bind(this)
		);
	},
	/**
	 * This function is used by updateImages
	 */
	updateImage: function(idx, img) {
		//this.imageElements[idx].up
		//new LoadImage(img.thumb, this.imageElements[idx]);
		
		var a = this.imageElements[idx].up('a');
		if (img.full != "") {
			a.href = img.full;
			a.rel = 'lightbox[arch]';
			a.setAttribute('title', img.text);
			this.imageElements[idx].setAttribute('alt', img.text);
			SiteCamLightbox.getLightbox().enableFor(a);
		}
		else {
			a.rel = '';
			a.href = 'javascript:void(0);';
			a.setAttribute('title', '');
			this.imageElements[idx].setAttribute('alt', '');
			SiteCamLightbox.getLightbox().disableFor(a);
		}
		return idx + 1;
	},
	finishedListener: function() {
		this.loading = false;
	},
	updateImages: function(transport) {
		try {
		var result = transport.responseText.evalJSON();
		this.firstId = result.firstId;
		this.lastId = result.lastId;
		
		if (this.hasNext != result.hasNext) 
			$('archive_next').toggleClassName('disabled');
		if (this.hasPrevious != result.hasPrevious)
			$('archive_prev').toggleClassName('disabled'); 
		
		this.hasNext = result.hasNext;
		this.hasPrevious = result.hasPrevious;
		
		// load new images
		var imgs = result.imgs;
		imgs.inject(0, this.updateImage.bind(this));
		
		var thumbs = new Array();
		for (var i = 0; i < imgs.length; ++i) {
			thumbs.push(imgs[i].thumb);
		}
		
		var lI = new LoadImages(thumbs, this.imageElements);
		lI.setFinishedListener(this.finishedListener.bind(this));
		
		// call done handler
		if (this.loadingFinishedListener != null) 
			this.loadingFinishedListener();
			
		if (this.date == null) {
			this.date = new Date(result.fromYear, result.fromMonth - 1, result.fromDay);
		}
		this.dateBar.gotoDate(this.date);
		
		} catch(e) {alert('Archive::updateImages -> ' + e + '\n' + transport.responseText);}
	}
};
