/****************************

 Global JavaScript functions and conditions for IFWORLD web site projects
 Version:
 	1.0, Beta 1.2, 05 Oct 2006

 Usage:
 	This file must be included within <body>
	<script type="text/javascript" src="IFscript.global.js"></script>

 Requires:
  IFscript.global.js

****************************/




/****************************
 Global variables for IFWORLD CADDIE projects.
****************************/
if (!IFs) IFs = new Array();
if (!IFs.global) IFs.global = new Array();
if (!IFs.global.documentRoot) IFs.global.documentRoot = '/';

if (!IFs.caddie) IFs.caddie = new Array();
if (!IFs.caddie.calendarRoot) IFs.caddie.calendarRoot = IFs.global.documentRoot + '_calendar/';
if (!IFs.caddie.mailing_listsRoot) IFs.caddie.mailing_listsRoot = IFs.global.documentRoot + '_mailing_lists/';





/****************************
 popup()
 
 How it works:
  Opens a javascript-controled window
 
 Use:
 	<a href="#" onevent="return IFs.caddie.popup('whichType', [arguments0], [arguments1], ... )">

 Returns:
  false

****************************/
IFs.caddie.popup = function (whichType, postArgs)
{
	var openerURI;
	var openerArgs = 'toolbar=1,status=1,resizable=1,scrollbars=1'; // Set default openerArgs. May be chagned in switch-cases below
	switch (whichType)
	{
		case 'calendar' :
			openerURI = IFs.caddie.calendarRoot + 'calendar.phtml';
			if (postArgs) openerURI += '?' + postArgs;
			openerArgs += ',height=460,width=600';
			break;

		case 'mailinglist' :
			openerURI = IFs.caddie.mailing_listsRoot;
			if (postArgs) openerURI += '?mailing_list_id=' + postArgs;
			openerArgs += ',height=410,width=600';
			break;

		case 'image' :
			openerURI = IFs.global.documentRoot + '_popup.phtml?whatType=image&imageURL=' + arguments[1] + '&&';
			openerArgs = 'toolbar=1,status=0,scrollbars=1,resizable=1,height=320,width=400';
			break;

		default : // Default is to treat first argument (whichType) as a url.
			openerURI = whichType;
			openerArgs = 'toolbar=1,status=1,scrollbars=1,resizable=1';
			break;
	}
	window.open(openerURI, whichType, openerArgs);
  return false;
}




/****************************
 popup()
 
 How it works:
 Opens a URL in opener window, and brings window to foreground
 HTML-specified content
 
 Use:
 	<tag onevent="openInOpeneer(this, 'http://www.URL.com');">

 Returns:
  false

****************************/
// Opens a URL in opener window, and brings window to foreground
// Usage: <a href="javascript: openInOpeneer(this, 'http://www.URL.com');">
IFs.caddie.openInOpeneer = function (whereFrom, openerURI)
{
	whereFrom.opener.location = openerURI;
	whereFrom.opener.focus();
}


// Resizes window to fit #theImage width and height properties
// Usage: <body onload="IFs.caddie.resize2img()"> <img id="theImage">
IFs.caddie.resize2img = function ()
{
	if(document.getElementById)
	{
		imgH = parseInt( document.getElementById('theImage').width );
		imgV = parseInt( document.getElementById('theImage').height );
		
		// buffer for looks/usability?
		buffer = 60; // total buffer in pixels, shared by both sides

		maxH = screen.availWidth - buffer;
		maxV = screen.availHeight - buffer;
		
		startH = parseInt( screen.availTop );
		startV = parseInt( screen.availLeft );
		if (isNaN(startH)) startH = 0;
		if (isNaN(startV)) startV = 0;

		centerH = screen.availWidth / 2;
		centerV = screen.availHeight / 2;
		
		chromeH = window.outerWidth - window.innerWidth;
		chromeV = window.outerHeight - window.innerHeight;
		if (isNaN(chromeH)) chromeH = 20;
		if (isNaN(chromeV)) chromeV = 90;

		if ((imgH+chromeH) > maxH){
			var newH = maxH;
		} else {
			var newH = imgH+chromeH;
		}
		if ((imgV+chromeV) > maxV){
			var newV = maxV;
		} else {
			var newV = imgV+chromeV;
		}

		window.moveTo( (centerH - (newH/2) ), (centerV - (newV/2)) );
		window.resizeTo(newH,newV);
		
	}
}