/**************************************************
trivantis-utils.js
This file contains objects for published html see
js/utils_designtime.js for additional methods that
are added to the prototypes of these objects.

Please keep in mind that we want to keep this as
small as possible.
**************************************************/

var n = null;
var t = true;
var f = false;

function webtoraAppl()
{
	this.is = new browserProperties();
	if (this.designTime) this.designTime();
}

/*****************************************
 Global functions
******************************************/
function browserProperties()
{
	var nav = navigator; // so packer will compress.
	var THIS = this; // so packer will compress.
	var name = nav.appName;
	var ua = nav.userAgent.toLowerCase();

	if ( name == "Netscape" ) name = "ns";
	else if ( name == "Microsoft Internet Explorer" ) name = "ie";

	THIS.console = typeof console != 'undefined' && typeof console.log == 'function';
	THIS.v = parseInt(nav.appVersion, 10);
	THIS.ns = (name == "ns" && THIS.v >= 4);
	THIS.ns4 = (THIS.ns && THIS.v == 4);
	THIS.ns5 = (THIS.ns && THIS.v == 5);
	THIS.nsMac = (THIS.ns && nav.platform.indexOf("Mac") >= 0 );
	THIS.ie = (name == "ie" && THIS.v >= 4);
	THIS.ie4 = (THIS.ie && nav.appVersion.indexOf('MSIE 4') > 0);
	THIS.ie5 = (THIS.ie && nav.appVersion.indexOf('MSIE 5') > 0);
	THIS.ie6 = (THIS.ie && nav.appVersion.indexOf('MSIE 6') > 0);
	THIS.ie7 = (THIS.ie && nav.appVersion.indexOf('MSIE 7') > 0);
	THIS.ie8 = (THIS.ie && nav.appVersion.indexOf('MSIE 8') > 0);
	THIS.ieMac = (THIS.ie && nav.platform.indexOf("Mac") >= 0);
	THIS.min = (THIS.ns || THIS.ie);
	THIS.Win = (nav.platform.indexOf("Win") >= 0);
	THIS.Mac = (nav.platform.indexOf("Mac") >= 0);
	THIS.safari = ( nav.vendor && nav.vendor.indexOf('Apple') >= 0 ? true : false );
	THIS.safariWin = ( THIS.safari && THIS.Win ? true : false );
	THIS.activeX = ( THIS.ie ? true : false );
	THIS.gecko = ua.indexOf('gecko/') != -1;
	THIS.ff = ua.indexOf('firefox/') != -1;
	THIS.chrome = ua.indexOf('chrome/') != -1;
	THIS.op = ua.indexOf("opera/") != -1;
	THIS.supportedBrowser = false;

	if ( THIS.gecko )
	{
		var geckoVersion = ua.match(/gecko\/(\d+)/)[1];
		THIS.gecko10 = geckoVersion < 20051111; // Actually "10" refers to Gecko versions before Firefox 1.5, where Gecko 20051111 has been released.
	}
	else
		THIS.IsGecko10 = false ;

	if ( THIS.ff ) THIS.supportedBrowser = (ua.match(/firefox\/(\d+\.\d+)/)[1] >= 2);
	else if ( THIS.safari ) THIS.supportedBrowser = (ua.match(/version\/(\d+\.\d+)/)[1] >= 3);
	else if ( THIS.ie ) THIS.supportedBrowser = THIS.ie7 || THIS.ie8;

	THIS.wmpVersion = 6; // default version number we only support 7 and up

	var player = null;

	try
	{
		if ( window.ActiveXObject )
			player = new ActiveXObject("WMPlayer.OCX.7");
		else if ( window.GeckoActiveXObject )
			player = new GeckoActiveXObject("WMPlayer.OCX.7");
		else if ( nav.mimeTypes["application/x-mplayer2"] )
			player = nav.mimeTypes["application/x-mplayer2"].enabledPlugin;
	}
	catch(e)
	{
		// Handle error only if title has wmp -- no WMP control
	}

	if ( player && player.versionInfo )
		THIS.wmpVersion = player.versionInfo.slice(0, player.versionInfo.indexOf('.'));

	THIS.getBrowserWidth = function()
	{
		return document.documentElement.offsetWidth;
	};
	THIS.getBrowserHeight = function()
	{
		return document.documentElement.offsetHeight;
	};
}
/*
 * Trivantis regular expression library. Add items here if also used in published mode
 * otherwise add them in utils_designtime.js
 */
var TRgEx = {
    /*
     * Identifies if a file name is a flash animation
     */
    flash : /.*(\.SWF|\.SPL|\.FLV)$/i
};

function jsRect(x,y,w,h,rb){
	this.x = x ? x : 0;
	this.y = y ? y : 0;
	this.width = w ? w : 0;
	this.height = h ? h : 0;
	if (rb) // calc width and height based on right and bottom being passed instead
	{
		this.width = w - x;
		this.height = h - y;
	}
}

jsRect.prototype.init = function(){
	this.set(0,0,0,0);
};

jsRect.prototype.set = function(x,y,w,h){
	this.x = x;
	this.y = y;
	this.width = w;
	this.height = h;
};

jsRect.prototype.setRect = function(r){
	this.x = r.x;
	this.y = r.y;
	this.width = r.width;
	this.height = r.height;
};

jsRect.prototype.left = function(){
	return this.x;
};

jsRect.prototype.top = function(){
	return this.y;
};

jsRect.prototype.right = function(){
	return this.x+this.width;
};

jsRect.prototype.bottom = function(){
	return this.y+this.height;
};

jsRect.prototype.isEqual = function(obj){
	for(var i in this){
		if(this[i] != obj[i]) return false;
	}
	return true;
};

jsRect.prototype.copy = function(){
	return new jsRect(this.x,this.y,this.width,this.height);
};

jsRect.intersectRect = function(r1,r2)
{
	return ! ( r2.x > (r1.x+r1.width)
			|| (r2.x+r2.width) < r1.x
		|| r2.y > (r1.y+r1.height)
		|| (r2.y+r2.height) < r1.y   );
};

function jsPoint(x,y){
	this.x = x ? x : 0;
	this.y = y ? y : 0;
}

jsPoint.prototype.init = function(){
	this.set(0,0);
};

jsPoint.prototype.set = function(x,y){
	this.x = x;
	this.y = y;
};

jsPoint.prototype.isEqual = function(obj){
	for(var i in this){
		if(this[i] != obj[i]) return false;
	}
	return true;
};

jsPoint.prototype.copy = function(){
	return new jsPoint(this.x,this.y);
};

function isEnterKey(e)
{
	return (getKeyCode(e)===13);
}

function getKeyCode(e)
{
	if(!e) e = window.event;
	return e.keyCode;
}

function cancelEvent(e)
{
	if ( document.all ) e = event;
	if ( !e ) return false;
	if ( e.preventDefault ) e.preventDefault();

	if ( e.stopPropagation )
		e.stopPropagation();
	else if ( e.preventBubble )
		e.preventBubble();

	e.returnValue = false;
	e.cancelBubble = true;
	try{
	e.keyCode = 0;
	//e.which = 0;
	}catch(e)
	{}
	return false;
}

/*
 * This is actually a core extension, but since it is used in wndobjs.js we
 * have to surface it when we are running in published mode. coreextensions.js
 * has ties to other pieces such as strings.js which we do not want to surface
 * so the following core extensions are moved here:
 */
String.prototype.IContains = function( textToCheck )
{
	return ( this.toUpperCase().indexOf( textToCheck.toUpperCase() ) > -1 ) ;
};

function rand(X){
	return Math.floor(X * (Math.random() % 1));
}

function randMinMax(min, max){
	return min + rand(max - min + 1);
}

function jsTrans(){
}

jsTrans.isFIFO = function(type){
	if(type >= TRANS_FLY_TOP && type <= TRANS_FLY_TOPLEFT) return 1;
	return 0;
};

jsTrans.xlateTrans = function(type){
	if(type==TRANS_RANDOM_NOFLY_EFFECT) return randMinMax(TRANS_BOX_IN,TRANS_VERTICAL_BARS);
	else if(type==TRANS_RANDOM_EFFECT) return randMinMax(TRANS_BOX_IN,TRANS_FLY_TOPLEFT);
	return type;
};

function trivIssueShowActs(arWnds,preview)
{
	trivArExec(arWnds,
		function(wnd){
    		var flags = ( preview===1 ? wnd.cwObj.dwFlags : wnd.dwFlags );
			if (flags&VISIBLE && (wnd.isVisible()||wnd.cwObj.getTrans(t)))
				wnd.show();
		});
}
/*
 * use this to execute a function on every element in an array
 *  return false in the function to break the looping
 *
 * example: trivArExec(this.arObjs,function(o,i){if (i==idx) o.updateData(tData); return false;});
 * where idx and tdata are local variables in the calling function
 *
 */
function trivArExec(a,f)
{
	var l = (a&&a.length ? a.length : 0);
	for (var i=0;i<l;i++) if ( f(a[i],i) == false ) break;
}

webtoraAppl.prototype.SetTimeout = function( func, milliseconds, thisObject, paramsArray, timerWindow )
{
	return ( timerWindow || window ).setTimeout(
		function()
		{
			if ( paramsArray )
				func.apply( thisObject, [].concat( paramsArray ) );
			else
				func.apply( thisObject );
		},
		milliseconds );
};



