// preload the loading gif
loadingImage = new Image();
loadingImage.src = "/images/loading.gif";

// create an object for the large image
largeImage = new Image();

function setElementText(id, text) {
	
	var objElement = document.getElementById ? document.getElementById(id) : document.all(id);
	
	if (objElement) {
		if (objElement.innerText != undefined)
			objElement.innerText = text;
		else if (objElement.textContent != undefined)
			objElement.textContent = text;
	}
}

function swapPhoto(memberID, photoID) {
	
	if (document.getElementById) {
		
		var photoPath;
		var memberMod;
		var prevPhotoID;
		var nextPhotoID;
		var prevOnClick;
		var nextOnClick;
		var prevHRef;
		var nextHRef;
		
		// show loading image
		if (document.getElementById('photo_large'))
			document.getElementById('photo_large').src = loadingImage.src;
		
		memberMod = '0' + memberID;
		memberMod = memberMod.substr(memberMod.length - 2, memberMod.length);
		
		// is restricted photo
		if (arrRestrictedPhotos[photoID])
			photoPath = '/images/restricted_500.jpg';
		// is large photo
		else if (photoID > 35038)
			photoPath = '/images/member_profile_large/' + memberMod + '/' + memberID + '_' + photoID + '.jpg';
		// is small photo
		else
			photoPath = '/images/member/' + memberMod + '/profile/medium/' + memberID + '_' + photoID + '.jpg';
		
		for (var i = 0; i < arrPhotos.length; i++) {
			
			// unselect small photo
			if (document.getElementById("td_" + arrPhotos[i]))
				document.getElementById("td_" + arrPhotos[i]).className = 'unselPhoto';
			
			if (photoID == arrPhotos[i]) {
				
				if (i > 0)
					prevPhotoID = arrPhotos[i - 1];
				else
					prevPhotoID = arrPhotos[arrPhotos.length - 1];
				
				if (i < arrPhotos.length - 1)
					nextPhotoID = arrPhotos[i + 1];
				else
					nextPhotoID = arrPhotos[0];
			}
		}
		
		if (document.getElementById("td_" + photoID))
			document.getElementById("td_" + photoID).className = 'selPhoto';
		
		// update large photo link
		if (document.getElementById('photo_large_link')) {
			if (arrRestrictedPhotos[photoID] && !isUnderAged)
				document.getElementById('photo_large_link').href = '/profile/Restricted.aspx?return_url=' + escape(photoURL + photoID);
			else
				document.getElementById('photo_large_link').href = profileURL;
		}
		
		if (document.getElementById('photo_large'))
		{
			// update alt text
			if (arrRestrictedPhotos[photoID] && !isUnderAged)
				document.getElementById('photo_large').alt = 'Restricted content';
			else
				document.getElementById('photo_large').alt = 'Back to profile';
			
			setElementText('photo_large_caption', arrCaptions[photoID] + (isThisMember && arrCaptions[photoID] != '' ? " -" : ''));
			
			if(isThisMember)
			{
				document.getElementById('edit_caption').href = '/register/PhotoCaptionEdit.aspx?photo_id='+ photoID +'&return_url=' + strProfilePath + '/photos/' + photoID + '#top'
				if(arrCaptions[photoID] == null || arrCaptions[photoID] == '')
					setElementText('edit_caption', 'Add a caption');
				else
					setElementText('edit_caption', 'Edit caption');
			}
			
			// add onload event for when large image has completly loaded
			largeImage.onload = showLarge;
			largeImage.src = photoPath;	
		}	
		
		// next and previous links
		prevOnClick = function(){return swapPhoto(memberID, prevPhotoID)};
		prevHRef = photoURL + prevPhotoID;
		nextOnClick = function(){return swapPhoto(memberID, nextPhotoID)};
		nextHRef = photoURL + nextPhotoID;
		
		// update next and previous links
		updateLink(document.getElementById('prev_img'), prevOnClick, prevHRef);
		updateLink(document.getElementById('next_img'), nextOnClick, nextHRef);
		
		//jump to top of page
		document.location.hash = "top";
		
		//finally save the view count
		if (!arrRestrictedPhotos[photoID])
			doXMLHTTPRequest('/profile/PhotosTrackView.aspx?photo_id=' + photoID);
		
		return false;
	}
	else
		return true;
}

function updateLink(elem, onClick, hRef) {
	if (elem) {
		elem.onclick = onClick;
		elem.href = hRef;
	}
}

function showLarge()
{
	document.getElementById('photo_large').src = largeImage.src;
}