function changeImagesWithOpacity(photogallery_id, makeVisible, id) {
	var myElementsEffects = new Fx.Elements($('main_picture_' + photogallery_id));
	
	opacity = makeVisible ? [0, 1] :  [1, 0];
	
	if (!makeVisible) {
		myElementsEffects.complete = function(){
			$('thumb_' + photogallery_id + '_' + pictures[photogallery_id].current).removeClass('photo_curr');
			
			pictures[photogallery_id].current = id;
			
			$('main_picture_' + photogallery_id).src = pictures[photogallery_id].items[id].src;
			$('main_picture_' + photogallery_id).width = pictures[photogallery_id].items[id].width;
			$('main_picture_' + photogallery_id).height = pictures[photogallery_id].items[id].height;
			
			$('counter_' + photogallery_id).set('html', id + '/' + pictures[photogallery_id].total); 
			$('thumb_' + photogallery_id + '_' + id).addClass('photo_curr');
			
			changeImagesWithOpacity(photogallery_id, true, id);
			
			this.cancel();
    	};
    }
    else {
    	myElementsEffects.complete = function() {

			if (id == pictures[photogallery_id].total) {
				$('view_next_' + photogallery_id).addClass('photo_str_' + lang);
			}
			else {
				$('view_next_' + photogallery_id).removeClass('photo_str_' + lang);
			}
			
			if (id == 1) {
				$('view_previous_' + photogallery_id).removeClass('photo_str_left');
			}
			else {
				$('view_previous_' + photogallery_id).addClass('photo_str_left');
			}
			    		
			this.cancel();
    	}
    }

	
	myElementsEffects.start({
    	'0': {
        	'opacity': opacity
    	}
	});
}

function changeImageNext(photogallery_id, id) {
	num = id + Math.floor((id - 1)/ max_images);

	if (num % max_images == 0 || num == 1) { 
		scrollPictureBar(photogallery_id, id, true);
	}
	else if ((num - 1) % max_images == 0) { 
		scrollPictureBar(photogallery_id, id - 1, true);
	}
	
	changeImagesWithOpacity(photogallery_id, false, id);
	return false;		
}

function changeImagePrevious(photogallery_id, id) {
	num = id + Math.floor((id - 1)/ max_images);
	
	if (num % max_images == 0) { 
		scrollPictureBar(photogallery_id, id, false);
	}
	else if ((num + 1) % max_images == 0 && id != 1) {
		scrollPictureBar(photogallery_id, id + 1, false);
	}
	
	
	changeImagesWithOpacity(photogallery_id, false, id);
	return false;		
}

function viewNext(photogallery_id) {
	var current = pictures[photogallery_id].current;
	id = (pictures[photogallery_id].current == pictures[photogallery_id].total) ? 1 : (parseInt(current) + 1);
	changeImageNext(photogallery_id, id);
	return false;
}

function viewPrevious(photogallery_id) {
	var current = pictures[photogallery_id].current;
	id = (pictures[photogallery_id].current == 1) ? pictures[photogallery_id].total : (parseInt(current) - 1);
	changeImagePrevious(photogallery_id, id);
	return false;
}

function changeImage(photogallery_id, id) {
	if (id < pictures[photogallery_id].current) {
		changeImagePrevious(photogallery_id, id)
	}
	else {
		changeImageNext(photogallery_id, id)
	}
}

function scrollPictureBar(photogallery_id, id, forward) {	
	width = 55;
	var elEffect = new Fx.Elements($('picture_bar_' + photogallery_id), {duration: 1500});
	var scrollValue = 0;
	var start = forward ? 1 : max_images;
	scrollValue = (start - parseInt(id)) * parseInt(width);
	newWidth = 550 + scrollValue*(-1);
	
	elEffect.start({
    	'0': {
        	'margin-left': scrollValue,
        	'width': newWidth
    	}
	});
}
