var TALL_SCROLL = 355;
var PROJECT_SCROLL = 285;

var IE6_FUDGE = 40;

var currentthumb = 1;
var finalposition = 0;

var cakepath = "/app/index.php/"

function get (name) {
	el = document.getElementById(name);
	if (el == null) {
		// Couldn't find this element. Hide the fact from sensitive browsers by making up an element with an innerHTML property.
		el = {innerHTML : ""};
	}
	return el
}

function installScrollbar(height) {
	
	/*
	height = height || PROJECT_SCROLL;
	
	var header = get("donotscroll");
	var target = get("doscroll");

	// measure the header
	var headerheight = header.offsetHeight;
	// fit the description text to the actual space left on the page.
	var descriptionheight = height - headerheight;
	target.style.height = descriptionheight + "px"; 

	var bottom = target.scrollHeight - descriptionheight;
	*/
	

}

function startScrollDescription (distance, height) {
	height = height || PROJECT_SCROLL;
	var header = get("donotscroll");
	var target = get("doscroll");

	// measure the header
	var headerheight = header.offsetHeight;
	// fit the description text to the actual space left on the page.
	var descriptionheight = height - headerheight;
	target.style.height = 100 + "px"; 

	var bottom = target.scrollHeight - descriptionheight;
	finalposition = target.scrollTop + distance;
	
	// Cannot scroll higher than the top.
	if (finalposition < 0) {
		finalposition = 0;
	}
	
	// Cannot scroll lower than the bottom.
	if (finalposition > bottom) {
		finalposition = bottom;
	} 
	
	// Internet Explorer is a waste of space. To circumvent a jerky start 
	// we schedule the first scroll in the future. This wakes up the JS
	// engine so that the rest of the scroll happens on time.
	// (Seriously, this fixes the bug. I'm not kidding.)
	window.setTimeout(function(){scrollDescription()}, 10);
}

function scrollDescription () {
	var target = get("doscroll");
	var position = target.scrollTop;
	
	// Go a proportion of this distance
	// (this gives an exponential scroll profile, sexy neh?)
	var remaining_distance = finalposition - position;
	var distance_to_scroll = (0.15 * remaining_distance);
	
	// Round bigger (up if positive, down if negative)
	if (distance_to_scroll < 0) {
		distance_to_scroll = Math.floor(distance_to_scroll);
	} else {
		distance_to_scroll = Math.ceil(distance_to_scroll);
	}
		
	// Go it.
	target.scrollTop = target.scrollTop + distance_to_scroll;
	
	// How much further now?
	var remaining = finalposition - position;
	
	if (remaining < 0.5 && remaining > -0.5) {
		// We're done.
		return;
	}
	
	// Schedule to go a little further in the next tick.
	window.setTimeout(function(){scrollDescription(remaining)}, 30); 
	
}

function loadMainImage (thumb, num, caption, copyright, projectid) {
	caption = caption || "";
	copyright = copyright || "";

	// Switch the selected thumb.
	get('thumb' + currentthumb).className = "";
	currentthumb = thumb;
	get('thumb' + currentthumb).className = "selected";
	
	// Replace caption and copyright.
	get("captiontext").innerHTML = caption;
	get("copyrighttext").innerHTML = copyright; 
	
	// If a projectid is also present, make the image clickable.
	if (projectid) {
		get('mainphotoimage').onclick = function () { window.location = cakepath + "projects/show/" + projectid; };
	}

	loader = new Image();
	loader.src = cakepath + "image/vertical/" + num;
	// This flushes the image from the screen, which is important to stop IE from flashing up the old image.
	get('mainphotoimage').src = "/img/placeholders/dummy.gif";
	// Test for completion. (We wait to give the image a chance load sufficiently fast not to merit a "loading" message)
	window.setTimeout(function(){testIsFinished(thumb, loader)}, 20);
}

function testIsFinished(thumb, loader) {
	if (thumb != currentthumb) 
		// User changed mind about which photo they wanted to look at. Abort.
		return;

	if (loader.complete) {
		// Push image onto page.
		get('mainphotoimage').src = loader.src;
		// Show it.
		get('mainphotoloading').className = "hidden";
		get('mainphotoimage').className = "";
	} else {
		// Show loading.
		get('mainphotoimage').className = "hidden";
		get('mainphotoloading').className = "";
		// Schedule another test.
		window.setTimeout(function(){testIsFinished(thumb, loader)}, 100);
	}
}

/*

 search capabilities
 
*/

function gosearch (type, value) {
	if ( value > -1 )
		window.location = cakepath + "projects/"+type+"/"+value;
}

function dosearch (type, value) {
	window.location = cakepath + "projects/"+type+"/"+value;
	return false;
}

function trysearch (e, type, value) {

	if (!e) var e = window.event;
	
	if (e.keyCode == 13) {
		window.location = cakepath + "projects/"+type+"/"+value;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	}
}
