/* Copyright Schools on the Net 2006. */

function getMousePosition(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		posy = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}

	return {x : posx, y : posy};
}

function getElementsOverallPositionX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getElementsOverallPositionY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	return curtop;
}

function getElementsOverallPosition(obj)
{
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
		curtop += obj.y;
	}
	return {x:curleft, y:curtop};
}

function indexOfElement(elem, parent) {
	var children = parent.childNodes;
	var actualIx = 0;
	for(var childIx = 0; childIx < children.length; childIx ++) {
		if(children[childIx].nodeType != 1) { // != ELEMENT
			continue;
		}
		if(children[childIx] == elem) {
			return actualIx;
		}
		actualIx ++;
	}
	return -1;
}

function getViewportSize() {
	var x,y;
	if (self.innerHeight)
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	} else {
		return null;
	}
	return { x:x, y:y };
}

function getViewportPosition() {
	var x,y;
	if (self.pageYOffset)
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	else {
		return null;
	}
	return { x:x, y:y };
}

function clipElementToViewport(elem)
{
	// Fetch the position and dimensions of the viewport.
	vpPosition = getViewportPosition();
	vpSize = getViewportSize();

	if(vpPosition == null || vpSize == null) {
		// Nothing can be done.
		return;
	}

	// Get the position of the element.
	elemPosition = getElementsOverallPosition(elem);

	// Clip against the top border.
	if(elemPosition.y < vpPosition.y) {
		elemPosition.y += vpPosition.y - elemPosition.y;
	}

	// Clip against the left border.
	if(elemPosition.x < vpPosition.x) {
		elemPosition.x += vpPosition.x - elemPosition.x;
	}

	// Right.
	if((elemPosition.x + elem.offsetWidth) > (vpPosition.x + vpSize.x)) {
		elemPosition.x -= (elemPosition.x + elem.offsetWidth) - (vpPosition.x + vpSize.x);
	}

	// Bottom.
	if((elemPosition.y + elem.offsetHeight) > (vpPosition.y + vpSize.y)) {
		elemPosition.y -= (elemPosition.y + elem.offsetHeight) - (vpPosition.y + vpSize.y);
	}

	// Assign the new position.
	elem.style.left = elemPosition.x.toString() + "px";
	elem.style.top = elemPosition.y.toString() + "px";
}

function findClosestSiblingTo(elem, point, tag) {
	var parent = elem.parentNode;
	var children = parent.childNodes;

	var closestChild = null;
	var closestChildIx = 0;
	var distanceToChild2 = 10000000;

	var elementIx = 0;

	if(tag != null) {
		tag = tag.toLowerCase();
	}

	for(childIx = 0; childIx < children.length; childIx ++) {
		var child = children[childIx];

		if(child.nodeType != 1) { // != Node.ELEMENT_NODE
			continue;
		}

		if(tag != null) {
			if(child.tagName.toLowerCase().match(tag) == null) {
				continue;
			}
		}

		var childPosition = getElementsOverallPosition(child);

		// Find the distance from the child to the point.
		var Vx = (point.x < childPosition.x) ? (point.x - childPosition.x) :
			((point.x > (childPosition.x + child.offsetWidth)) ? (point.x - (childPosition.x + child.offsetWidth)) : 0);
		var Vy = (point.y < childPosition.y) ? (point.y - childPosition.y) :
			((point.y > (childPosition.y + child.offsetHeight)) ? (point.y - (childPosition.y + child.offsetHeight)) : 0);

		var d2 = Vx * Vx + Vy * Vy;

		if(d2 < distanceToChild2) {
			// Found a new closest child.
			closestChild = child;
			distanceToChild2 = d2;
			closestChildIx = elementIx;
		}

		elementIx ++;
	}

	if(closestChild == null) {
		return null;
	}

	return { element : closestChild, ix : closestChildIx };
}

function updatePreview(contentId, controller, modelId) {
	if(tinyMCE != null) {
		var editor = tinyMCE.getInstanceById('mce_editor_0');
		if(editor != null) {
			editor.triggerSave();
		}
	}
	var content = document.getElementById(contentId);
	if(content == null) {
		return;
	}
	var params = "content=" + escape(content.value);
	if(modelId == null) {
		var modelId = 0;
	}
	var modelIdS = modelId.toString();
	new Ajax.Updater(
		contentId + '_preview',
		'/' + controller + '/preview/' + modelIdS, {
		parameters: params,
		asynchronous:true,
		onComplete:function(request) { Element.hide('preview_please_wait_' + modelIdS); },
		onLoading:function(request) { Element.show('preview_please_wait_' + modelIdS); } });
}

var ConfirmDeletes = {
	initElements : function() {
		forEachElementOfTag("a", function(potential) {
			if(potential.className != 'admin_delete') {
				return;
			}
			potential.onclick = ConfirmDeletes._makeOnClick(potential.title);
		});
	},

	_makeOnClick : function(caption) {
		return function(e) {
			if (confirm(caption + ', are you sure?')) {
				var f = document.createElement('form');
				this.parentNode.appendChild(f);
				f.method = 'POST';
				f.action = this.href + '?confirm=1';
				f.submit();
			}
			return false;
		}
	}
};

addOnLoad(ConfirmDeletes.initElements);
