var images_id = new Array(), images_thumb = new Array(), images_large = new Array(), images_hr = new Array(), zindex = 9999;

var total_thumbs = images_id.length, active_thumb = 0, active_marc_thumb = 0, total_marc_thumbs = 0;

function initGallery() {
	if (total_thumbs>0) {
		for (i=0;i<total_thumbs;i++) {
			if ( (i==0)||(i==6)||(i==12)||(i==18)||(i==24) ) {
				total_marc_thumbs = i;
				var display = (i==0) ? 'inline' : 'none';
				var element = Builder.node("div", { id: "marc_thumbs_"+total_marc_thumbs, className: "marc_thumbs", style: "display: "+display });
				$("thumbs").appendChild(element);
			}
			var element = Builder.node("div", { id: "thumb_"+i, className: "thumb_off" });
			$("marc_thumbs_"+total_marc_thumbs).appendChild(element);
			var element = Builder.node("img", { src: images_thumb[i], width: "81", height: "61", alt: "", id: "thumb_img_"+i, className: "thumb_img", onclick: "selectThumb("+i+")" });
			$("thumb_"+i).appendChild(element);
		}
		selectThumb(0);
	}
}

function selectThumb(id) {
	$("thumb_visor").update("");
	var element = Builder.node("img", { src: images_large[id], alt: "", id: "img_thumb", className: "thumbing", style: "display: none;", rel : "lightbox", onmousedown: "active_thumb="+id+";zindex+=1;", onload: "showThumb();" });
	$("thumb_visor").appendChild(element);
	$("thumb_"+active_thumb).className = "thumb_off";
	active_thumb = id;
	$("thumb_"+active_thumb).className = "thumb_on";
	var ampliar = "";
	for (i=0;i<total_thumbs;i++) {
		ampliar += (i!=id) ? '<a href="'+images_hr[i]+'" rel="lightbox" style="display: none;">x</a>' : '<a href="'+images_hr[i]+'" rel="lightbox" style="float: right;"><img src="fileadmin/templates/img/ampliar.jpg" width="48" height="10" alt="AMPLIAR" /></a>';
	}
	id++;
	$("thumb_counter").update('<span style="float: left;">&nbsp;'+id+'_'+total_thumbs+'</span>');
	$("thumb_amplia").update(ampliar);
	var marc = 0;
	if (id>24) {
		marc = 24;
	} else if (id>18) {
		marc = 18;
	} else if (id>12) {
		marc = 12;
	} else if (id>6) {
		marc = 6;
	}
	if (marc!=active_marc_thumb) {
		$("marc_thumbs_"+active_marc_thumb).setStyle({"display":"none"});
		active_marc_thumb = marc;
		$("marc_thumbs_"+active_marc_thumb).setStyle({"display":"inline"});
	}
}

function paginateThumbs(what){
	var thumbing = active_thumb+what;
	if (thumbing>(total_thumbs-1)) {
		thumbing = 0;
	} else if (thumbing<0) {
		thumbing = total_thumbs-1;
	}
	selectThumb(thumbing);
}

function paginateMarcThumbs(what){
	var marc = active_marc_thumb+what;
	if (marc>(total_marc_thumbs)) {
		marc = 0;
	} else if (marc<0) {
		marc = total_marc_thumbs;
	}
	if (marc!=active_marc_thumb) {
		$("marc_thumbs_"+active_marc_thumb).setStyle({"display":"none"});
		active_marc_thumb = marc;
		$("marc_thumbs_"+active_marc_thumb).setStyle({"display":"inline"});
	}
}

function showThumb() {
	Effect.Appear("img_thumb");
}