/*
  Author: Moises Beltran
  About: Given an offset, will return time for that timezone
         Performs Basic formatting. 
*/
//Can take offset and city
var UtcTime = Base.extend({
  constructor: function() {
    if(arguments[0]) this.offset = arguments[0];
    if(arguments[1]) this.city = arguments[1];
  },
  utc: function () {
    //create Date object for current location
    this.date = new Date();
    //convert to msec, add local time zone offset, get UTC time in msec
    return this.date.getTime() + (this.date.getTimezoneOffset() * 60000);
  },
  get_date: function(){
    //Calcuate the new UTC time using given/defined offset
    nd = new Date(this.utc() + (3600000*this.offset));
    return nd;	
  },
  //Will calculate the time difference
  calcTime: function(city, offset) {
    //return either true or false, will determine if time is within 12am and 5pm ET
    return this.withinWindow();
  },
  check_day: function(a,b){
	return (a == b);
  },
  is_friday: function(){
	return this.check_day(this.get_date().getDay(),5);
  },
  is_saturday: function(){
	return this.check_day(this.get_date().getDay(),6);
  },
  is_sunday: function(){
	return this.check_day(this.get_date().getDay(),0);
  },
  dst_range: function(date){
/*
dst start and end dates
2008: March 9 - Nov. 2
2009: March 8 - Nov. 1
2010: March 14 - Nov. 7
2011: March 13 - Nov. 6
*/
    year = date.getYear() + 1900; //we need to add 1900 to get current year
    month = date.getMonth();
    day = date.getDate();
    
    if(year == 2008){
      if ( (month == 2 && day >= 9) || (month == 10 && day >= 2) || (month > 2 && month < 10) ) return true;
    }
    else if(year == 2009){
      if ( (month == 2 && day >= 8) || (month == 10 && day >= 1) || (month > 2 && month < 10) ) return true;
    }
    else if(year == 2010){
      if ( (month == 2 && day >= 13) || (month == 10 && day >= 7) || (month > 2 && month < 10) ) return true;
    }
    else if(year == 2011){
      if ( (month == 2 && day >= 14) || (month == 10 && day >= 6) || (month > 2 && month < 10) ) return true;
    }
    return false;
  },
  dst: function(){
    rightNow = new Date();
    date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
    date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
    temp = date1.toGMTString();
    date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
    temp = date2.toGMTString();
    date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
    hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
    hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
    if (hoursDiffDaylightTime == hoursDiffStdTime) {
      //does not observe dst
      return 0;
    } else {
      //observes dst.
      //alert(this.dst_range(rightNow))
      if(this.dst_range(rightNow) == false) return 1; //we not within the dst range
      else
        return 2;
    }
  },
  format_time: function(hours,minutes){
    //this.daylight_savings_offset();
    var h = this.max_utc_hour - hours  - this.dst() ;
    if( BrowserDetect.browser == "Explorer") h -= 1;
    //if( BrowserDetect.browser == "Firefox") h += 1;
    var m = 60 - minutes;
    this.time = ''
    
    if(h > 0){
      this.time = h+ ' hour'+(h > 1 ? 's' : '') + ' and '
    }
    if(hours == this.max_utc_hour && minutes == 0) this.time = ' minute ';
    else
      this.time += m + ' minute' + (m > 1 ? 's ' : ' ')
    if(h < 0) this.time = ''; 
  },
  withinWindow: function(){
    //Calcuate the new UTC time using given/defined offset
    nd = this.get_date();
    //return either true or false, will determine if time is within 12am and 5pm ET
    this.format_time(nd.getHours(),nd.getMinutes())
    //if its less than 5pm ET
    if(this.time != '' && (nd.getHours() < this.max_utc_hour || (nd.getHours() == this.max_utc_hour && nd.getMinutes() == 0)) ) return true; 
    else
      return false;
  },
  time: "",
  city: 'Georgia, USA',
  offset: '-5',
  max_utc_hour: 17
});
