document.write('<script src="http://www.apple.com/library/quicktime/scripts/ac_quicktime.js" language="JavaScript" type="text/javascript"></script>');
if ($chk(twopixels)==false) {
	var twopixels={};
};

twopixels.qtmovie = new Class({
	initialize: function() {
	
	},
	
	initMovies: function() {
		this.movies=this.findMovies();
		this.movies.each(this.replaceWithPlayer.bind(this));
	},
	
	replaceWithPlayer: function(item) {
		var width=item.getProperty('width');
		var height=item.getProperty('height');
		var src=item.getProperty('src');
		var parent = item.parentNode.parentNode;
		item.parentNode.parentNode.removeChild(item.parentNode);
		this.putPlayer(width,height,src,parent);
	},
	
	putPlayer: function(width, height, src, where) {
		this.playerDiv=new Element('div', {styles: {
			'width': width.toInt()+'px',
			'height': height.toInt()+20+'px',
			'position': 'relative'
		}}).inject(where);
		if (window.gecko) {
			var str=QT_GenerateOBJECTText_XHTML(src,width,height.toInt()+20,
			'',
			'scale', 'aspect',
			'autoplay','false',
			'controller','true');
			this.playerDiv.innerHTML=str;
			this.player={};
			return;
		};
		this.buttonBar = new Element('div', {styles: {
			width: width.toInt()+'px',
			height: '20px',
			position: 'absolute',
			bottom: '0',
			left: '0'
		}}).inject(this.playerDiv);
		this.playPause = new Element('div', {styles:{
			width: '20px',
			height: '20px',
			background: 'transparent url(img/play.png) no-repeat center center'
		}}).inject(this.buttonBar);
		this.createTimeBars();
		
		var tmp=new Element('div');
		var str=QT_GenerateOBJECTText_XHTML(src,width,height,
		'',
		'scale', 'aspect',
		'autoplay','false',
		'controller','false');
		tmp.innerHTML=str;
		tmp.inject(this.playerDiv);
		
		if (window.opera || window.webkit) {
			this.player=$E('embed', this.playerDiv);
		};
		if (window.ie) {
			this.player=$E('object', this.playerDiv);
		};
		this.waitForLoad();
	},

	createTimeBars: function() {
		this.timeBarBack = new Element('div', {styles: {
			width: this.buttonBar.getStyle('width').toInt()-15+'px',
			height: '20px',
			position: 'absolute',
			left: '18px',
			top: '0',
			overflow: 'hidden',
			background: 'transparent url(img/left.png) no-repeat left center'
		}}).inject(this.buttonBar);
		new Element('div', {styles: {
			height: '20px',
			width: this.buttonBar.getStyle('width').toInt()-15+'px',
			background: 'transparent url(img/right.png) no-repeat right center'
		}}).inject(this.timeBarBack);
		new Element('div', {styles: {
			height: '20px',
			width: this.timeBarBack.getStyle('width').toInt()-26+'px',
			left: '13px',
			top: '0',
			position: 'absolute',
			background: 'transparent url(img/center.png) repeat-x center center'
		}}).inject(this.timeBarBack);
		this.timeBarBack.setOpacity(0.6);
		this.timeBar = this.timeBarBack.clone();
		this.timeBar.setOpacity(1);
		this.timeBar.setStyle('width', 0);
		this.timeBar.inject(this.buttonBar);
		this.dotBar=new Element('div', {styles: {
			width: this.buttonBar.getStyle('width').toInt()-15+'px',
			height: '20px',
			position: 'absolute',
			left: '18px',
			top: '0'
		}}).inject(this.buttonBar);
		this.dot= new Element('div', {styles: {
			width: '13px',
			height: '20px',
			background: 'transparent url(img/dot.png) no-repeat center center',
			position: 'absolute',
			left: '3px',
			top: '0',
			zIndex: '5000'
		}}).inject(this.dotBar);
	},
	
	placeDot: function() {
		if (this.player.GetRate()!=0) {
			this.playPause.setStyle('background', 'transparent url(img/pause.png) no-repeat center center');
		} else {
			this.playPause.setStyle('background', 'transparent url(img/play.png) no-repeat center center');
		};
		this.dot.setStyle('left', 3+(this.player.GetTime()/this.player.GetDuration()*(this.timeBarBack.getStyle('width').toInt()-this.dot.getStyle('width').toInt()-7)).round()+'px');
	},
	
	playPauseClick: function() {
		if (this.player.GetRate()==0) {
			this.player.Play();
		} else {
			this.player.Stop();
		};
	},
	
	waitForLoad: function() {
		if ($chk(this.player.GetDuration)) {
			this.loadObserver();
			this.playPause.addEvent('click', this.playPauseClick.bindAsEventListener(this));
			this.dotPlacer=$clear(this.dotPlacer);
			this.dotPlacer=this.placeDot.periodical(300,this);
			this.activateDragging();
		} else {
			this.waitForLoad.delay(100,this);
		};
	},
	
	activateDragging: function() {
		this.dot.addEvent('mousedown', this.dragStart.bindAsEventListener(this));
		this.dotBar.addEvent('mousedown', this.timeClick.bindAsEventListener(this));
	},
	
	timeClick: function(event) {
		event = new Event(event);
		event.stop();
		event.preventDefault();
		
		var where=event.page.x-this.timeBar.getLeft()-7;
		if (where<0) where=0;
		
		var max=this.timeBarBack.getStyle('width').toInt()-this.dot.getStyle('width').toInt()-7;
		this.player.SetTime(where/max*this.player.GetDuration());
		this.placeDot();
	},
	
	dragStart: function(event) {
		event = new Event(event);
		event.stop();
		event.preventDefault();		
		this.dotPlacer=$clear(this.dotPlacer);
		this.startmousex=event.client.x;
		this.startdotx=this.dot.getStyle('left').toInt();
		this.dragStopAction=this.dragStop.bindAsEventListener(this);
		this.dragMoveAction=this.dragMove.bindAsEventListener(this);
		window.addEvent('mousemove', this.dragMoveAction);
		window.addEvent('mouseup', this.dragStopAction);
	},
	
	dragMove: function(event) {
		event = new Event(event);
		event.stop();
		event.preventDefault();
		var delta=event.client.x-this.startmousex;
		var targetx=this.startdotx+delta;
		if (targetx<3) {
			targetx=3;
		} else if (targetx>this.timeBar.getStyle('width').toInt()-this.dot.getStyle('width').toInt()-4) {
			targetx=this.timeBar.getStyle('width').toInt()-this.dot.getStyle('width').toInt()-4;
		};
		this.dot.setStyle('left', targetx+'px');
	},
	
	dragStop: function(event) {
		event = new Event(event);
		event.stop();
		event.preventDefault();
		window.removeEvent('mousemove', this.dragMoveAction);
		window.removeEvent('mouseup', this.dragStopAction);
		var place=this.dot.getStyle('left').toInt()-3;
		var max=this.timeBarBack.getStyle('width').toInt()-this.dot.getStyle('width').toInt()-7;
		this.player.SetTime(place/max*this.player.GetDuration());
		this.dotPlacer=$clear(this.dotPlacer);
		this.dotPlacer=this.placeDot.periodical(300,this);
	},
	
	loadObserver: function() {
		var length=this.player.GetDuration();
		var loaded=this.player.GetMaxTimeLoaded();
		this.timeBar.setStyle('width', (loaded/length*this.timeBarBack.getStyle('width').toInt()).round()+'px');
		if (length!=loaded) {
			this.loadObserver.delay(100,this);
		};
	},
	
	findMovies: function() {
		return $$('embed').filter(function(item){return item.getProperty('pluginspage')=='http://www.apple.com/quicktime/download/'});
	}
});
