CmdUtils.CreateCommand({
  name: "view_ontology",
  homepage: "http://jowl.ontologyonline.org/",
  author: { name: "David Decraene", email: "ontologyonline@ontologyonline.org"},
  license: "MIT",
  description: "Attempts to load and visualize an identified OWL-DL ontology document with jOWL.",
  help: "Hit enter to open the jOWL browser.",

  /**
  * configuration settings.
  */
  _config: {
  	browserURL  : "http://jowl.ontologyonline.org/jOWLBrowser.html?mode=Ubiquity",
	noOWLFile : 'Document not recognized as a potential OWL-DL File',
	OWLFile : "Try to view this ontology with jOWL. Hit enter to open the viewer"
  },


  _jOWL : {
	thumbs_up : false,
	document : false,
	/** Load the jOWL Browser */
    init : function(){
		var self = this;
		Utils.openUrlInBrowser(self._config.browserURL);	 
		//poll until jOWL Browser page is loaded
		function pollBrowser(){
			var href = Application.activeWindow.activeTab.document.location.href;
			if(href != self._config.browserURL) { Utils.setTimeout(pollBrowser, 200); return; }
			else self._jOWL.onLoad.call(self);
		}
		pollBrowser();	
	},
	/** Feed the owl file to the jOWL Browser*/
	onLoad : function(){
		var self = this; self._jOWL.document = false;
		var win = CmdUtils.getWindowInsecure();
		//poll until window is ready
		function pollWindow(){
		if(!win.$) { Utils.setTimeout(pollWindow, 200); return; }
		win.$(win.document).ready(function(){
					if(typeof self._jOWL.thumbs_up == "string"){self._jOWL.document = self._jOWL.thumbs_up; }
					win.jOWLParseDoc(self._jOWL.document);
				});
		}
		pollWindow();
	},
	/** check if the current document might be an OWL-DL file, returns content */
	checkDocument : function(doc, window){
		var self = this;
		if(doc instanceof window.HTMLDocument){
				var loc = doc.location.protocol+"//"+doc.location.host+doc.location.pathname;
				if(loc.match("[.]owl$")) { //firefox -> .owl extension is wrapped inside an HTML page with a <pre> code block  
					var xml = jQuery('body > pre', doc); if(xml.length) { var t = xml.text(); if(t.indexOf("<rdf:RDF")) return t;}
					}
				else if(doc.location.pathname.split('.').length == 1){//no extension given, may attempt to parse
					var xml = jQuery('body > pre', doc); if(xml.length) { var t = xml.text(); if(t.indexOf("<rdf:RDF ")) return t; }
				}
			}
		else if(doc instanceof window.XMLDocument && doc.documentElement.nodeName == 'rdf:RDF'){
			var serializer = new window.XMLSerializer(); 
			return serializer.serializeToString(doc);		
		}
		return false;	
	}
  },
 
  /** nudges the document and checks whether this could be a potential OWL-DL file */
  preview: function( pblock ) {
	this._jOWL.thumbs_up = this._jOWL.checkDocument.call(this, context.focusedWindow.document, window); 
	pblock.innerHTML = (this._jOWL.thumbs_up) ? this._config.OWLFile: this._config.noOWLFile;
  },
  /** If the preview determined it is a potential OWL-DL file then this tries to load the file into the jOWL browser. */
  execute: function() {
	  if(this._jOWL.thumbs_up) this._jOWL.init.call(this);
  }
})

