﻿// Resource Loader Client Script Library
// Alfa-XP, Michael Isipciuc, 2007.
//

Type.registerNamespace("FM");
trace= function(s, end) { if (this.dt == null) {window.Start= this.dt= new Date();return;} var dt2= new Date(); var i= dt2 - dt; this.dt= dt2;	window.status+= s + ":" + i + ","; if (end) window.status+= " Total:" + (dt2 - window.Start); }
trace2= function(s, end) { if (this.dt == null) {window.Start= this.dt= new Date();window._tr= "";return;} var dt2= new Date(); var i= dt2 - dt; this.dt= dt2;window._tr+= s + ":" + i + "\n"; if (end) {alert("Total:" + (dt2 - window.Start) + "\n\n" + window._tr);this.dt= null;} }

if (typeof FM.Res === "undefined") FM.Res= [];

FM._ResourceLoader= function() {
	this._queueResourcesCount= 0;
	this._registeredResources= [];
	var scripts= (document.scripts) ? document.scripts : document.getElementsByTagName('SCRIPT');
	for (var i= 0; i < scripts.length; i++) {
		var src= scripts[i].getAttribute('src');
		if (src != null) this._registeredResources[src]= true;
	}
	var links= (document.links) ? document.links : document.getElementsByTagName('LINK');
	for (var i= 0; i < links.length; i++) {
		var hr= links[i].getAttribute('href');
		this._registeredResources[hr]= true;
	}
	
	this._callback= null;
	this._head= document.getElementsByTagName('HEAD')[0];
}

FM._ResourceLoader.prototype= {

Register : function(urls, inlineScript, callback) {
	this._urls= urls;
  this._callback= callback;
  this._inlineScript= inlineScript;
  this.resIndex= -1;
  if (urls != null) {
		this.RegisterNextResource();
		if (FM._ProgressBar.getInstance().isActive) FM._ProgressBar.getInstance().setText("Load required resources...");
	} else {
		this.execCallback();
	}
  return this._queueResourcesCount;
},

RegisterNextResource : function() {
	this.resIndex++;
  if (this.resIndex >= this._urls.length) { this.execCallback(); return; }
	var url= this._urls[this.resIndex];
	if (this._registeredResources[url]) { this.RegisterNextResource(); return; }
	var file= null;
	if (url.indexOf(".js") != -1 || url.indexOf(".asmx/js") != -1) {
		file= document.createElement('script')
		file.setAttribute("type", "text/javascript");
		file.setAttribute("src", url);
	} else if (url.indexOf(".css") != -1) {
		file= document.createElement("link")
		file.setAttribute("rel", "stylesheet");
		file.setAttribute("type", "text/css");
		file.setAttribute("href", url);
	} else {
		this.RegisterNextResource(); return;
	}

	file.ResourceLoader= this;
	if (Sys.Browser.agent !== Sys.Browser.InternetExplorer) {
		file.readyState= 'loaded';
		$addHandlers(file, { 'load': this.onReadyStateChanged }, file);
	} else {
		$addHandlers(file, { 'readystatechange': this.onReadyStateChanged }, file);
	}
	this._head.appendChild(file);
	this._registeredResources[url]= true;
},
		
	globalEval: function(code) {
		//try {
			if (window.execScript) window.execScript(code, 'javascript');
			else window.eval(code);
		//} catch (e) { alert("globalEval:\n\n" + e.description); }
  },
	
  onReadyStateChanged : function() {
		if (this.readyState == "loaded" || this.readyState == "complete") {
			this.ResourceLoader.onResourceLoaded(this);
		}	
	},
	
	onResourceLoaded : function(file) {
		$clearHandlers(file); file.ResourceLoader= null;
		this.RegisterNextResource();
	},
	
	execCallback : function() {
		if (this._callback) { this._callback(); this._callback= null; }
		if (this._inlineScript) { this.globalEval(this._inlineScript); this._inlineScript= null; }		
		this._urls= null;
	}
}

FM._ResourceLoader.registerClass('FM._ResourceLoader');
// INSTANCE
FM._ResourceLoader.getCurrent= function () {
  var sl= FM._ResourceLoader._activeInstance;
  if (!sl) sl= FM._ResourceLoader._activeInstance= new FM._ResourceLoader();
  return sl;
}

// SERVICES
//
FM._ResourceLoader.getLookupXExtender= function() {
  var sv= FM._ResourceLoader._LookupXExtender;
  if (!sv) sv= new AX.FM.RecordsService.LookupXExtender();
  return sv;
}

FM._ResourceLoader.getResourceLoaderService= function() {
  var sv= FM._ResourceLoader._ResourceLoaderService;
  if (!sv) sv= new AX.FM.RecordsService.ResourceLoader();
  return sv;
}

FM._ResourceLoader.OpenDataForm= function() {
	//
}

FM._ResourceLoader.StartTransitionalEffect= function(o, ms, step, op) {
	//return;
	o.ms= (ms) ? ms : 50;
	o.step= (step) ? step : 20;
	if (!o.startFunc) o.startFunc= Function.createDelegate(o, FM._ResourceLoader.DoTransitionalEffect);
	o.opacity= (op) ? op : o.step;
	o._cssText= o.style.cssText;
	o.style.filter= "alpha(opacity=" + o.opacity + ");";
	window.setTimeout(o.startFunc, o.ms);
}
FM._ResourceLoader.DoTransitionalEffect= function() {
	var o= this; o.opacity+= o.step;
	if (o.opacity < 100) { 
		o.style.filter= "alpha(opacity=" + o.opacity + ")"; 
		window.setTimeout(o.startFunc, o.ms);
	} else { 
		o.style.cssText= o._cssText;
	}
}
FM._ResourceLoader.StartTransitionalCloseEffect= function(o) {
	o.ms= 50; var step= 40;
	o.opacity= 80;
	o.endFunc= Function.createDelegate(o, FM._ResourceLoader.DoTransitionalCloseEffect);
	o.step= step; 
	o._cssText= o.style.cssText;
	o.style.filter= "alpha(opacity=80);";
	window.setTimeout(o.endFunc, o.ms);
}
FM._ResourceLoader.DoTransitionalCloseEffect= function() {
	var o= this; o.opacity-= o.step;
	if (o.opacity > 0) {
		o.style.filter= "alpha(opacity=" + o.opacity + ")";
		window.setTimeout(o.endFunc, o.ms);
	} else { 
		o.style.cssText= o._cssText;
		o.parentNode.removeChild(o);
	}
}

// PROGRESS BAR
FM._ProgressBar= function() {
	this._timeout= 400;
	this._timer= null;
	this._width= 200;
	this._panel= document.createElement("DIV");
	this._panel.className= "pb_panel";
	this._panel.style.display= "none";
	this._panel.style.zIndex= 2001;
	this._panel.style.width= this._width + "px";
	this._panel.innerHTML= "<table width='100%'><tr><td style='width:25px'><img width='20px' height='20px' src='" + fmCommonPath + "Images/ico_progress.gif' /></td><td class='pb_usertext'>"+fmres("please_wait", "Please wait")+",<br><label></label></td></tr></table>";
	document.body.insertBefore(this._panel, document.body.firstChild);
	this._label= this._panel.firstChild.rows[0].cells[1].lastChild;
	this.isActive= false;
	this.ShowDlg= Function.createDelegate(this, this.Show);
}

FM._ProgressBar.prototype= {

	Prepare : function(text, timeout) {
		this._text= text;
		window.status= (text != null) ? text : "Loading...";
		if (timeout == null) timeout= this._timeout;
		if (!this._timer) {
			this._timer= window.setTimeout(this.ShowDlg, timeout);
		} else {
			window.clearTimeout(this._timer); // will be null on Hide
			this.Show();
		}
	},
	
	Show : function(e, s) { // fix for FireFox // e - is number
		if (s) this._text= s;
		this.isActive= true;
		this._label.innerHTML= (this._text) ? this._text : "content is being loaded...";
		this._panel.style.top= (getBrowserHeight() / 2 - 50) + "px";
		this._panel.style.left= (getBrowserWidth() - this._width) / 2 + "px";
		this._panel.style.display= "inline";
	}, 
	
	Hide : function() {
		window.status= "";
		this._text= "";
		if (this._timer) { window.clearTimeout(this._timer); this._timer= null; }
		this._panel.style.display= "none";
		this.isActive= false;
	},
	
	setText : function(s) {
		this._text= s;
		this._label.innerHTML= this._text;
	}
	
}

FM._ProgressBar.registerClass('FM._ProgressBar');
FM._ProgressBar.getInstance= function () {
  if (!FM._ProgressBar._activeInstance) FM._ProgressBar._activeInstance= new FM._ProgressBar();
  return FM._ProgressBar._activeInstance;
}

FM.CurrentFrame= null;

// Open Record by handler
//
openRecordByHandler= function(tableName, handler, qs, w, h, full, refreshCallback, bReadonly) {
if (!w) w= 800; if (!h) h= 500;

var i2= handler.indexOf('?');
var qs2= (i2 != -1) ? handler.substr(i2+1) : "";
if (i2 != -1) handler= handler.substr(0, i2);
if (!qs || qs == "") qs= qs2; else if (qs2 != "") qs+= "&" + qs2;

var i1= handler.indexOf(':');
var controlType= (i1 != -1) ? handler.substr(0, i1) : "";
var controlName= (i1 != -1) ? handler.substr(i1+1) : handler;
if (handler.indexOf('.aspx') > 0) controlType= "URL";

var callback= null;
switch (controlType) {
case "":
	controlType= "FORM";
case "FORM":
	if (controlName == "" || controlName.indexOf('/') == -1) controlName= tableName + '/' + controlName;
	callback= function(oWnd, hostWnd) { 
		var fc= new oWnd.FM.FormContainer(hostWnd)
		hostWnd.innerObject= fc;
		fc.LoadByEvidence(controlName, qs, bReadonly);
	}
	break;
case "PROFILE":
	callback= function(oWnd, hostWnd) { 
		var pf= new oWnd.FM.Profile(hostWnd);
		hostWnd.innerObject= pf;
		pf.InputParameters= qs;
		pf.LoadContainer(controlName);
	}
	break;
case "WORKFLOW":
	callback= function(oWnd, hostWnd) { 
		var wf= new oWnd.FM.Workflow(controlName, hostWnd);
		hostWnd.innerObject= wf;
		wf.InputParameters= qs;
		wf.Run('forward');
	}
	break;
case "WORKFLOW2":
	callback= function(oWnd, hostWnd) { 
		var wf= new oWnd.FM.Workflow2(controlName, hostWnd);
		hostWnd.innerObject= wf;
		wf.InputParameters= qs;
		wf.Run('forward');
	}
	break;
case "URL":
	var url= WorkspaceRoot + controlName + (qs != "" ? ("?" + qs) : "");
	openStdWin(url, buildWinName(), w, h, true);
	return;
}
var wnd= window.parent.FM.ModalWindow.createWindowFrame();
wnd.Open(callback, refreshCallback, w, h, full);
wnd.SetStatus(qs);
wnd.parentFrame= window.FM.CurrentFrame;
return wnd;
}

function openFrameWindow(callback, refreshCallback, w, h) {
var wnd= window.parent.FM.ModalWindow.createWindowFrame();
wnd.Open(callback, refreshCallback, w, h);
wnd.parentFrame= window.FM.CurrentFrame;
return wnd;
}

Sys.Application.notifyScriptLoaded();
