var currImage;

function initGalleryImages() {
	if($('gallery_selector')) {
		var thumbs = $('gallery_selector').getElementsByTagName('li');
		for(var x=0; x < thumbs.length; x++) {
			thumbs[x].onclick = function() {changeImage(this);};
		}
	}
	
	if($('big_images')) {
		var images = $('big_images').getElementsByTagName('div');
		for(var y=0; y < images.length; y++) {
			if(images[y].id) {
				$(images[y]).style.display = "none";
				$(images[y]).setOpacity(0.0);
			}
		}
		$(images[0]).style.display = "block";
		$(images[0]).setOpacity(1.0);
		currImage = images[0].id;

		$(getCounterpart(currImage)).addClassName('active');
	}
}

function changeImage(obj) {
	var id = getCounterpart(obj.id);
	
	if(id != currImage) {
		if($(id)) { //image exists in the markup
			$(getCounterpart(currImage)).removeClassName('active');
			if($(id).getElementsByTagName('embed').length > 0 || $(currImage).getElementsByTagName('embed').length > 0) {
				$(id).style.display = "block";
				$(currImage).style.display = "none";
			} else {
				Effect.Fade(currImage,{ duration:0.5, from:1.0, to:0.0});
				Effect.Appear(id,{ duration:0.5, from:0.0, to:1.0});			
			}
			currImage = id;
			$(getCounterpart(currImage)).addClassName('active');	
		} else { //we need to get it with ajax
			$(getCounterpart(currImage)).removeClassName('active');
			var old_image = currImage;
			currImage = id;
			var new_thumb = $(getCounterpart(currImage));
			//var objLoadingImage = document.createElement("img");
			//objLoadingImage.setAttribute('src', '/images/loading.gif');
			//objLoadingImage.setAttribute('id','gallery_loading');
			//new_thumb.appendChild(objLoadingImage);
			new_thumb.addClassName('active');
			imageId = id.toString().split('\_')[1];
			urlString = 'id='+imageId+'&form=gallery_big_ajax_just_image';
			
			new Ajax.Request('/get_image',{
				parameters: urlString, 
				method: 'post', 
				onSuccess: function(originalRequest) {
					new Insertion.Bottom('big_images',originalRequest.responseText);
					$(currImage).style.display = "none";
					if($(currImage).getElementsByTagName('H1').length > 0 || $(old_image).getElementsByTagName('embed').length > 0) {
						$(currImage).style.display = "block";
						$(old_image).style.display = "none";
					} else {
						Effect.Fade(old_image,{ duration:0.5, from:1.0, to:0.0});
						Effect.Appear(currImage,{ duration:0.5, from:0.0, to:1.0});			
					}
					//initLightbox();
					//var gloader = $('gallery_loading');
					//Effect.Fade(gloader,{ duration:0.5, from:1.0, to:0.0});
					//gloader.parentNode.removeChild(gloader);
				}});

		}
		
	}
}

function getCounterpart(inString) {
	var array = Array();
	array = inString.toString().split('\_');
	var front;
	if(array[0] == 'thumb') {
		front = 'image_';
	} else {
		front = 'thumb_';
	}
	var id = front+array[1].toString();
	
	return id;
}

function handleGalleryButtons(button, enabled) {
 posx = '0px';
 if (button == "gallery_left") {
	$('gallery_left').style.display = enabled ? 'block' : 'none';
	$('gallery_left').style.cursor = enabled ? 'pointer' : 'default';
	return false;
 }
 else {
	$('gallery_right').style.display = enabled ? 'block' : 'none';
	$('gallery_right').style.cursor = enabled ? 'pointer' : 'default';
	return false;
 }
 return false;
}

function handleGalleryButtonsRecognition(button, enabled) {
 posx = '0px';
 if (button == "recog_up") {
	$('recog_up').style.display = enabled ? 'block' : 'none';
	$('recog_up').style.cursor = enabled ? 'pointer' : 'default';
	return false;
 }
 else {
	$('recog_down').style.display = enabled ? 'block' : 'none';
	$('recog_down').style.cursor = enabled ? 'pointer' : 'default';
	return false;
 }
 return false;
}

addLoadEvent(initGalleryImages);
