// This file holds the functions that work directly with the data gotten from
// the HEK API.

// I use some of Yahoo's YUI library
// Here is the YUI liscence:
/*
 * 
 * Software License Agreement (BSD License) Copyright (c) 2010, Yahoo! Inc. All
 * rights reserved.
 * 
 * Redistribution and use of this software in source and binary forms, with or
 * without modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Yahoo! Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission of Yahoo! Inc.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

// Author: Ian Ruotsala, The Evergreen State College
// email : ruoian27 at evergreen dot edu
var DEBUG = true; // change this value to true for debuging output

// this maps the events abbv to an array index
var lmsalDiamond_event_type = ["ar", "ce", "cd", "ch", "cw", "fi", "fe", "fa",
		"fl", "lp", "os", "ss", "ef", "cj", "pg", "ot", "nr", "sg", "sp"];

// this will hold the parsed JSON for all the events
var lmsalDiamond_events = new Array(lmsalDiamond_event_type.length);

// this holds the first part of the URL, up to event type
var lmsalDiamond_url_prefix = "http://www.lmsal.com/hek/her?cosec=2&cmd=search&type=column&event_type=";

// this holds the end part of the URL, after time span
var lmsalDiamond_url_suffix = "&event_coordsys=helioprojective&x1=-1200&x2=1200&y1=-1200&y2=1200";

//this array holds the time allowed (in millis) for each API query attempt before returning timeout
var lmsalDiamond_query_timeout = [200, 500, 1000];

// make variables to hold starttime and endtime of present query

// this will be the function called when the program starts
var lmsalDiamond_initialize = function() {

}

var lmsalDiamond_get_events = function(event_index, starttime, endtime, attempt) {
	// send present url to query, update the stored JSON
	var returnObj = "timeout"; // it will return this if no responce from
								// server

	var httpQuery = new XMLHttpRequest();

	var startStr = "&event_starttime=" + starttime.getFullYear() + "-"
			+ padZero(starttime.getMonth() + 1) + "-"
			+ padZero(starttime.getDate()) + "-"
			+ padZero(starttime.getHours()) + "-"
	padZero(starttime.getMinutes()) + "-" + padZero(starttime.getSeconds());

	var endStr = "&event_endtime=" + endtime.getFullYear() + "-"
			+ padZero(endtime.getMonth() + 1) + "-"
			+ padZero(endtime.getDate()) + "-" + padZero(endtime.getHours())
			+ "-"
	padZero(endtime.getMinutes()) + "-" + padZero(endtime.getSeconds());

	var full_url = lmsalDiamond_url_prefix
			+ lmsalDiamond_event_type[event_index] + startStr + endStr
			+ lmsalDiamond_url_suffix;

	if (DEBUG){
		document.write("full_url set to: " + full_url);
		full_url = lmsalDiamond_event_type[event_index];
	}
	
	httpQuery.open("GET",full_url,false);
	httpQuery.send(null);
	
	// var returnString = httpQuery.responseText;
	// returnObj = YAHOO.lang.JSON.parse(returnString);

	if (returnObj.result.length >= 200) {
		return true; // I want to know if the server's limit of 200
						// events/query has been reached
	}

	return returnObj;
}

var padZero = function(padNumb) {
	// local method to pad 1-digit numbers with a leading 0
	if (padNumb < 10) {
		return "0" + padNumb;
	}
	return "" + padNumb;
}