/**
 * PGX Framework, version 1.2
 * Copyright (c) 2010 PGX Framework
 *
 * Author: Jasper Ancher
 * Info: http://www.pgxframework.com
 *
 * This software is released under the MIT License
 * Info: http://www.opensource.org/licenses/mit-license.php
 */

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

// PGX Class
var PGX = Class.create({
												
	version: '1.0',

	// Object initialization
	initialize: function() {
		// Initializes the prefix and path. 
		this.Prefix = this.getPrefix();
		this.Path 	= this.getPath();
	},
	
	// Get prefix
	getPrefix: function() {
		var sPrefix = window.location.href;
		var aPrefix = sPrefix.split('/');
		sPrefix = aPrefix[0] + '//' + aPrefix[2];
		return sPrefix;
	},
	
	// Get path
	getPath: function() {
		var aScripts = $$('head script').pluck('src');
		var sPath = aScripts.find(function(sPath){
			return sPath.match(/_pgxframework/);
		});
		var aPath = sPath.split('_pgxframework');
		var sPath = aPath[0].toString().substring(0, (sPath.length - 1));
		// Remove prefix
		return sPath.replace(this.Prefix, '');
	},
	
	// Get config of the requested PGX function
	getConfig: function(sFunction) {
		var sURL 		= this.Path + '/_pgxframework/' + sFunction + '/pgx.config.xml';
		var aConfig = new Array;
		new Ajax.Request(sURL,{
		    method:'get',
		    onSuccess: function(oXML){
					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() {
		var oPgxAjaxElement = new Element('span', { 'id': 'pgxAjaxElement' });
		document.getElementsByTagName("body")[0].appendChild(oPgxAjaxElement);
		$('pgxAjaxElement').hide();
	},
	
	// Check in array
	inArray: function(aArray, value) {
		if (aArray.indexOf(value) == -1) {
			// Add to array
			aArray[aArray.length] = value;
			return false;
		} 
		return true;
	},
	
	// Load css
	cssLoad: function(css) {
		if (!PGX.inArray(pgxCSS, css)) {
			var eLink = new Element('link', { 'href': css, 'rel': 'stylesheet', 'type': 'text/css' });
			document.getElementsByTagName("head")[0].appendChild(eLink);
		}
	},
	
	// Load js
	jsLoad: function(js) {
		if (!PGX.inArray(pgxJS, js)) {
			var eScript = new Element('script', { 'src': js, 'type': 'text/javascript' });
			document.getElementsByTagName("head")[0].appendChild(eScript);
		}
	},
	
	// Element
	pgx: function(sElement, sFunction, aParams) {
		var sURL 		= this.Path;
		// Force params object
		var aParams = Object.clone(aParams);
		
		// Check if sElement exists if not do the request again.
		if (!sElement) {
			var sElement = 'pgxAjaxElement';
			
			// Check if the pgxAjaxElement exists
			if (!$(sElement)) {
				setTimeout(function() {
					PGX.pgx(sElement, sFunction, aParams);
				}, 100);
				return;
			}
		}
		
		// Config AJAX
		aParams.pgxFunction = sFunction;
		var aAJAXParams 		= { method:'post', evalScripts:true };
		
		if (aParams.onComplete) {
			aAJAXParams.onComplete = aParams.onComplete;
		}
		
		if (aParams.onSuccess) {
			aAJAXParams.onSuccess = aParams.onSuccess;
		}
		
		aAJAXParams.parameters = aParams;
		// Execute AJAX the ajax request to {host}/_pgxframework/pgx.php
		new Ajax.Updater(sElement, sURL, aAJAXParams);
	}
	
});

// Initializes the public PGX object
PGX = new PGX();

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

// Direct call to pgx function of the PGX object.
function pgx(sElement, sFunction, aParams) {
	if (sElement != '' && !$(sElement)) {
		aParams = sFunction;
		sFunction = sElement;
		sElement = '';
	}	
	return PGX.pgx(sElement, sFunction, aParams);
}

// Extend the prototype object
Element.Methods.pgx = function(sObject, sElement, sFunction, aParams) {
	if (!sElement.blank() && !$(sElement)) {
		aParams 	= sFunction;
		sFunction = sElement;
		sElement 	= sObject;
	}
	return PGX.pgx(sElement, sFunction, aParams);
}
Element.addMethods();