/*______________________________________________________________________________________*/
/*	Copyright (c) 2009-2010 Canto GmbH													*/
/*______________________________________________________________________________________*/
var SitesApplication = Class.create (
{
	initialize: function(sessionID, initialRecordView, imageSizes, recordsPerPage, currentCollectionBasket, isPINCollection)
	{
		this.showDebugMessages = false;
		this.sessionID = sessionID;
		this.currentCollectionBasket = currentCollectionBasket;
		this.isPINCollection = isPINCollection;
		this.isIE = navigator.appVersion.indexOf ('MSIE') > 0;
		this.isIE7 = navigator.appVersion.indexOf ('MSIE 7.0') > 0;
		this.isMac = navigator.appVersion.indexOf ('Mac') > 0 || navigator.appVersion.indexOf ('Apple') > 0;
		this.isSafari = navigator.appVersion.indexOf ('AppleWebKit/') > 0 && this.isMac;
		this.timezoneOffset = new Date ().getTimezoneOffset () * 60;
		this.currentView = new ViewDescriptor ("recordCollection", initialRecordView, imageSizes, recordsPerPage);
		this.slideShowTimingInSeconds = 3;
		this.flashPluginAvailable = CantoBrowserPluginDetector.hasFlashPlayer ();
		// initialize image size slider
		this.imageSizeSlider = new Control.Slider ($ ('imageSizeSliderKnob'), $ ('imageSizeSliderTrack'),
		{
			range: $R (this.currentView.getImageSizeMinimum (), this.currentView.getImageSizeMaximum ()),
			sliderValue: this.currentView.getImageSizeCurrent (),
			onSlide:function(v)
			{
				$ ('recordsContainer').select (".recordSelector").each (function (singleRecord)
				{
					singleRecord.setStyle ({
						width: v + "px"
					});
					var childDivs = singleRecord.select ('div[class!="clearFloat"]');
					childDivs.each (function (childDiv)
					{
						childDiv.setStyle ({
							width: v + "px"
						});
					}.bind (this));
					singleRecord.select (".resizableImage").each (function (childIMG)
					{
						if (childIMG.width > childIMG.height)
						{
							childIMG.width = v;
						}
						else
						{
							childIMG.height = v;
						}
						var thumbnailContainer = childIMG.up (".fieldSelector");
						if (thumbnailContainer)
						{
							thumbnailContainer.setStyle ({
								height: v + "px",
								lineHeight: v + "px"
							});
						}
					}.bind (this));
				}.bind (this));
				this.imageSizeSlider.handles[0].setAttribute ("title", Math.round (v) + "px");
			}.bind (this),
			onChange:function(v)
			{
				var pixelSize = Math.round (v);
				this.setImageSize (pixelSize);
				this.imageSizeSlider.handles[0].setAttribute ("title", pixelSize + "px");
			}.bind (this)
		});
		var autoCompleterInputElement = $ ("quickSearchInput");
		if (autoCompleterInputElement)
		{
			this.quickSearchAutoCompleter = new CantoAutocompleter (autoCompleterInputElement, "quickSearchAutoCompletePopup", "quicksearchautocompletepopup.jspx;jsessionid=" + this.sessionID,
			{
				paramName: 'query',
				contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
				minChars: 1,
				tokens: [' ', '|', '&'],
				afterUpdateElement: function () {
					app.doQuickSearch ($F('quickSearchInput'));
				}
			});
		}
		var myGlobalHandlers = {
			onCreate: this.showWorker.bind (this),
			onComplete: this.hideWorker.bind (this)
		};
		Ajax.Responders.register (myGlobalHandlers);
		this.updateRecordsPerPage ();
	},
	showWorker: function()
	{
		window.setTimeout (function ()
		{
			if (Ajax.activeRequestCount > 0)
			{
				$ ('workIndicator').src = "images/working.gif";
			}
		}.bind(this), 500);
	},
	hideWorker: function()
	{
		if (Ajax.activeRequestCount <= 0)
		{
			$ ('workIndicator').src = "images/transparent.gif";
		}
	},
	buildPage: function (page)
	{
		if (this.isPINCollection && (page == "infoview.jspx" || page == "infowindow.jspx" || page == "thumbnailview.jspx"))
		{
			return "pincollection" + page;
		}
		return page;
	},
	showAlert: function (message)
	{
		if (this.showDebugMessages)
		{
			alert (message);
		}
	},
	applySessionID: function (page)
	{
		if (this.sessionID)
		{
			page += ";jsessionid=" + this.sessionID;
		}
		return page;
	},
	doRequest: function (page, params, onCompleteFunction, evalScripts)
	{
		page = this.buildPage (page);
		if (this.sessionID)
		{
			page += ";jsessionid=" + this.sessionID;
		}
		new Ajax.Request (page, { parameters: params, evalScripts: evalScripts, contentType : "application/x-www-form-urlencoded; charset=UTF-8", onComplete: function(response, json)
		{
			try
			{
				if (Object.isFunction (onCompleteFunction))
					onCompleteFunction (response, json);
			}
			catch (e)
			{
				this.showAlert ("Oops, there's been an error on page " + page + " with " + params + "\n" + e);
			}
		}.bind (this),
			onFailure : function ()
			{
				this.showAlert ("Oops, there's been an error on page " + page + " with " + params);
			}.bind (this)});
	},
	doUpdate: function (page, params, target, onCompleteFunction, evalScripts, additionalTargets)
	{
		page = this.buildPage (page);
		if (this.sessionID)
		{
			page += ";jsessionid=" + this.sessionID;
		}
		new Ajax.Updater (target, page, { parameters: params, evalScripts: evalScripts, contentType : "application/x-www-form-urlencoded; charset=UTF-8",  onComplete: function(response, json)
		{
			if (additionalTargets)
			{
				additionalTargets.each (function (target)
				{
					Element.update (target, evalScripts ? response.responseText : response.responseText.stripScripts ());
				}.bind (this));
			}
			try
			{
				if (Object.isFunction (onCompleteFunction))
					onCompleteFunction (response, json);
			}
			catch(e)
			{
				this.showAlert ("Oops, there's been an error on page " + page + " with " + params);
			}
		}.bind (this),
			onFailure : function(resp)
			{
				this.showAlert ("Oops, there's been an error on page " + page + " with " + params + " for target " + target);
				if (resp.status == 500 && !this.showDebugMessages)
				{
					// redirect to login page if an update call failed (session expired)
					exitApplication ('SessionIsExpired');
				}
			}.bind (this)});
	},
	setImageSize: function (imageSize)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&imageSizeLabel=' + imageSize;
		this.doRequest ("changerecordviewthumbnailsize.jspx", params, function (originalRequest)
		{
			var configuredSizeInPixel = Number (originalRequest.responseText);
			this.currentView.setImageSizeCurrent (configuredSizeInPixel);
			this.updateRecords ();
		}.bind (this));
	},
	setImageSizeForCurrentCollectionBasket: function (imageSize)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&imageSizeLabel=' + imageSize + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doRequest ("changerecordviewthumbnailsize.jspx", params, function ()
		{
			this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
		}.bind (this));
	},
	setSorting: function (sortingFieldUID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&uid=' + sortingFieldUID;
		this.doRequest ("setsorting.jspx", params, function ()
		{
			this.setSortingDirection (true);
		}.bind (this));
	},
	setSortingDirection: function (updateRecords, direction)
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		if (direction)
		{
			params += '&direction=' + direction;
		}
		this.doUpdate ("setsortingdirection.jspx", params, 'sortDirectionIndicator', function ()
		{
			if (updateRecords)
			{
				this.updateRecords ();
			}
		}.bind (this));
	},
	setRecordsPerPage: function (numberOfRecords)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&rows=' + numberOfRecords + '&storeRowValueInLRU=storeRowValueInLRU';
		this.doRequest ('changeviewsettings.jspx', params);
		this.currentView.setRecordsPerPage (numberOfRecords);
		params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&rows=' + numberOfRecords;
		this.doUpdate (this.currentView.getRecordViewRenderer (), params, 'recordsContainer', function ()
		{
			this.updateNavigationBar ();
		}.bind (this), true);
	},
	setRecordsPerPageForCurrentCollectionBasket: function (numberOfRecords)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&rows=' + numberOfRecords + '&storeRowValueInLRU=storeRowValueInLRU';
		this.doRequest ('changeviewsettings.jspx', params);
		params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&rows=' + numberOfRecords;
		this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
	},
	gotoPage: function (recordView, pageNumber)
	{
		//noinspection PointlessArithmeticExpressionJS
		var params = 'recordView=' + recordView + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&page=' + parseInt (0 + pageNumber, 10);
		if (recordView == 'recordCollectionThumbnailView' || recordView == 'recordCollectionReportView')
		{
			this.doUpdate (this.currentView.getRecordViewRenderer (), params, 'recordsContainer', function ()
			{
				this.updateNavigationBar ();
				this.updateSearchTermStatistics ();
			}.bind (this), true);
		}
		else if (recordView == 'recordCollectionPreviewView')
		{
			this.doRequest ('previewviewimagedescriptor.jspx', params + "&flash=" + this.flashPluginAvailable, function (originalRequest)
			{
				var documentElement = originalRequest.responseXML.documentElement;
				var urlObject = {
					recordName : documentElement.getElementsByTagName ("title")[0].childNodes[0],
					thumbnailURL : documentElement.getElementsByTagName ("thumbnail")[0].childNodes[0].data,
					assetURL: documentElement.getElementsByTagName ("assetURL")[0].childNodes[0].data,
					assetType : documentElement.getElementsByTagName ("assetType")[0].childNodes[0].data
				};
				this.currentPreviewURL = urlObject.assetURL;
				var recordNameElement = $ ("previewImageMetadataContainer");
				var previewImageElement = $ ("previewImageThumbnail");
				//				var previewFrameBodyElement = $ ("popupFrameBody");
				CantoUtils.update (recordNameElement, urlObject.recordName);
				if (urlObject.assetType == 'image')
				{
					// remove 
					$ ('previewViewPluginContainer').update ('');
					previewImageElement.src = urlObject.thumbnailURL;
					previewImageElement.setStyle ({display: 'inline'});
					var imgPreloader = new Image ();
					imgPreloader.onload = function()
					{
						if (urlObject.assetURL == this.currentPreviewURL)
						{
							previewImageElement.src = urlObject.assetURL;
							if (this.isIE7)
							{
								var widthImg = imgPreloader.width;
								if (widthImg < 256)
								{
									widthImg = 256;
								}
								$ ("previewViewPopupHeader").setStyle ({width: widthImg + "px"});
								$ ("previewViewPopupBody").setStyle ({width: widthImg + "px"});
							}
							var previewImageLoadtextElement = $ ('previewImageLoadtext');
							if (previewImageLoadtextElement)
							{
								Effect.BlindUp (previewImageLoadtextElement,
								{
									delay: 0.0,
									duration: 0.3,
									queue: 'end'
								});
							}
						}
					}.bind (this);
					(function ()
					{
						if (previewImageElement.src.indexOf (urlObject.assetURL) == -1 && this.currentPreviewURL == urlObject.assetURL)
						{
							var previewImageLoadtextElement = $ ('previewImageLoadtext');
							if (previewImageLoadtextElement)
							{
								Effect.BlindDown (previewImageLoadtextElement,
								{
									delay: 0.0,
									duration: 0.3,
									queue: 'end'
								});
							}
						}
					}).bind (this).delay (2);
					imgPreloader.src = urlObject.assetURL;
				}
				else
				{
					//disable image preview and loading preview container
					previewImageElement.setStyle ({display: 'none'});
					$ ('previewRecordContainer').select (".leftOverlay")[0].update ('');
					$ ('previewRecordContainer').select (".rightOverlay")[0].update ('');
					var previewImageLoadtextElement = $ ('previewImageLoadtext');
					if (previewImageLoadtextElement)
					{
						Effect.BlindUp (previewImageLoadtextElement,
						{
							delay: 0.0,
							duration: 0.3,
							queue: 'end'
						});
					}
					var previewImageBoundingBox = 128;
					try
					{
						previewImageBoundingBox = documentElement.getElementsByTagName ("previewImageBoundingBox")[0].childNodes[0].data
					}
					catch (e)
					{
					}
					if (this.isIE7)
					{
						$ ("previewViewPopupHeader").setStyle ({width: previewImageBoundingBox + "px"});
						$ ("previewViewPopupBody").setStyle ({width: previewImageBoundingBox + "px"});
					}
					if (urlObject.assetType == 'flv')
					{
						var paramsVideo = 'assetURL=' + urlObject.assetURL.replace ('?', '%3F') + '&thumbnailURL=' + urlObject.thumbnailURL.replace ('?', '%3F').replace (/&/g, '%26');
						this.doUpdate ('previewviewvideoplayer.jspx', paramsVideo, 'previewViewPluginContainer', undefined, true);
					}
					else if (urlObject.assetType == 'mp3')
					{
						var paramsAudio = 'assetURL=' + urlObject.assetURL.replace ('?', '%3F') + '&thumbnailURL=' + urlObject.thumbnailURL.replace ('?', '%3F').replace (/&/g, '%26');
						this.doUpdate ('previewviewaudioplayer.jspx', paramsAudio, 'previewViewPluginContainer', undefined, true);
					}
					else if (urlObject.assetType == 'iframe')
					{
						var iframeElement = Builder.node ("iframe", {style : 'width: 100%; min-width: ' + previewImageBoundingBox + 'px; height:' + previewImageBoundingBox + 'px'});
						$ ('previewViewPluginContainer').update (iframeElement);
						iframeElement.src = urlObject.assetURL.replace (/%26/g, '&');
					}
				}
				this.doUpdate ('navigation.jspx', params, 'previewNavigationBar', function (originalRequest)
				{
					var responseText = originalRequest.responseText;
					var leftOverlay = $ ('previewRecordContainer').select (".leftOverlay")[0];
					var rightOverlay = $ ('previewRecordContainer').select (".rightOverlay")[0];
					responseText.indexOf ("hasPreviousPage") > -1 ? leftOverlay.addClassName ("action") : leftOverlay.removeClassName ("action");
					responseText.indexOf ("hasNextPage") > -1 ? rightOverlay.addClassName ("action") : rightOverlay.removeClassName ("action");
				}.bind (this));
				params += "&isSlideShowPlaying=" + (this.slideShowInterval != undefined ? 'true' : 'false');
				this.doUpdate ('previewviewfunctions.jspx', params, 'previewViewFunctions');
			}.bind (this));
		}
		else if (recordView == 'recordCollectionInfoView')
		{
			this.doUpdate ('infoview.jspx', params, 'infoViewDataContainer', function ()
			{
				this.doUpdate ('navigation.jspx', params, 'infoNavigationBar');
				this.doUpdate ('infoviewfunctions.jspx', params, 'infoViewFunctions');
			}.bind (this), true);
		}
		else if (recordView == 'recordCollectionCollectionBasketView')
		{
			this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
		}
	},
	updateRecords: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate (this.currentView.getRecordViewRenderer (), params, 'recordsContainer', function ()
		{
			this.updateNavigationBar ();
			this.updateSearchTermStatistics ();
		}.bind (this), true);
	},
	resetToInitialState: function ()
	{
		this.clearAllFilters (function ()
		{
			this.gotoPage (this.currentView.getRecordView (), 1);
		}.bind (this));
	},
	clearAllFilters: function (onCompleteFunction)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&skipFindAll=true';
		this.doRequest ('fieldfilterreset.jspx', params, function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
			this.doRequest ("collectionbasketrevealdeletefilter.jspx", params, function()
			{
				this.doRequest ("mypincollectionsrevealdeletefilter.jspx", params, function()
				{
					this.doRequest ("recordsrevealdeletefilter.jspx", params, function()
					{
						this.updateAllFieldFilters (true, function ()
						{
							this.setCurrentCategory (undefined, undefined, function ()
							{
								this.doQuickSearch ('', function ()
								{
									if (onCompleteFunction)
									{
										onCompleteFunction ();
									}
								}.bind (this));
							}.bind (this));
						}.bind (this));
					}.bind (this));
				}.bind (this));
			}.bind (this));
		}.bind (this));
	},
	findRecord: function (catalogID, recordID)
	{
		this.clearAllFilters (function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&singleCatalogID=' + catalogID + '&recordID=' + recordID;
			this.doRequest ('search.jspx', params, function ()
			{
				// update Info Window that called us
				this.gotoPage ('recordCollectionInfoView', 1);
				// updated main view in the background
				this.gotoPage (this.currentView.getRecordView (), 1);
			}.bind (this));
		}.bind (this));
	},
	revealRecords: function (catalogID, recordIDs)
	{
		this.clearAllFilters (function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&singleCatalogID=' + catalogID + '&records=' + recordIDs;
			this.doRequest ('recordsreveal.jspx', params, function ()
			{
				this.updateAllAdvancedSearchBarBoxes ();
				this.gotoPage (this.currentView.getRecordView (), 1);
			}.bind (this));
		}.bind (this));
	},
	deleteRevealedRecordsFilter: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doRequest ("recordsrevealdeletefilter.jspx", params, function()
		{
			this.updateAllAdvancedSearchBarBoxes ();
			this.closeAllAdvancedSearchPopups ();
			this.gotoPage (this.currentView.getRecordView (), 1);
		}.bind (this));
	},
	setCurrentCategory: function (catalogID, categoryID, onCompleteFunction)
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		if (catalogID != undefined && categoryID != undefined)
		{
			params += '&singleCatalogID=' + catalogID + '&categoryID=' + categoryID;
		}
		this.doUpdate ('categoryfilter.jspx', params, 'categoryFilterBox', function ()
		{
			this.doUpdate ('breadcrumb.jspx', params, 'categoryBreadcrumb', function ()
			{
				if (onCompleteFunction)
				{
					onCompleteFunction ();
				}
				else
				{
					this.gotoPage (this.currentView.getRecordView (), 1);
				}
				this.updateSortingState ();
			}.bind (this));
		}.bind (this));
	},
	setCurrentFieldFilter: function (fieldUID, inputParam)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&uid=' + fieldUID + '&state=';
		if ($ (inputParam))
		{
			params += encodeURIComponent ($F (inputParam));
		}
		else
		{
			params += encodeURIComponent (inputParam);
		}
		this.doUpdate ('fieldfilter.jspx', params, 'ff' + fieldUID, function ()
		{
			this.gotoPage (this.currentView.getRecordView (), 1);
			this.updateSortingState ();
			this.updateAllAdvancedSearchBarBoxes (function ()
			{
				var popupWindow = $ ('ffpopup' + fieldUID);
				var filterBox = $ ('asffb' + fieldUID);
				this.closeAllAdvancedSearchPopups (filterBox ? 'ffpopupframe' + fieldUID : undefined);
				if (popupWindow && filterBox)
				{
					this.showFieldFilterPopup (fieldUID);
				}
			}.bind (this));
		}.bind (this));
	},
	updateAllFieldFilters: function (skipFindAll, onCompleteFunction)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&timezoneOffsetInSeconds=' + this.timezoneOffset + '&skipFindAll=' + skipFindAll;
		if ($('fieldFilterBox'))	// not existing for pin collections
		{
			this.doUpdate ('fieldfiltercontainer.jspx', params, 'fieldFilterBox', function ()
			{
				this.closeAllAdvancedSearchPopups ();
				this.updateAllAdvancedSearchBarBoxes (function ()
				{
					if (onCompleteFunction)
					{
						onCompleteFunction ();
					}
				}.bind (this));
			}.bind (this));
		}
		else
		{
			this.closeAllAdvancedSearchPopups ();
			this.updateAllAdvancedSearchBarBoxes (function ()
			{
				if (onCompleteFunction)
				{
					onCompleteFunction ();
				}
			}.bind (this));
		}
	},
	showFieldFilterPopup: function (fieldUID)
	{
		this.closeAllAdvancedSearchPopups ('ffpopupframe' + fieldUID);
		var params = 'recordView=' + this.currentView.getRecordView () + '&skipHeader=true&uid=' + fieldUID;
		var containerElement = $ ('ffpopupframe' + fieldUID) || $ (Builder.node ('div', {  id: 'ffpopupframe' + fieldUID, className: 'popupContainer', style : 'display: none;' },
				[
					Builder.node ('div', { className: 'popupBorderTop' }),
					Builder.node ('div', { className: 'popupBorderLeft' },
							[
								Builder.node ('div', { className: 'popupBorderRight' },
										[
											Builder.node ('div', { className: 'popupContentWrapper' },
													[
														Builder.node ('div', { id: 'ffpopup' + fieldUID, className: 'advancedSearchPopup' }),
														Builder.node ('div', { className: 'popupContentFooter' },
																[
																	Builder.node ('span', {className: 'button', onclick: 'app.closeAllAdvancedSearchPopups ();'}, 'close'),
																	Builder.node ('div', {className: 'clearFloat'})
																])
													])
										])
							]),
					Builder.node ('div', { className: 'popupBorderBottom' })
				]));
		$ ('body').appendChild (containerElement);
		this.doUpdate ('fieldfilter.jspx', params, 'ffpopup' + fieldUID, function ()
		{
			var clickedElement = $ ('asffb' + fieldUID);
			var offsets = clickedElement.positionedOffset ();
			var dimensions = clickedElement.getDimensions ();
			containerElement.setStyle (
			{
				top: (offsets.top + dimensions.height - 1) + "px",
				left: offsets.left + "px"
			});
			this.setElementVisible (containerElement, true);
		}.bind (this));
	},
	closeAllAdvancedSearchPopups: function (exceptionElementID)
	{
		$$ ('.popupContainer').each (function (elementFound)
		{
			if ((!exceptionElementID || elementFound.id != exceptionElementID) && !elementFound.hasClassName ('keepPopupContainer'))
			{
				this.setElementVisible (elementFound, false, undefined, function ()
				{
					elementFound.parentNode.removeChild (elementFound);
				}.bind (this));
			}
			else if (elementFound.hasClassName ('keepPopupContainer') && (!exceptionElementID || elementFound.id != exceptionElementID))
			{
				if (elementFound.visible ())
				{
					// hide static visible popups
					this.setElementVisible (elementFound, false);
				}
			}
		}.bind (this));
	},
	updateAllAdvancedSearchBarBoxes: function (onCompleteFunction)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&timezoneOffsetInSeconds=' + this.timezoneOffset;
		this.doUpdate ('advancedsearchcontainer.jspx', params, 'advancedSearchBoxContainer', function ()
		{
			this.doUpdate ('advancedsearchstatistics.jspx', params, 'advancedSearchStatisticsContainer');
			if (onCompleteFunction)
			{
				onCompleteFunction ();
			}
		}.bind (this));
	},
	showAdvancedSearchFieldListPopup: function ()
	{
		this.closeAllAdvancedSearchPopups ('advancedSearchFieldListPopup');
		var popupElement = $ ('advancedSearchFieldListPopup');
		var clickedElement = $ ('advancedSearchFieldListPopupAction');
		var offsets = clickedElement.positionedOffset ();
		var dimensions = clickedElement.getDimensions ();
		popupElement.setStyle (
		{
			top: (offsets.top + dimensions.height - 1) + "px",
			left: offsets.left + "px"
		});
		this.setElementVisible (popupElement, true);
	},
	addAdvancedSearchCondition: function (fieldUID)
	{
		var containerElement = $ (Builder.node ('div', {  id: 'aspopupframe' + fieldUID, className: 'popupContainer' },
				[
					Builder.node ('div', { className: 'popupBorderTop' }),
					Builder.node ('div', { className: 'popupBorderLeft' },
							[
								Builder.node ('div', { className: 'popupBorderRight' },
										[
											Builder.node ('div', { className: 'popupContentWrapper' },
													[
														Builder.node ('div', { id: 'aspopup' + fieldUID, className: 'advancedSearchPopup' })
													])
										])
							]),
					Builder.node ('div', { className: 'popupBorderBottom' })
				]));
		$ ('body').appendChild (containerElement);
		var params = 'recordView=' + this.currentView.getRecordView () + '&uid=' + fieldUID;
		this.doUpdate ("advancedsearchconditionadd.jspx", params, 'aspopup' + fieldUID, function ()
		{
			var clickedElement = $ ('advancedSearchFieldListPopupAction');
			var offsets = clickedElement.positionedOffset ();
			var dimensions = clickedElement.getDimensions ();
			containerElement.setStyle (
			{
				top: (offsets.top + dimensions.height - 1) + "px",
				left: offsets.left + "px"
			});
		}.bind (this), true);
	},
	editAdvancedSearchCondition: function (index)
	{
		this.closeAllAdvancedSearchPopups ('aspopupframe' + index);
		var containerElement = $ ('aspopupframe' + index) || $ (Builder.node ('div', {  id: 'aspopupframe' + index, className: 'popupContainer', style : 'display: none;' },
				[
					Builder.node ('div', { className: 'popupBorderTop' }),
					Builder.node ('div', { className: 'popupBorderLeft' },
							[
								Builder.node ('div', { className: 'popupBorderRight' },
										[
											Builder.node ('div', { className: 'popupContentWrapper' },
													[
														Builder.node ('div', { id: 'aspopup' + index, className: 'advancedSearchPopup' })
													])
										])
							]),
					Builder.node ('div', { className: 'popupBorderBottom' })
				]));
		$ ('body').appendChild (containerElement);
		var params = 'recordView=' + this.currentView.getRecordView () + '&page=' + index;
		this.doUpdate ("advancedsearchconditionedit.jspx", params, 'aspopup' + index, function ()
		{
			var clickedElement = $ ('asqc' + index);
			var offsets = clickedElement.positionedOffset ();
			var dimensions = clickedElement.getDimensions ();
			containerElement.setStyle (
			{
				top: (offsets.top + dimensions.height - 1) + "px",
				left: offsets.left + "px"
			});
			this.setElementVisible (containerElement, true);
		}.bind (this), true);
	},
	saveAdvancedSearchCondition: function (index, formElement)
	{
		var values = Form.serialize (formElement);
		var params = 'recordView=' + this.currentView.getRecordView () + '&page=' + index + '&' + values;
		this.doRequest ("advancedsearchconditioncheck.jspx", params, function (response)
		{
			if (response.responseText)
			{
				alert (response.responseText);
			}
			else
			{
				this.closeAllAdvancedSearchPopups ();
				this.doRequest ("advancedsearchconditionsave.jspx", params, function ()
				{
					this.updateAllAdvancedSearchBarBoxes ();
					this.gotoPage (this.currentView.getRecordView (), 1);
					this.updateSortingState ();
				}.bind (this));
			}
		}.bind (this));
	},
	deleteAdvancedSearchCondition: function (index)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&page=' + index;
		this.doRequest ("advancedsearchconditiondelete.jspx", params, function ()
		{
			this.updateAllAdvancedSearchBarBoxes ();
			this.closeAllAdvancedSearchPopups ();
			this.doQuickSearch ($F ('quickSearchInput'));
		}.bind (this));
	},
	deleteAllAdvancedSearchConditionsAndFilters: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&removeall=true';
		this.doRequest ("advancedsearchconditiondelete.jspx", params, function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&skipFindAll=true';
			this.doRequest ('fieldfilterreset.jspx', params, function ()
			{
				var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
				this.doRequest ("collectionbasketrevealdeletefilter.jspx", params, function()
				{
					this.doRequest ("mypincollectionsrevealdeletefilter.jspx", params, function()
					{
						this.doRequest ("recordsrevealdeletefilter.jspx", params, function()
						{
							this.updateAllFieldFilters (true, function ()
							{
								this.doQuickSearch ($F ('quickSearchInput'));
								this.updateAllAdvancedSearchBarBoxes ();
							}.bind (this));
						}.bind (this));
					}.bind (this));
				}.bind (this));
			}.bind (this));
		}.bind (this));
	},
	doQuickSearch: function (quicksearchstring, onCompleteFunction)
	{
		$ ('quickSearchInput').value = quicksearchstring;
		var params = 'recordView=' + this.currentView.getRecordView () + '&query=' + encodeURIComponent (quicksearchstring);
		this.doRequest ("quicksearch.jspx", params, function()
		{
			if (onCompleteFunction)
			{
				onCompleteFunction ();
			}
			else
			{
				this.gotoPage (this.currentView.getRecordView (), 1);
				this.updateSortingState ();
			}
		}.bind (this));
	},
	clearQuickSearch: function (onCompleteFunction)
	{
		$ ('quickSearchInput').value = "";
		var params = 'recordView=' + this.currentView.getRecordView () + '&query=';
		this.doRequest ("quicksearch.jspx", params, function()
		{
			if (onCompleteFunction)
			{
				onCompleteFunction ();
			}
			else
			{
				this.gotoPage (this.currentView.getRecordView (), 1);
			}
		}.bind (this));
	},
	updateNavigationBar: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('navigation.jspx', params, 'navigationBarTop');
		this.doUpdate ('navigation.jspx', params, 'navigationBarBottom');
	},
	updateSearchTermStatistics: function ()
	{
		if ($ ('quickSearchInput'))
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&query=' + encodeURIComponent ($F ('quickSearchInput'));
			this.doUpdate ('searchtermstatistics.jspx', params, 'searchTermStatistics');
		}
	},
	updateCollectionBasketButton: function ()
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate ('collectionbasketbutton.jspx', params, 'collectionBasketToolbarButton');
	},
	addRecordToCollectionBasket: function (collectionBasket, catalogID, recordID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (collectionBasket || this.currentCollectionBasket) + '&singleCatalogID=' + catalogID + "&recordID=" + recordID;
		this.doRequest ("addtocollectionbasket.jspx", params, function()
		{
			$$ (".collectionBasketStateImg" + catalogID + "_" + recordID).each (function (stateImage)
			{
				this.doUpdate ("recordstateincollectionbasket.jspx", params, stateImage.parentNode);
			}.bind (this));
			this.updateCollectionBasketButton ();
		}.bind (this));
	},
	removeRecordFromCollectionBasket: function (collectionBasket, catalogID, recordID, updateCollectionBasketWindow)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (collectionBasket || this.currentCollectionBasket) + '&singleCatalogID=' + catalogID + "&recordID=" + recordID;
		this.doRequest ("removefromcollectionbasket.jspx", params, function()
		{
			$$ (".collectionBasketStateImg" + catalogID + "_" + recordID).each (function (stateImage)
			{
				this.doUpdate ("recordstateincollectionbasket.jspx", params, stateImage.parentNode);
			}.bind (this));
			this.updateCollectionBasketButton ();
			if (updateCollectionBasketWindow)
			{
				this.updateCollectionBasketWindow ();
			}
		}.bind (this));
	},
	addPageToCollectionBasket: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&bulkMode=page';
		this.doRequest ("addtocollectionbasket.jspx", params, function()
		{
			this.updateRecords ();
			this.updateCollectionBasketButton ();
		}.bind (this));
	},
	addCollectionToCollectionBasket: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&bulkMode=collection';
		this.doRequest ("addtocollectionbasket.jspx", params, function()
		{
			this.updateRecords ();
			this.updateCollectionBasketButton ();
		}.bind (this));
	},
	clearCollectionBasket: function ()
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&page=1';
		this.doRequest ("collectionbasketclear.jspx", params, function()
		{
			this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
			this.updateRecords ();
			this.updateCollectionBasketButton ();
		}.bind (this));
	},
	revealCollectionBasket: function ()
	{
		this.clearAllFilters (function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
			this.doRequest ("collectionbasketreveal.jspx", params, function()
			{
				this.updateAllAdvancedSearchBarBoxes ();
				this.gotoPage (this.currentView.getRecordView (), 1);
			}.bind (this));
		}.bind (this));
	},
	deleteRevealedCollectionBasketFilter: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doRequest ("collectionbasketrevealdeletefilter.jspx", params, function()
		{
			this.updateAllAdvancedSearchBarBoxes ();
			this.closeAllAdvancedSearchPopups ();
			this.gotoPage (this.currentView.getRecordView (), 1);
		}.bind (this));
	},
	showCreatePinCollection: function (prefilledForm)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		if (prefilledForm)
		{
			params += '&' + Form.serialize (prefilledForm);
		}
		this.doUpdate ('createpincollectionconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this), true);
	},
	createPinCollection: function (formSendMail)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&' + Form.serialize (formSendMail);
		this.doRequest ("createpincollection.jspx", params, function (originalRequest)
		{
			var responseText = originalRequest.responseText;
			if (responseText.length > 0)
			{
				$ ('createPinCollectionError').update (Builder.node ('span').update (responseText)).show ();
			}
			else
			{
				this.doUpdate ('mypincollections.jspx', 'recordView=' + this.currentView.getRecordView (), 'myPinCollectionsFrameBody');
				this.hidePopupWindow ();
			}
		}.bind (this));
	},
	revealPinCollection: function (collectionUID)
	{
		this.clearAllFilters (function ()
		{
			var params = 'recordView=' + this.currentView.getRecordView () + '&collectionName=' + collectionUID;
			this.doRequest ("mypincollectionsreveal.jspx", params, function()
			{
				this.updateAllAdvancedSearchBarBoxes ();
				this.gotoPage (this.currentView.getRecordView (), 1);
			}.bind (this));
		}.bind (this));
	},
	deleteRevealedPinCollectionFilter: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doRequest ("mypincollectionsrevealdeletefilter.jspx", params, function()
		{
			this.updateAllAdvancedSearchBarBoxes ();
			this.closeAllAdvancedSearchPopups ();
			this.gotoPage (this.currentView.getRecordView (), 1);
		}.bind (this));
	},
	deletePinCollection: function(collectionUID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionName=' + collectionUID;
		this.doRequest ("mypincollectionsdelete.jspx", params, function()
		{
			this.doUpdate ('mypincollections.jspx', 'recordView=' + this.currentView.getRecordView (), 'myPinCollectionsFrameBody');
		}.bind (this));
	},
	showPinCollectionInformationWindow: function(collectionUID)
	{
		var params = 'recordView=recordCollectionThumbnailView&collectionName=' + collectionUID;
		this.doUpdate ('mypincollectioninfowindow.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showInfoWindow: function (recordIndex)
	{
		var params = 'recordView=recordCollectionInfoView&page=' + (recordIndex + 1)+'&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate ('infowindow.jspx', params, 'popupFrameBody', function ()
		{
			this.gotoPage ("recordCollectionInfoView", recordIndex + 1);
			this.showPopupWindow ();
		}.bind (this));
	},
	showPopupWindow: function ()
	{
		var scrollOffsets = document.viewport.getScrollOffsets ();
		var topOffset = scrollOffsets[1] + (document.viewport.getHeight () / 10);
		var leftOffset = scrollOffsets[0];
		this.setVisibleOpacityLayer (true);
		$ ("popupFrame").setStyle (
		{
			top: topOffset + 'px',
			left: leftOffset + 'px'
		});
		this.setElementVisible ($ ("popupFrame"), true);
	},
	hidePopupWindow: function ()
	{
		this.setElementVisible ($ ("popupFrame"), false);
		this.setVisibleOpacityLayer (false);
	},
	showPreviewWindow: function (recordIndex)
	{
		var params = 'recordView=recordCollectionPreviewView&page=' + (recordIndex + 1)+'&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate ('previewview.jspx', params, 'popupFrameBody', function ()
		{
			this.gotoPage ("recordCollectionPreviewView", recordIndex + 1);
			this.showPopupWindow ();
		}.bind (this), true);
	},
	hidePreviewWindow: function ()
	{
		this.stopSlideShow ();
		this.hidePopupWindow ();
		var flowplayerInstance = $f("flowPlayerContainer");
		if (flowplayerInstance)
		{
			flowplayerInstance.stop();
		}
	},
	updateCollectionBasketWindow: function ()
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
	},
	showCollectionBasketWindow: function ()
	{
		this.closeAllAdvancedSearchPopups ();
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&page=1';
		this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this), true);
	},
	setCurrentCollectionBasket: function (collectionBasketName)
	{
		if (collectionBasketName && collectionBasketName.length > 0)
		{
			var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (collectionBasketName);
			this.doRequest ("collectionbasketsetcurrent.jspx", params, function()
			{
				this.currentCollectionBasket = collectionBasketName;
				this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
				this.updateRecords ();
				this.updateCollectionBasketButton ();
			}.bind (this));
		}
	},
	deleteCollectionBasket: function (collectionBasketName)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (collectionBasketName);
		this.doRequest ("collectionbasketdelete.jspx", params, function()
		{
			params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
			this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
		}.bind (this));
	},
	renameCurrentCollectionBasket: function (newCollectionBasketName)
	{
		if (newCollectionBasketName && newCollectionBasketName.length > 0)
		{
			var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&newCollectionBasketName=' + encodeURIComponent (newCollectionBasketName);
			this.doRequest ("collectionbasketrename.jspx", params, function()
			{
				this.setCurrentCollectionBasket (newCollectionBasketName);
			}.bind (this));
		}
	},
	setSortingForCurrentCollectionBasket: function (sortingFieldUID)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&uid=' + sortingFieldUID;
		this.doRequest ("setsorting.jspx", params, function ()
		{
			this.setSortingDirectionForCurrentCollectionBasket (true);
		}.bind (this));
	},
	setSortingDirectionForCurrentCollectionBasket: function (updateRecords, direction)
	{
		var params = 'recordView=recordCollectionCollectionBasketView';
		if (direction)
		{
			params += '&direction=' + direction;
		}
		this.doUpdate ("setsortingdirection.jspx", params, 'sortDirectionBasketIndicator', function ()
		{
			if (updateRecords)
			{
				var params = 'collectionBasket=' + encodeURIComponent (this.currentCollectionBasket) + '&recordView=recordCollectionCollectionBasketView';
				this.doUpdate ('collectionbasketview.jspx', params, 'popupFrameBody', undefined, true);
			}
		}.bind (this));
	},
	updateNavigationBarForCollectionBasket: function ()
	{
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (this.currentCollectionBasket);
		this.doUpdate ('navigation.jspx', params, 'collectionBasketNavigationBarTop');
		this.doUpdate ('navigation.jspx', params, 'collectionBasketNavigationBarBottom');
	},
	showDirectPrintWindow: function (catalogID, recordID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&singleCatalogID=' + catalogID + '&recordID=' + recordID;
		this.doUpdate ('directprintconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showCollectionBasketPrintWindow: function (collectionBasket)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + encodeURIComponent (collectionBasket);
		this.doUpdate ('printconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showPINCollectionPrintWindow: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('pincollectionprintconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	updatePrintPreviewImage: function (previewImage, imageSize, collectionBasket, printSetChooser, printSetUserDefinedValue)
	{
		var printSet = printSetChooser == '{90eb9f3f-ef03-442c-807c-88c70d257174}' ? printSetUserDefinedValue : printSetChooser;
		$ (previewImage).src = 'Thumbnail.ThumbnailServlet;jsessionid=' + this.sessionID + '?recordView=' + this.currentView.getRecordView () + '&printSet=' + encodeURIComponent (printSet) + '&collectionBasket=' + encodeURIComponent (collectionBasket) + '&imageSize=' + imageSize + '&random=' + Math.random ();
	},
	printCollectionBasket: function (collectionBasket, printSetChooser, printSetUserDefinedValue, asyncDownload)
	{
		var printSet = printSetChooser == '{90eb9f3f-ef03-442c-807c-88c70d257174}' ? printSetUserDefinedValue : printSetChooser;
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (collectionBasket) +
					 "&template=" + encodeURIComponent (printSet) + "&async=" + asyncDownload;
		this.doRequest ("print.jspx", params);
		if (!asyncDownload)
		{
			(function ()
			{
				this.updateDownloadState ();
			}.bind (this).delay (0.5));
		}
		this.hidePopupWindow ();
	},
	showDirectDownloadWindow: function (catalogID, recordID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&singleCatalogID=' + catalogID + '&recordID=' + recordID;
		this.doUpdate ('directdownloadconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showDownloadWindow: function (collectionBasket)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&collectionBasket=' + collectionBasket;
		this.doUpdate ('downloadconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showPINCollectionDownloadWindow: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('pincollectiondownloadconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	downloadCollectionBasket: function (collectionBasket, assetActionChooser, assetActionUserDefinedValue, asyncDownload)
	{
		var assetAction = assetActionChooser == '{90eb9f3f-ef03-442c-807c-88c70d257174}' ? assetActionUserDefinedValue : assetActionChooser;
		var params = 'recordView=recordCollectionCollectionBasketView&collectionBasket=' + encodeURIComponent (collectionBasket) +
					 "&assetAction=" + encodeURIComponent (assetAction) + "&async=" + asyncDownload;
		this.doRequest ("download.jspx", params);
		if (!asyncDownload)
		{
			(function ()
			{
				this.updateDownloadState ();
			}.bind (this).delay (0.5));
		}
		this.hidePopupWindow ();
	},
	updateDownloadState: function ()
	{
		if (!this.runningDownloadInterval)
		{
			var params = 'recordView=' + this.currentView.getRecordView ();
			this.doUpdate ('downloadstate.jspx', params, 'downloadStateContainer', function ()
			{
				this.runningDownloadInterval = setInterval (function ()
				{
					this.doUpdate ('downloadstate.jspx', params, 'downloadStateContainer', function (originalRequest)
					{
						if (originalRequest.responseText.indexOf ('noDownloadsRunning') > -1)
						{
							window.clearInterval (this.runningDownloadInterval);
							this.runningDownloadInterval = undefined;
						}
					}.bind (this), true);
				}.bind (this), 1000);
			}.bind (this), true);
		}
	},
	updateSortingState: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('sortingstate.jspx', params, 'sortingStateContainer');
	},
	deleteProcess: function (processID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&processID=' + processID;
		this.doRequest ('deleteprocess.jspx', params);
	},
	setVisibleOpacityLayer: function (visible)
	{
		if (visible)
		{
			$ ('opacityLayer').setStyle ({display: 'block'});
		}
		else
		{
			$ ('opacityLayer').hide ();
		}
	},
	storeDatePickerValue : function (eventObj)
	{
		var storeObj = $ (eventObj.id + "Store");
		if (storeObj)
		{
			storeObj.value = eventObj.yyyy + eventObj.mm + eventObj.dd + eventObj.xxx;
		}
	},
	switchSession: function (userName, password)
	{
		var params = 'recordView=recordCollectionCollectionBasketView&user=' + encodeURIComponent (userName) + '&password=' + encodeURIComponent (password);
		this.doRequest ('switchsession.jspx', params, function (originalRequest)
		{
			var responseText = originalRequest.responseText;
			if (responseText.length > 0)
			{
				$ ('wrongUserNameOrPassword').update (Builder.node ('span').update (responseText)).show ();
			}
			else
			{
				this.doUpdate ('greetings.jspx', 'recordView=recordCollectionCollectionBasketView', 'greetingsBody');
				this.updateRecordViewSetChooser ();
				this.clearAllFilters (function ()
				{
					this.doUpdate ("mypincollections.jspx", 'recordView=' + this.currentView.getRecordView (), 'myPinCollectionsFrameBody');
					this.gotoPage (this.currentView.getRecordView (), 1);
					this.updateCollectionBasketButton ();
					$ ("recordViewSet").href = this.applySessionID ("rvs2css.jspx") + "?recordView=recordCollectionCollectionBasketView";
				}.bind (this));
			}
		}.bind (this));
	},
	showChangePasswordRequestWindow: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('changepasswordrequestwindowconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	changePasswordRequest: function (userName)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&user=' + encodeURIComponent (userName);
		this.doRequest ('changepasswordrequestwindow.jspx', params, function (originalRequest)
		{
			var responseText = originalRequest.responseText;
			if (responseText.length > 0)
			{
				$ ('changePasswordErrorContainer').update (Builder.node ('span').update (responseText)).show ();
			}
			else
			{
				this.hidePopupWindow ();
			}
		}.bind (this));
	},
	showCreateAccountWindow: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ('createaccountwindowconfiguration.jspx', params, 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	createAccount : function (createAccountForm)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&' + Form.serialize (createAccountForm);
		this.doRequest ('createaccountwindow.jspx', params, function (originalRequest)
		{
			var responseText = originalRequest.responseText;
			if (responseText.length > 0)
			{
				if (responseText.indexOf ('userSuccessfullyCreatedIdentifier') >= 0)
				{
					$ ('createAccountContainer').update (responseText);
				}
				else
				{
					$ ('createAccountErrorContainer').update (Builder.node ('span').update (responseText)).show ();
				}
			}
			else
			{
				this.hidePopupWindow ();
			}
		}.bind (this));
	},
	showContactWindow: function ()
	{
		this.doUpdate ('contactview.jspx', '', 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	showHelpWindow: function ()
	{
		this.doUpdate ('helpview.jspx', '', 'popupFrameBody', function ()
		{
			this.showPopupWindow ();
		}.bind (this));
	},
	startBreadcrumbPopupShowing: function (popupElement, immediate)
	{
		this.stopBreadcrumbPopupShowing ();
		this.timerBreadcrumbPopupShowing = setTimeout (function ()
		{
			var elementRequired = $ (popupElement);
			if (elementRequired)
			{
				this.closeAllAdvancedSearchPopups (elementRequired.id);
				this.setElementVisible (popupElement, true, function (effect)
				{
					effect.element.style.display = "inline";
				}.bind (this));
			}
		}.bind (this), immediate ? 10 : 1000);
	},
	stopBreadcrumbPopupShowing: function ()
	{
		if (this.timerBreadcrumbPopupShowing != undefined)
		{
			clearTimeout (this.timerBreadcrumbPopupShowing);
		}
	},
	startBreadcrumbPopupHiding: function (popupElement)
	{
		this.stopBreadcrumbPopupHiding ();
		this.timerBreadcrumbPopupHiding = setTimeout (function ()
		{
			var elementRequired = $ (popupElement);
			if (elementRequired)
			{
				this.setElementVisible (elementRequired, false);
			}
		}.bind (this), 1000);
	},
	stopBreadcrumbPopupHiding: function ()
	{
		if (this.timerBreadcrumbPopupHiding != undefined)
		{
			clearTimeout (this.timerBreadcrumbPopupHiding);
		}
	},
	showBox : function (boxTitle, boxID)
	{
		this.closeAllAdvancedSearchPopups ();
		var boxContent = $ (boxTitle).next ();
		var isVisible = boxContent.visible ();
		if (!isVisible)
		{
			boxTitle.removeClassName ("collapsed");
			Effect.toggle (boxContent, 'blind',
			{
				delay: 0.0,
				duration: 0.2,
				queue: 'end'
			});
			if (boxID)
			{
				var params = 'collapsed=false&boxID=' + encodeURIComponent (boxID);
				this.doRequest ('boxcollapsestate.jspx', params);
			}
		}
		else
		{
			Effect.Shake (boxContent,
			{
				delay: 0.0,
				queue: 'end'
			});
		}
	},
	toggleBox : function (boxTitle, boxID)
	{
		this.closeAllAdvancedSearchPopups ();
		var boxContent = $ (boxTitle).next ();
		var isCollapsed = boxContent.visible ();
		Effect.toggle (boxContent, 'blind',
		{
			delay: 0.0,
			duration: 0.2,
			queue: 'end'
		});
		if (isCollapsed)
		{
			boxTitle.addClassName ("collapsed");
		}
		else
		{
			boxTitle.removeClassName ("collapsed");
		}
		if (boxID)
		{
			var params = 'collapsed=' + isCollapsed + '&boxID=' + encodeURIComponent (boxID);
			this.doRequest ('boxcollapsestate.jspx', params);
		}
	},
	setElementVisible: function (element, visible, beforeStartFunction, onCompleteFunction)
	{
		if (visible)
		{
			Effect.Appear (element,
			{
				delay: 0.0,
				duration: 0.2,
				beforeSetup: function(effect)
				{
					effect.element.setOpacity (effect.options.from).show ();
					if (beforeStartFunction)
					{
						beforeStartFunction (effect);
					}
				},
				afterFinish: function(effect)
				{
					if (onCompleteFunction)
					{
						onCompleteFunction (effect);
					}
				},
				queue: 'end'
			});
		}
		else
		{
			Effect.Fade (element,
			{
				delay: 0.0,
				duration: 0.2,
				afterFinish: function (effect)
				{
					if (onCompleteFunction)
					{
						onCompleteFunction (effect);
					}
				}.bind (this),
				queue: 'end'
			});
		}
	},
	updateImageSizeChooser: function ()
	{
		this.imageSizeSlider.inStateChange = true;
		this.imageSizeSlider.range = $R (this.currentView.getImageSizeMinimum (), this.currentView.getImageSizeMaximum ());
		this.imageSizeSlider.setValue (this.currentView.getImageSizeCurrent (), 0);
		this.imageSizeSlider.inStateChange = false;
	},
	updateRecordsPerPage: function ()
	{
		var currentNumberOfRecords = this.currentView.getRecordsPerPage ();
		var element = $ ('recordsPerPageChooser');
		for (var i = 0, length = element.length; i < length; i++)
		{
			if (element.options[i].value == currentNumberOfRecords)
			{
				element.options[i].selected = true;
				break;
			}
		}
	},
	setRecordView: function (recordView)
	{
		this.currentView.setRecordView (recordView);
		//		var neededId = "viewMode" + recordView;
		//		$ ('applicationToolbar').select (".viewMode").each (function (item)
		//		{
		//			if (item.id == neededId)
		//			{
		//				Element.addClassName (item, "toggledButton");
		//			}
		//			else
		//			{
		//				Element.removeClassName (item, "toggledButton");
		//			}
		//		}.bind (this));
		this.updateRecords ();
		this.updateImageSizeChooser ();
		this.updateRecordsPerPage ();
		this.hideRecordInformationTooltip ();
		this.closeAllAdvancedSearchPopups ();
	},
	setRecordViewSet: function (recordViewSet)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&recordViewSet=' + encodeURIComponent (recordViewSet);
		this.doRequest ("setviewset.jspx", params, function ()
		{
			$ ("recordViewSet").href = this.applySessionID ("rvs2css.jspx") + "?" + params;
			this.updateRecords ();
		}.bind (this));
	},
	updateRecordViewSetChooser: function ()
	{
		var params = 'recordView=' + this.currentView.getRecordView ();
		this.doUpdate ("recordviewsetchooser.jspx", params, 'recordViewSetChooserContainer', function (/*originalRequest*/)
		{
			$ ("recordViewSet").href = this.applySessionID ("rvs2css.jspx") + "?" + params;
		}.bind (this));
	},
	startSlideShow: function (pageNumber, maximumPageNumber, askForIntervalTimeoutText)
	{
		if (askForIntervalTimeoutText)
		{
			// stop the slide show initiated by the first click of this double click
			this.enableStartSlideShow = false;
			this.slideShowTimingInSeconds =  Math.max (Number (prompt (askForIntervalTimeoutText, this.slideShowTimingInSeconds)), 1);
		}
		this.enableStartSlideShow = true;
		setTimeout (function ()
		{
			if (this.enableStartSlideShow && this.slideShowInterval == undefined)
			{
				var currentPageNumber = pageNumber;
				if (currentPageNumber < maximumPageNumber)
				{
					this.gotoPage ('recordCollectionPreviewView', ++currentPageNumber);
					this.slideShowInterval = setInterval (function ()
					{
						if (currentPageNumber < maximumPageNumber)
						{
							app.gotoPage ('recordCollectionPreviewView', ++currentPageNumber);
						}
						else
						{
							app.stopSlideShow (currentPageNumber);
						}
					}, this.slideShowTimingInSeconds * 1000);
				}
			}
		}.bind (this), askForIntervalTimeoutText ? 0 : 400);
	},
	stopSlideShow: function (pageNumber)
	{
		this.enableStartSlideShow = false;
		if (this.slideShowInterval != undefined)
		{
			clearInterval (this.slideShowInterval);
		}
		this.slideShowInterval = undefined;
		if (pageNumber)
		{
			this.gotoPage ('recordCollectionPreviewView', pageNumber);
		}
	},
	showRecordInformationTooltip: function (mouseEvent, catalogID, recordID, recordIndex)
	{
		if (!this.isPINCollection)
		{
			var containerElement = $ ('recordInformationTooltipContainer');
			if (!containerElement)
			{
				containerElement = $ (Builder.node ('div', {  id: 'recordInformationTooltipContainer' }));
				$ ('body').appendChild (containerElement);
				CantoUtils.updateTooltipPosition (mouseEvent, containerElement, 15, 2);
				var params = 'recordView=' + this.currentView.getRecordView () + '&singleCatalogID=' + catalogID + "&recordID=" + recordID + "&recordIndex=" + recordIndex;
				this.doUpdate ('tooltipinfoview.jspx', params, 'recordInformationTooltipContainer', function ()
				{
					this.setElementVisible (containerElement, true);
				}.bind (this));
			}
			else
			{
				CantoUtils.updateTooltipPosition (mouseEvent, containerElement, 15, 2);
			}
		}
	},
	hideRecordInformationTooltip: function ()
	{
		var containerElement = $ ('recordInformationTooltipContainer');
		if (containerElement)
		{
			containerElement.remove ();
		}
	},
	logout: function (errorID)
	{
		var params = 'recordView=' + this.currentView.getRecordView () + '&ri=' + Math.random ();
		this.doRequest ('logout.jspx', params, function()
		{
			if (errorID != null)
			{
				location.href = 'index.jspx?com.canto.cumulus.web.ErrorID=' + errorID;
			}
			else
			{
				location.href = 'index.jspx';
			}
		}.bind (this));
	}
});
var app;
var componentInitialization = false;
function exitApplication (errorID)
{
	if (app)
	{
		app.logout (errorID);
		app = undefined;
	}
}
function initApplication (sessionID, initialRecordView, imageSizes, recordsPerPage, currentCollectionBasket, isPINCollection)
{
	app = new SitesApplication (sessionID, initialRecordView, imageSizes, recordsPerPage, currentCollectionBasket, isPINCollection);
	app.updateAllFieldFilters (true);
}

