/** 
 * Antiques Roadshow Video Player
 * March 2009 - mitch_smith@wgbh.org
 *
 * aspect: x43, x169
 * filetype: qt, wm
 * bitrate: hi, lo, auto
 * split: fourway, fsplit, bsplit
 * 
**/

$.arPlayer = function(container,options) {
	this.opts = options;
	this.player_id = container[0].id;
	var defaults = {
		title: $('title').html(),
		file_stem: document.location.href.replace(/.*\/([^\/\.]+)\.html.*/g,'$1'),
		aspect: 'x169',
		filetype: 'qt',
		bitrate: 'hi',
		split: 'fourway',
		asset_path: '../../archive/assets/',
		asx_path: '/wgbh/roadshow/archive/media/',
		image_path: 'images/',
		salte: 'trunk_wide.jpg'
	};
	
	var choices = {
		fourway: ['qt_lo','qt_hi','wm_lo','wm_hi'],
		fsplit: ['wm_hi', 'qt_hi'],
		bsplit: ['qt_lo','qt_hi']
	};
	
	var suffix = {
		qt_hi: '_350.mov',
		qt_lo: '_100.mov',
		qt_auto: '_ref.mov',
		wm_hi: '_350.asx',
		wm_lo: '_036.asx',
		wm_auto: '_350.asx'
	};	
	
	if(container[0].title) {
		this.opts['file_stem'] = container[0].title;
	}
	
	if (container.hasClass('x43')) {
		this.opts['aspect'] = 'x43';
	}
	
	if (container.hasClass('bsplit')) {
		this.opts['split'] = 'bsplit';
	}
	
	if (container.hasClass('fsplit')) {
		this.opts['split'] = 'fsplit';
	}
	
	for (var key in defaults) {
		if (!this.opts[key]) {
			this.opts[key] = defaults[key];
		}
	}

/*

*/
	this.embed = function(embedtag) {
		try {
			this.watch = '#' + $('#watch')[0].id;
			}
		catch(err) {
			this.watch = '#player';
			}
			//alert('#watch_id: ' + this.watch);
		this.mediadiv = $(this.watch).replaceWith('<div id="' + this.opts.filetype + '" class="' + this.opts.aspect + '">' + embedtag + '<div id="player_controls"><a id="change_settings" style="float:right;" href="#">Change settings</a><a href="http://www.pbs.org/wgbh/roadshow/videohelp.html" target="_blank">Video not playing?</a></div></div>');		
		$('#player_controls').find('#change_settings').bind('click', function(e){
			e.preventDefault();
			$.player.change_settings();
		});
//		$('#'+this.opts.filetype).append('<p>' + this.filename + '</p>');
//		container.addClass('inactive');
		
	};

	this.play = function(choice) {
    $.cookie("ar_media_pref", choice, { expires: 999, path: '/'});
		var det = choice.split(/_/);
		this.filename = this.opts.file_stem + suffix[choice];
		this.opts.filetype = det[0];
		this.opts.bitrate = det[1];
		//alert('Requesting ' + this.filename);
		this.embed(this.make_embed(choice));
	};
	
	this.change_settings = function() {
		$.cookie("ar_media_pref", null, { path: '/' });
		$('#' + this.opts.filetype).replaceWith(container);
		document.location.href = document.location.href;
	};
	
	this.make_embed = function(ftbr) {
			var s = ftbr.split(/_/);
			var f = s[0];
			var a = '_wide';
			var h = 234;
			var w = 384;
			if (this.opts['aspect'] == 'x43') {
				a = '';
				h = 256;
				w = 320;
			}
			
			if (f == 'wm') {
				h += 56;
			}
			
			var templates = {
				qt: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + w + '" height="' + h + '" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="../video/media/ar_head' + a + '_hi.mov"><param name="autoplay" value="true"><param name="controller" value="true"><param name="cache" value="true"><param name="bgcolor" value="#FFFFFF"><param name="moviename" value="playback"><param name="qtnext1" value="<' + this.opts.asset_path + this.filename + '> T<myself>">	<param name="qtnext2" value="<../../video/media/ar_tail' + a + '_hi.mov> T<myself>">	<param name="saveembedtags" value="true">	<embed src="../video/media/ar_head' + a + '_hi.mov" width="' + w + '" height="' + h + '" alt="" cache="TRUE" autoplay="true" controller="true" bgcolor="#FFFFFF" moviename="playback" 	qtnext1="<' + this.opts.asset_path + this.filename + '> T<myself>" qtnext2="<../../video/media/ar_tail' + a + '_hi.mov> T<myself>" saveembedtags="true" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
			
				wm: '<object id="videoie" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" width="' + w + '" height="' + h + '"><param name="FileName" value="' + this.opts.asx_path + this.filename + '" /><param name="AutoStart" value="1" /><param name="ShowControls" value="1" /><param name="ShowStatusBar" value="0" /><param name="ShowDisplay" value="0" />			<embed bgcolor="#fff" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" src="' + this.opts.asx_path + this.filename + '" name="MediaPlayer" width="' + w + '" height="' + h + '"></embed> </object>'
				};
			
			return templates[f];
	};
	
	this.init = function() {
		var splt = choices[this.opts['split']];
		container.find('a').each(function(i) {
			$(this).bind('click', {vfile:splt[i]}, function(e){
     		e.preventDefault();
     		$.player.play(e.data.vfile);
  		});
		});
		if ($.cookie("ar_media_pref")) {
			container = container.replaceWith('<div id="watch" class="' + this.opts.aspect + ' watch" title="' + container[0].title + '"><a id="clip" href="#">Play</a></div>');
			$('#watch').find('#clip').bind('click', {vfile:$.cookie("ar_media_pref")}, function(e){
     		e.preventDefault();
     		$.player.play(e.data.vfile);
  		});
		}
	};
	
}

