<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Name Resolve 2" 
    height="10"
    scrolling="true">
     <Require feature="dynamic-height"/>
     <Require feature="opensocial-0.8"/> 
     <Require feature="minimessage"/>
</ModulePrefs> 

<Content type="html"><![CDATA[

<script src="http://sky.astro.washington.edu/gadgets/js/gDataInterface.js" type="text/javascript"></script>
<script src="http://sky.astro.washington.edu/gadgets/js/widgetlib1.js" type="text/javascript"></script>
<script src="http://sky.astro.washington.edu/gadgets/js/astroObjectLib.js" type="text/javascript"></script>


<script type="text/javascript">
var tq = null,
    debug = true;

var GADGET_NAME = "NameResolve" + "_" + Math.floor(Math.random()*100000+1).toString();

// Writes to the console if debug (global) is true  
function debugLog(msg){
    if ((debug === true) && (window.console && console.log)) {
        console.log(GADGET_NAME + ": " + msg);
    }
    return;
}

// create gadget object
function init(){
    tq = new NameResolve();
    gadgets.window.adjustHeight();
}

function NameResolve(){
    var my = new Object();
    // CONSTANTS
    var DEFAULT_NAMESPACE = "NS2",
        MM_TIMER_DURATION = 4;

    // PRIVATE
    var _namespaceList = [ DEFAULT_NAMESPACE ];      // list of our current Namespaces. Just init with the default
    
    // Private Helper Variables
    var di = new gDataInterface(GADGET_NAME, DEFAULT_NAMESPACE); // make dataInterface object, init with default
    var miniMsg = new gadgets.MiniMessage();// make MiniMessage object

    // INIT
    var msg = miniMsg.createStaticMessage("Attempting to register gadget...");
    di.registerMe( { gadgetName : GADGET_NAME,
                    namespaceList : _namespaceList,
                    variableList : [ "VP_Coord", "VP_Speed" ],
                    triggerList : [],
                    successCallback : registrationSuccess,
                    failureCallback : registrationFailure
                } );
    
    // Callback after successful registration
    function registrationSuccess(){
        debugLog('Registration successful');
        if (msg) {
            miniMsg.dismissMessage(msg);
            // free up msg
            msg = null;
            miniMsg.createTimerMessage("Registration Succesful.", MM_TIMER_DURATION);
        }
    }
    // Callback after a failure of registration
    // displays message showing failure
    function registrationFailure(){
        debugLog('Registration FAILED!');
        miniMsg.dismissMessage(msg);
        miniMsg.createDismissibleMessage("Unable to register gadget.\nWe cannot communicate with other gadgets.");
    }
    
    
    function makeRequestGet(url) {
      params = {};
      params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
      // params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 0;
      // To turn off Google anti-refresh, add a "random" number so the
      // url is different every time
      var ts = new Date().getTime();
      var sep = "?";
      if (url.indexOf("?") > -1) sep = "&";
      url = [ url, sep, "nocache=", ts ].join("");
      gadgets.io.makeRequest(url, response, params);
    }

    function response(obj) {
          //alert("from input"+obj.text);
          // This is a first attempt to parse the response.  We should
          //  work on improving this.  Not sure if it works exactly, either.  :-)
          //lines = obj.text.split("\n");
        
        if (!(typeof obj.text === "string")){
            showError('No results found');
            return;
        }
        // a string was returned, but does it have relevant data in it?
        var dataLine = obj.text.match(/^%J.*$/gm);
        if (dataLine === null) {
            showError('No results found');
            return;
        }
        //alert(dataLine);
        dataLine=dataLine.toString();
        dataLine=dataLine.split(" ");
        radeg = dataLine[1]-0;
        newdec = dataLine[2]-0;
        
        debugLog("Writing Coods...");
        var ra = (radeg/15).toString();  //decimal hours
        var dec = newdec.toString();  //decimal degrees
        var coord = new UW.astro.CelestialCoordinate(ra, dec);
		di.writeVariable(DEFAULT_NAMESPACE, "VP_Speed", 1.0);
        if (di.writeVariable(DEFAULT_NAMESPACE, "VP_Coord", coord)){
            debugLog('Wrote variable "VP_Coord"');
        }
    }

    // Indicate to the user that something went wrong
    function showError(msg){
        alert(msg);
    }

    // METHODS
    // Coordinates are entered, tell the DataInterface
   
    my.getCoords=function(){
      var text = document.getElementById("input");
      text = text.value;
      text = text.replace(/ /g, "+");
      //alert(text);
      makeRequestGet("http://cdsws.u-strasbg.fr/axis/services/Sesame?method=sesame&name="+text+"&resultType=p/c", 0);
      

    }
    
    return my;
}

gadgets.util.registerOnLoadHandler(init); 


</script>
<div>
<form name="form" action="#" onsubmit="tq.getCoords(); return false">
<p><input type="text" id="input" value="Input Object Name"/>
   <input type="submit" value="Go"/>
</p>


  </div>



]]></Content>
</Module>
