/*==================================================
 *  Exhibit.eRDFImporter
 *  author: Keith Alexander 
 *==================================================
 */


var eRDF = new Object();


Exhibit.eRDFImporter = {
};

Exhibit.importers["application/eRDF"] = Exhibit.eRDFImporter;

Exhibit.eRDFImporter.load = function(link, database, cont) {


//console.log("importer is being loaded");
    try {

		if((link.getAttribute('href')||"").length == 0)
		{
				//use current document
				//not sure what to do about hiding the existing content -  how to know what is exhibit markup, and what isn't?
				Exhibit.eRDFImporter.load_eRDF(document, database);
		}
		else
		{
			
				var iframe = document.createElement("iframe");
				iframe.style.display = 'none';
					// 
					// if (SimileAjax.Platform.browser.isIE)
					// {
				iframe.onload = function(){
								var db = window.database;
							Exhibit.eRDFImporter.load_eRDF(this.contentDocument, db);
							
							};
			// }
			// 		else
			// 		{
			// 			iframe.setAttribute('onLoad','Exhibit.eRDFImporter.load_eRDF(this.contentDocument, database)');
			// 		}
			// 	
					iframe.src = link.href;
				Exhibit.eRDFImporter.iframe = iframe;
				document.body.appendChild( Exhibit.eRDFImporter.iframe );
				
						
		}

    } catch( e ) {
	window.console && console.log && console.log( e );
    } finally {
        if (cont) {
            cont();
        }
    }
}

Exhibit.eRDFImporter.load_eRDF = function(document, database) {

//get eRDF Library
				eRDF.CALLBACK_DONE_LOADING = function(){
					try {

					var parser = eRDF.parser(document);
					var parsed = parser.get_ExhibitJSON();
					
				    database.loadData( parsed, Exhibit.Persistence.resolveURL(document.location.href) );
					// the database gets loaded, but unless I use setAttribute, no data gets rendered
				
					if(Exhibit.eRDFImporter.iframe) window.document.body.removeChild(Exhibit.eRDFImporter.iframe);					
					
					}catch(e){ alert(e); }
				}

			    var s = document.createElement("script");
			    s.type = 'text/javascript';
			    s.src = 'http://semwebdev.keithalexander.co.uk/erdf.js';
				
			    // add it to the document tree, load it up!
			    window.document.getElementsByTagName('body')[0].appendChild(s);
};
