////////////////////////////////////////////////////////////////////////////////
// ajax.js
// -----------------------------------------------------------------------------
// Provides an AJAX interface into the MediaZoo PHP backend scripts
////////////////////////////////////////////////////////////////////////////////

/* GLOBALS */
YAHOO.namespace("MZ");

////////////////////////////////////////////////////////////////////////////////
// AJAX calls to get select box options for genus/species and phase/stage
////////////////////////////////////////////////////////////////////////////////

YAHOO.MZ.collectionAjax = { 
	
	handlesuccess:function(o) { 
		// Apply an XSLT and show results
		YAHOO.MZ.XSLAjax.startRequest(o.responseText, 'collection.xsl', YAHOO.MZ.collectionAjax.target);
	},

	handlefailure:function(o) {
		alert('AJAX call failed at the request level:' + o.statusText);
	},

	startRequest:function(span) {
		// Remember what image was clicked
		YAHOO.MZ.lastClicked = span;
		
		// Clear out target div, put in placeholder image
		YAHOO.MZ.collectionAjax.target = document.getElementById('collectionContent_'+span.id);		
		YAHOO.MZ.collectionAjax.target.innerHTML = 'Loading...';
		YAHOO.MZ.collectionAjax.target.style.display = 'block';

		// Get media
		YAHOO.util.Connect.asyncRequest('POST', 'lib/getMedia.php', YAHOO.MZ.collectionCallback, 'collection='+span.id); 
	}

};

YAHOO.MZ.collectionCallback = { 
	success:YAHOO.MZ.collectionAjax.handlesuccess, 
	failure:YAHOO.MZ.collectionAjax.handlefailure,
	scope:YAHOO.MZ.collectionAjax
};


////////////////////////////////////////////////////////////////////////////////
// Functions to load and apply XSL files
////////////////////////////////////////////////////////////////////////////////

YAHOO.MZ.XSLAjax = { 
	
	handlesuccess:function(o) {
		// Process XSLT
		YAHOO.MZ.XSLAjax.style = xmlParse(o.responseText);
		var html = xsltProcess(YAHOO.MZ.XSLAjax.data, YAHOO.MZ.XSLAjax.style);
		YAHOO.MZ.XSLAjax.target.innerHTML = html;
	
		// Toggle button
		YAHOO.MZ.toggleCollection(YAHOO.MZ.lastClicked, 'open');
	},

	handlefailure:function(o) {
		alert('AJAX call failed at the request level:' + o.statusText);
	},

	startRequest:function(xmltext, xslfile, target) {
		YAHOO.MZ.XSLAjax.data = xmlParse(xmltext);
		YAHOO.MZ.XSLAjax.target = target;
		YAHOO.util.Connect.asyncRequest('GET', 'xslt/'+xslfile, YAHOO.MZ.XSLCallback); 
	}
	
};

YAHOO.MZ.XSLCallback = { 
	success:YAHOO.MZ.XSLAjax.handlesuccess, 
	failure:YAHOO.MZ.XSLAjax.handlefailure,
	scope:YAHOO.MZ.XSLAjax
};
