// Function: We take in jstime and convert it to a time string according to the time in MM:SS.HH or SS.HH.
function jstimetostring(jstime) {
    workingtime = jstime / 1000
    timemintemp = (workingtime % 3600) / 60
    timesectemp = workingtime % 60
    timehuntemp = (timesectemp % 1) * 100

    // Convert to string the time min
    timemintemp = timemintemp - (timemintemp % 1)
    timemin = timemintemp.toFixed()

    // Convert to string the seconds, adding a leading zero if less than 10
    if (timesectemp < 10) {
        timesectemp = timesectemp - (timesectemp % 1)
        timesec = "0" + timesectemp.toFixed()
    } else {
        timesectemp = timesectemp - (timesectemp % 1)
        timesec = timesectemp.toFixed()
    }

    // Convert to string the hundredths, adding a leading zero if less than 10
    if (timehuntemp < 10) {
        timehuntemp = Math.round(timehuntemp * 1000) / 1000
        timehuntemp = timehuntemp - (timehuntemp % 1)
        timehun = "0" + timehuntemp.toFixed()
    } else {
        timehuntemp = Math.round(timehuntemp * 1000) / 1000
        timehuntemp = timehuntemp - (timehuntemp % 1)
        timehun = timehuntemp.toFixed()
    }

    // if minutes == 0, then it's SS.HH otherwise it's MM:SS.HH        
    if (timemintemp == 0) {
        formatedtimestring = timesec + "." + timehun
    } else {
        formatedtimestring = timemin + ":" + timesec + "." + timehun
    }

    return formatedtimestring;
}


// Function: We take in jstime and convert it to a DD-MMM-YYYY format
// graphtype: 1 - tool tip / 2 - bottom of the graph
function jstimetomonthyear(jstime, graphtype) {

    // 3 Letter Month Abbreviations
    m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    // Create a date object with the jstime
    var d = new Date(parseFloat(jstime));

    // Get the month and year
    curr_date = d.getDate();
    curr_month = d.getMonth();
    curr_year = d.getFullYear();

    // Format the string (if graphtype == 1 then we are using the tool tip, if == 2 the we are doing the bottom of the graph)
    if (graphtype == 1) {
        formatedmonthyearstring = curr_date + " " + m_names[curr_month] + " " + curr_year
    } else {
        curr_year = curr_year.toString().slice(2)
        formatedmonthyearstring = m_names[curr_month] + " " + curr_year
    }


    return formatedmonthyearstring;
}


// graphtype: 1 - tool tip / 2 - bottom of the graph
// This is used for the season compare
function jstimetomonthyearseason(jstime, yeartoadd, graphtype) {

    // 3 Letter Month Abbreviations
    m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    // Create a date object with the jstime
    var temp = parseFloat(jstime) + parseFloat(yeartoadd);
    var done = new Date(temp);

    // Get the month and year
    curr_date = done.getDate();
    curr_month = done.getMonth();
    curr_year = done.getFullYear();

    // Format the string (if graphtype == 1 then we are using the tool tip, if == 2 the we are doing the bottom of the graph)
    if (graphtype == 1) {
        formatedmonthyearstring = curr_date + " " + m_names[curr_month] + " " + curr_year
    } else {
        formatedmonthyearstring = m_names[curr_month]
    }


    return formatedmonthyearstring;
}


// Function: We take in an x and a y from the graph:hover and compare it to an array that contains
// x's and y's pertaining to a meet and a meet url.
// we return an array [meetname, meeturl] that can be accessed by 
// var meetinfo = jsmeetinfo()
// templatemeetname = meetinfo[0]
// templatemeeturl  = meetinfo[1]
function jsmeetinfo(hover_x, hover_y, mytimesmeetdata) {

    // Initialize variables
    meetname = ""
    meeturl = ""

    // Get the length of mytimesmeetdata array
    for (var i=0;i<mytimesmeetdata.length;i++)
    {
	    
        // Check to see if we have the same x value
        if (mytimesmeetdata[i][0] == hover_x) {
        
            // Check to see if we have the same y value
            if (mytimesmeetdata[i][1] == hover_y) {

                // If same x and y value then we return the meet name and meet url
                meetname = mytimesmeetdata[i][2]
                meeturl = mytimesmeetdata[i][3]
            }
        }
    }

  return [meetname, meeturl];
}

