var currentPos = 0;
var onAfterShowDisplayPhotoView;

function loadPhotoViewContent(type, disp, contextPath) {
	if (document.location.hash.indexOf('#sel_') == 0) { 
		var uri = document.location.pathname;
		var index = uri.lastIndexOf("/photo/") + "/photo/".length; 
		if (uri.indexOf(contextPath) == 0) {
			uri = uri.substring(contextPath.length, index);
		}
		if (uri.match("^"+"my")=="my") {
			uri = uri.replace("my/", "my/ajax/");
		} else {
			uri = 'ajax/' + uri;
		}
		uri = uri + document.location.hash.substring(5) + document.location.search;
		if (uri.indexOf("?") != -1) {
			uri = uri + '&';
		} else {
			uri = uri + '?';
		}
		uri = uri + 'reload=' + type; 
		if (type == 'all') {
			Gallery.addOnAfterActionHandler('loadItems', function(itype) {
				if (itype == 'comment') {
					//showme('displayPhotoView');
					if (onAfterShowDisplayPhotoView != null) {
						onAfterShowDisplayPhotoView();			
					}
					Gallery.setCallback('loadItemsOnAfterHandler', null);
				}
			});
		}
		Gallery.loadItems('comment', uri, disp, 10, contextPath);
	} else {
		//showme('displayPhotoView');
		if (onAfterShowDisplayPhotoView != null) {
			onAfterShowDisplayPhotoView();			
		}
	}
}

function setCurrentPhoto(id, pos, numberToShow, numberToLoad, contextPath) {
	var oldel = document.getElementById('photo_strip_' + currentPos);
	oldel.className = '';
	
	document.location.hash = '#sel_' + id;
	currentPos = pos;
	
	if (Math.abs(pos) > parseInt(numberToLoad/2) - parseInt(numberToShow/2)) {
		loadPhotoViewContent('all', 'displayPhotoView', contextPath);
		currentPos = 0;
	} else {
		loadPhotoViewContent('photodata', 'displayPhotoData', contextPath);
	}
	resetRatingBar();
}

function selectNextPhoto(firstPos, numberToShow) {
	seekPhotoStrip(1, firstPos, numberToShow)
}

function selectPrevPhoto(firstPos, numberToShow) {
	seekPhotoStrip(-1, firstPos, numberToShow)
}

function seekPhotoStrip(increment, firstPos, numberToShow) {
	var seekPos = (currentPos + increment) - firstPos - parseInt(numberToShow/2);
	if (seekPos > 0) {
		jQuery("#photoStripContainer").scrollable("seekTo", seekPos);
	}
	jQuery("#photoStripContainer").scrollable("seekTo", seekPos);
	var el = document.getElementById('photo_strip_' + (currentPos + increment));
	if (el != null) {
		var oldel = document.getElementById('photo_strip_' + currentPos);
		oldel.className = '';
		
		currentPos += increment;
		el.onclick();
		
		var newel = document.getElementById('photo_strip_' + currentPos);
		newel.className = 'active';
	}
}

function showAllPhotos() {
	document.location.hash = '';
	var index = document.location.href.lastIndexOf('/photo');
	document.location.href = document.location.href.substring(0, index) + document.location.search; 
}