/* popup */
var PopupTour = Class.create();
Object.extend(Object.extend(PopupTour.prototype, PopupGallery.prototype), {
/*
	setImageAttributes: function() {
 		for (var i=0; i<this.items.length; i++) {
 			var item = this.items[i];
 			var movielinks = item.getElementsByClassName('movielink');
	 		item.movieUrl = movielinks[0].href;
			item.id = movielinks[0].target;
			item.description = item.getElementsByClassName('description')[0];
			item.title = item.description.getElementsByTagName('h3')[0].getElementsByTagName('img')[0].alt;
			item.title = item.title.substring(0, item.title.indexOf('.'));
			var posterFrameSpan = item.getElementsByClassName('posterFrame')[0];
 			item.posterFrameUrl = posterFrameSpan.innerHTML.match(/src="(.*)"/)[1];
 			
 			this.setEvent(item, i);
 		}
 		var tourbutton = $('tourbutton');
		Event.observe(tourbutton, 'click', this.onClick.bindAsEventListener(this, this.items[0], 0), false);
	},
*/
 	movieLinks: function(i) {
 		if (AC.Detector.isOpera()) {
 			this.previous = true;
 			this.next = true;
 		} else {
			if (!this.next && !this.previous) {
				this.previous = Builder.node('a', {'class':'previous'}, 'Previous');
				this.next = Builder.node('a', {'class':'next'}, 'Next');
				var links = Builder.node('div', {'class':'links'}, [this.previous, this.next]);
				this.controllerPanel.parentNode.appendChild(links);
			}
			this.setMovieLinks(i);
		}
	},

 	setMovieLinks: function(i) {
		var pindex = (i==0) ? this.items.length-1 : i-1;
		var previous = this.items[pindex];
		this.previous.innerHTML = previous.title;
		this.previous.href = previous.movieUrl;
		this.previous.onclick = function(e) {
			//this.frontrow.show(previous.section);
			this.descriptionPanel.innerHTML = previous.description.innerHTML;
			if (this.movieController) this.movieController.SetURL(previous.movieUrl);
			this.setMovieLinks(pindex);
			return false;
		}.bind(this, previous, pindex)

		var nindex = (i==this.items.length-1) ? 0 : i+1;
		var next = this.items[nindex];
		this.next.innerHTML = next.title;
		this.next.href = next.movieUrl;
		this.next.onclick = function(e) {
			//this.frontrow.show(next.section);
			this.descriptionPanel.innerHTML = next.description.innerHTML;
			if (this.movieController) this.movieController.SetURL(next.movieUrl);
			this.setMovieLinks(nindex);
			return false;
		}.bind(this, next, nindex)
	},

 	popMovie: function(evt, item, i) {
 		// reset the image stuff
		this.popup.onclick = '';

		// we're the movie so look like it
 		Element.addClassName(this.popup, 'video');
 		if (AC.Detector.isQTInstalled()) Element.addClassName(this.popup, 'tour');
 		
		// movie size
		var width = 720; //980;
		this.padleft = (this.popup.offsetWidth-width);
		var height = (AC.Detector.isQTInstalled()) ? 544 : 488;
		this.padtop = (this.popup.offsetHeight-height);

		// if we're Opera, use the standard movie controller, otherwise attach movie controller
		var movieheight = 416;
		if (AC.Detector.isFirefox() || AC.Detector.isOpera()) {
			var controllerstatus = true;
			movieheight += 16;
		} else {
			var controllerstatus = false;
		}

		// initalize the movie
		if (!AC.Detector.isiPhone() && AC.Detector.isQTInstalled()) {
			this.movieController = new AC.QuicktimeController();
		}

		// set up what we're going to do after the effect
		var after = function() {
			Element.removeClassName(this.popup, 'isanim');
			Element.addClassName(this.popup, 'popped');
			// set the description
			this.descriptionPanel.innerHTML = item.description.innerHTML;

			if (AC.Detector.isQTInstalled()) {

				var movie = AC.Quicktime.packageMovie('movie', item.movieUrl, {
						width: 672,
						height: movieheight,
						posterFrame: item.posterFrameUrl,
						controller: controllerstatus,
						showlogo: false,
						cache: true
				});

				this.displayPanel.appendChild(movie);

				if(!AC.Detector.isiPhone()) { this.movieLinks(i); }
	
				if (!AC.Detector.isFirefox() && 
					!AC.Detector.isOpera() && 
					!AC.Detector.isiPhone()) { 
					this.movieController.render(this.controllerPanel); 
				}
				
				if (this.movieController) {
					this.movieController.attachToMovie(movie);
					this.movieController.monitorMovie();
				}
				
				var movie = null;			
			} else {
				this.displayPanel.innerHTML = '<a href="/quicktime/download/"><img src="http://images.apple.com/macosx/leopard/images/popup_quicktimerequired20070611.gif" alt="QuickTime 7 Required." width="672" height="438" border="0" class="getquicktime"></a>';
			}
		}.bind(this)

		// set the position of the image according to the height/width we just found
		var left = this.windowSize().x+(this.windowSize().width-width)/2;
		if (left<this.closeBtn.offsetWidth/2) left = this.closeBtn.offsetWidth/2;
		var top = this.windowSize().y+(this.windowSize().height-height)*.6;
		if (top<this.padtop/4) top = this.padtop/4;

		// call the effect
		this.pop(width, top, height, left, after);
 	}

});

/* local */
Event.observe(window, 'load', function() {
	var images = [
		'popup_movie_bgfrontrow20070611.png',
		'popup_movie_next20070611.gif',
		'popup_movie_previous20070611.gif'
	];
	for (var i=0; i<images.length; i++) {
		var image = new Image();
		image.src = images[i];
	}

	new PopupTour('toursection');
}, false);
