	var iRelationCount = 0;

	function RelationManager(sPrimaryTable, sForeignTable, iPrimaryID, sWindowName, sWindowTitle) {
		this.iSelectedID=null;
		this.iSelectedAttachedID=null;
		this.sPrimaryTable=sPrimaryTable;
		this.sForeignTable=sForeignTable;
		this.iPrimaryID=iPrimaryID;
		this.sWindowIdentifier=sWindowName;
		this.sWindowTitle=sWindowTitle;
		this.sSortColumn=null;
		this.sSortDir=null;
		
		this.oStupidIFRAMEUGGHHH = null;
		
		this.oRelationManager=null;
		this.sRelationManagerID=null;
		this.oTitleBar=null;
		this.oTitleText=null;
		this.oTitleClose=null;
		this.oTitleCloseAnchor=null;
		this.oTitleCloseImage=null;
		this.oBodyArea=null;
		
		this.sRowIDPrefix = 'rm_list_row_';
		
		this.sBaseURL='/corespin/relation_manager';
		this.sActionURL=this.sBaseURL + '/actions';
		this.sListURL=this.sBaseURL + '/lists';

		this.oSearchTimer=null;
		this.sSearchQuery=null;
		this.iFilterID=null;
		
		this.init();
	}
	
	RelationManager.prototype = {
		init:function() {
			iRelationCount = iRelationCount+1;
			window['oRelationManagerSpawn' + iRelationCount] = this;
			
			this.sRelationManagerID = 'rm_' + this.sWindowIdentifier;
			this.oRelationManager = document.createElement('div');
			this.oRelationManager.setAttribute("id", this.sRelationManagerID);
			this.oRelationManager.setAttribute("class", "relation_manager");
			// This is hardcoded for now. Eventually all IDs will reference the window name in order to remain unique.
			// That would allows us to have multiple instances of Relation Manager running on a page.
			this.oTitleBar = document.createElement('div');
			this.oTitleBar.setAttribute("id", 'title_bar');
			this.oTitleBar.setAttribute("class", 'title_bar');
			
			this.oTitleClose = document.createElement('div');
			this.oTitleClose.setAttribute("id", 'title_bar_options');
			this.oTitleClose.setAttribute("class", 'title_bar_options');
			
			this.oTitleCloseAnchor = document.createElement('a');
			this.oTitleCloseAnchor.setAttribute("href", 'javascript://this.close()');
			this.oTitleCloseAnchor.setAttribute("title", 'Close');
			
			this.oTitleCloseAnchor.onclick = function() { window['oRelationManagerSpawn' + iRelationCount].close(); };
			
			this.oTitleCloseImage = document.createElement('img');
			this.oTitleCloseImage.setAttribute("src", '/box/icons/cross.png');
			this.oTitleCloseImage.setAttribute("alt", 'Close');
			
			this.oTitleText = document.createElement('div');
			this.oTitleText.innerHTML = 'CORESPIN Relation Manager :: ' + this.sWindowTitle;
			
			this.oTitleCloseAnchor.appendChild(this.oTitleCloseImage);
			this.oTitleClose.appendChild(this.oTitleCloseAnchor);
			this.oTitleBar.appendChild(this.oTitleClose);
			this.oTitleBar.appendChild(this.oTitleText);
			
			this.oBodyArea = document.createElement('div');
			this.oBodyArea.setAttribute("id", 'rm_body');
			
			this.oRelationManager.appendChild(this.oTitleBar);
			this.oRelationManager.appendChild(this.oBodyArea);
			
			this.oStupidIFRAMEUGGHHH = document.createElement('iframe');
			this.oStupidIFRAMEUGGHHH.style.width = '700px';
			this.oStupidIFRAMEUGGHHH.style.height = '500px';
			
			this.oDimensions = new getDimensions();
			
			this.oRelationManager.style.top = this.oDimensions.iScrollTop + ((this.oDimensions.iDocHeight - this.oDimensions.iScrollTop)/2) + 'px';
			this.oRelationManager.style.left = this.oDimensions.iDocWidth / 2 - 350 + 'px';
			this.oStupidIFRAMEUGGHHH.style.top = this.oDimensions.iScrollTop + ((this.oDimensions.iDocHeight - this.oDimensions.iScrollTop)/2) + 'px';
			this.oStupidIFRAMEUGGHHH.style.left = this.oDimensions.iDocWidth / 2 - 350 + 'px';
			this.oStupidIFRAMEUGGHHH.style.zIndex = '9999';
			
			
			
			document.body.appendChild(this.oRelationManager);
			document.body.appendChild(this.oStupidIFRAMEUGGHHH);
			
			ajaxEvent(this.sBaseURL + this.queryString(), this.oBodyArea, false, false);
			
			fadeControl(this.sRelationManagerID, 0, 100, 1000);
			return true;
		},
		queryString:function(sAttached) {
			sQueryString = '?_primary=' + this.sPrimaryTable + '&_foreign=' + this.sForeignTable + '&_primary_id=' + this.iPrimaryID + '&_window_name=' + this.sWindowIdentifier + '&_window_title=' + this.sWindowTitle;
			if (sAttached != 'attached') {
				if (this.iFilterID != null) {
					sQueryString += '&_f=' + this.iFilterID;
				}
				if (this.sSearchQuery != null) {
					sQueryString += '&_q=' + this.sSearchQuery;
				}
				sAttached = 'browse-all';
			}
			sQueryString += '&_attached=' + sAttached;
			return sQueryString;
		},
		close:function() {
			this.oStupidIFRAMEUGGHHH.parentNode.removeChild(this.oStupidIFRAMEUGGHHH);
			fadeControl(this.sRelationManagerID, 100, 0, 600);
			window.setTimeout("$('" + this.sRelationManagerID + "').parentNode.removeChild($('" + this.sRelationManagerID + "'));", 600);
		},
		refreshList:function(sAttached) {
			if (sAttached == null) {
				sAttached = 'attached';
			}
			
			if (sAttached == 'attached') {
				$('attached_elements').innerHTML = '';
				ajaxEvent(this.sListURL + this.queryString(sAttached) + '&_attached=attached&_enabled=1&_action=attach', $('attached_elements'), false, false);
			} else {
				$('all_elements').innerHTML = '';
				ajaxEvent(this.sListURL + this.queryString() + '&_attached=browse-all&_enabled=0&_action=attach', $('all_elements'), false, false);
			}
		},
		attachElement:function(iForeignIdentifier) {
			if (iForeignIdentifier == null) {
				if (this.iSelectedID == null) {
					alert('Please select an item to attach.');
					return;
				} else {
					iForeignIdentifier=this.iSelectedID;
				}
			}
			sRowID = $(this.sRowIDPrefix + 'browse-all_' + iForeignIdentifier);
			ClassFX.removeClass(sRowID, 'selected');
			clonedNode = sRowID.cloneNode(true);
			$('attached_elements').appendChild(clonedNode);
			this.iSelectedID = null;
			ajaxEvent(this.sActionURL + this.queryString('attached') + '&_attached=attached&_enabled=1&_action=attach&_foreign_id=' + iForeignIdentifier, $('attached_elements'), false, false);
			return;
		},
		removeElement:function(iRelationIdentifier) {
			if (iRelationIdentifier == null) {
				if (this.iSelectedAttachedID == null) {
					alert('Please select an item to remove.');
					return;
				} else {
					iRelationIdentifier=this.iSelectedAttachedID;
				}
			}
			$(this.sRowIDPrefix + 'attached_' + iRelationIdentifier).style.display = 'none';
			this.iSelectedAttachedID = null;
			ajaxEvent(this.sActionURL + this.queryString('attached') + '&_attached=attached&_enabled=1&_action=remove&_primary_foreign_id=' + iRelationIdentifier, $('attached_elements'), false, false);
			return;
		},
		selectRow:function(iIdentifier, sAttached) {
			if (iIdentifier == null) {
				return false;
			}
			if (sAttached == 'attached') {
				if (this.iSelectedAttachedID != null) {
					ClassFX.removeClass($(this.sRowIDPrefix + sAttached + '_' + this.iSelectedAttachedID), 'selected');
				}
				this.iSelectedAttachedID = iIdentifier;
			} else {
				if (this.iSelectedID != null) {
					ClassFX.removeClass($(this.sRowIDPrefix + sAttached + '_' + this.iSelectedID), 'selected');
				}
				this.iSelectedID = iIdentifier
			}
			ClassFX.addClass($(this.sRowIDPrefix + sAttached + '_' + iIdentifier), 'selected');
		},
		mouseOverRow:function(iIdentifier, sAttached) {
			if (iIdentifier == null) {
				return false;
			}
			ClassFX.addClass($(this.sRowIDPrefix + sAttached + '_' + iIdentifier), 'hover');
		},
		mouseOutRow:function(iIdentifier, sAttached) {
			if (iIdentifier == null) {
				return false;
			}
			ClassFX.removeClass($(this.sRowIDPrefix + sAttached + '_' + iIdentifier), 'hover');
		},
		mouseOverSort:function(oSortButton) {
			ClassFX.addClass(oSortButton, 'sort_hover');
		},
		mouseOutSort:function(oSortButton) {
			ClassFX.removeClass(oSortButton, 'sort_hover');
		},
		selectSort:function(sSortColumn, sAttached) {
			if (sAttached != 'attached') {
				if (this.sSortColumn == sSortColumn) {
					if (this.sSortDir == 'ASC') {
						this.sSortDir = 'DESC';
						sSortDir='DESC';
					} else {
						this.sSortDir = 'ASC';
						sSortDir='ASC';
					}
				} else {
					this.sSortColumn = sSortColumn;
					this.sSortDir = 'ASC';
					sSortDir='ASC';
				}
				sAttached='browse-all';
				oListContainer = $('all_elements');
			} else {
				if (this.sSortColumnAttached == sSortColumn) {
					if (this.sSortDirAttached == 'ASC') {
						this.sSortDirAttached = 'DESC';
						sSortDir = 'DESC';
					} else {
						this.sSortDirAttached = 'ASC';
						sSortDir = 'ASC';
					}
				} else {
					this.sSortColumnAttached = sSortColumn;
					this.sSortDirAttached = 'ASC';
					sSortDir = 'ASC';
				}
				oListContainer = $('attached_elements');				
			}
			ajaxEvent(this.sListURL + this.queryString(sAttached) + '&_sort=' + sSortColumn + '&_dir=' + sSortDir, oListContainer, false, false);
		},
		viewDetail:function(iIdentifier, sAttached) {
			if (iIdentifier == null) {
				if (sAttached == 'attached') {
					alert('Please select an item to view.');
					return;
				} else {
					iIdentifier = this.iSelectedID;
				}
			}
			newWindow = window.open('/corespin/' + this.sForeignTable + '/' + iIdentifier, 'detail');
		},
		createNew:function() {
			newWindow = window.open('/corespin/' + this.sForeignTable + '/new', 'detail');
		},
		timedSearch:function(sSearchValue) {
			if (this.oSearchTimer != null) {
				window.clearTimeout(this.oSearchTimer);
			}
			this.sSearchQuery = sSearchValue;
			this.oSearchTimer = window.setTimeout("ajaxEvent('" + this.sListURL + this.queryString() + "', $('all_elements'), false, true);", 500);
		},
		setFilter:function(iFilterID) {
			if (iFilterID == 'empty') {
				this.iFilterID = null;
			} else {
				this.iFilterID = iFilterID;
			}
			ajaxEvent(this.sListURL + this.queryString(), $('all_elements'), false, false);
		}
		
	}