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

videoChooser = Class.create();
Object.extend(videoChooser.prototype, Event.Listener);
Object.extend(videoChooser.prototype, Event.Publisher);
Object.extend(videoChooser.prototype, {

	duration: .5,
	moviePanelSelector: '.moviePanel',
	overlayPanelID: 'OverlayPanel',
	videoChooserID: 'videoChooser',
	eventName: 'mousemove',
	isFirefox: AC.Detector.isFirefox(),
	isWebKit: AC.Detector.isWebKit(),
	linkLimit: 3, // For large width overlays
	linkLimitMedium: 2, // For medium width overlays

	initialize: function(overlay) {
		this.alwaysShowChooser = 0;

        qtEventSource = document.getElementsByTagName('body')[0];
		Event.observe(qtEventSource, 'QuickTime:begin', function(){
			this.alwaysShowChooser = 0;
			if(!this.isWebKit && $(this.videoChooserID)) $(this.videoChooserID).setStyle({'opacity':0});
			if(!this.isWebKit && $(this.videoChooserID)) $(this.videoChooserID).hide();
		}.bind(this));
		Event.observe(qtEventSource, 'QuickTime:end', function(){
			this.alwaysShowChooser = 1;
			if(!this.isWebKit && $(this.videoChooserID)) $(this.videoChooserID).setStyle({'opacity':1});
			if(!this.isWebKit && $(this.videoChooserID)) $(this.videoChooserID).show();
		}.bind(this));

		this.listenForEvent(overlay, 'afterPop', false, this.hoverTrackerStart);
		this.listenForEvent(overlay, 'afterClose', false, this.hoverTrackerEnd);
	},

	hoverTrackerStart: function(){
		var toto = $('toto');
		if($(this.videoChooserID)) $(this.videoChooserID).setStyle({'display':'none'});

		if(AC.Detector._isQTInstalled){
			// This part needs to be calculated dynamically once we figure out how to implement video dimensions...
			if($(this.videoChooserID).hasClassName("videochooser-medium")) this.linkLimit = this.linkLimitMedium;
			else this.linkLimit = 3;
	
			if(this.isFirefox) $(this.overlayPanelID).addClassName('isFirefox');
			if(this.isWebKit) $(this.overlayPanelID).addClassName('isWebKit');
			
			this.moviePanelHover = this.handleHover.bindAsEventListener(this);
			document.observe(this.eventName, this.moviePanelHover);
			
			this.videoChooser = $(this.videoChooserID);
			this.overlayPanel = $(this.overlayPanelID);
			if(this.isWebKit && !this.videoChooser.visible()) this.videoChooser.show();
			
			// Sets the 'top' property on the video chooser so that it is right below the video.
			this.updateContainerHeight();
			this.listenForEvent(AC.ViewMaster, 'ViewMasterDidShowNotification', false, this.updateContainerHeight);
	
			this.links = $(this.videoChooserID).down('.container').childElements();
			if(this.links.length && (this.links.length>this.linkLimit)) this.slyderInit();
			else if(this.links.length == 1) $(this.overlayPanelID).addClassName('single');
		}
	},

	hoverTrackerEnd: function(){
		if($(this.videoChooserID)) $(this.videoChooserID).removeClassName('slyder');
		document.stopObserving(this.eventName, this.moviePanelHover);
		this.moviePanelHover = null;
	},
	
	handleHover: function(evt){
		if (
			(evt.findElement(this.moviePanelSelector)) ||
			(evt.findElement('#'+this.videoChooserID)) ||
			(evt.findElement('#OverlayPanel .close'))
		) {
			$(this.overlayPanelID).addClassName('hover');

			if(!this.alwaysShowChooser){
				$(this.overlayPanelID).removeClassName('force-show');	

				if((this.links.length && this.links.length != 1) && !this.videoChooser.visible() && !this.isWebKit) {
					this.videoChooser.appear({
						duration: this.duration,
						afterFinish: function() {
	
						}.bind(this)
					});
				}
			} else {
				$(this.overlayPanelID).addClassName('force-show');	
			}
		} else {
			$(this.overlayPanelID).removeClassName('hover');

			if(!this.alwaysShowChooser){
				$(this.overlayPanelID).removeClassName('force-show');	

				if(this.links && (this.links.length != 1) && this.videoChooser.visible() && !this.isWebKit) {
					this.videoChooser.fade({
						duration: this.duration,
						afterFinish: function() {
	
						}.bind(this)
					});
				}
			} else {
				$(this.overlayPanelID).addClassName('force-show');	
			}
		}
	},

	slyderInit: function(){
		if($(this.videoChooserID)) $(this.videoChooserID).setStyle({'display':'block'});
		if($(this.videoChooserID)) $(this.videoChooserID).addClassName('slyder');
		this.slider = new Slyder(this.videoChooserID, {showNav:false});
		//this.sliderSetLinkWidth();
	},
	
	sliderSetLinkWidth: function(){
		linkWidth = Math.floor(this.slider.pageWidth / Math.ceil(this.links.length/this.slider.pages.length));
		this.links.each(function(link){
			link.setStyle({'width':linkWidth+'px','marginRight':0});
		});
	},
	
	updateContainerHeight: function(evt){
		var embed = this.overlayPanel.down('embed');
		if(embed) { var top = embed.getAttribute('height'); }
		else {
			var object = this.overlayPanel.down('object');
			var video = this.overlayPanel.down('video');
			if(object) { var top = object.getAttribute('height'); }
			else if(video) { var top = video.getAttribute('height'); }
			else {
				var overlaycontent = this.overlayPanel.down('.overlaycontent');
				if(overlaycontent) var top = overlaycontent.getHeight();
				else var top = 0;
			}
		}
		if(top > 50){
			var moviepanel = this.overlayPanel.down('.moviePanel');
			if(moviepanel) moviepanel.setStyle({'height':top+'px'});
			var overlaycontent = this.overlayPanel.down('.overlaycontent');
			if(overlaycontent) overlaycontent.setStyle({'height':top+'px'});
			this.overlayPanel.setStyle({'height':top+'px'});
			this.videoChooser.setStyle({'top':top+'px'});
		}
	}
});


Event.onDOMReady(function() {
	AC.OverlayPanel.videoChooser = new videoChooser(AC.OverlayPanel.overlay);
});

