if (typeof(Trailers) == 'undefined') Trailers = {};

Trailers.Dropdown = Class.create({});
Object.extend(Trailers.Dropdown.prototype, Event.Listener);
Object.extend(Trailers.Dropdown.prototype, {
	duration: .5,
	handleClassName: 'dropdown-handle',
	overlayClassName: 'dropdown-overlay',
	eventName: 'click',

	initialize: function(button, overlay, docClick) {
		this.lastScrollState = 0;
		if(AC.Detector.isIE()) this.duration = .25;
		this.docClick = docClick;
		this.button = button;
		this.overlay = overlay;
		this.container = overlay.up();
		if (!this.button || !this.overlay) return;
		//if (this.button.tagName.toLowerCase() != 'a' && !this.button.down('a')) return;
		
		/*this._escape = Event.observe(window, 'keyup', function(evt) {
			if (evt.keyCode == Event.KEY_ESC) this.close();
		}.bind(this));*/
		
		this.eventListeners();
	},

	handleDocClick: function(evt) {
		//if (!(evt.findElement('.'+this.handleClassName)) && !(evt.findElement('.'+this.overlayClassName))) {
		if (!(evt.findElement('.'+this.overlayClassName))) {
			this.close();
		}
	},

	toggle: function(evt) {
		evt.stop();

		if (!this.overlay.visible()) {
			this.open();
		} else {
			this.close();
		}
	},

	open: function() {
		//if(hasScrollbar && this.docClick) this.overlay.up().absolutize();
		if(dropdownTimer) window.clearTimeout(dropdownTimer); dropdownTimer = null;
		this.close();

		this.button.addClassName('active');

		if (!this.overlay.visible()) {
			this.overlay.appear({
				duration: this.duration,
				afterFinish: function() {
					if(this.docClick) {
						this.docClickEvent = this.handleDocClick.bindAsEventListener(this);
						document.body.observe(this.eventName, this.docClickEvent);
						
					}
				}.bind(this)
			});
		}
	},
	
	eventListeners: function(){
		this.button.observe(this.eventName, this.toggle.bind(this));
		
		if(this.docClick){
			this.listenForEvent(AC.OverlayPanel.overlay, 'afterPop', false, function(){this.close();}.bind(this));

			if(AC.targetQuicktimeplayer){
				this.listenForEvent(AC.targetQuicktimeplayer, 'lanchedQTPlayer', false, function(e){
					//try { console.info('QuickTime Player has been launched');} catch(e){}
					
					this.close();
				}.bind(this));
			} else if(AC.targetiTunesDownload){
				this.listenForEvent(AC.targetiTunesDownload, 'iTunesDownload', false, function(e){
					//try { console.info('iTunes anonymous download has been initiated');} catch(e){}
					
					this.close();
				}.bind(this));
			}	

			if(hasScrollbar) {
				this.listenForEvent(scrollbar.scrollbar, 'scrollChange', false, function(e){
					scrollOffset = e.event_data.data;
					if(scrollOffset == null) scrollOffset = 0;

					if(scrollOffset != this.lastScrollState){
						this.lastScrollState = scrollOffset;
						if (this.overlay.visible()) this.close(.05);
					}
				}.bind(this));
			}
		}
	},

	close: function(durationOveride) {
		if(durationOveride >= 0) { duration = durationOveride; } else { duration = this.duration; }
		if (this.docClick) {
			document.body.stopObserving(this.eventName, this.docClickEvent);
			this.docClickEvent = null;
			this.hide(duration);
		}

		Trailers.Dropdown.lists.each(function(overlay) {
			overlay.hide(duration);
		});

		Trailers.Dropdown.containers.each(function(overlay) {
			if(this.overlay === overlay.overlay){
				overlay.hide(duration);
			}
		}.bind(this));
	},

	hide: function(duration) {
		this.button.removeClassName('active');

		if (this.overlay.visible()) {
			if(duration == 0) { this.overlay.hide(); }
			else { this.overlay.fade({ duration: duration }); }
		}
	}
});

Trailers.Dropdown.lists = [];
Trailers.Dropdown.containers = [];

