// 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('r').innerHTML = response;
}

function viewGallery(galleryID) {
	url = "gallery/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;
		}
	}

	ajax_routing.open("GET",url,true);
	ajax_routing.send(null)

}

function showPic(url, caption, imageID) {
	document.getElementById('mainImage').src = "gallery/images/"+url;
	var captionText = "";
	if (caption!="") 
		captionText = "Caption: "+caption;
	document.getElementById('captionText').innerHTML = captionText;
	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 = "gallery/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 = "- you must upload a jpg image file\n";	
	}
	if(err!="") {
		alert(err);
		return false;
	}
	return AIM.submit(theThis, {'onStart' : startCallback, 'onComplete' : completeCallback});
}

