/*By Lawrence Wilkowske
     *12-28-2010*/


/*Uses Date.js (http://www.datejs.com/) to parse dates.
     *
     *Is automatically setup to take in an rss pubdate (RFC-822 I think)
     *and change it from 24 hour to 12 hour time.
     *
     *Other in and out formats can be used.
     *
     *Automatically defaults to an input format of
     *"American" Style (month - day - year with whatever seperator) so you can
     *can leave in_format blank if you're messing with US datetimes
     *
     *If you're going to use another input style 
     *(IE the "international" day - month - year) 
     *, you'll need to specify that in the in_format
     *
     *in_format and out_formats are defined here
     *
     *http://code.google.com/p/datejs/wiki/FormatSpecifiers (under the .toString documentation)
     *
     *NOTE: I could never get the single letter date formats specified
     *on the website to work,
     *but stringing together "Date.CultureInfo.formatPatterns.foo" works well

      The CultureInfo shortDate Format Pattern                                     "M/d/yyyy"
      The CultureInfo longDate Format Pattern                                      "dddd, MMMM dd, yyyy"
      The CultureInfo fullDateTime Format Pattern                                  "dddd, MMMM dd, yyyy h:mm:ss tt"
      The CultureInfo monthDay Format Pattern                                      "MMMM dd"
      The CultureInfo rfc1123 Format Pattern                                       "ddd, dd MMM yyyy HH:mm:ss GMT"
      The CultureInfo sortableDateTime Format Pattern                              "yyyy-MM-ddTHH:mm:ss"
      The CultureInfo shortTime Format Pattern                                     "h:mm tt"
      The CultureInfo longTime Format Pattern                                      "h:mm:ss tt"
      The CultureInfo universalSortableDateTime Format Pattern                     "yyyy-MM-dd HH:mm:ssZ"
      The CultureInfo yearMonth Format Pattern                                     "MMMM, yyyy"
 .
     *
     * you can also make up custom date formats by combining the
     * standard mm/dd/yyy and so on. That information is also listed
     * in the link.
     *
     */

/**
     *  in_date : date to be parsed
     *  format_info: array that can have 'in_format' and 'out_format' elements (or neither, there is a default!)
     */
function fixDate(in_date, format_info){

    correct_date = ''
    use_fixHours = false;
    //    correct_date = Date.parse(in_date).toString('MMMM dd, yyyy h:mm tt');

    //    //Set Output
    if(!format_info.out_format){
        out_format = "dddd, MMMM dd, yyyy h:mm:ss tt";
    }
    else{
        out_format = format_info.out_format;
    }
    //If using 12 hour time, reguardless of helper formats
    //change format so that when fixHours does it's thing, it
    //works properly
    if(out_format.indexOf(':ss') !=-1){
        use_seconds = true;
    }
    else{
        use_seconds = false;
    }
    if(out_format.indexOf('hh:') !=-1){
        use_two_digit_hours = true;
    }
    else{
        use_two_digit_hours = false;
    }

    if(out_format.indexOf('h:')!=-1||out_format.indexOf('hh:')!=-1){
        use_fixHours = true;
        out_format = out_format.replace('h:mm tt','HH:mm:ss');
    }

    //Do Parse
    if(format_info.in_format){
        correct_date = Date.parse(in_date, format_info.in_format).toString(out_format);
    }
    else{
        correct_date = Date.parse(in_date).toString(out_format);
    }
    //Actually fix hours if using 12 hour time
    if(use_fixHours){
        correct_date = (fixHours(correct_date, use_two_digit_hours, use_seconds));
    }
    return correct_date
}

function fixHours(date, use_two_digit_hours, use_seconds) {
    var d = new Date(date);
    var hh = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    var dd = "AM";
    var h = hh;
    if (h >= 12) {
        h = hh-12;
        dd = "PM";
    }
    if (h == 0) {
        h = 12;
    }
    m = m<10?"0"+m:m;

    s = s<10?"0"+s:s;

    if(use_two_digit_hours){
        h = h<10?"0"+h:h;
    }
    var pattern = new RegExp("0?"+hh+":"+m+":"+s);

    var repalcement = h+":"+m;
    if(use_seconds){
        repalcement += ":"+s;
    }
    repalcement += " "+dd;

    return date.replace(pattern,repalcement);
}

