frontRowSection = Class.create();
Object.extend(Object.extend(frontRowSection.prototype, AC.FrontRowSection.prototype), {

	activate: function() {
		Element.addClassName(this.trigger, 'active');
		this.trigger.innerHTML = this.trigger.innerHTML.replace(/Watch it/, 'Now Playing');
	},
	
	deactivate: function() {
		Element.removeClassName(this.trigger, 'active');
		this.trigger.innerHTML = this.trigger.innerHTML.replace(/Now Playing/, 'Watch it');
	}
	
});

QuicktimeGallery = Class.create();
QuicktimeGallery.prototype = {
	initialize: function() {
		// DOM
		var displayPanel = $('quicktime');
		var controller = 'quicktimecontroller';
		//var descriptionPanel = $('descriptionpanel');
		var descriptionPanel = $('video-controls-bottom');
		//var gallerySections = $('content').getElementsByClassName('section');
		var gallerySections = $('contentcontainer').getElementsByClassName('section');

		// if no qt...
		if (!AC.Detector.isQTInstalled()) {
			displayPanel.innerHTML = '<a href="/go/quicktime/"><img src="http://images.apple.com/finalcutstudio/images/quicktime_required20070414.gif" alt="QuickTime 7 Required." width="640" height="360" border="0"></a>';
		}

		// figure out the default section
		var initialMovie = document.location.search.toQueryParams()['movie'];
		
		
		if (!initialMovie && defaultId) initialMovie = defaultId;
		
		

		var initialSection = null;

		//create sections to populate frontrow with
		var sections = [];
		for(var i = 0; i < gallerySections.length; i++) {
			if (gallerySections[i].hasAttribute('id')) {
				var movieLinks = gallerySections[i].getElementsByClassName('movielink');
				var movieUrl = movieLinks[0].href;
				var title = movieLinks[0].innerHTML;
				var description = gallerySections[i].getElementsByClassName('description');
				description = (description.length>0) ? gallerySections[i].getElementsByClassName('description')[0] : title;
	
				var id = gallerySections[i].id.replace(/^mov-/, '');
				
				//replace href of all the movielinks 
				for (var j=0; j<movieLinks.length; j++){
					movieLinks[j].setAttribute('href', '?movie=' + id);
				};
				if (i == 1) {
					initialMovie = id;
				}
				var newSection = new frontRowSection(gallerySections[i], 'object'+i, movieUrl, description);
				sections.push(newSection);
				//if this is the requested movie, be sure to show it first
				if (initialMovie == id) {
					initialSection = newSection;
				}
			}
		}
		// set the height & width
		var moviewidth = 640;
		var movieheight = 360;

		// if we're Opera, use the standard movie controller, otherwise attach movie controller
		if (AC.Detector.isOpera()) {
			var controllerstatus = true;
			$(controller).style.display = 'none';
			movieheight += 16;
			displayPanel.style.width = moviewidth+'px';
		} else {
			var controllerstatus = false;
			var movieController = new AC.QuicktimeController();
			movieController.render(controller);
		}

		// package movie
		var movie = new AC.Quicktime.packageMovie('gallery-movie', initialSection.movieUrl, {
			width: moviewidth,
			height: movieheight,
			autostart: true,
			controller: controllerstatus,
			showlogo: false,
			cache: true,
			bgcolor: '#000000'
		});

		// sort of initialize frontrow
		displayPanel.innerHTML = '';

		var frontrow = new AC.FrontRow(movie, displayPanel, descriptionPanel, sections, 
			{controller: movieController});
			
		frontrow.show(initialSection);

		if (movieController) {
			movieController.attachToMovie(movie);
			movieController.monitorMovie();
		}
		
	}
}

// LOCAL
Event.observe(window, 'load', function() { new QuicktimeGallery(); }, false);
