// JavaScript Document
function startCallback() {
	// make something useful before submit (onStart)
	document.getElementById('r').innerHTML = "file uploading...";
	return true;
}

function completeCallback(response) {
	// make something useful after (onComplete)
	document.getElementById('uploadSubmit').disabled = false;
	document.getElementById('r').innerHTML = response;
}

function viewGallery(galleryID) {
	url = "php/gallery_view.php?g="+galleryID;
	var ajax_routing = ajax_factory();

	ajax_routing.onreadystatechange=function() {
		if (ajax_routing.readyState==4 && ajax_routing.status==200) {
			var r = ajax_routing.responseText;
			document.getElementById('galleryView').innerHTML = r;
			if (galleryID != -1) {
				selectGallery(galleryID); // set dropdown to current gallery
			}
			highlightLink('galleryLink'+galleryID);
		}
	}

	ajax_routing.open("GET",url,true);
	ajax_routing.send(null)

}

function selectGallery(galleryID) {
	galleryDropdown = document.getElementById('gallery_id');
	for(i=0;i<galleryDropdown.length;i++) {
		if(galleryDropdown.options[i].value == galleryID) {
			galleryDropdown.selectedIndex = i;	
		}
	}
}

var currentLink = 0;
function highlightLink(linkname) {
	if(currentLink != 0) {
		document.getElementById(currentLink).className = "galleryLink";
	}	
	document.getElementById(linkname).className = "selectedGallery";
	currentLink = linkname;
}

function showPic(url, caption, imageID) {
	document.getElementById('mainImage').src = url;
	document.getElementById('mainImage').alt = caption;
	var captionText = "";
	if (caption!="") 
		captionText = "Caption: "+caption;
	document.getElementById('captionText').innerHTML = captionText;
	if(document.getElementById('captionVoteText')) {
		document.getElementById('captionVoteText').innerHTML = caption;
	}
	document.getElementById('currImage').value = imageID;
}

function scroll(first, direction) {
	if (first) {
		if(direction==0) {
			currDistance = 0;
			fullDistance = 360;
			offset = 10;	
		} else {
			currDistance = 360;
			fullDistance = 0;
			offset = -10;
		}
	}
	document.getElementById('thumbHolder').scrollLeft = document.getElementById('thumbHolder').scrollLeft+offset;
	currDistance = currDistance+offset;
	//document.getElementById('galleryVote').innerHTML = "currDistance: "+currDistance+" offset: "+offset;
	if(direction==0){
		if(currDistance < fullDistance) {
			var t = setTimeout("scroll(false, "+direction+")", 10);
		}
	} else {
		if(currDistance > fullDistance) {
			var t = setTimeout("scroll(false, "+direction+")", 10);
		}
	}		
}

function submitVote(galleryID, userID) {
	url = "php/gallery_submit_vote.php?g="+galleryID+"&i="+document.getElementById('currImage').value+"&u="+userID;
	var ajax_routing = ajax_factory();

	ajax_routing.onreadystatechange=function() {
		if (ajax_routing.readyState==4 && ajax_routing.status==200) {
			var r = ajax_routing.responseText;
			document.getElementById('galleryVote').innerHTML = r;
		}
	}

	ajax_routing.open("GET",url,true);
	ajax_routing.send(null);

}

function uploadValidation(theThis) {
	err = "";
	el = document.getElementById('image');
	if(el.value == "" || (el.value.substring(el.value.length-3).toLowerCase() != "jpg" && el.value.substring(el.value.length-4).toLowerCase() != "jpeg")) {
		err = "Sorry! You can only upload JPG image files!\n";	
	}
	el = document.getElementById('copyCheck');
	if(el.checked==false) {
		err += "Please confirm you have the right to use this photo!\n";	
	}
	if(err!="") {
		alert(err);
		return false;
	}
	document.getElementById('uploadSubmit').disabled = true;
	return AIM.submit(theThis, {'onStart' : startCallback, 'onComplete' : completeCallback});
}

function showUpload(show) {
	if (show) {
		document.getElementById('uploadWindow').style.display = "block";
		document.getElementById('uploadSubmit').disabled = false;
	} else {
		document.getElementById('uploadWindow').style.display = "none";		
	}
}
