/* UTF-8 編碼
 * --------------------------------------------------------------------
 * func.parse-extrahtml.js
 *
 * by Guan-Ting Chen, fi@livemail.tw
 *
 * Copyright (c) 2009 Guan-Ting Chen
 * Licensed under MIT (http://www.opensource.org/licenses/MIT-license.php)
*/
function build_extrahtml (t, s, w, h, security) {
	switch (t) {
		case '_er_youtube':
			v = /v=([\w-_]+)/i.exec(s)[1] || '';
			return '<object width="'+w+'" height="'+h+'"><param name="movie" value="http://www.youtube.com/v/'+v+'"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+v+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed></object>';
		break;	
		case '_er_flash':
			var allowscriptaccess = true == security ? 'never' : 'always';
			return $.flash.create({
				swf		: s,
				width 	: w,
				height	: h,
				params 	: {
					allowscriptaccess : allowscriptaccess,
					allowfullscreen : 'false',
					alt : s	
				}
			});
		break;	
		case '_er_video':
			switch (s.replace(/^.+\./, '')) {
				case 'flv':
				case 'mp4':
					var allowscriptaccess = true == security ? 'never' : 'always';
					return $.flash.create({
						swf		: 'player.swf',
						width 	: w,
						height	: h,
						params 	: {
							allowscriptaccess : allowscriptaccess,
							allowfullscreen : 'false',
							alt : s	
						},
						flashvars : {
							file : s
						}
					});
				break;
				case 'avi':
				case 'mpg':
				case 'wmv':
					h = h * 1 + 60;
					return '<object type="application/x-mplayer2" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'+w+'" height="'+h+'"><param name="url" value="'+s+'" /><param name="autostart" value="true" /><param name="balance" value="0" /><param name="currentPosition" value="0" /><param name="currentMarker" value="0" /><param name="enableContextMenu" value="false" /><param name="enableErrorDialogs" value="false" /><param name="enabled" value="true" /><param name="fullScreen" value="false" /><param name="invokeURLs" value="false" /><param name="mute" value="false" /><param name="playCount" value="1" /><param name="rate" value="1" /><param name="ShowStatusBar" value="true" /><param name="uimode" value="full" /><param name="volume" value="100" /><embed width="'+w+'" height="'+h+'" type="application/x-mplayer2" src="'+s+'" enableContextMenu="0" autoStart="1" stretchToFit="1" showStatusBar="1" showPositionControls="0" BufferingTime="3" AutoSize="1" windowlessVideo="1" showcontrols="1" playCount="1" volume="0"  pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" /></object>';
				break;
			}
		break;
	}
}

function parse_extrahtml (content, security) {
	security = undefined == security ? true : false;
	var width, height, src;
	var piece, html;
	
	content.each(function (i, self) {
		self = $(self);
		html 	= self.html();
		piece 	= /<img[\s]*class=['"]?(_er_youtube|_er_flash|_er_video)['"]?[^>]+>/i.exec(html);

		while (null != piece){
			width 	= /width=['"]?(\d+)/i.exec(piece[0])[1] || '';
			height 	= /height=['"]?(\d+)/i.exec(piece[0])[1] || '';
			src 	= /alt=['"]?([^" ]+)/i.exec(piece[0])[1] || '';
			html 	= html.replace(piece[0], build_extrahtml(piece[1], src, width, height, security));
			piece 	= /<img[\s]*class=['"]?(_er_youtube|_er_flash|_er_video)['"]?[^>]+>/i.exec(html);
		}	
		self.html(html);
	});
/*
	content.find('a[href*=#]').bind('click', function(event) {
		event.preventDefault();
		var self 	= $(this);
		var id 		= self.attr('href').replace(/[^#]*#/, '');
		var target 	= $('#'+id);
	
		if (0 < target.size()) {
			 var targetOffset = target.offset().top;
			 $('html,body').animate({scrollTop: targetOffset - 50} , 500);
			 target.animate();
		}
	});*/
}

