var myb = {

  /**
   * refreshes ad(s)
   * @param String jQuery selector pattern
   * @param Integer (optional) time in milliseconds between allowed ad refreshes. Default = 7000ms (7s)
   */
  refreshAd : function ( pattern, timeDelay )
  {
    if ( ! timeDelay )
    {
      timeDelay = 30000;
    }

    $(pattern).each(function(){
      var t = new Date().getTime();
      if ( this.newAdAble > t )
      {
        return;
      }
      if ( this.src.substring(0, 11) == 'javascript:' )
      {
        return;
      }
      this.newAdAble = t + timeDelay;
      var s = this.src.replace(/[?&]mybt=[0-9]+/, '');
      this.src = s + (s.indexOf('?') > 0 ? '&' : '?') + 'mybt=' + t;
    });
  },

  timeAgoText : function ( ts )
  {
    if ( ! ts || isNaN( parseInt( ts ) ) )
    {
      return false;
    }

    var now = Math.floor( new Date().getTime() / 1000 );

    // If it looks like a timestamp, convert it to number of seconds ago
    if ( ts >= 915170400 )
    {
      ts = now - ts;
    }

    if ( ts < 5 )
    {
      return 'a moment ago';
    }
    var duration = ts;
    var units = [ "second", "minute", "hour", "day", "month", false ];
    var divisors = [ 60, 60, 24, 28, 12 ];
    var x = 0;
    do
    {
      if ( duration < divisors[x] )
      {
        return duration + ' ' + units[x] + (duration == 1 ? '' : 's') + ' ago';
      }
      duration = Math.floor( duration / divisors[x++] );
    } while ( units[x] );
    var dateobj = new Date( 1000 * (now - ts) );
    var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    return months[dateobj.getMonth()] + ' ' + dateobj.getDate() + ', ' + dateobj.getFullYear();
  },

  /**
   * Base64 encodes an input string
   * @param String
   * @return String base64-encoded string
   */
  encode64 : function ( input )
  {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    do
    {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;
      if (isNaN(chr2))
      {
        enc3 = enc4 = 64;
      }
      else if (isNaN(chr3))
      {
        enc4 = 64;
      }
      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
    } while (i < input.length);
    return output;
  },

  formatNum : function ( num )
  {
    num += '';
    var re = /(\d+)(\d{3})/;
    while ( re.test(num) )
    {
      num = num.replace(re, '$1' + ',' + '$2');
    }
    return num;
  },
  
  reportImage : function ( id, is_user )
  {
    var url = '/ajax/reportImage.php';
    if( window.SERVICE_NAME )
    {
      url = '/comet/reportimage';
    }

    var data = {};
    if ( is_user === true )
    {
      data.userID = id;
    } else {
      data.imageID = id;
    }
    $.dragonConfirm( '<div>myYearbook strictly prohibits pictures depicting nudity, violence, and weapons.  Any time you see a picture that violates this policy, please report the image.</div><div>Are you sure you want to report this image?</div>', { title: "Report This Image", curtain: false, cancelImg: 'btn_cancel.gif', confirmCallback: function () 
      {
        $.ajax( {url:url, type:'post', data:data, dataType:'text', success: function() 
          {
            $.dragonAlert('Your report about this image has been delivered to myYearbook Member Services staff.', {title:'Confirmation', curtain: false});
          }, error: function()
          {
            $.dragonAlert('An error occurred while trying to deliver your report about this image.  Please try again later.', {title:'An error occurred', curtain: false});
          }
        });
      }
    });
  }


};




/**
 * Some String object extended methods
 */
String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
        return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
        return this.replace(/\s+$/,"");
};
String.prototype.toNumberFormat = function() {
	var out = this + '';
	var re = /(\d+)(\d{3})/;
	while ( re.test(out) )
	{
	  out = out.replace(re, '$1' + ',' + '$2');
	}
	return out;
}
String.prototype.toProperCase = function(){
     return this.toLowerCase().replace(/\w+/g,function(s){
          return s.charAt(0).toUpperCase() + s.substr(1);
     })
};

