// Summary: General(), a general class with methods that is used often.
//         Instead of rewriting the same method for other classes, this is used instead

var GENERAL_G = new function() {
  this.ajax_cnt = 0;
};

function General() {

};

// Summary: Ajax Call
// params: url - url that needs to be used
//         methods - an array of methods to invoke
//         args(optional) - object with specific criterias 
General.prototype.ajaxCall = function(url, methods, args) {
   var len = 0;
   var error_msg = '';
   var datatype = 'json';
   var tmpThis = this;

   // check to make sure methods is an Object
   if (methods instanceof Object) {
     len = methods.length;
   } else {
      len = [];
   }

   //len = methods.length;
   // json is default error type

   if (args instanceof Object) {
       if ( args.error_msg ) {
           error_msg = args.error_msg;
       } else if ( args.datatype ) {
           datatype = args.datatype;
       } else if (args.resend == 1) {
           // this means ajax got resent
           GENERAL_G.ajax_cnt = 1;
       }
   } else {
      args = {};
   }

   $.ajax( { type: 'GET',
             url: url,
             dataType: datatype,
             success: function(msg) {
                for (var i=0; i < len; i++) {
                  var method = methods[i];
                  tmpThis[method](msg,args);
                 }
                tmpThis.clearGlobals();
              },
             cache: false, 
             timeout: 4000,
             error: function(xhttp, textStatus, errorThrown) {
                             var args = { 'resend' : 1 };
                             if (GENERAL_G.ajax_cnt == 0) {
                                 tmpThis.ajaxCall(url, methods, args);
                             }
                             if (GENERAL_G.ajax_cnt == 1 && xhttp.readyState == 4) { //alert message after 2nd ajax call
                                 var error = ' RESPONSE ERROR ';
                                 if (textStatus) {
                                     error = textStatus;
                                 } else if (errorThrown) {
                                     error = errorThrown;
                                 }
                                 alert('ERROR WITH AJAX SERVER: ' + error);
                             }
                    }   
           });
};

// Summary: Get the current domain for the user
General.prototype.getDomain = function() {
    var ref = window.location.href;
    var pattern = /(http:\/\/[^\/]+)/;
    var result = ref.match(pattern);
    // default to my sandbox
    var url = 'http://www.friendigo.com:7070/';
    if (result.length) {
        url = result[1];
    } 
    return url;
}

//Find Position of any element
//params: obj - the event object element e.g. <div ></div> 
General.prototype.findPos_ = function(obj) {
   var curleft = 0;
   var curtop = 0;
   if (obj.offsetParent) {
     curleft = obj.offsetLeft;
     curtop = obj.offsetTop;
     while (obj = obj.offsetParent) {
       curleft += obj.offsetLeft;
       curtop  += obj.offsetTop;
     }
   }
   return [curleft,curtop];
 }

// Summary: Clear Globals
General.prototype.clearGlobals = function() {
  GENERAL_G.ajax_cnt = 0;
}

