var gallery = {

	imag_id: 'gallery_imag',
	desc_id: 'gallery_desc',
	imag_folder: 'images',
	loading_img: 'images/_loading.jpg',
	ajax_uri: 'index.php',

	thumbs: Array(),
	theimag: '',
	thedesc: '',

	init: function() {
		(new Image()).src = gallery.loading_img; // preload the loading image
		
		gallery.theimag = document.getElementById(gallery.imag_id);
		gallery.thedesc     = document.getElementById(gallery.desc_id);

		try {
			gallery.thumbs = document.getElementById('thumbs').getElementsByTagName('IMG');
			for(i=0; i<gallery.thumbs.length; i++) {
				gallery.thumbs[i].onclick = gallery.changeImage;
				// set in functions.php
				// thumbs[i].id = 'thumb_'+(i+1);
				gallery.thumbs[i].style.cursor = 'pointer';
			}
			if(gallery.thumbs[0]) gallery.changeImage(gallery.thumbs[0].id);
		} catch(e) { alert(e); }
	},

	changeImage: function(imgId) {
		if(this.id) imgId = this.id;
		if(!gallery.theimag) return;
		gallery.theimag.src = gallery.loading_img;
		
		for(i=0; i<gallery.thumbs.length; i++) {
			gallery.thumbs[i].className = (gallery.thumbs[i].id == imgId) ? 'at' : '';
		}
	
		var ajax = new gallery.ajax_request(gallery.ajax_uri,'action=showimage&img='+imgId.replace(/thumb_/i, ''));
		ajax.send();
	},

	ajax_changeImage: function(imgSrc, imgDesc) {
		if(!gallery.theimag) return;
		gallery.theimag.src = gallery.imag_folder+'/'+imgSrc;
		if(gallery.thedesc) gallery.thedesc.innerHTML = imgDesc;
	},
	
	ajax_request: function(uri, query) {
		this.uri = uri;
		this.query = query;
		this.send = function() {
			var t = new Date();
				t = t.getTime();
			ruri = this.uri+"?uid="+t+"&"+this.query;
			var html_doc = document.getElementsByTagName('head').item(0);
			var js = document.createElement('script');
			js.setAttribute('language', 'javascript');
			js.setAttribute('type', 'text/javascript');
			js.setAttribute('src', ruri);
			html_doc.appendChild(js);
		}
	},
	
	addLoadEvent: function() {
		var oldonload = window.onload;
		window.onload = (typeof oldonload == 'function') ? function() { oldonload(); gallery.init(); } : gallery.init;
	}
};

gallery.addLoadEvent();

