var enlarge_div = null;
var enlarge_img = null;
var enlarge_txt = null;
var currentImgNum = -1;
var arrImgs;
function enlarge(img) {
	if (null != img) {
		var img_id = img.id.split('_')[1];
		var text_id = 'text_' + img_id;
		var img_text = document.getElementById(text_id).innerHTML;
		if (null == enlarge_div) {
			enlarge_div = document.getElementById("enlarge_div");
			enlarge_img = document.getElementById("enlarge_img");
			enlarge_txt = document.getElementById("enlarge_txt");
		}
		enlarge_img.src = "/images/cp/" + img_id + ".jpg";
		enlarge_img.onload = function() {
									var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
									enlarge_div.style.height = _docHeight;
									enlarge_div.style.display = "inline";
									arrImgs.setCurrent(img);
									enlarge_txt.innerHTML = img_text;
								}		
	} else {
		alert("img: " + img);	
	}
}

function closeEnlarge() {
	if (null == enlarge_div) {
		enlarge_div = document.getElementById("enlarge_div");
	}
	enlarge_div.style.display = "none";	
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return i;
		}
	}
	return -1;
};

Array.prototype.next = function () {
	switch(this.index) {
		case (this.length-1):
		  	this.index = 0;
		  	return this[0];
		  	break;
		default:
			this.index++;
		  	return this[this.index];
	}
}

Array.prototype.previous = function () {
	switch(this.index) {
		case 0:
		  	this.index = this.length - 1;
		  	return this[this.length-1];
		  	break;
		default:
			this.index--;
		  	return this[this.index-1];
	}			
}

Array.prototype.index = function (value) {
	this.index = value;
}

Array.prototype.setCurrent = function (el) {
	this.index = this.inArray(el);
}

function nextImage() {
	enlarge(arrImgs.next());
}

function previousImage() {
	enlarge(arrImgs.previous());
}

function findEnlargeableImages() {
	arrImgs = getElementsByClass("enlargeableImage");
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
