/**
 * PGX Framework, version 1.0
 * Copyright (c) 2009 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',
	
	// 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));
				
				// Remove prefix
				sPath = sPath.replace(this.Prefix, '');
				
				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() {
		tmpElement = new Element('span', { 'id': 'pgxAjaxElement' } );
		document.getElementsByTagName("body")[0].appendChild(tmpElement);
		$('pgxAjaxElement').hide();
	},
	
	// 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 = 'pgxAjaxElement';
			
			// Check if pgxAjaxElement 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 + '/';
		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) {
	if (sElement != '' && !$(sElement)) {
		aParams = sFunction;
		sFunction = sElement;
		sElement = '';
	}
	
	return PGX.pgx(sElement, sFunction, aParams);
}

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