// Set variables
var strFilterToggle = "expanded";
var strFlipToggle = "flipped";
var browseFilterHeight = 604;
var artTermsListHeight = 490;
var minContentHeight = 580;
var finalSelectedArtists = new Array();
var lteIE7 = false /*@cc_on || @_jscript_version <= 5.7 @*/;
var searchFieldValue = "Search by Artist, Work, or Keyword";
var browser=navigator.appName;

// Class that handles the browse filter form functionality
MoMA.FiltersExtended = new Class({

	initialize: function(el) {    
	
	    // PERFORM ON INITIALISE CLASS

		// Hide these elements		
		// Note needed as adding hideContent to input[type=checkbox] is done in search.tpl.
		// Reinstate if using this code elsewhere
	    // el.getElements('form.browseFilter input[type=checkbox]').addClass('hideContent');
		
	    // Grab content
	    this.el = el;
	    this.artistList = el.getElement('ul.artistList');
    	this.tableHeaders = el.getElements('form.browseFilter table tr th');
    	this.tableLabels = el.getElements('form.browseFilter label');
    	this.artTermsLink = el.getElement('ul.artTermsLink');
	    this.artTermsList = el.getElement('ul.artTermsList');
	    this.closeArtTerms = el.getElement('a#closeArtTerms');
	    
		this.coverFilterContent = el.getElements('div#coverFilterOptions');
		this.tableOfContent = el.getElements('ul.alphabetList li');
		this.tableLinks = this.tableOfContent.getElements('a');
		this.formSubmit = el.getElement('input#filterSubmit')
		this.artTermsBar = el.getElement('a#byArtTermSelected');
		this.browseFilter = el.getElement('.extendedFilter');
		this.filterOptions = el.getElement('div#filterOptions');

		var previousSelectedArtists = el.getElement('input#previousSelectedArtists').get('value');

		if (previousSelectedArtists == "") {
			finalSelectedArtists = new Array();
		} else {
			finalSelectedArtists = previousSelectedArtists.split(',')
		}

		this.tableLinks.each(function(linkItem) {
			linkItem.addEvent('click', function() {
	
				// Grab the hidden artist initial variable
				// Set it's value to the letter just clicked
				
				var hiddenArtistInitial = el.getElement('input#artistFilterInitial');
				hiddenArtistInitial.set('value', linkItem.get('text'))
				
				//Grab href of current link. Convert to string with (+ '')
				var linkValue = linkItem.get('href') + '';
				
				// Grab from the '?' to the end, of the href
				var linkQueryString = linkValue.substring( linkValue.indexOf('?') , linkValue.length );
				
				var artistList = el.getElement('ul.artistList');
				// Grab HTML content from given 'url'
				// Place it in the artist list
				var req = new Request({  
					method: 'get',  
					url: "get_artists_for_initial.php" + linkQueryString,
					data: { 'do' : '1' },  
					onRequest: function() {
						artistList.fade('out')
					},  
					onComplete: function(response) {
						this.tableLinks.each(function(alphabetLink) {
							alphabetLink.removeClass('selected');
						});
						linkItem.setProperty('class', 'selected');
						artistList.scrollTop = 0;
						artistList.set('html', response);
						
						artistList.getElements('input[type=checkbox]').each(function(checkbox){
							checkbox.setProperty('class', 'hideContent');
						});
						artistList.fade('in');
												
						var tableHeader = el.getElement('th#JS_ArtistList');
						var headerID = tableHeader.id;
						
						if (el.getElements('form.browseFilter table tr td.' + headerID + ' label') != "") {
							var cellLabels = el.getElements('form.browseFilter table tr td.' + headerID + ' label');
							// Remove or add that artists ID from the hidden input string 'previousSelectedArtists'
							
							// on click of table header label, unhighlight all of that sections input fields
							tableHeader.getElement('label').addEvent('click', function() {
								cellLabels.removeClass('selected');
								el.getElements('form.browseFilter table tr td.' + headerID + ' input[type=checkbox]').setProperty('checked',false);
								
								finalSelectedArtists = new Array();
							});
							
							// on click of that sections input fields, unhighlight table header label
							cellLabels.addEvent('click', function() {
								tableHeader.getElement('label').removeClass('selected');
								tableHeader.getElement('input').setProperty('checked',false);
							});
						}

						if (el.getElements('form.browseFilter table tr td.' + headerID + ' ul.artistList li') != "") {
							this.artistCells = el.getElements('form.browseFilter table tr td.' + headerID + ' ul.artistList li');
							this.artistListBehaviour();
						}
						
						// Clicking any of the filter options will toggle the 'selected' class,
						// in sync with the checkbox state
					    cellLabels.each(function(item) {
						    item.addEvent('click', function(){
						    	this.toggleClass('selected');
						    });
					    }.bind(this));
						
					}.bind(this)
				}).send();
				return false;
			}.bind(this));
		}.bind(this));
		
	   // Display the 'click here to view art terms' link
	    this.artTermsLink.setStyle('display', 'block');
	    this.artTermsList.setStyle('display', 'none');
    	this.coverFilterContent.setStyle('height', browseFilterHeight);    	
    	
	    // Set properties for tweening the art terms list
	    this.artTermsList.set('tween', {
	      duration: 500,
	      transition: Fx.Transitions.Quart.easeOut,
	      // For smoothness, only reactivate the art terms link once the art terms list has disappeared
	      onComplete: function() {
	      	if (this.artTermsList.getStyle('height') == "0px") {
	      		this.artTermsList.setStyle('display', 'none');
	      		this.artTermsLink.setStyle('display', 'block');
	      	} else {
	      		this.closeArtTerms.setStyle('display', 'block');
	      	}
	      }.bind(this)
	    });

		// RUN ON CLICK
		// Clicking on the art terms link or header bar will show art terms
	    this.artTermsLink.addEvent('click', this.showArtTerms.bind(this));
		this.artTermsBar.addEvent('click', this.showArtTerms.bind(this));

		// Clicking on the filter bar or the close art terms button will hide the art terms
	    this.filterOptions.addEvent('click', this.hideArtTerms.bind(this));
	    this.closeArtTerms.addEvent('click', this.hideArtTerms.bind(this));

		// Clicking any of the filter options will toggle the 'selected' class,
		// in sync with the checkbox state
	    this.tableLabels.each(function(item) {
		    item.addEvent('click', function(){
		    	this.toggleClass('selected');
		    });
	    }.bind(this));

		// Clicking on any table header label will deactivate it's related table cell labels, and vice versa
		this.tableHeaders.each(function(tableHeader){
			var headerID = tableHeader.id;

			if (el.getElements('form.browseFilter table tr td.' + headerID + ' label') != "") {
				var cellLabels = el.getElements('form.browseFilter table tr td.' + headerID + ' label');

				if (tableHeader.getElement('label') != "") {
					// on click of table header label, unhighlight all of that sections input fields
					tableHeader.getElement('label').addEvent('click', function() {
						cellLabels.removeClass('selected');
						el.getElements('form.browseFilter table tr td.' + headerID + ' input[type=checkbox]').setProperty('checked',false);

						// If artist button is clicked wipe all the artist data
						if (tableHeader.getElement('label').get('for') == "allArtists")
							finalSelectedArtists = new Array();

					}.bind(this));
					
					// on click of that sections input fields, unhighlight table header label
					cellLabels.addEvent('click', function() {
						tableHeader.getElement('label').removeClass('selected');
						tableHeader.getElement('input').setProperty('checked',false);
					});
				}
			}
			
			if (el.getElements('form.browseFilter table tr td.' + headerID + ' ul.artistList li') != "") {
				this.artistCells = el.getElements('form.browseFilter table tr td.' + headerID + ' ul.artistList li');
				this.artistListBehaviour();
			}
			
		}.bind(this));

		// Loop through all labels and if their associated input checkboxes are ticked,
		// add a 'selected' class to the label
		this.tableLabels.each(function(item) {

			// Have to use an old JavaScript term for cross browser purposes
			var relatedInputField = item.htmlFor;
	    	var cellInput = el.getElement('#' + relatedInputField);
	    	
    		// if our tick box is checked, then make our label class selected
			if (cellInput.getProperty('checked') == true) {
    			item.addClass('selected');
	    	}
		}.bind(this));

		// On click of form  field, convert the array of saved artists into ticked checkboxes
		this.formSubmit.addEvent('click', function(evt){
			new Event(evt).stop();
			this.artistList.set('html', '');
			for (var i = 0; i < finalSelectedArtists.length; i++) {
				this.artistList.set('html', this.artistList.get('html') + "<li><input type='checkbox' value='" + finalSelectedArtists[i] + "' class='checkbox hideContent' name='selectedArtists[]' checked='checked'/></li>");
			}
			document.browseFilter.submit();
		}.bind(this));
    },
    
    hideArtTerms: function() {
    	if (this.artTermsLink.getStyle('display') == "none") {
			this.artTermsList.tween('height',0);
			this.closeArtTerms.setStyle('display', 'none');
		    this.coverFilterContent.setStyle('display', 'none');
		    this.artistList.setStyle('overflow', 'auto');
		    this.filterOptions.removeClass('selected');
		}
		return false;
    },
    
    showArtTerms: function(){
	    if (this.artTermsLink.getStyle('display') == "block") {
	    	this.artTermsList.setStyle('height',0);
			this.artTermsLink.setStyle('display', 'none');
	    	this.artTermsList.setStyle('display', 'block');
		    this.artTermsList.tween('height',artTermsListHeight);
		    this.coverFilterContent.setStyle('display', 'block');
			this.artistList.setStyle('overflow', 'hidden');
		}
		return false;
    },
    
    artistListBehaviour: function() {
		this.artistCells.each(function(artistCell){
			var cellInput = artistCell.getElement('input');

			for (var i = 0; i < finalSelectedArtists.length; i++) {
				if (finalSelectedArtists[i] == cellInput.get('value')) {
					cellInput.setProperty('checked', true);
					artistCell.getElement('label').addClass('selected');
				}
			}
			
			// on click of that sections input fields, unhighlight table header label
	
			//if (artistTableHeader.getElement('input').getProperty('checked') == true) {
			//	cellLabel.setProperty('checked',true);
			//}

			artistCell.getElement('label').addEvent('click', function() {
				var artistInput = artistCell.getElement('input')
				var artistID = artistInput.get('value');
				var artistInArray = false;
				
				// Check if clicked label is in selected artists array,
				// If so remove it, if not add it to add
				for (var i = 0; i < finalSelectedArtists.length; i++) {
					
					if (finalSelectedArtists[i] == artistID) {
						artistInArray = i;
						break;
					}
				}
								
				if (artistInArray === false) {
					finalSelectedArtists.push(artistID)
				} else {
					finalSelectedArtists.erase(artistID);
				}
			}.bind(this));
		}.bind(this));
		
    }
});

// This class will handle drop down menu behaviour for the content
MoMA.SectionMenu = new Class({

	initialize: function(el) {
		this.el = el;
		
		if (el.getElement('select#contents-list'))
		    this.dropDownLinks(el.getElement('select#contents-list'))
	    
	},
	
	dropDownLinks: function(tableOfContent) {
		// on click of the option, go to it's value - the GET variable which gets appended to the url
	    tableOfContent.addEvent('change', function(){
	    	var selectedOption = tableOfContent.getSelected();
	    	location.href = selectedOption.getProperty('value');
		}.bind(this));
	}
	
});

//The Class to swap in the second Images once their links have been clicked on

MoMA.SecondaryImages = new Class({
	
	initialize: function(el) {
	
	
		this.el = el;
		this.secondImageLinks = el.getElements('ul.pages li a');
		var imageSource = el.getElement('div.holder');
		var controls = el.getElement('div.controls');
		this.secondImageLinks.each(function(link){
			
			 link.addEvent('click', function(){
				var linkID = link.getProperty('id');
				var imageID = linkID.replace("secondImage",'');
				
				for (key in imageArray){
					if(key == (imageID-1)){
						
						var src = imageArray[key]['src'];
						var width = imageArray[key]['width'];
						var height = imageArray[key]['height'];
						var alt = imageArray[key]['alt'];
						//set the html of the div.holder to include the image source. this replaces the flash object when zoomify is there instead.
						imageSource.set('html','<p id="mainImage"><img src="'+src+'" width="'+width+'" height="'+height+'" alt="'+alt+'" /></p>');

						if(imageArray[key]['zoom']){
							
							controls.set('html','<div id="zoomifyDiv"></div>');
							
							if (swfobject.hasFlashPlayerVersion('6'))
							{
								if (BrowserDetect.browser == 'Opera' || BrowserDetect.browser == 'Chrome')
								{
									var fullscreenMode = 'false';
								}
								else
								{
									var fullscreenMode = 'true';
								}
								//add the zoomify links to the div.controls
								addZoomifyLinks(imageArray[key]['zoom'], 'zoomifyDiv', '', '', '', fullscreenMode);
							}
							else {
								clearZoomifyLink('zoomifyDiv');
							}
						}
						else {
							clearZoomifyLink('zoomifyDiv');
						}
					}
				}
				
				 return false;
			 });
		});
		
	},

	swapImage: function(link, imageSource) { 	
		
		imageSource.getElementgetProperty('src') = link.getProperty('href');
		return false;
    }
})

// This Class will handle the onclick values of the tabs, hiding and showing the relevant content
MoMA.SectionContentTabs = new Class({

	initialize: function(el) {
		this.el = el;
		// Grab all the content tab links
		this.listItems = el.getElements('div.tabs ul li a');
		
		// Loop through all tab links
		this.listItems.each(function(listItem){
	    	// Grab all the content where the class is the same as the ID of the tab link
			var relatedContent = el.getElements('div.' + listItem.id);

			// Check if the link has an ID and there is related content
			if (listItem.id.length > 0 && relatedContent.length > 0) {
				
				// When a tab link is clicked...
				listItem.addEvent('click', function() {
					// Hide all the content...
					el.getElements('div.content').setStyle('display', 'none');
					relatedContent.each(function(relatedContentItem) {
						/// and unhide the related content
						relatedContentItem.setStyle('display', 'block');
						
						// Unselect all the tabs, and highlight the clicked tab
						el.getElements('div.tabs ul li a').removeClass('selected');
						listItem.addClass('selected');
					});
					return false;
				});
			}
	    });
	}
});


// The Class which handles the browser filter transitions
MoMA.FilterTransitions = new Class({
    
  initialize: function(el) {
    // Grab content that needs to be changed
    this.el = el;
    
    // Get the height of the mainContent, and create a content cover of the same size
    this.coverContent = el.getElement('div#coverContent');
    // Calculate height of the popover style content covering div
	var mainContentDims = el.getElement('div#middle').getSize();
	//var collectionDims = el.getElement('div#collection').getSize();
    var footerDims = el.getElement('div#bottomContent').getSize();
    if (mainContentDims.y < minContentHeight) {
    	this.minContentHeight = minContentHeight;
    	mainContentDims.y = minContentHeight;
    }
    this.contentCoverHeight = mainContentDims.y + footerDims.y;
    this.coverContent.setStyle('height', this.contentCoverHeight);
    
    this.filterBar = el.getElement('form.browseFilter');
    this.filterLists = this.filterBar.getElements('ul');
    
	this.toggleFilterBar = el.getElement('div#toggleFilter div#filterOptions');
    this.advancedLink = el.getElement('a.advancedOptions');
    this.fileDropDown = el.getElement('.extendedFilter');
    this.artTermsLink = el.getElement('ul.artTermsLink');
    this.artTermsList = el.getElement('ul.artTermsList');
    this.filterStatus = el.getElement('a#filterStatus');
    this.tabbedInfo = el.getElement('#middle.object .text .content');
    this.thumbnailSlider = el.getElement('div.thumbnail_images');
    this.collectionNav = el.getElement('div#collection-nav');
    this.middleContent = el.getElement('div#middle');
    this.artTermsBar = el.getElement('a#byArtTermSelected');
    this.coverFilterContent = el.getElements('div#coverFilterOptions');
    this.bottomContent = el.getElement('div#bottomContent');
    if (this.fileDropDown.getStyle('height') == "0px") {
 	   el.getElement('ul.artistList').setStyle('overflow', 'hidden');
    }
    
    //TODO: Add functionality to move popover to middle of screen, set overflow: hidden and activate close button
    this.noSearchResults = el.getElement('div#errorMessage');
    
    if (el.getElement('div.untoggleable') != "") {
    	this.toggleFilterBar.addEvent('click', function(evt){
    		new Event(evt).stop();
    	});
    }	
    
   //if there is no search results then output an error in the middle of the screen, hiding other content
    if (this.noSearchResults) {
    	this.coverNoResults = el.getElement('div#coverNoResults')
    	this.coverNoResults.setStyle('display', 'block');
    	this.noSearchResults.set('html', '<p id="errorHeader">Sorry, your search criteria didn\'t return any results.</p><p>You may want to broaden your search and try again.</p><a href="#"><img id="close_message" src="images/collection/error_message_close_icon.gif" alt="close" /></a>');
    	
    	//for when the user clicks the close icon
    	this.noSearchResults.addEvent('click', function() {
    		this.filterBar.setStyle('display', 'block');
			this.noSearchResults.setStyle('display', 'none');
			this.coverNoResults.setStyle('display', 'none');
			return false;	
		}.bind(this))
		
		//for when the user clicks on the background.
		this.coverNoResults.addEvent('click', function() {
			this.filterBar.setStyle('display', 'block');
			this.noSearchResults.setStyle('display', 'none');
			this.coverNoResults.setStyle('display', 'none');
		}.bind(this))
		
    }
    
    // TRANSITION OPTIONS
    // Set properties for tweening the filter drop down
    this.fileDropDown.set('tween', {
      duration: 500,
      transition: Fx.Transitions.Quart.easeOut,
      onComplete: function(e) {
		if (this.toggleFilterBar.className.indexOf(strFilterToggle) != -1) {
		    el.getElements('form.browseFilter ul').setStyle('overflow', 'auto');
		    el.getElement('div#coverContent').setStyle('display', 'block');
		} else {
			if (lteIE7) {
				if (this.middleContent.getElements('select') != "")
					this.middleContent.getElements('select').setStyle('display', 'inline');
				
				if (this.bottomContent.getElements('select') != "")
					this.bottomContent.getElements('select').setStyle('display', 'inline');
			}
				
		}
      }.bind(this)
    });
	
	// If you click the filter bar, the content covering div or the 'advanced' search link,
	// the browse filter menu will drop down
	
	// Only toggle filter bar if not on the stand alone search page
	if (el.getElements('div.toggleable') != "") {
	    this.toggleFilterBar.addEvent('click', this.toggleMainFilter.bind(this));
    	this.artTermsBar.addEvent('click', this.toggleArtTermsFilter.bind(this));
	    this.advancedLink.addEvent('click', this.toggleAdvancedFilter.bind(this));
	    this.coverContent.addEvent('click', this.toggleFilter.bind(this));
	    // TODO: Not opening main options
	    // this.coverFilterContent.addEvent('click', this.toggleMainFilter.bind(this));
	}
  },
    
  // Function that toggle the filter drop down menu, and
  // hides the rest of the website behind a semi opaque div
  toggleFilter: function() {
	// Use toggle class to keep track of whether the filter bar is expand or not
	this.toggleFilterBar.toggleClass(strFilterToggle);

	// If the drop down is closed, then show it and hide the main content
	// Else close the drop down and show the main content
	if (this.toggleFilterBar.className.indexOf(strFilterToggle) != -1) {
		this.coverContent.setStyle('display', 'block');
	
		// Elements that may not appear on the page
		if (this.tabbedInfo) {
			this.tabbedInfo.setStyle('overflow', 'hidden');
		}
	    if (this.thumbnailSlider) {
	    	this.thumbnailSlider.setStyle('overflow', 'hidden');
	    }
	    
	    // Animate the browse filter to its preset height
	    this.fileDropDown.tween('height', browseFilterHeight);
	    // IE6 bug fix: Hide drop downs when the browse filter is expanded
		if (lteIE7) {
		    if (this.minContentHeight != "")
			    this.middleContent.setStyle('height', this.minContentHeight);
			    
			if (this.middleContent.getElements('select') != "")
				this.middleContent.getElements('select').setStyle('display', 'none');
				
			if (this.bottomContent.getElements('select') != "")
				this.bottomContent.getElements('select').setStyle('display', 'none');
		}

	    this.filterStatus.set('html', '<strong class="filter">Close Filter</strong>');
	    this.filterStatus.addClass('open');
	    this.filterStatus.removeClass('closed');
	} else {
		this.coverContent.setStyle('display', 'none');
	    this.filterLists.setStyle('overflow', 'hidden');
	    // Elements that may not appear on the page
	    if (this.tabbedInfo) {
	    	this.tabbedInfo.setStyle('overflow', 'auto');
	    }
	    if (this.thumbnailSlider) {
	    	this.thumbnailSlider.setStyle('overflow', 'auto');
	    }

		// Close up the browse filter
		this.fileDropDown.tween('height', 0	);
		this.filterStatus.set('html', '<strong class="filter">Browse Filter</strong>');
	    this.filterStatus.addClass('closed');
	    this.filterStatus.removeClass('open');

		// If mainContent height has been fixed, then revert back to 'auto'
		if ((lteIE7) && (this.middleContent.getStyle('height').toInt() == minContentHeight))
			this.middleContent.setStyle('height', 'auto');
	}

  },
  
  // Function that sets the browse filter behaviour based if the art terms header button is pressed
  toggleAdvancedFilter: function(e) {
    e = new Event(e);
    e.preventDefault();
    
	this.toggleFilter();
  },
  
  // Function that sets the browse filter behaviour based if the art terms header button is pressed
  toggleArtTermsFilter: function(e) {
    e = new Event(e);
    e.preventDefault();

	if (this.fileDropDown.getStyle('height') == "0px") {
		this.toggleFilter();
	} else if ((this.coverFilterContent.getStyle('display') == "block") && (this.artTermsBar.getProperty('class').indexOf("selected") > 0)) {
		this.toggleFilter();
	}

	if (this.artTermsBar.getProperty('class').indexOf("selected") == -1) {
		this.artTermsBar.setProperty('class',this.artTermsBar.getProperty('class') + ' selected');
	}
	this.toggleFilterBar.removeClass('selected');
  },
  
  // Function that sets the browse filter behaviour based if the main filter bar is pressed
  toggleMainFilter: function(e) {
    e = new Event(e);
    e.preventDefault();

	if (this.fileDropDown.getStyle('height') == "0px") {
		this.toggleFilter();
	} else if ((this.coverFilterContent.getStyle('display') == "none") && (this.toggleFilterBar.getProperty('class').indexOf("selected") != -1)) {
		this.toggleFilter();
	}
	
	if (this.toggleFilterBar.getProperty('class').indexOf("selected") == -1) {
		this.toggleFilterBar.setProperty('class',this.toggleFilterBar.getProperty('class') + ' selected');
	}
	this.artTermsBar.removeClass('selected');	
  }
  
  
  
    
});

// Class that deals with the collection nav search form behavior
MoMA.Search = new Class({
	initialize: function(el) {
		this.el = el;
		this.searchField = el.getElement('input#searchAll');
		
		this.searchFieldBehaviour(this.searchField);

	},
	
	searchFieldBehaviour: function(rollOverItem) {
		this.searchField.setAttribute('value', searchFieldValue);
		this.searchField.addEvent('focus', function(){
			$(this).setAttribute('value', '')
		});
		
		this.searchField.addEvent('blur', function(){
			if ($(this).getAttribute('value') == "") {
				$(this).setAttribute('value', searchFieldValue)
			}
		});
	}
	
	
});

MoMA.ClearForm = new Class({
	initialize: function(el) {
		this.el = el;
		this.resetForm(el.getElement('a#filterReset'), el.getElement('form#browseFilter'));
	},
	
	resetForm: function(buttonToRest, formToRest) {
		buttonToRest.addEvent('click', function(){
			formToRest.getElements('table.browseFilterTable td label').removeClass('selected');
			formToRest.getElements('table.browseFilterTable td input').setProperty('checked', false);
			
			var formHeaders = formToRest.getElements('table.browseFilterTable th');
			formHeaders.each(function(formHeader){
				formHeader.getElement('label').setProperty('class', 'selected');
				formHeader.getElement('input').setProperty('checked', true);
			});
			finalSelectedArtists = new Array();
	    	
	    	// Cross browser method for auto clicking a button on the page
	    	// NOTE: Used this as AJAX commands are in another Class
	    	var firstLetter = document.getElementById("firstLetter");
			if (typeof firstLetter.click != 'undefined') {
	            firstLetter.click();
	            return false;
	        } else if(document.createEvent) {
	            var evObj = document.createEvent('MouseEvents');
	            evObj.initEvent('click',true,true);
	            firstLetter.dispatchEvent(evObj);
	            return false;
	        }
	        else if(document.createEventObject) {
	            firstLetter.fireEvent('onclick');
	       	    return false;
	        } else {
	            firstLetter.click();
	            return false;
	        }
		})
	}
	
});

MoMA.RollOverStates = new Class({

	initialize: function(el) {
		if (lteIE7) {
		    // Grab content for IE6
		    this.el = el;
		    if (el.getElements('div.tabs ul li.dropdown') != "")
			    this.rollOverIE(el.getElements('div.tabs ul li.dropdown'))
			    
			if (el.getElements('ul#thumbnail-view-items li') != "")
			    this.rollOverIE(el.getElements('ul#thumbnail-view-items li'))
			
			if (el.getElements('ul#list-view-items li') != "")
			    this.rollOverIE(el.getElements('ul#list-view-items li'))
			    
			if (el.getElements('ul.list-view-items li') != "")
			    this.rollOverIE(el.getElements('ul.list-view-items li'))
			    
			if (el.getElements('div#filterOptions') != "")
			    this.rollOverIE(el.getElements('div#filterOptions'))
		}

		this.imageRollOver = el.getElements('ul#thumbnail-view-items li div.shadow div.description a img');
		this.verticalAlignment(this.imageRollOver);
		
		// Grab Content
		if (el.getElement('input#filterSubmit') != "")
		    this.rollOverImageSubmit(el.getElement('input#filterSubmit'), "images/collection/search-RO.gif");
	},
	
	// Function that adds class on hover for IE6 styles
	rollOverIE: function(rollOverItems) { 	
		rollOverItems.each(function(thumbnail){
	    	thumbnail
		  	.addEvent('mouseover', function() {
				this.addClass('hoverState');
			})
		    .addEvent('mouseout', function() {
		    	this.removeClass('hoverState');
		    })
		});
    },
    
   	// Function that takes an image submit button, and changes its source on rollover
    rollOverImageSubmit: function(rollOverButton, rollOverSource) { 	
    	var oldSrc = rollOverButton.getAttribute('src');
    	rollOverButton
		.addEvent('mouseover', function() {
			this.setAttribute('src', rollOverSource);
		})
		.addEvent('mouseout', function() {
		  	this.setAttribute('src', oldSrc);
		})
    },
    
	verticalAlignment: function(images) {
    		//alert('found ' + images.length + ' images')
        	images.each(function(image){
        		var remainder_height = 170 - image.get('height');
        		image.setStyle('margin-top', remainder_height / 2);
        		image.setStyle('margin-bottom', remainder_height / 2);
        	});
        }
   
});

// Class that deals with any functionality for link behavior
MoMA.LinkFunctions = new Class({
	initialize: function(el) {
		if ($chk(el.getElements('ul#list-view-items li'))) {
    		this.listViewItems = el.getElements('ul#list-view-items li');
		    this.blockLink(this.listViewItems, 0);
	    }
	    
	    if ($chk(el.getElements('ul.list-view-items li'))) {
    		this.listViewItems = el.getElements('ul.list-view-items li');
		    this.blockLink(this.listViewItems, 0);
	    }
	    
		if ($chk(el.getElements('ul#thumbnail-view-items li'))) {
    		this.thumbnailViewItems = el.getElements('ul#thumbnail-view-items li');
		    this.blockLink(this.thumbnailViewItems, 1);
	    }
	},
	
	// Function that makes an entire element linkable,
	// based on a specific link within that div
	blockLink: function(linkItems, linkNumber) {
		linkItems.setStyle('cursor','pointer');
		
		linkItems.each(function(linkItem){
			var primaryLink = linkItem.getElements('a')[linkNumber];
			linkItem.addEvent('click', function() {
				document.location = primaryLink.getAttribute('href');
			});
		});
	}
});

// Class that deals with any functionality for link behavior
MoMA.Banner = new Class({
	initialize: function(el) {
		
		if ($chk(el.getElements('div.banner_info'))) {
			
    		this.listViewItems = el.getElements('div.banner_info');
    		this.listViewItems.setStyle("display", "block");
    		
    		
		    this.listViewItems.getElement("a").addEvent('mouseover', function() {
		    	this.listViewItems.getElement("p").setStyle('visibility', 'visible');
		    }.bind(this));
		    
		    this.listViewItems.getElement("p").addEvent('mouseout', function() {
		    	this.listViewItems.getElement("p").setStyle('visibility', 'hidden');
		    }.bind(this));
	    }
	}
});


/* 
	DEFER: May reinstate for the 'about this department' popover
	MoMA.PopOvers = new Class({

	initialize: function(el) {
	    // Grab content
	    this.el = el;

	    this.showContent(
	    	el.getElement('.readText'),
	    	el.getElement('.info_box_wrap'),
	    	el.getElement('div.close_box a')
	    );
	},
	
	// Take in an activate button, a revealing element and an optional close element
	showContent: function(activateLink, showItem, closeButton) {	
		activateLink.addEvent('click', function() {
			if (showItem.getStyle('display') == "none") {
				showItem.setStyle('display', 'block');
			} else {
				showItem.setStyle('display', 'none');
			}
		});
		
		closeButton.addEvent('click', function() {
			showItem.setStyle('display', 'none');
		});
    }
   
}); */