/*______________________________________________________________________________________*/
/*	Copyright (c) 2009-2010 Canto GmbH													*/
/*______________________________________________________________________________________*/
var CantoUtils = {
	update: function (parentElement, newContentNode)
	{
		if (newContentNode.nodeType != document.ELEMENT_NODE)
		{
			newContentNode = newContentNode.nextSibling;
		}
		if (newContentNode)
		{
			var importedNode = CantoUtils.importNode (newContentNode, true);
			parentElement.innerHTML = '';
			parentElement.appendChild (importedNode);
			if (!document.importNode)
			{
				//noinspection SillyAssignmentJS
				parentElement.innerHTML = parentElement.innerHTML;  // this makes the updated element visible
			}
		}
	},
	importNode: function (node, deepCopy)
	{
		switch (node.nodeType)
		{
			case document.ELEMENT_NODE:
				var newNode = document.createElement (node.nodeName);
				if (node.attributes && node.attributes.length > 0)
				{
					for (var i = 0, iLength = node.attributes.length; i < iLength;)
					{
						newNode.setAttribute (node.attributes[i].nodeName, node.getAttribute (node.attributes[i++].nodeName));
					}
				}
				if (deepCopy && node.childNodes && node.childNodes.length > 0)
				{
					for (var j = 0, jLength = node.childNodes.length; j < jLength;)
					{
						newNode.appendChild (CantoUtils.importNode (node.childNodes[j++], deepCopy));
					}
				}
				return newNode;
			case document.TEXT_NODE:
			case document.CDATA_SECTION_NODE:
				return document.createTextNode (node.nodeValue);
			case document.COMMENT_NODE:
				return document.createComment (node.nodeValue);
		}
	},
	updateTooltipPosition: function (mouseEvent, containerElement, offsetFromPointerToTooltip, offsetFromTooltipToLowerBorder)
	{
		var mousePosX = Event.pointerX (mouseEvent);
		var mousePosY = Event.pointerY (mouseEvent);
		var viewPortDimensions = document.viewport.getDimensions ();
		var viewPortScrollOffsetTop = document.viewport.getScrollOffsets ().top;
		var tooltipElementDimensions = Element.getDimensions (containerElement);
		if ((tooltipElementDimensions.width + mousePosX + offsetFromPointerToTooltip) >= (viewPortDimensions.width - 20))
		{
			// move tooltip to the left side of the pointer
			mousePosX = mousePosX - tooltipElementDimensions.width - offsetFromPointerToTooltip;
		}
		else
		{
			mousePosX = mousePosX + offsetFromPointerToTooltip;
		}
		var visibleLowerBrowserBorder = viewPortScrollOffsetTop + viewPortDimensions.height;
		if ((mousePosY + tooltipElementDimensions.height + offsetFromPointerToTooltip) >= visibleLowerBrowserBorder &&
			(mousePosY - viewPortScrollOffsetTop - tooltipElementDimensions.height + offsetFromPointerToTooltip > 0))
		{
			// not enough space below
			mousePosY = Math.min (mousePosY - tooltipElementDimensions.height + offsetFromPointerToTooltip,
								  visibleLowerBrowserBorder - tooltipElementDimensions.height - offsetFromTooltipToLowerBorder);
		}
		else
		{
			mousePosY = mousePosY + offsetFromPointerToTooltip;
		}
		containerElement.setStyle (
		{
			top: (mousePosY) + "px",
			left: (mousePosX) + "px"
		});
	},
	activateOrDeactivateMasterCheckboxForChildren: function (masterCheckBoxElement, childrenContainer)
	{
		if($(childrenContainer).select ('input[type="checkbox"]').detect(function(checkbox)
		{
			return checkbox.checked;
		}) != undefined)
		{
			$(masterCheckBoxElement).checked='checked';
		}
		else
		{
			$(masterCheckBoxElement).checked='';
		}
	}
};
var CantoBrowserPluginDetector = {
	hasFlashPlayer : function()
	{
		return CantoBrowserPluginDetector.getFlashPlayerVersion () != undefined;
	},
	getFlashPlayerVersion : function()
	{
		try
		{
			// IE first
			try
			{
				// avoid bug as described at http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
				var flashActiveXControl = new ActiveXObject ('ShockwaveFlash.ShockwaveFlash.6');
				try
				{
					flashActiveXControl.AllowScriptAccess = 'always';
				}
				catch(e)
				{
					return '6,0,0';
				}
			}
			catch(e)
			{
			}
			return new ActiveXObject ('ShockwaveFlash.ShockwaveFlash').GetVariable ('$version').replace (/\D+/g, ',').match (/^,?(.+),?$/)[1];
			// other browsers
		}
		catch(e)
		{
			// try modern browsers
			try
			{
				if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
				{
					return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace (/\D+/g, ",").match (/^,?(.+),?$/)[1];
				}
			}
			catch(e)
			{
			}
		}
		return undefined;
	}
};
var CantoAutocompleter = Class.create (Ajax.Autocompleter,
{
	onKeyPress: function(event) {
	  if(this.active)
		switch(event.keyCode) {
		 case Event.KEY_TAB:
		 case Event.KEY_RETURN:
		   // BEGIN CHANGE CW - allow pressing enter without selected element
		   if (this.index == -1)
		   {
			   this.hide();
			   this.active = false;
			   return;
		   }
		   // END CHANGE CW
		   this.selectEntry();
		   Event.stop(event);
		 case Event.KEY_ESC:
		   this.hide();
		   this.active = false;
		   Event.stop(event);
		   return;
		 case Event.KEY_LEFT:
		 case Event.KEY_RIGHT:
		   return;
		 case Event.KEY_UP:
		   this.markPrevious();
		   this.render();
		   Event.stop(event);
		   return;
		 case Event.KEY_DOWN:
		   this.markNext();
		   this.render();
		   Event.stop(event);
		   return;
		}
	   else
		 if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN ||
		   (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;
	  this.changed = true;
	  this.hasFocus = true;
	  if(this.observer) clearTimeout(this.observer);
		this.observer =
		  setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
	},
	markPrevious: function()
	{
		if (this.index > 0)
		{
			this.index--;
		}
		else
		{
			this.index = this.entryCount - 1;
			this.update.scrollTop = this.update.scrollHeight;
		}
		selection = this.getEntry (this.index);
		selection_top = selection.offsetTop;
		if (selection_top < this.update.scrollTop)
		{
			this.update.scrollTop = this.update.scrollTop - selection.offsetHeight;
		}
	},
	markNext: function()
	{
		if (this.index < this.entryCount - 1)
		{
			this.index++;
		}
		else
		{
			this.index = 0;
			this.update.scrollTop = 0;
		}
		selection = this.getEntry (this.index);
		selection_bottom = selection.offsetTop + selection.offsetHeight;
		if (selection_bottom > this.update.scrollTop + this.update.offsetHeight)
		{
			this.update.scrollTop = this.update.scrollTop + selection.offsetHeight;
		}
	},
	updateChoices: function(choices)
	{
		if (!this.changed && this.hasFocus)
		{
			this.update.innerHTML = choices;
			Element.cleanWhitespace (this.update);
			Element.cleanWhitespace (this.update.down ());
			if (this.update.firstChild && this.update.down ().childNodes)
			{
				this.entryCount =
				this.update.down ().childNodes.length;
				for (var i = 0; i < this.entryCount; i++)
				{
					var entry = this.getEntry (i);
					entry.autocompleteIndex = i;
					this.addObservers (entry);
				}
			}
			else
			{
				this.entryCount = 0;
			}
			this.stopIndicator ();
			this.update.scrollTop = 0;
			this.index = -1;
			if (this.entryCount == 1 && this.options.autoSelect)
			{
				this.selectEntry ();
				this.hide ();
			}
			else
			{
				this.render ();
			}
		}
	}
});
/* is this stuff defined? */
if (!document.ELEMENT_NODE)
{
	document.ELEMENT_NODE = 1;
	document.ATTRIBUTE_NODE = 2;
	document.TEXT_NODE = 3;
	document.CDATA_SECTION_NODE = 4;
	document.ENTITY_REFERENCE_NODE = 5;
	document.ENTITY_NODE = 6;
	document.PROCESSING_INSTRUCTION_NODE = 7;
	document.COMMENT_NODE = 8;
	document.DOCUMENT_NODE = 9;
	document.DOCUMENT_TYPE_NODE = 10;
	document.DOCUMENT_FRAGMENT_NODE = 11;
	document.NOTATION_NODE = 12;
}
var ViewDescriptor = Class.create (
{
	initialize: function(recordCollection, recordView, imageSizes, recordsPerPage)
	{
		this.recordCollection = recordCollection;
		this.recordView = recordView;
		this.imageSizes = $H (imageSizes);
		this.recordsPerPage = $H (recordsPerPage);
	},
	getRecordView: function()
	{
		return this.recordCollection + this.recordView;
	},
	setRecordView: function(recordView)
	{
		this.recordView = recordView;
	},
	getRecordViewRenderer: function()
	{
		return this.recordView.toLowerCase () + '.jspx';
	},
	getImageSizeMinimum: function ()
	{
		return this.imageSizes.get ([this.recordView + 'Minimum']);
	},
	getImageSizeMaximum: function ()
	{
		return this.imageSizes.get ([this.recordView + 'Maximum']);
	},
	getImageSizeCurrent: function ()
	{
		return this.imageSizes.get ([this.recordView + 'Current']);
	},
	setImageSizeCurrent: function (imageSizePixel)
	{
		this.imageSizes.set ([this.recordView + 'Current'], imageSizePixel);
	},
	getRecordsPerPage: function ()
	{
		return this.recordsPerPage.get ([this.recordView]);
	},
	setRecordsPerPage: function (recordsPerPage)
	{
		this.recordsPerPage.set ([this.recordView], recordsPerPage);
	},
	equals: function (other)
	{
		return other != undefined && (this.recordView == other.recordView);
	}
});

