// JavaScript Document

Event.observe(window, 'load', function() {
	if ($$('a.pic_control').length > 0) {
		$$('a.pic_control').each(function(ele) {
			ele.observe('click', function(e) {
				e.stop();
				changePicture(ele.getAttribute('id'));
			});
		});
	}
	
	if ($('picture_click')) {
		$('picture_click').observe('click', function(e) {
			e.stop();
			var ele = Event.element(e);
			
			window.open('images/listing_images/' + large_images[ele.getAttribute('name')]);
		});
	}
});

var changePicture = function(id) {
	if (id == 'picture_left') {
		images_count = (images_count == 0) ? images.length - 1 : images_count - 1;
	} else {
		images_count = (images_count == (images.length - 1)) ? 0 : images_count + 1;
	}
	
	var a = new Element('a').update(new Element('img', { src: 'images/listing_images/' + images[images_count], name: images_count })).observe('click', pictureClick);
	$('picture').update(a);
	$('pic_caption').update(captions[images_count]);
};

var pictureClick = function(e) {
	e.stop();
	var ele = Event.element(e);
	
	window.open('images/listing_images/' + large_images[ele.getAttribute('name')]);
};