/*****************************************************************************************
* Project Manager functionality - load, show, and hide projects and project images
*****************************************************************************************/
var projectManager = {
	_waiting: false
	, _cur_project: -1
	, init: function() {
		var proj_id = $('project-list').down().id.substr(8,4);
		
		projectManager._cur_project = -1;
		projectManager._waiting = false;		
		projectManager.showProject(proj_id);
	}, showProject: function(id) {
		if (!projectManager._waiting) {
			if (id != projectManager._cur_project) {
				if ($('project-'+projectManager._cur_project)) {
					$('project-'+projectManager._cur_project).removeClassName('selected');
				}
				$('project-'+id).addClassName('selected');
				
				if ($('project-'+id).project) {
					// already downloaded this project
					projectManager._displayProject($('project-'+id).project);
				} else {
					// have to fetch project from the server - show loading screen?
					ajax_req($H({'qc': 1, 'id': id}), projectManager._projectLoaded);
				}
			}
		}
	}, showImage: function(src) {
		// switch background image
		$('project-viewer').setStyle('background-image:url('+src+');');
	}, toggleDetails: function() {
		// show or hide the project details
		if (!$('project-details').fading) {
			$('project-details').fading = true;
			if ($('project-details').hasClassName('show')) {
				var top = 400 - $('project-details').offsetHeight;
				var over = 0;
				if (top < 0) {
					over = top * -1;
					top = 0;
				}
				$('project-details').removeClassName('show');
				$('project-details-button').addClassName('down');
				$('project-details').visualEffect('morph', {duration:.5, style:'top:'+top+'px;', beforeStart:function() { $('project-details').setStyle('overflow:hidden;'); }, afterFinish:function() { $('project-details').fading = false; 
						// this fixes a strange bug in some browsers - the padding on paragraphs and the details window itself isn't accounted for in the offset height until the div is actually visible.  once it is accounted for, it'll work every time the content is updated.  in other words, this will cause the details to jump after the first raise, but move smoothly every time after that.
						newtop = (400 - $('project-details').offsetHeight);
						newover = 0;
						if (newtop < 0) {
							newover = newtop * -1;
							newtop = 0;							
						}
						$('project-details').setStyle('top:'+newtop+'px;');
						$('project-viewer').setStyle('height:'+(400 + newover)+'px;');
				}});
				$('project-viewer').visualEffect('morph', {duration:.5, style:'height:'+(400 + over)+'px;'});
			} else {
				$('project-details').addClassName('show');
				$('project-details-button').removeClassName('down');
				$('project-details').visualEffect('morph', {duration:.5, style:'top:399px;', beforeStart:function() { $('project-details').setStyle('overflow:hidden;'); }, afterFinish:function() { $('project-details').fading = false; }});
				$('project-viewer').visualEffect('morph', {duration:.5, style:'height:400px;'});
			}
		}
	}, _projectLoaded: function() {
		// project came back from server - set it up and display it
		if ($('project-'+this.body.id)) {
			$('project-'+this.body.id).project = this.body;
			projectManager._displayProject(this.body);
			return;
		}
		// errors
	}, _displayProject: function(project) {
		// display a loaded project
		
		// flush old images
		$('project-images').update('');
		
		// load new images
		if (project.images.length > 0) {
			var thumb = 0;
			for (var i = 0, j = project.images.length; i < j; ++i) {
				thumb = new Element('img');
				thumb.src = project.images[i];
				thumb.addClassName('thumb');
				thumb.onclick = function() { projectManager.showImage(this.src); };
				$('project-images').insert({bottom: thumb});
			}
			projectManager.showImage(project.images[0]);
		}
		$('project-header').update('<h1>'+project.title+'</h1>');
		
		// hide details
		$('project-details').setStyle('top:399px;');
		$('project-viewer').setStyle('height:400px;');
		$('project-details').addClassName('show');
		$('project-details-button').removeClassName('down');
		
		$('project-details').update(project.details);
		/*if (project.summary) {
			$('project-summary').show();
			$('project-summary').update(project.summary);
		} else {
			$('project-summary').hide();
			$('project-summary').update('');
		}*/		
		projectManager._cur_project = project.id;
		projectManager._waiting = false;
	}
};



/*****************************************************
* Basic Ajax Functions
*****************************************************/
function ajax_validResponse(json) {
	if (json.length > 0) {
		eval('response = '+json);		
		if (response.header) {
			return true;
		}
	}
	return false;
}

function ajax_req(hash, successFunction) {
	req(hash, successFunction, '/crain-ajax.js');
}

function req(hash, successFunction, dest) {
	new Ajax.Request(dest, {
		method: 'get',
		parameters: hash,
		onSuccess: function(transport) { 
			if (ajax_validResponse(transport.responseText)) {
				eval('response = '+transport.responseText);
				successFunction.call(response);				
			}
		},
		onFailure: function(transport) { 
			generalError('Error connecting to server.'); 
		}
	});
}
