/*
 * PhpFoxModules Javascript Library v0.1
 * http://www.PhpFoxModules.com
 *
 * Copyright PhpFoxModules.com
 *
 */


/* Extensions */


Function.prototype.bind = function(obj) {
  var method = this, temp = function() {
    return method.apply(obj, arguments)
  };
  return(temp);
}; 


/*
__super_class = function (obj) {
  this.__super=obj;
  this.__parent=obj.prototype.parent;
};

__super_class.prototype = {
  __super_method : function(method, pointer) {
    var __pointer=pointer;
    this[method]=function() {
      var __parent=this.__context.parent;
      this.__context.parent=__parent ? __parent.parent : null;
      var __ret=__pointer.apply(this.__context, arguments);
      this.__context.parent=__parent;
      __parent=null;
      return __ret;
    };
  },
  
  construct : function(context) {
    this.__context=context;
    var a=new Array();
    for (var i=1; i<arguments.length; i++) {
      a.push(arguments[i]);
    }
    this.__context.parent=this.__parent;
    var __ret=this.__super.apply(context, a);
    this.__context.parent=this;
    return __ret;
  }
};

Function.prototype.extend=function(obj) {
  this.prototype.parent=new __super_class(obj);
  for (var i in obj.prototype) {
    if (typeof obj.prototype[i]=='function') {
      this.prototype[i]=obj.prototype[i];
      this.prototype.parent.__super_method(i, obj.prototype[i]);
    }
    else if (i!='parent') {
      this.prototype[i]=obj.prototype[i];
    }
  }
};

*/


/* PMods */


if (typeof PMods == 'undefined') {
  PMods = function () {};
}


/* PMods Utils */


PMods.Utils = function () {};
PMods.Utils = {
  dbgFunc : null,
  
  htmlspecialchars : function (text) {
	  return text ? text.toString().replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#039;').replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
  },

  toHtml : function (text) {
	  return PMods.Utils.htmlspecialchars(text).replace(/\n/g, '<br />');
  },
  
  debug : function (message) {
	if(PMods.debug)
	  this.dbgFunc ? this.dbgFunc(message) : alert(message);
  },
  
  setDebugger : function(dbgFunc) {
	this.dbgFunc = dbgFunc;
  },
  
  unescapeQuotes : function (word) {

	// jsesq is a key/acronym for javascript escaped single quote
	// jsdsq is a key/acronym for javascript escaped double quote
  
	escaped = word.replace(/:jsesq:/g, "'"); 
	escaped = escaped.replace(/:jsedq:/g, '"'); 
	escaped = escaped.replace(/:jselb:/g, '\['); 
	escaped = escaped.replace(/:jserb:/g, '\]'); 
	escaped = escaped.replace(/:jsebs:/g, '\\'); 
  
	return escaped;
  },
  
  arrayToQueryString : function (queryArray)  {
	var query = '';
	
	for( var key in queryArray ) {
	  query += encodeURIComponent(key) + '=' + encodeURIComponent(queryArray[key]) + '&';
	}
	
	return query.slice(0, -1);
  }

};


/* PMods Browser */


PMods.Browser = function () {};
PMods.Browser = {
    isIE : (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ),
    isFireFox : (/FireFox/i.test(navigator.userAgent)),
    isOpera : (/Opera/i.test(navigator.userAgent)),

    addEvent : function (obj, type, func) {
        if (obj.addEventListener) {
            obj.addEventListener(type, func, 0);
        } else if (obj.attachEvent) {
            obj.attachEvent("on" + type, func);
        }
    },
    
	register_onload : function (handler) {
	  if (window.onload) {
	    var original_handler=window.onload;
	    window.onload=function() { original_handler(); handler(); };
	  }
	  else {
	    window.onload=handler;
	  }
	},
	
	ge : function(element) {
      var elem;
      if( typeof element == 'string' ) {
        elem = document.getElementById(element);
      } else {
        elem = element;
      }
      return elem;
	},
    
	geByName : function(element) {
		var elems = document.getElementsByName(element);
        if(elems.length == 1)
          return elems[0];
        return null;
	},
    
    show : function () {
      for( var i = 0; i < arguments.length; i++ ) {
        var element = PMods.B.ge(arguments[i]);
        if (element && element.style) element.style.display = 'block';
      }
    },
    
    hide : function () {
      for( var i = 0; i < arguments.length; i++ ) {
        var element = PMods.B.ge(arguments[i]);
        if (element && element.style) element.style.display = 'none';
      }
    },

    toggle : function () {
      for( var i = 0; i < arguments.length; i++ ) {
        var element = PMods.B.ge(arguments[i]);
	    element.style.display = (element.style.display == 'block') ? 'none' : 'block';
      }
    },

    // Get Absolute X Position of HTML Element
    findX : function(obj) {
      var curleft = 0;
      if (obj.offsetParent) {
        while (obj.offsetParent) {
          curleft += obj.offsetLeft
          obj = obj.offsetParent;
        }
      }
      else if (obj.x)
        curleft += obj.x;
      return curleft;
    },
    
  
    // Get Absolute Y Position of HTML Element
    findY : function (obj) {
      var curtop = 0;
      if(obj.offsetParent) {
        while (obj.offsetParent) {
          curtop += obj.offsetTop
          obj = obj.offsetParent;
        }
      }
      else if (obj.y)
        curtop += obj.y;
      return curtop;
    },
    
    mousePosX : function (e) {
      var posx = 0;
      if (!e) var e = window.event;
      if (e.pageX)
        posx = e.pageX;
      else if (e.clientX && document.body.scrollLeft)
        posx = e.clientX + document.body.scrollLeft;
      else if (e.clientX && document.documentElement.scrollLeft)
        posx = e.clientX + document.documentElement.scrollLeft;
      else if (e.clientX)
        posx = e.clientX;
      return posx;
    },
    
    mousePosY : function (e) {
      var posy = 0;
      if (!e) var e = window.event;
      if (e.pageY)
        posy = e.pageY;
      else if (e.clientY && document.body.scrollTop)
        posy = e.clientY + document.body.scrollTop;
      else if (e.clientY && document.documentElement.scrollTop)
        posy = e.clientY + document.documentElement.scrollTop;
      else if (e.clientY)
        posy = e.clientY;
      return posy;
    },
    
    getStyle : function(obj, property) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(obj, null).getPropertyValue(property);
        }
        if (document.defaultView && document.defaultView.getComputedStyle) {
            var computedStyle = document.defaultView.getComputedStyle(obj, null);
            if (computedStyle) return computedStyle.getPropertyValue(property);
        }
        if (obj.currentStyle) {
            return obj.currentStyle[property];
        }
        return obj.style[property];
    },
    
    getStyleName : function(stylename) {
      return PMods.Browser.isIE ? stylename : stylename.replace(/[A-Z]/g, function(a){return'-'+a.toLowerCase();} );
    },
    
    // em's not supported for now
    getPXMetrics : function(metric, defvalue) {
      var metricBase = parseFloat(metric);
      if(isNaN(metricBase)) return defvalue!=null ? defvalue : metricBase;
      return /px/i.test(metric) ? metricBase : /pt/i.test(metric) ? 1.3333*metricBase  : metricBase;
    },
    
    createDiv : function( parent, id, cname ) {
      var div = document.createElement("div");
      if(id) div.id = id;
      if(cname) div.className = cname;
      parent.appendChild( div );
      return div;
  }
  
};



/* Shortcuts */



PMods.B = PMods.Browser;
PMods.U = PMods.Utils;




/* PMods Ajax */



PMods.Ajax = function (doneHandler, failHandler)
{
  this.onDone = doneHandler;
  this.onFail = failHandler;
  this.transport = this.getTransport();
  this.transport.onreadystatechange = this.stateDispatch.bind(this);
};

PMods.Ajax.prototype = {
  
  get : function (uri, query, force_sync)  {
    // Firefox doesn't call onDone and onFail handlers if you force_sync
    force_sync = force_sync || false;
    if( typeof query != 'string' )
      query = PMods.U.arrayToQueryString(query);
    fullURI = uri+(query ? ('?'+query) : '');
    this.transport.open('GET', fullURI, !force_sync );
    this.transport.send('');
  },

  post : function (uri, data, force_sync) {
    force_sync = force_sync || false;
    if( typeof data != 'string' )
      data = PMods.U.arrayToQueryString(data);
    this.transport.open('POST', uri, !force_sync);
    this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.transport.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    this.transport.send(data);
  },
  
  call : function (uri, call, params, force_sync) {
	params = params || '';
    if( typeof params != 'string' )
      params = PMods.U.arrayToQueryString(params);
	var sParams = 'phpfox[ajax]=true&phpfox[call]=' + call + '&phpfox[security_token]=' + oCore['log.security_token'];
  	params = (params == '' ? sParams : sParams + '&' + params);
	this.post(openidconnect_api_endpoint, params)	
  },

  stateDispatch : function () {

    if( this.transport.readyState == 4 ) {
      if( this.transport.status >= 200 &&
          this.transport.status < 300 &&
          this.transport.responseText.length > 0 ) {
        if( this.onDone ) this.onDone(this, this.transport.responseText);
      } else {
        if( this.onFail ) this.onFail(this);
      }
    }
  },

  getTransport : function () {
    var ajax = null;
    
    try { ajax = new XMLHttpRequest(); }
    catch(e) { ajax = null; }
    
    try { if(!ajax) ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e) { ajax = null; }
    
    try { if(!ajax) ajax = new ActiveXObject("Microsoft.XMLHTTP")}
    catch(e) { ajax = null; }
    
    return ajax;
  },
  
  toResponse : function(responseText) {
	responseText = responseText || this.transport.responseText;
	var r = [];
	try {
	  r = eval('(' + responseText + ')')
	} catch(e) {
	  r.status = 1;
	  r.err_msg = 'HTTP Error';
	  r.err_code = 100;
	};
	return r;
  }
  
};

