<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Input Coords" 
    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>

<div id="input">
<table> 
  <TBODY><tr> 
<!--  <td>RA:  </td><td><input id="RA" type="text" value="Hour:Min:Sec or Dec Degrees"></td>--> 
 
  <td>RA:  </td><td><input id="RA" type="text"></td> 
  </tr> 
  <tr> 
<!--  <td>dec: </td><td><input id="DEC" type="text"></td> -->
    <td>dec: </td><td><input id="DEC" type="text"></td>
  </tr> 
  <TR><td colspan="2"><input id="center" onclick="ic.writeCoords();" type="button" value="GO"></td> 
  </TR> 
</TBODY></table> 
  </div>

<script type="text/javascript">

// InputCoords object is global
var ic = null,
    debug = true;


// CONSTANTS
//var GADGET_NAME = "InputCoords";
var GADGET_NAME = "InputCoords" + "_" + 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 InputCoords object
function init(){
    ic = new InputCoords();
    gadgets.window.adjustHeight();
}


function InputCoords(){
    var my = new Object();
    // CONSTANTS
    var DEFAULT_NAMESPACE = "NS1",
        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
                } );
// rewrote   
    // doRegistration(_namespaceList);
    
    
//    // REGISTRATION stuff
//    // do complete registration for all namespaces in the array
//    function doRegistration(namespaceArray){
//        // TODO: check that namespaceArray is actually and array
//        for (var i=0, len=namespaceArray.length; i<len; i++){
//            registerMe(namespaceArray[i]);
//        }
//    }
//    // register this gadget in a particular namespace
//    function registerMe(namespaceName){
//        //F irst, Register the gadget with success and failure callbacks
//        di.registerGadget(namespaceName, function(){continueRegistration(namespaceName);}, 
//                                                function(){registrationFailure(namespaceName);});
//    }    

// rewrote 
   // // Callback after a successful registration of this gadget in a namespace
   // // Displays messages and registers variables
   // function continueRegistration(namespaceName){
   //     debugLog('Registration was total Success!');
   //     if (msg) {
   //         miniMsg.dismissMessage(msg);
   //         // free up msg
   //         msg = null;
   //         miniMsg.createTimerMessage("Registration Succesful.", MM_TIMER_DURATION);
   //     }
   //     registerVariables(namespaceName);
   //     // di.registerVariable();
   //     // di.addTrigger();
   // }
    
   // // Callback after a failure of registration
   // // displays message showing failure
   // function registrationFailure(namespaceName){
   //     debugLog('Registration was total failure!');
   //     miniMsg.dismissMessage(msg);
   //     miniMsg.createDismissibleMessage("Unable to register gadget.\nWe cannot communicate with other gadgets.");
   // }
    
//    // Registers variables, and if successful, calls registerTriggers
//    function registerVariables(namespaceName){
//        debugLog('Attempting to register variables...');
//        var varList = [ "VP_Coord" ] // list of variables to register
//
//        for (var i=0, len=varList.length; i<len; i++) {
//            // TODO: should we do this for every namespace?
//            if (di.registerVariable(namespaceName, varList[i], null)) {
//                debugLog('Successfully registered variable "' + varList[i] + '"');
//                registerTriggers(namespaceName);
//            }
//            else {
//                debugLog('FAILED to register variable "' + varList[i] + '"');
//            }
//        }
//    }
//    
//    // registers Triggers
//    function registerTriggers(namespaceName){
//        // none to register
//    }

	// 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.");
    }
        

    // METHODS
    // Coordinates are entered, tell the DataInterface
    my.writeCoords = function(){
        debugLog("Writing Coods...");
        var ra = (document.getElementById("RA").value).toString();
        var dec = (document.getElementById("DEC").value).toString();
        var coord = new UW.astro.CelestialCoordinate(ra, dec);
        di.writeVariable(DEFAULT_NAMESPACE, "VP_Speed", 1);
		if (di.writeVariable(DEFAULT_NAMESPACE, "VP_Coord", coord)){
            debugLog('Wrote variable "VP_Coord"');
        }

    }
    
    return my;
}

var coord = null;
var timeIntervalId = 0;

function sendCoords(){
  ra = (document.getElementById("RA").value).toString();
  dec = (document.getElementById("DEC").value).toString();
  
  if (dec.search(/:|\s/) != -1) {
    var split1 =  dec.split(/:|\s/);      
    if (split1[0].search(/-/) != -1){
      dec=Number(split1[0])-(split1[1]/60+split1[2]/3600);
    }
    if (split1[0].search(/-/) == -1) {
      dec=Number(split1[0])+split1[1]/60+split1[2]/3600;
    }
    else {
      dec = dec;
    }
  }
  if (ra.search(/:|\s/) != -1) {
    var split2 = ra.split(/:|\s/);
    ra = (Number(split2[0])+split2[1]/60+split2[2]/3600)*15;
  }
  else{
    ra=ra;
  }
  ra=ra2lon(ra);
  dataWrap.cellng = ra;
  dataWrap.cellat = dec;
  dataWrap.earthlat = dec;
  dataWrap.earthlng = ra;
  
  //post();

  //writeDataGadgetVar("VPCoords", dataWrap);
}

gadgets.util.registerOnLoadHandler(init); 

</script>

]]></Content>
</Module>
