//current swf id
var fswfid="";
//current windows
var fwindows = {};

//init swf id
function fsetfswfid(id) {
	fswfid = id;
};

//call function
function callFunc(win, func, obj, arr) {	
	return win[func](obj, arr);
};

//set window javascript name
function fgetWindowName() {
	return window.name;
};

//get window javascript name
function fsetWindowName(value) {
	window.name = value;
};

//get flash object by id
function fgetSWF(swfId) {
	//if (navigator.appName.indexOf('Microsoft') != -1)
	if (window[swfId]) {
		return window[swfId];
	}
	return document[swfId];
};	

//open window, with timer
function fopenWindow(url, name, params, cont, menu, lifetime) {
	setTimeout(function () {fopenWindowTimer(url, name, params, cont, menu, lifetime, true); }, 500);
};

//function called with opening timer
function fopenWindowTimer(url, name, params, cont, menu, lifetime, fromtimer) {
	var win = window.open(url, name, params);
	//if popup blocked window
	if (win == null || win.closed)
	{
		fgetSWF(fswfid).onPopupBlocked(name);
		return null;
	};
	fwindows[name] = win;
	//if content defined
	if (cont != null && cont.length > 0) {
		//write html which defines function which will close this window by timer
		win.document.open();
		//if lifetime exists
		if (lifetime > 0) {
			win.document.write('<script language=\"javascript\">function fcltimer(lifetime){return setTimeout(function () {this.close();}, lifetime);}</script>');
		};
		win.document.write(cont);
		win.document.close();
	};
	//if not context menu
	if (! menu) {
		try
		{
			callFunc(win.document, 'getElementsByTagName', 'body')[0].oncontextmenu = function (){return false;};
		}
		catch(err)
		{
		};
	};
	//if lifetime exists
	if (lifetime > 0) {
		try {
			//if content defined
			if (cont != null && cont.length > 0) {
				//usually this line is called
				//call function from another window
				callFunc(win, 'fcltimer', lifetime);
			}
			//if not written content
			//this function works fine in firefox, but IE fails when main window was closed
			else {
				callFunc(win, 'setTimeout', function () {win.close();}, lifetime);
			};
		}
		catch(err) {
		};
	};
	fgetSWF(fswfid).onWindowOpened(name);
	//if params then try resize to correct size
	//works only for MAC
	if (params.length > 0 && navigator.userAgent.indexOf('Macintosh') != -1) {
		var arr = params.split(',');
		var ww = 0;
		var hh = 0;
		for (i = 0; i < arr.length; i++) {
			//get width from params
			if (arr[i].indexOf('width=') == 0) {
				ww = parseInt(arr[i].substring(6));
			};
			//get height from params
			if (arr[i].indexOf('height=') == 0) {
				hh = parseInt(arr[i].substring(7));
			};
		};
		//if width & height
		if (ww > 0 && hh > 0 && typeof(win.innerWidth) == 'number') {
			//and size can be asked without accessing body (basically only non-IE needs resizing)
			if (fromtimer) {
				//if function was called from timer
				//so we have enough time
				setTimeout(function () {fresize(name, ww, hh); }, 100);
			}
			//try to make it fast
			else {
				fresize(name, ww, hh);
			};
		};
	};
	return win;
};

//close window
function fcloseWindow(name, fast) {
	setTimeout(function () {fcloseWindowTimer(name, fast); }, 100);
};

//close window immediately
function fcloseImmediately(name)
{
	if (fgetWindowByName(name) && ! fgetWindowByName(name).closed) {
		fgetWindowByName(name).close();
	};
};

//close window, with timer or without timer
function fcloseWindowTimer(name, fast)
{		
	if (fgetWindowByName(name) && ! fgetWindowByName(name).closed) {
		//if fast close
		if (fast) {
			fgetWindowByName(name).close();
		}
		//if not fast close
		else {
			try
			{
				fgetWindowByName(name).setTimeout('close()', 500);
			}
			catch(err)
			{
			};
		};
		if (name == window.name && window.opener && ! window.opener.closed) {
			//firefox in mac and linux can't close opened child window itselt, still, opener can
			try
			{
				callFunc(window.opener, 'fcloseWindow', name);
			}
			catch(err)
			{
			};
		};
		fdeleteWindow(name);
	};
};

//delete window from child list
function fdeleteWindow(name)
{
	try
	{
		delete fwindows[name];
	}
	catch(err)
	{
	};
};

//get window by name, if window is not closed
function fgetWindowByName(name)
{
	if (name == window.name)
	{
		return window;
	};
	try
	{
		if (window.opener && name == window.opener.name && ! window.opener.closed)
		{
			return window.opener;
		};
	}
	catch(err)
	{
	};
	if (fwindows[name] && ! fwindows[name].closed)
	{
		return fwindows[name];
	};
	return null;
};

//call a function in window
function fcall(name, obj, arr)
{
	return callFunc(fgetWindowByName(name), 'fcallHandler', obj, arr);
};

//insert call handler
function fcallHandler(obj, arr)
{
	return fgetSWF(fswfid).callHandler(obj, arr);
};

//clear unload events before close
function fclearUnload() {
	window.onunload = null;
};

//if on unload appears, send event to this window and to parent and child windows
//if name exists, then function is already in parent or child window,
//and no need to send to parent or child again
function fwindowUnload(e, wname)
{
	if (wname)
	{
		fgetSWF(fswfid).onUnloadHandler(wname);
	}
	else
	{
		//this line throws error when closing firefox tab!!!
		try
		{
			fgetSWF(fswfid).onUnloadHandler(window.name);
		}
		//closing event failed on current window
		catch(err)
		{
		};
		if (window.opener && ! window.opener.closed)
		{
			try
			{
				callFunc(window.opener, 'fwindowUnload', e, window.name);
			}
			catch(err)
			{
			};
		};
		for (var i in fwindows)
		{
			if (fwindows[i] && ! fwindows[i].closed)
			{
				try
				{
					callFunc(fwindows[i], 'fwindowUnload', e, window.name);
				}
				catch(err)
				{
				};
			};
		};
	};
};

//get focus for window
function fgrabFocus(name)
{
	if (name == window.name)
	{
		setTimeout('focus()', 100);
	}
	else
	{
		try
		{
			fgetWindowByName(name).focus();
		}
		catch(err)
		{
		};
	};
};

//init function
function fdefineEvents() {
	//define onfocus event
	window.onfocus=fwindowFocus;
	//document.body.onfocus=fwindowFocus;
	fgetSWF(fswfid).onfocus=fwindowFocus;

	//define onblur event
	window.onblur=fwindowBlur;
	//document.body.onblur=fwindowBlur;
	fgetSWF(fswfid).onblur=fwindowBlur;

	//define window onunload event
	window.onunload=fwindowUnload;

	//onunload crashes on safari
	if (navigator.userAgent.indexOf('Safari') != -1)
	{
		//Safari 3 and higher have Version string in userAgent
		if (navigator.userAgent.indexOf('Version') == -1) {
			//So if we have safari smaller version than 3, then we cannot detect onunload
			fclearUnload();
		};
	};

	window.onresize=fwindowResize;
};

function fwindowFocus(e)
{
	try
	{
		fgetSWF(fswfid).onFocusHandler(window.name);
	}
	catch(err)
	{
	};
};


function fwindowBlur(e)
{
	try
	{
		fgetSWF(fswfid).onBlurHandler(window.name);
	}
	catch(err)
	{
	};
};

//search closed windows
function fgetClosedWindow(checkOpener)
{
	if (checkOpener && ( ! window.opener || window.opener.closed))
	{
		return 'opener';
	};
	for (var i in fwindows)
	{
		if (! fwindows[i] || fwindows[i].closed)
		{
			fdeleteWindow(i);
			return i;
		};
	};
	return null;
};

//change window title
function fchangeTitle(newtitle)
{
	document.title = newtitle;
};

//set on beforeunload message
function funloadMessage(message)
{
	if (message == null)
	{
		window.onbeforeunload = null;
	}
	else
	{
		window.onbeforeunload = function() {return message;};
	};
};

//change window location
function fchangeLocation(name, value) {
	fgetWindowByName(name).location=value;
};

//define onresize event
function fwindowResize(e)
{
	try
	{
		fgetSWF(fswfid).onResizeHandler(window.name);
	}
	catch(err)
	{
	};
};

//get inner width
function fgetInnerWidth(winName) {
	var myWidth = 0;
	var win = fgetWindowByName(winName);
	if(typeof( win.innerWidth) == 'number') {
		//Non-IE
    		myWidth = win.innerWidth;
	}
	else if( win.document.documentElement && win.document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
    		myWidth = win.document.documentElement.clientWidth;
	}
	else if (win.document.body && win.document.body.clientWidth) {
		//IE 4
	    	myWidth = win.document.body.clientWidth;
	};
  	return myWidth;
};

//get inner height
function fgetInnerHeight(winName) {
	var myHeight = 0;
	var win = fgetWindowByName(winName);
	if(typeof( win.innerHeight) == 'number') {
		//Non-IE
	    	myHeight = win.innerHeight;
	} else if( win.document.documentElement && win.document.documentElement.clientHeight) {
		//IE 6+ in 'standards compliant mode'
	    	myHeight = win.document.documentElement.clientHeight;
	}
	else if (win.document.body && win.document.body.clientHeight) {
		//IE 4
	    	myHeight = win.document.body.clientHeight;
	};
  	return myHeight;
};

//get left
function fgetLeftPosition(winName) {
	var myLeft = 0;
	var win = fgetWindowByName(winName);
	if(typeof(win.screenX) == 'number') {	//Non-IE
    		myLeft = win.screenX;
	}
	else {
		//IE 6+ in 'standards compliant mode'
		//hack for IE, -3 pixels from left
    		myLeft = win.screenLeft - 3;
	};
  	return myLeft;
};

//get top
function fgetTopPosition(winName) {
	var myTop = 0;
	var win = fgetWindowByName(winName);
	if(typeof(win.screenY) == 'number') {
		//Non-IE
    		myTop = win.screenY;
	}
	else {
		//IE 6+ in 'standards compliant mode'
		//hack for IE, -40 pixels from top
    		myTop = win.screenTop - 40;
	};
  	return myTop;
};

//resize function for inner dimensions
function fresize(winName, newWidth, newHeight, secondTime) {
	//get window object
	var win = fgetWindowByName(winName);
	//resize to full screen
	if(newWidth == -1 && newHeight == -1) {
		//move position to 0
		win.moveTo(0,0);
		//IE, probably
		if (typeof(screen.width) == 'number') {
			win.resizeTo(screen.width, screen.height);
		}
    		else {
			//Firefox and other
			win.resizeTo(screen.availableWidth, screen.availableHeight);
		};
		//exit full screen resizing
		return;
	};
	//current inner width
	var myWidth = fgetInnerWidth(winName);
	//current inner height
	var myHeight = fgetInnerHeight(winName);
	//if already correct, then return
	if(myWidth == newWidth && myHeight == newHeight) {
    		return;
	};
	//temp width var
	var tempWidth = newWidth;
	//temp height var
	var tempHeight = newHeight;
	if(typeof(win.outerWidth) == 'number' && typeof(win.outerHeight) == 'number') {
		//Non-IE
		//if border size in known then add it to new
		//dimensions, then next resize is exact
		tempWidth += win.outerWidth - myWidth;
		tempHeight += win.outerHeight - myHeight;
	};
	//first resize to new dimensions
	win.resizeTo(tempWidth, tempHeight);
	//new current inner width
	myWidth = fgetInnerWidth(winName);
	//new current inner height
	myHeight = fgetInnerHeight(winName);
	//if size is correct then finish

	if(myWidth == newWidth && myHeight == newHeight) {

		return;
	};

	//non IE except safari 1.3 must finish here
	if (typeof(win.outerWidth) == 'number') {

		//if we have safari, but not safari 3

		if (navigator.userAgent.indexOf('Safari') == -1 || navigator.userAgent.indexOf('Version') != -1) {			
			return;
		}
	}
	//temp width var
	tempWidth = newWidth;
	//temp height var
	tempHeight = newHeight;
	//if current width is not correct, it can only be smaller
	if(myWidth < newWidth) {
		//we know what value we wanted, add missing pixels	
		tempWidth += newWidth - myWidth;
	};
	//if current height is not correct, it can only be smaller
	if(myHeight < newHeight) {
		//border size is missing, add it
		tempHeight += newHeight - myHeight;
	};
	//second resize, to new dimensions
	win.resizeTo(tempWidth, tempHeight);
	if(secondTime) {
		//do not loop forever, if fresize was called twice
	    	return;
	};
	//new current inner width
	myWidth = fgetInnerWidth(winName);
	//new current inner height
	myHeight = fgetInnerHeight(winName);
	//if already correct, then return
	if(myWidth == newWidth && myHeight == newHeight) {
    		return;
		//IE usually finishes here
	};
	//except, when screen border stops resizing
	//use temp variable to move x position
	tempWidth = 0;
	//use temp variable to move y position
	tempHeight = 0;
	//IE reaches here if browser window is too right on screen
	if(myWidth < newWidth) {
		//move x by...
	    	tempWidth = newWidth - myWidth;
	};
	//IE reaches here if browser window is too bottom on screen
	if(myHeight < newHeight) {
		//move y by...
    		tempHeight = newHeight - myHeight;
	};
	//move window to left and top
	win.moveBy(-tempWidth, -tempHeight);
	//start resizing again
	fresize(winName, newWidth, newHeight, true);
};

//get parent window
function fgetParentWindow() {
	var ret = new Object();
	ret.op = false;
	ret.name = null;
	if (window.opener) {
		ret.op = true;
		try {
			ret.name = window.opener.name;
		}
		catch(err) {
		};
	};
	return ret;
};

//set parent window name
function fsetParentWindowName(value) {
	try {
		window.opener.name = value;
	}
	catch(err) {
	};
};
