//-------------------------------------------------------------------------------------
//	SimpleView Gallery Lite v2.0
//	(c) 2007 Chris Jaure
//	SimpleView Gallery is freely distributable under the terms of an MIT-style license.
//	website: http://www.chromasynthetic.com/
//
//	svgscript.js
//-------------------------------------------------------------------------------------
var simpleView = { // required element ids: sv_image, sv_first, sv_previous, sv_next, sv_last, sv_archive, sv_archive_dd, sv_number
	files: [],
	objects: {},
	offset: 1,
	change: function (num) {
	  if (num >= 1 && num <= this.files.length && num!=this.offset) { // if image offset is valid then change src
	    this.loading();
			this.objects.image.src = this.files[num - 1];
			this.objects.image.title = this.files[num - 1];
			this.offset = num;
			this.objects.number.innerHTML = '[ ' + num + ' / ' + this.files.length + ' ]';
			this.preload();
			this.setClass(null, [this.objects.first, this.objects.previous, this.objects.next, this.objects.last]);
			if (num <= 1) { //check whether first and previous links should be disabled
	      this.setClass("disabled", [this.objects.first, this.objects.previous]);
	    }
			if (num >= this.files.length) { // check whether next and last links should be disabled
	      this.setClass("disabled", [this.objects.next, this.objects.last]);
			}
			if (window.opera && this.objects.image.complete) {
				this.loaded();
			}
		}
	},
	setClass: function (to_class, objs) {
		for (var i = 0; i < objs.length; i++) objs[i].className = to_class;
	},
	loading: function () {
		this.objects.loading.style.visibility = "visible";
		document.body.style.cursor = "wait";
	},
	loaded: function () {
		simpleView.objects.loading.style.visibility = "hidden";
    document.body.style.cursor = "default";
	},
	preload: function () {
	  if (this.files.length > this.offset) {
			this.objects.nextImage = new Image();
			this.objects.nextImage.src = this.files[this.offset];
		}
		if (this.offset > 1) {
			this.objects.prevImage = new Image();
			this.objects.prevImage.src = this.files[this.offset - 2];
		}
	},
	init: function (files, offset) { // must be called on window load
		this.files = files;
		this.offset = offset;
	  this.objects.image = document.getElementById("sv_image");
	  if (this.objects.image) {
		  this.objects.image.onload = simpleView.loaded;
			this.objects.first = document.getElementById("sv_first");
		  this.objects.first.onclick = function () {
			  this.blur();
				simpleView.change(1);
				return false;
			};
		  this.objects.previous = document.getElementById("sv_previous");
		  this.objects.previous.onclick = function () {
			  this.blur();
				simpleView.change(simpleView.offset - 1);
				return false;
			};
			this.objects.next=document.getElementById("sv_next");
			this.objects.next.onclick = function () {
			  this.blur();
				simpleView.change(simpleView.offset + 1);
				return false;
			};
			this.objects.last = document.getElementById("sv_last");
			this.objects.last.onclick = function () {
			  this.blur();
				simpleView.change(simpleView.files.length);
				return false;
			};
			document.forms["sv_archive"].onsubmit = function () {
				simpleView.change(+this.elements["sv_archive_dd"].value);
				return false;
			};
			///////////////////////////////////////
			document.getElementById("simpleView").appendChild(objLoading);
			this.objects.loading = document.getElementById("sv_loading");
			this.objects.number = document.getElementById("sv_number");
		}
		this.preload();
	}
};
