// Config
var pgxCSS = new Array;
var pgxJS = new Array;

// PGX Class
var PGX = Class.create({
											 	
	version: '0.1',
	
	// Init
	initialize: function() {
		// Prefix
		this.Prefix = this.getPrefix();
		
		// Path
		this.Path = this.getPath();
	},
	
	// Get prefix
	getPrefix: function() {
		sPrefix = window.location.href;
		aPrefix = sPrefix.split('/');
		sPrefix = aPrefix[0] + '//' + aPrefix[2];

		return sPrefix;
	},
	
	// Get path
	getPath: function() {
		aScripts = document.getElementsByTagName('script');
		
		for (iCount = 0; iCount < aScripts.length; iCount++) {
			sPath = aScripts[iCount].src;
			
			if (sPath.match(/_pgxframework/)) {
				aPath = sPath.split('_pgxframework');
				sPath = aPath[0];
				sPath = sPath.toString();
				sPath = sPath.substring(0, (sPath.length - 1));
				
				return sPath;
			}
		}
	},
	
	// Get config
	getConfig: function(sFunction) {
		sURL = this.Path + '/_pgxframework/' + sFunction + '/pgx.config.xml';
		aConfig = new Array();
		
		if (window.XMLHttpRequest) {
			oXML = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			oXML = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (oXML) {
			oXML.open("GET", sURL, false);
			oXML.send(null);
			
			if (oXML.readyState == 4 && oXML.status == 200) {
				aConfig['defaultVersion'] = oXML.responseXML.getElementsByTagName('defaultVersion')[0].firstChild.nodeValue;
				aConfig['securityPolicy'] = oXML.responseXML.getElementsByTagName('securityPolicy')[0].firstChild.nodeValue;
			}
		}
		
		return aConfig;
	},
	
	// Create AJAX element for execute javascript
	ajaxElement: function() {
		try {
			tmpElement = new Element('span', { 'id': 'ajaxElement' } );
			document.getElementsByTagName("body")[0].appendChild(tmpElement);
		}
		
		catch (error) {
			setTimeout( function() {
				PGX.ajaxElement();
			}, 100);
		}
		
	},
	
	// Check in array
	inArray: function(arr, value) {
		if (arr.indexOf(value) == -1) {
			// Add to array
			arr[arr.length] = value;
			
			return true;
		}
		
		return false;
	},
	
	// Load css
	cssLoad: function (css) {
		if (PGX.inArray(pgxCSS, css)) {
    	link = new Element('link', { 'href': css, 'rel': 'stylesheet', 'type': 'text/css' });
			document.getElementsByTagName("head")[0].appendChild(link);
		}
	},
	
	// Load js
	jsLoad: function (js) {
		if (PGX.inArray(pgxJS, js)) {
    	script = new Element('script', { 'src': js, 'type': 'text/javascript' });
			document.getElementsByTagName("head")[0].appendChild(script);
		}
	},
	
	// Element
	pgx: function(sElement, sFunction, aParams) {
		// Force params object
		aParams = Object.clone(aParams);
		
		// Check sElement
		if (!sElement) {
			sElement = 'ajaxElement';
			
			// Check if ajaxElement exists
			if (!$(sElement)) {
				setTimeout( function() {
					PGX.pgx(sElement, sFunction, aParams);
				}, 100);
				
				return;
			}
		}
		
		// Config AJAX
		aParams.pgxFunction = sFunction;
		aAJAXParams = { method:'post', evalScripts:true };
		
		if (aParams.onComplete) {
			aAJAXParams.onComplete = aParams.onComplete;
		}
		
		if (aParams.onSuccess) {
			aAJAXParams.onSuccess = aParams.onSuccess;
		}
		
		aAJAXParams.parameters = aParams;
		
		// Execute AJAX
		sURL = this.Path + '/_pgxframework/pgx.php';		
		new Ajax.Updater(sElement, sURL, aAJAXParams);
	}
	
});

// Init
PGX = new PGX();

// Create AJAX element to execute javascript
document.observe("dom:loaded", function() {
	PGX.ajaxElement();
});

// Direct call to function in PGX Class
function pgx(sElement, sFunction, aParams) {
	return PGX.pgx(sElement, sFunction, aParams);
}