/**
 * @fileoverview Loads generic modules required for all widgets.
 *
 * <pre>
 * Copyright (c) 2003-2008 by BWin Technologies Ltd.
 * http://www.bwintech.com
 * Quay House, South Esplanade, St. Peter Port
 * GY1 4EJ Guernsey, CI, UK
 * All rights reserved.
 * </pre>
 */

/* $Id: bwin.js */

if (typeof BWin == 'undefined') {BWin = function() {};}																		// Namespace definition.
/**
	* BWin version.
	* @private
	*/
BWin.version = '2.4';
/**
	* Path to main BWin script.
	* @private
	*/
BWin.bwinPath = function() {
  var arrScripts = document.getElementsByTagName('script');																// Get all script elements
  for (var iScript = arrScripts.length - 1; iScript >= 0; iScript--) {										// Find the script in the list
    var strSrc = arrScripts[iScript].getAttribute('src');
    if (!strSrc) {
      continue;
    }
    var arrTokens = strSrc.split('/');
    var strLastToken;																																			// Remove last token
    if (Array.prototype.pop) {
      strLastToken = arrTokens.pop();
    } else {
      strLastToken = arrTokens[arrTokens.length - 1];																			// IE 5
      arrTokens.length -= 1;
    }
    if (strLastToken == '?id=49823') {
      return arrTokens.length ? arrTokens.join('/') + '/' : '';
    }
  }
  return '';																																							// Not found
} ();
/**
	* Convenience funtion.
	*
	* <pre>
	* Instead of the syntax: $(element)
	* One can use now the shorter syntax: $(element)
	* </pre>
	* returns an array of elements or a single element object
	*/
function $() {
  var elements = [];

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') { element = document.getElementById(element); }
    if (arguments.length == 1) { return element; }
    elements.push(element);
  }
  return elements;
}

/**
	* Convenience funtion.
	* Addition to the document. interface
	* <pre>
	* Syntax: document.getElementByClassName(className, parentElement)
	* The dom tree is inspected from the specified parentElement
	* </pre>
	* returns an array of elements or a single element object
	*/
if(!document.getElementsByClassName){
	document.getElementsByClassName = function(className, parentElement) {
	  var results = [];
	  var children = ($(parentElement) || document.body).getElementsByTagName('*');

		for(var n = 0; n < children.length; n++){
			if(BWin.Utils.hasClassName(children[n], className)){
				results.push(children[n]);
			}
		}

		return results;
	};
}

/**
	* Simply writes script tag to the document.
	*
	* <pre>
	* If special BWin.doNotInclude flag is set, this function does nothing.
	* </pre>
	*
	* @private
	* @param {string} strSrc Src attribute value of the script element
	* @param {string} strId Optional. Id of the script element
	*/
BWin.include = function(strSrc, strId) {
  if (BWin.doNotInclude) {return;}																												// Check flag
  document.write('<script type="text/javascript" src="' + 																// Include file
   strSrc + (typeof strId == 'string' ? '" id="' + strId : '') + '"></script>');
};

BWin.Scriptloader = function(objScripts){
	if(!this.Scripts){ alert("Scripts object not found."); return; }

	this.idString = "";

	this._load(this.Scripts, "Utils");

	this._startScriptLoad(objScripts);
	BWin.include("./?mid=" + this.idString, "BWin.ScriptLoaded");
};

BWin.Scriptloader.prototype._load = function(objScript, strScript){
	if(strScript == "Base.Progress"){ debugger; }
	var dotOccur = strScript.indexOf(".");
	var strScrSplit, objScriptSrc, objScrDef, boolLoadAll, sid;
	if(dotOccur > 0){
		strScrSplit = strScript.split(".");
		if(strScrSplit[1] != "All"){
			if(strScrSplit.length == 2){
				objScriptSrc = objScript[strScrSplit[0]];
				if( typeof objScriptSrc[strScrSplit[1]] == "number"){
					this._loadScript(objScriptSrc[strScrSplit[1]], objScriptSrc);
				}
			}
			this._load(objScript[strScrSplit[0]], strScript.substr(dotOccur+1));
			return;
		} else {
			strScript = strScrSplit[0];
			boolLoadAll = true;
		}
	}
	
	if(objScript[strScript]){
		objScriptSrc = objScript[strScript];
		if(objScriptSrc.reqObjs){
			this._startScriptLoad(objScriptSrc.reqObjs);
		}
		if(boolLoadAll){
			for(sid in objScriptSrc){
				if (objScriptSrc.hasOwnProperty(sid)){
					if( sid == "reqObjs" || sid == "defObjs" ){ continue; }
					if( typeof objScriptSrc[sid] == "number"){
						this._loadScript(objScriptSrc[sid], objScriptSrc);
					} else {
						this._load(objScriptSrc, sid);
					}
				}
			}
		}
		if(objScriptSrc.defObjs){
			objScrDef = objScriptSrc.defObjs;
			for(sid in objScrDef){
				if (objScrDef.hasOwnProperty(sid)){
					if( typeof objScriptSrc[objScrDef[sid]] == "number"){
						this._loadScript(objScriptSrc[objScrDef[sid]], objScriptSrc);
					} else {
						this._load(objScriptSrc, sid);
					}
				}
			}
		} 
	}
};

BWin.Scriptloader.prototype._loadScript = function(numId, objScript){
	if(!objScript.loaded){
		objScript.loaded = [];
	}
	if(!objScript.loaded[numId]){
		if(this.idString.length > 0){ this.idString += ","; }
		this.idString += numId;
		objScript.loaded[numId] = true;
	}
};

BWin.Scriptloader.prototype._startScriptLoad = function(objScripts){
	for( var module in objScripts){
		if (objScripts.hasOwnProperty(module)){
			this._load(this.Scripts, objScripts[module]);
		}
	}
};

BWin.Scriptloader.prototype.Scripts = {
	"Utils" : {
		"defObjs": [ "Utils", "EventDriven", "Transport", "Crypto", "Widget", "Stylesheet", "Preload" ],
		"Utils": 49824,
		"EventDriven": 50112,
		"Transport": 49825,
		"Crypto": 50129,
		"Widget": 49826,
		"Progress": 49830,
		"Preload": 49870,
		"Stylesheet": 49869,
		"Pane": 49831,
		"Button": 49828,
		"Tooltips": 50106,
		"Dragndrop": 49829,
		"Dialog": 50113,
		"Dropdown": 49871,
		"Parse": 50114,
		"Popup": 50115
	},
	"Bmp": {
		"defObjs": [ "Core" ],
		"Core": 51911
	},
	"Calendar": {
		"defObjs": [ "Core", "DateCore", "Setup" ],
		"Core": 49953,
		"DateCore": 49954,
		"Setup": 49955
	},
	"CinemaSlide": {
		"defObjs": [ "Core" ],
		"Core": 52779
	},
	"Colorpicker": {
		"reqObjs": [ "Utils.Dragndrop", "Slider" ],
		"defObjs": [ "Core" ],
		"Core": 52405
	},
	"Dialog": {
		"defObjs": [ "Alert", "Confirm", "Generic" ],
		"Alert": 50400,
		"Confirm": 50401,
		"Generic": 50402
	},
	"Editor": {
		"reqObjs": [ "Utils.Button", "Utils.Pane", "Utils.Tooltips" ],
		"defObjs": [ "Core", "Picker" ],
		"Core": 50331,
		"Picker": 50330
	},
	"Effects": {
		"defObjs": [ "Core" ],
		"Core": 51156
	},
	"Form": {
		"defObjs": [ "Core", "Field", "Validator", "Utils" ],
		"Core": 50209,
		"Field": 50210,
		"Validator": 50211,
		"Utils": 50212
	},
	"Grid": {
		"defObjs": [ "Core", "Convert", "Html", "Output" ],
		"Core": 49819,
		"Convert": 49820,
		"Html": 49821,
		"Output": 49822,
		"Aggregate": 51011,
		"Compatibility": 51632,
		"Editable": 51001,
		"Query": 50783,
		"Validate": 52403,
		"Xml": 52404
	},
	"Menu": {
		"defObjs": [ "Core" ],
		"Core": 50380,
		"Kbd": 50381
	},
	"Paneset": {
		"reqObjs": [ "Utils.Dragndrop", "Utils.Pane" ],
		"defObjs": [ "Core", "Controlling", "Globals", "Setup", "States", "Utils" ],
		"Core": 50384,
		"Controlling": 50386,
		"Globals": 50388,
		"Setup": 50389,
		"States": 50385,
		"Utils": 50387
	},
	"Slider": {
		"reqObjs": [ "Utils.Dragndrop", "Utils.Button" ],
		"defObjs": [ "Core", "Controlling", "States", "Utils" ],
		"Core": 51002,
		"Controlling": 51004,
		"States": 51003,
		"Utils": 51005
	},
	"Tabs": {
		"reqObjs": [ "Utils.Pane" ],
		"defObjs": [ "Core", "Wizard" ],
		"Core": 50681,
		"Accordion": 50683,
		"Wizard": 50684
	},
	"Tree": {
		"defObjs": [ "Core" ],
		"Core": 50073
	},
	"TreeDev": {
		"defObjs": [ "Base" ],
		"Base": 52386
	},
	"Window": {
		"reqObjs": [ "Utils.Button", "Utils.Dragndrop", "Utils.Pane", "Utils.Progress" ],
		"defObjs": [ "Core", "Setup", "Globals", "States", "Controlling", "Utils" ],
		"Core": 49832,
		"Setup": 49833,
		"Globals": 49834,
		"States": 49835,
		"Controlling": 49836,
		"Utils": 49837
	}
};