var DCGallery = [];
var DCGalleryCurrentImage = 0;
var DCGalleryTransitionTime = DCGlobalTransitionTime;

function DCGalleryItem() {
	this.imageURL;
	this.thumbURL;
	this.index;
	this.caption;
	this.width;
	this.height;
	this.ratio;
}

function DCBuildGalleryData() {
	for (var n = 0; n<$$(".DCImgURL").length; n++) {
		DCGallery[n] = new DCGalleryItem;
		for (var m = 0; m<$$(".DCImgURL").length; m++) {
			if (n+1 == parseInt($$(".DCImgIDX")[m].innerHTML)) {
				DCGallery[n].imageURL = $$(".DCImgURL")[m].innerHTML;
				DCGallery[n].thumbURL = $$(".DCImgPRE")[m].innerHTML;
				DCGallery[n].index = $$(".DCImgIDX")[m].innerHTML;
				DCGallery[n].caption = $$(".DCImgTXT")[m].innerHTML;
				DCGallery[n].width = $$(".DCImgWDT")[m].innerHTML;			
				DCGallery[n].height = $$(".DCImgHGT")[m].innerHTML;
				DCGallery[n].ratio = DCGallery[n].width/DCGallery[n].height; 		
			} 
		}
	}
}

function DCInitGallery() {
	$('DCImageCountCurrent').innerHTML = "1";
	$('DCImageCountTotal').innerHTML = DCGallery.length;
	$('DCImageThumb').src = DCGallery[0].thumbURL;
	DCSetThumbSize();
	$('DCImageSource').href = DCGallery[0].imageURL;
	$('DCImageCaption').innerHTML = DCGallery[0].caption;
	setupZoom();
}

function DCSetupGallery() {
	DCBuildGalleryData();
	DCInitGallery();
	Event.observe($('DCPrevImage'), "click", function() { DCMove('prev') });
	Event.observe($('DCNextImage'), "click", function() { DCMove('next') });
	setTimeout(function(){ $('DCGalleryContainer').show(); }, DCGalleryTransitionTime*10);	
}

function DCSetThumbSize() {
	if (DCGallery[DCGalleryCurrentImage].width < DCGallery[DCGalleryCurrentImage].height) {
		var ratio = 320/DCGallery[DCGalleryCurrentImage].height;
		$('DCImageThumb').setStyle({
			height: '320px',
			width: DCGallery[DCGalleryCurrentImage].width * ratio + 'px'
		});	
	} 
	if (DCGallery[DCGalleryCurrentImage].width > DCGallery[DCGalleryCurrentImage].height) {
		var ratio = 320/DCGallery[DCGalleryCurrentImage].width;
		$('DCImageThumb').setStyle({
			width: '320px',
			height: DCGallery[DCGalleryCurrentImage].height * ratio + 'px'
		});		
	}
	if (DCGallery[DCGalleryCurrentImage].width == DCGallery[DCGalleryCurrentImage].height) {
		$('DCImageThumb').setStyle({
			height: '320px',
			width: '320px'
		});		
	}
}

function DCChangeImage() {
		$('DCImageCaption').fade({ duration: DCGalleryTransitionTime/2 });
		$('DCImageThumb').fade({ duration: DCGalleryTransitionTime });
		setTimeout(function(){
			$('DCImageThumb').src = DCGallery[DCGalleryCurrentImage].thumbURL;
			DCSetThumbSize();
			$('DCImageCountCurrent').innerHTML = DCGallery[DCGalleryCurrentImage].index;
			$('DCImageCountTotal').innerHTML = DCGallery.length;
			$('DCImageSource').href = DCGallery[DCGalleryCurrentImage].imageURL;
			$('DCImageCaption').innerHTML = DCGallery[DCGalleryCurrentImage].caption;

			$('DCImageThumb').appear({ duration: DCGalleryTransitionTime, queue: 'end'});
			$('DCImageCaption').appear({ duration: DCGalleryTransitionTime/2 });
		}, DCGalleryTransitionTime*1000);
		setupZoom();		
}

function DCCheckGalleryBounds(direction) {
	if (direction == 'start') { if (DCGalleryCurrentImage > 0) DCGalleryCurrentImage--; }
	if (direction == 'end') { if (DCGalleryCurrentImage < DCGallery.length-1) DCGalleryCurrentImage++; }
}

function DCMove(direction) {
	if ((direction == "next") && (DCGalleryCurrentImage != DCGallery.length-1)) { DCCheckGalleryBounds('end'); DCChangeImage(); }
	if ((direction == "prev") && (DCGalleryCurrentImage != 0)) { DCCheckGalleryBounds('start'); DCChangeImage(); }
}

/* Event.observe(window, "load", function() { DCSetupGallery(); }); */