var ArtistRooms = new Class({

	/* initialize needed methods
	 ======================================================================= */
	initialize: function() {
		this.videoCont = $('ar-video-cont');
		this.tabNavEl = $('ar-tab-nav');
		this.mapSmall = $('ar-small-map');
		this.mapBig = $('ar-large-map');
		this.pins = $$('.arsmItem');
		
		this.start();
	},

	/* attach events and add the initially fired methods
	 ======================================================================= */
	start: function() {	
		if($chk(this.mapSmall) || $chk(this.mapBig)) this.manageMap();
		if($chk(this.videoCont)) this.toggleVideo();
		this.sortTables();
	},
	
	manageMap: function() {
		var activeHighlight = this.pins.some(function(item){
			return item.getElement('span').hasClass('arPinHighlight');
		});
		
		var timer;

		this.pins.each(function(el, i, array) {

			var pin = el;
			var spot = el.getElement('.arPin');
			var tip = el.getElement('ul');
			tip.set('tween', {duration: 150, transition: Fx.Transitions.sineOut});
			
			if(activeHighlight) {
				(function() {
					if(!spot.hasClass('arPinHighlight')) {
						if(Browser.Engine.trident5) {
							spot.removeClass('arPin');
							spot.addClass('arPinNa');
						} else {
							spot.fade(0.5);
						}
					}
				}).delay(400);
			}

			pin.addEvents({
				mouseenter: function(e) {
					tip.set('styles', {
						'display': 'block',
						'opacity': 0
					});
					tip.tween('opacity', [0, 0.9]);
				},
				mouseleave: function(e) {
					(function(){
						tip.setStyle('display', 'none');
					}).delay(100);
					tip.tween('opacity', [0.9, 0]);
				}
			});
		});
	},
		
	toggleVideo: function() {
		var initHeight = $('arvc').getSize().y;
		var showVideo = $$('#arin-video a');

		showVideo.addEvent('click', function(e) {
			e.stop();

			this.videoCont.tween('height', this.videoCont.getSize().y < 10 ? initHeight : 0);
		}.bind(this));
	},
	
	sortTables: function(){
		var sortableEl = $('works-table');
		
		if (!sortableEl) return false;
		
		// define my own parser for the accession number
		HtmlTable.Parsers.accession = {
			match: null,
			convert: function() {
				return this.get('text');
			},
			number: true
		};

		new HtmlTable(sortableEl, {
			classHeadSort: 'current',
			sortable: true,
			sortIndex:  window.location.href.indexOf('ar_home') > 0 ? 3 : 1,
			classNoSort: 'noSort',
			parsers: ['string', 'title', 'title', 'title', 'accession']
		});
	}
});
