function setImage(id, image) {
	if (document.getElementById) {
		if (img = document.getElementById(id)) {
			img.src = image;
		}
	}
}

// Moses' crack at ibrowser integration with textbox
function iBrowser_textbox(textbox) {
	ib.isMSIE = (navigator.appName == 'Microsoft Internet Explorer');
	ib.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
	ib.oEditor = document.getElementById(textbox);
	ib.editor = "textbox";		
	ib.baseURL = 'tiny_mce/plugins/ibrowser/ibrowser.php';
	iBrowser_open(); // starting iBrowser
}

// Shows the definitions
function showDef(definition, width, height) 
{
   var obj = null;
   if (document.getElementById) {
      obj = document.getElementById("definition");
   }

   if (obj) {
      obj.innerHTML = '<p align=left><table border="1" cellspacing="0" id="AutoNumber1" cellpadding="0" style="border-collapse: collapse" bordercolor="#800000"><tr><td class="definition">' + definition + '</td></tr></table>';
      obj.style.left = window.pageX + 10;
      obj.style.top = window.pageY;
      if (width > 0) {
         obj.style.width = width;
      }
      if (height > 0) {
         obj.style.height = height;
      }
      obj.style.visibility = "visible";
   }
}

function hideDef() 
{
   var obj = null;
   if (document.getElementById) {
      obj = document.getElementById("definition");
   }

   if (obj) {
      obj.style.visibility = "hidden";
   }
}

// Get the height of a window
function getWindowHeight()
{
   if (window.innerHeight > 0) {
      return window.innerHeight;
   }
   else if (document.body) {
      if (document.body.clientHeight > 0)
         return document.body.clientHeight;
   }
   return 0;
}

// Get the width of a window
function getWindowWidth()
{
   if (window.innerWidth > 0) {
      return window.innerWidth;
   }
   else if (document.body) {
      if (document.body.clientWidth > 0)
         return document.body.clientWidth;
   }
   return 0;
}

// Get the offset of a window
function getWindowOffset()
{
   var myOffset = (getWindowWidth() - 640) / 2;
   if (myOffset < 0)
      return 0;
   else 
      return Math.round(myOffset);
}

// Tracks the location of the mouse
function mouseTracker(e) 
{
    e = e || window.Event || window.event;
    window.pageX = parseInt(e.pageX) || parseInt(e.clientX) + parseInt(document.body.scrollLeft);
    window.pageY = parseInt(e.pageY) || parseInt(e.clientY) + parseInt(document.body.scrollTop);
}

// Setup the tracking of the mouse
function setMouseTracker() {
    if (document.captureEvents) {
        document.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
    }
    document.onmousemove = this.mouseTracker;
}

// Initialisation
function initialise()
{
   // Create the Definition Box
   document.write('<div id="definition" style="position:absolute;top:0px;left:0px;width:200px;height:100px;clip:rect(0 200 100 0);text-align:center;visibility:hidden;z-index:1"></div>')

   // Start tracking of mouse
   setMouseTracker();

   // Scrolling
   window.onscroll = scrolling;

   // For the Showing of Images
   document.write("<img name='mylargepicture' id='mylargepicture' style='position:absolute;visibility:hidden;z-index:100'>")
}

// Scrolling
function scrolling() {
   if (window.parent.left) {
      if (document.layers) {
         window.parent.left.pageYOffset = window.pageYOffset
      }
      else if (document.all) {
         window.parent.left.document.body.scrollTop = window.document.body.scrollTop
      }
   }
}
initialise()


// Show the large picture
function showLargePicture(pic, width, height) {
   if (document.getElementById) {
      objName = "MyLargePicture"
      obj = document.getElementById(objName)
   }
   if (obj) {
      obj.onclick = hideLargePicture
      
      obj.src = pic
      obj.style.width = 0
      obj.style.height = 0
      obj.style.left = getWindowWidth() / 2
      obj.style.top = getWindowHeight() / 2 + document.body.scrollTop
      obj.style.visibility = "visible"

      PicWidth = parseInt(obj.style.width)
      PicHeight = parseInt(obj.style.height)
      PicLeft = parseInt(obj.style.left)
      PicTop = parseInt(obj.style.top)
      
      step = 50
      if (width > height)
         ratio = (getWindowWidth()) / width
      else
         ratio = (getWindowHeight()) / height 
      xStep = ratio * width / step
      yStep = ratio * height / step

      Stretch(objName, xStep, yStep, step)
   }
}

// Hide the large picture
function hideLargePicture() {
   if (document.getElementById) {
      objName = "MyLargePicture"
      obj = document.getElementById(objName)
   }
   if (obj) {
      step = 50
      xStep = -(parseInt(obj.style.width) / step)
      yStep = -(parseInt(obj.style.height) / step)
    
      PicWidth = parseInt(obj.style.width)
      PicHeight = parseInt(obj.style.height)
      PicLeft = parseInt(obj.style.left)
      PicTop = parseInt(obj.style.top)
      
      Stretch(objName, xStep, yStep, step)
   }
}

var PicWidth
var PicHeight
var PicLeft
var PicTop

function Stretch(objName, xStep, yStep, step) {   
   if (document.getElementById) {
      obj = document.getElementById(objName);
   }
   if (obj) {
      PicWidth += xStep
      PicHeight += yStep
      PicLeft -= xStep / 2
      PicTop -= yStep / 2
      obj.style.width = PicWidth
      obj.style.height = PicHeight
      obj.style.left = PicLeft
      obj.style.top = PicTop
      
      if ((PicWidth > getWindowWidth()) || (PicHeight > getWindowHeight()) && (step < 30))
         step = 0

      step--
      if (step > 0)
         setTimeout("Stretch('" + objName + "', " + xStep + ", " + yStep + ", " + step + ")", 10)
      else if (xStep < 0)
         obj.style.visibility = "hidden"
   }
}

function WriteContact() {
   var domain = "@truong.wattle.id.au"
   var mEmail = "png" + domain
   document.write("<p>You can request regular email updates/prayer request by sending an email to ")
   document.write("<b><i><a href=\"mailto:" + mEmail + "\">" + mEmail + "</a></i></b> or write to ")
   document.write("<br>Moses Truong")
   document.write("<br>New Tribes Mission")
   document.write("<br>PO Box 1079")
   document.write("<br>Goroka EHP 441")
   document.write("<br>Papua New Guinea</p>")
}

function MAE_image_manager_show(textbox) {
	window.ImageManagerApplyTo = textbox;
	window.SetUrl = MAE_image_manager_set;

	var new_window = window.open("/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=../../connectors/php/connector.php",'ImageManager','menubar=no,scrollbars=yes,resizable=yes,location=no,toolbar=no,width=600,height=500',true);

	new_window.focus();
}

function MAE_image_manager_set(fileUrl) {
	if (window.ImageManagerApplyTo) {
		window.ImageManagerApplyTo.value = fileUrl;
		window.ImageManagerApplyTo = null;
	}
}

function MAE_calendar_show(textbox, textbox2) {
	var dropdown = document.MAE.dropdown;

	var date;
	dateString = textbox.value;
	dateString = dateString.replace(/-/g, "/");

	MAE_calendar_draw(dateString);

	dropdown.applyTo = textbox;
	dropdown.applyTo2 = textbox2;

	dropdown.style.width = textbox.offsetWidth + 'px';
	pos = findPos(textbox);
	dropdown.style.left = pos[0] + 'px';
	dropdown.style.top = (pos[1] + textbox.offsetHeight) + 'px';
	dropdown.style.overflow = 'visible';
	dropdown.style.display = 'block';

	dropdown.blur = MAE_calendar_hide;
//	dropdown.focus();
}

function MAE_calendar_draw(dateString) {
	var dropdown = document.MAE.dropdown;
	if (dropdown.hiding) {
		clearTimeout(dropdown.hiding);
	}

	if (Date.parse(dateString)) {
		date = new Date(dateString);
	}
	else {
		date = new Date();
	}

	var start = new Date(date.getFullYear(), date.getMonth(), 1);
	start.setDate(1-start.getDay());

	var temp = new Date(date.getFullYear(), date.getMonth() - 1, date.getDate());
	var lastMonth = temp.getFullYear() + '/' + (temp.getMonth() + 1) + '/' + temp.getDate();

	var temp = new Date(date.getFullYear(), date.getMonth() + 1, date.getDate());
	var nextMonth = temp.getFullYear() + '/' + (temp.getMonth() + 1) + '/' + temp.getDate();

	var html = '<div class="smallcalendar"><div class="title">';
	html += '<span class="button" onclick="MAE_calendar_draw(\'' + lastMonth + '\')">&lt;</span> ';
	html += getMonthName(date.getMonth()) + ' ' + date.getFullYear();
	html += ' <span class="button" onclick="MAE_calendar_draw(\'' + nextMonth + '\')">&gt;</span>';
	html += '</div>';

	html += '<table class="table">';
	html += '<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>';

	var done = false;

	while (!done) {
		if (start.getDay() == 0) {
			html += '<tr>';
		}

		startDate = start.getFullYear() + '-' + (start.getMonth() + 1) + '-' + start.getDate();
		if (start.getTime() == date.getTime()) {
			html += '<td class="button highlight" ';
		}
		else {
			html += '<td class="button" ';
		}
		html += 'onclick="MAE_calendar_set(\'' + startDate + '\'); return false;">' + start.getDate() + '</td>';

		if (start.getDay() == 6) {
			html += '</tr>';
			if (start.getMonth() != date.getMonth()) {
				done = true;
			}
		}

		start.setDate(start.getDate() + 1);
	}
	html += '</table>';
	html += '</div>';

	dropdown.innerHTML = html;
}

function MAE_calendar_delayhide() {
	var dropdown = document.MAE.dropdown;
	dropdown.hiding = setTimeout('MAE_calendar_hide()', 250);
}

function MAE_calendar_hide() {
	var dropdown = document.MAE.dropdown;
	dropdown.applyTo = null;
	dropdown.style.display = 'none';
}

function MAE_calendar_set(date) {
	var dropdown = document.MAE.dropdown;

	if (dropdown.applyTo) {
		dropdown.applyTo.value = date;
	}
	if (dropdown.applyTo2) {
		dropdown.applyTo2.value = date;
	}

	MAE_calendar_hide();
}

function MAE_lookup(me) {
	var dropdown = document.MAE.dropdown;

	// Lookup the text
	var text = me.value;
	var len = text.length;
	var guess = '';
	for (i=0; i<dropdown.childNodes.length; i++) {
		var element = dropdown.childNodes[i];
		if (element.getAttribute('text').substr(0, len) == text) {
			element.style.display = 'block';
			if (guess == '') {
				guess = element.getAttribute('text');
			}
		}
		else {
			element.style.display = 'none';
		}
	}
	me.value = guess;
	
}

function MAE_show(me, lookup) {
	MAE_populate(lookup, me.id, me.value);
	var dropdown = document.MAE.dropdown;
	dropdown.style.width = me.offsetWidth + 'px';
	pos = findPos(me);
	dropdown.style.left = pos[0] + 'px';
	dropdown.style.top = (pos[1] + me.offsetHeight) + 'px';
	dropdown.style.display = 'block';
}

function MAE_setvalue(me, lookup, textbox) {
	MAE_hide(me);
}

function MAE_hide(me) {
	MAE_unpopulate();
	var dropdown = document.MAE.dropdown;
	dropdown.style.display = 'none';
}

function MAE_populate(lookup, textbox, value) {
	// Populate with things in the lookup
	var dropdown = document.MAE.dropdown;
	eval('var table = document.MAE.lookup.' + lookup);
	for (i=0; i<table.length; i++) {
		var element = document.createElement('div');
		element.setAttribute('value', table[i][0]);
		element.setAttribute('text', table[i][1]);
		element.innerHTML = '<a href="#" onclick="document.getElementById(\'' + textbox + '\').value = \'' + table[i][1] + '\'; return false">' + table[i][1] + '</a>';
		dropdown.appendChild(element);
	}
}

function MAE_unpopulate() {
	// Clear the dropdown
	var dropdown = document.MAE.dropdown;
	while (dropdown.childNodes.length>0) {
		dropdown.removeChild(dropdown.childNodes[0]);
	}
}



if (document.MAE == undefined) {
	document.MAE = new Object();
}

// Create the dropdown screen
var MAE_dropdown = document.createElement('div');
MAE_dropdown.setAttribute('class', 'dropdown');
MAE_dropdown.setAttribute('id', 'MAE_dropdown');
document.body.appendChild(MAE_dropdown);

document.MAE.dropdown = MAE_dropdown;
MAE_dropdown.style.backgroundColor = 'white';
MAE_dropdown.style.position = 'absolute';
MAE_dropdown.style.zIndex = 100;

