/**
 * MPA Solutions ©2006
 * Via Lung'Adige San Nicoḷ n. 18 - 38100 Trento, ITALY
 *
 * Tel. +39-0461-230400/264354
 * Fax: +39-0461-264353
 * email: info@mpasol.it
 * url: http://www.mpasol.it
 */

//------------------------------------------------------------------------
// DEPENDENCIES: 

//------------------------------------------------------------------------
// CONSTANTS:

//------------------------------------------------------------------------
// GLOBALS:
var OS,BROWSER,BROWSERVERSION,THESTRING;					// Browser and platform infos
var INCLUDED_JS_FILES = new Array();							// list of included JS library files
var INCLUDED_CSS_FILES = new Array();							// list of included CSS files 
var BEFORE_LOAD_FRAMES = new Array();							// frames for which <head> loading is still in progress (<body> not loaded yet)
var AFTER_LOAD_FRAMES = new Array();							// frames for which the <body> was loaded completely
//var NUMBER_OF_FRAMES = 4;
	
//------------------------------------------------------------------------
// ROUTINES SECTION:

UADetect();
// include configuration (as a Javascript library)
//include_JS('lib/GlobalConfig.js');
// include common stylesheets (superseded by platform specific stylesheets, if any)



/****************************************************************************************************/
function beforeLoad(frameName){				// callback function from each page <HEAD>
		BEFORE_LOAD_FRAMES.push(frameName);
		//frames[frameName].document.write(frameName);
		var HEAD_code = "";
		HEAD_code += '<meta http-equiv="pragma" content="nocache">\n'
		HEAD_code += '<meta http-equiv="expires" content="-1">\n'
		HEAD_code += '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />\n'
		for (var i=0; i<INCLUDED_CSS_FILES.length; i++){
				HEAD_code += '<link rel=\'stylesheet\' type=\'text/css\' href=\''+ INCLUDED_CSS_FILES[i] + '\' />\n';
		}
		return HEAD_code;	
}

function afterLoad(frameName){		 		// callback function from each page '<BODY onload=...>' event	
		AFTER_LOAD_FRAMES.push(frameName);
}

/****************************************************************************************************
 * include_CSS(FileToInclude)
 *
 * Creates the list of stylesheets to include in each frame
 * @param  FileToInclude 	filename containing the stylesheet
 */	
 function include_CSS(FileToInclude){
		    if (!in_array(FileToInclude, INCLUDED_CSS_FILES)) {
		        INCLUDED_CSS_FILES.push(FileToInclude);
		    }		
 }
		
/****************************************************************************************************
 * include_JS(FileToInclude)
 *
 * Dynamic Javascript libarary inclusion
 * @param  FileToInclude 	filename containing the Javascript library
 * @see    								http://www.phpied.com/javascript-include/
 */	
 function include_JS(FileToInclude) {
		    if (!in_array(FileToInclude, INCLUDED_JS_FILES)) {
		        INCLUDED_JS_FILES.push(FileToInclude);
						include_dom(FileToInclude);
		    }
				function include_dom(script_filename) {
						switch(BROWSER){
								case "PocketIE":
										//http://www.codehouse.com/javascript/articles/external/
										var html_doc = '<script src=\'' + script_filename + '\' type=\'text/JavaScript\'><\/script>\n';		   	
										document.write(html_doc); 								
								break;
								default:
											// supported document.getElementsByTagName method
    									var html_doc = document.getElementsByTagName('head').item(0);
    									var js = document.createElement('script');
    									js.setAttribute('language', 'javascript');
    									js.setAttribute('type', 'text/javascript');
    									js.setAttribute('src', script_filename);
    									//js.setAttribute('', 'defer');
    									html_doc.appendChild(js);
    									return false;								
								break;
						}		

				}							
 }

function in_array(key, file_hash) {
    for (var i = 0; i < file_hash.length; i++) {
        if (file_hash[i] == key) {return true;}
    }
    return false;
}		





/****************************************************************************************************
 * UADetect()
 *
 * Detects browser and platform
 * @param  none
 * @return global parameters: OS,BROWSER,BROWSERVERSION,THESTRING;
 * @see    http://www.quirksmode.org/js/detect.html
 */
function UADetect(){
var detect = navigator.userAgent.toLowerCase();
// navigator.userAgent:
//  - iPAQ H5550 (Microsoft Windows PocketPC 2003 Premium Edition): Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)
//	- Acer n311(Windows Mobile 2005): Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)
// Una lista per gli useragent dei PDA e' in http://www.zytrax.com/tech/web/mobile_ids.html  

if (checkIt('windows ce')){				// Check for Windows CE (Pocket PC, Palm-size PC, Handheld PC, Handheld PC Pro)
	OS = navigator.platform; 				// iPAQH5550: WinCE, Win32, Linuxi686
	if (checkIt('240x320')) {OS = "Pocket PC"};
	if (checkIt('msie')) {BROWSER = "PocketIE"}
	else {BROWSER = "unknown browser"};
	}
else if (checkIt('konqueror')){
	BROWSER = "Konqueror";
	OS = "Linux";
	}
else if (checkIt('safari')) {BROWSER = "Safari"}
else if (checkIt('omniweb')) {BROWSER = "OmniWeb"}
else if (checkIt('opera')) {BROWSER = "Opera"}
else if (checkIt('webtv')) {BROWSER = "WebTV"}
else if (checkIt('icab')) {BROWSER = "iCab"}
else if (checkIt('msie')) {BROWSER = "Internet Explorer"}
else if (!checkIt('compatible')){
	BROWSER = "Netscape Navigator"
	//BROWSERVERSION = detect.charAt(8);
	BROWSERVERSION = detect.substring(detect.lastIndexOf('/')+1, detect.length);
	}
else {BROWSER = "An unknown browser"};
if (!BROWSERVERSION) {
	BROWSERVERSION = detect.charAt(place + THESTRING.length)
	};
if (!OS){
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
	}



function checkIt(string){
	place = detect.indexOf(string) + 1;
	THESTRING = string;
	return place;
	}
}
/****************************************************************************************************/
				

