/*
 * PROJECT:			Insight Builder
 * VERSION:			2.0
 *
 * Copyright (c) IMS Polar Design, 2000-2006. All rights reserved.
 * Do not distribute without the express permission of Polar Design.
 *
 * PACKAGE:			Client Software \ Interface Classes
 * AUTHOR:			Victor S. Popov
 * DATE:			28.11.2001
 *
 * IB2CLASSES.JS	Implementation File
 *
 */

// ----------------------------------------------------------------------------

var TMSG_COPYRIGHT =		d ("p", tc ("copyright"), 
							"Copyright &copy; IMS Polar Design, 2000-2006. All rights reserved.<br/>Insight Builder and Insight Builder logo are trademarks of IMS Polar Design.");

var RLOC_INTERFACE =		"/interface/";
var RLOC_IMAGES =			"/images/";
var RLOC_SCRIPTS =			"/scripts/";
var RLOC_STATIC =			"/static/";
var RLOC_ICONS =			RLOC_IMAGES + "icons/";

var RSRC_LOADING_PAGE =		RLOC_STATIC + "loading.html";
var RSRC_LOGIN_PAGE =		RLOC_STATIC + "index.html";
var RSRC_IB2_STYLE =		RLOC_INTERFACE + "ib2style.css";

var JS_CLIENT_CODE =		escript ("JavaScript", RLOC_SCRIPTS + "ib2html.js") + 
							escript ("JavaScript", RLOC_SCRIPTS + "ib2classes.js") +
							escript ("JavaScript", RLOC_SCRIPTS + "chromeless_35.js");

var BC_DESKTOP =			"#304c78";
var BI_DESKTOP =			RLOC_IMAGES + "bg.desktop.gif";
var DI_DESKTOP =			RLOC_IMAGES + "logo.desktop.gif";

var BC_TASKBAR =			"#d6dbe4";

var BC_ICON_OFF =			"#d6dbe4";
var BC_ICON_ON =			"#ffffff";
var FC_ICON_OFF =			"#304c78";
var FC_ICON_ON	=			"#000000";
var IS_ICON_OFF =			".36.0.gif";
var IS_ICON_ON =			".36.1.gif";

var SPACER =				image (src (RLOC_IMAGES + "spacer.gif") + a ("width", "2") + a ("height", "2") + a ("border", "0"));
var THINSPACER =			image (src (RLOC_IMAGES + "spacer.gif") + a ("width", "1") + a ("height", "1") + a ("border", "0"));

// ----------------------------------------------------------------------------
//
//	ONLOAD EVENT HANDLER

function doEnter () {
	return d ("script", a ("language", "JavaScript") + a ("for", "window") + a ("event", "onload"), "document.body.style.overflow = \"hidden\";");
}

// ----------------------------------------------------------------------------
//
// DATE & TIME RENDERING FUNCTIONS

function doRT () {
	return "function rT () { var d = new Date (); hrs = new String (d.getHours ()); mns = new String (d.getMinutes ()); scs = new String (d.getSeconds ()); mns = (mns.length == 1)?(\"0\"+mns):mns; scs = (scs.length == 1)?(\"0\"+scs):scs; return hrs + \":\" + mns + \":\" + scs; } ";
}

function doRD () {
	return "function rD () { var d = new Date (); var m = new Array (\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"); mth = m [d.getMonth ()]; day = new String (d.getDate ()); yea = new String (d.getYear ()); return mth + \" \" + day + \", \" + yea; } ";
}

function doCU () {
	return doRT () + doRD () + "function uC () { document.all [\"systemtime\"].value = rT (); document.all [\"systemdate\"].value = rD (); setTimeout (uC, 1000); }; uC (); ";
}

// ----------------------------------------------------------------------------
//
//	IBAPPLICATION CLASS

var application;
// var windowhandle;

function idle () {
	if (application.handle.closed)
		application.shutdown ();
	else
		setTimeout (idle, 1000);
}

function IBApplication (session, start) {

	this.session = session;
	this.icons = new Array ();
	this.windows = new Array ();
	this.start = start;
	
	this.render = 
		
		function () {
			clock = 
				s ("input", id ("systemtime") + a ("type", "text") + tc ("time") + a ("value", "")) + s (none, "br") +
				s ("input", id ("systemdate") + a ("type", "text") + tc ("date") + a ("value", "")) + 
				iscript ("JavaScript", doCU ());

			taskbar = 
				table (nospacing + nopadding + width ("100%") + height ("40"),
					tr (valign ("middle") + height ("1"), td (bgc ("white") + a ("colspan", "4"), THINSPACER)) +
					tr (valign ("middle") + height ("40"), 
						td (bgc (BC_TASKBAR), SPACER) +
						td (bgc (BC_TASKBAR) + a ("rowspan", "2"), clock) +	
						td (width ("100%") + bgc (BC_TASKBAR) + a ("rowspan", "2"), SPACER) +
						td (bgc (BC_TASKBAR), SPACER)
					) + 
					tr (valign ("bottom"), 
						td (align ("left"), img (RLOC_IMAGES + "c3.taskbar.gif")) +
						td (align ("right"), img (RLOC_IMAGES + "c4.taskbar.gif"))
					));


			desktop = 
				table (nospacing + nopadding + width ("100%") + height ("100%"), 
					tr (valign ("top"), 
						td (align ("left"), img (RLOC_IMAGES + "c1.desktop.gif")) + 
						td (align ("right"), img (RLOC_IMAGES + "c2.desktop.gif"))
					) + 
					tr (valign ("bottom") + height ("100%"),
						td (align ("right") + a ("colspan", "2"), image (src (DI_DESKTOP) + id ("ib2logo") + a ("border", "0") + a ("hspace", "10") + style ("filter: blendtrans(duration=6.0); visibility: hidden;"))
						) 
					) +
					tr (valign ("bottom"),
						td (align ("center") + a ("colspan", "2"), taskbar)
					) +
					tr (valign ("middle"),
						td (align ("center") + a ("colspan", "2") + bgc ("black"), TMSG_COPYRIGHT)
					)
				);

			for (i = 0; i < this.icons.length; i++)
				desktop += this.icons [i].render ();

			//hookback = iscript ("JavaScript", "var application = self.opener.application; document.onclick = application.makeovers;");
			hookback = iscript ("JavaScript", "var application = self.opener.application;");

			header = head (title (session) + css (RSRC_IB2_STYLE) + JS_CLIENT_CODE + doEnter ());
			footer = body (nomargins + bgc (BC_DESKTOP) + bgi (BI_DESKTOP), desktop + hookback);
			var sSize = "width="+window.screen.availWidth+",height="+window.screen.availHeight + ",left=0,top=0"; 
			this.handle = window.open ("", "IB2DESKTOP", sSize);
			this.handle.resizeTo(window.screen.availWidth, window.screen.availHeight);
			this.handle.document.write (html (header, footer)); 
			this.handle.document.body.style.overflow = "hidden";

			// Blend IB2 logo
			this.handle.document.all ["ib2logo"].filters [0].Apply ();
			this.handle.document.all ["ib2logo"].style.visibility = "";
			this.handle.document.all ["ib2logo"].filters [0].Play ();
		};

	this.addaction = 
		
		function (icon) {
			icon.number = this.icons.length; 
			this.icons [icon.number] = icon;
		};

	this.run = 
		
		function () {
			this.render ();
			idle ();
		};

	this.shutdown =

		function () {
			for (i = 0; i < this.icons.length; i++) {
				if (this.icons[i].handle) {
					this.icons[i].handle.close ();
					this.icons[i].handle = null;
				}
			}
			this.handle.close ();
			document.location.href = start;
		};
	
	this.makeovers = 

		function() {
			for (i = 0; i < application.icons.length; i++) {
				if (application.icons[i].handle) {
					application.icons[i].handle.focus();
					//alert(i);
				}
			}
		};
}

// ----------------------------------------------------------------------------
//
//	IBACTION CLASS

function actionstate (action, number, state) {
	if (state) {
		document.all ["icon_image_" + action].src = application.icons [number].over.src;
		document.all ["icon_label_" + action].style.color = FC_ICON_ON;
		document.all ["icon_frame_" + action].style.background = BC_ICON_ON;
	} else {
		document.all ["icon_image_" + action].src = application.icons [number].out.src;
		document.all ["icon_label_" + action].style.color = FC_ICON_OFF;
		document.all ["icon_frame_" + action].style.background = BC_ICON_OFF;
	};
}

function IBAction (action, title, x, y, value, ahref, icontitle, iconnormal, iconhighlight) {

	this.action = action;
	this.title = title;
	this.x = x;
	this.y = y;
	this.value = value;
	this.number = 0;
	this.icontitle = icontitle;
	this.iconnormal = iconnormal;
	this.iconhighlight = iconhighlight;

	if (iconnormal == null && iconhighlight == null){
		this.out = new Image ();
		this.out.src = RLOC_ICONS + action + IS_ICON_OFF;

		this.over = new Image ();
		this.over.src = RLOC_ICONS + action + IS_ICON_ON;
	} else {
		this.out = new Image ();
		this.out.src = iconnormal;

		this.over = new Image ();
		this.over.src = iconhighlight;
	};

	this.render =

		function () {
			result = 
				div ("icon_" + this.action, tc ("icon") + style (spos (this.x, this.y) + "filter: DropShadow (color = '#000000', offx = '2', offy = '2', positive = '2')"),  
					table (width (40), 
						tr (none, td (align ("center"),
							link (href ("#") + click(eval(this.value)) + mouseover ("actionstate ('" + this.action + "', '" + this.number + "', true)") + mouseout ("actionstate ('" + this.action + "', '" + this.number + "', false)") + tc ("icon"),
								image (id ("icon_image_" + this.action) + src (this.out.src) + alt (this.title) + a ("vspace", 4) + a ("border", 0))
							)
						)) +
						tr (none, td (align ("center") + height(30), d ("p", id ("icon_frame_" + this.action) + tc ("icon"), 
							link (href ("#") + click(eval(this.value)) + id ("icon_label_" + this.action) + mouseover ("actionstate ('" + this.action + "', '" + this.number + "', true)") + mouseout ("actionstate ('" + this.action + "', '" + this.number + "', false)") + tc ("icon"), this.icontitle)										
						)))
					)
				);
			return result;
		};
	//alert(application.icons.length);
	this.SHOWWND =
	
		function () {
			return jsa ("application.icons["+this.number+"].handle = openIT('"+ahref+"',850,500,null,null,'"+action+"',4,true,true,true,'"+title+"');");
			
		}; 
	this.SHOWMENU =
	
		function () {
			return jsa ("application.icons["+this.number+"].handle = openIT('"+ahref+"',250,500,null,null,'"+action+"',4,true,true,true,'"+title+"');");
		}; 
	this.SHOWPUBLISH =
	
		function () {
			return jsa ("application.icons["+this.number+"].handle = openIT('"+ahref+"',520,580,null,null,'"+action+"',4,true,true,true,'"+title+"');");
		}; 
}

// ----------------------------------------------------------------------------
//
//	ACTIONS SUPPORT 

function LOGOFF() {
	return jsa ("application.shutdown ()");
}
function TEST() {
	return jsa ("");
}

