/**
 * This script contains embed functions for common plugins. This scripts are complety free to use for any purpose.
 */
 
function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}  
 

function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}


function writeShockWave(p) {
document.write('<div id="'+p.src+'"></div>');
jwplayer(p.src).setup({
flashplayer: "/jwplayer/player.swf", file: p.src, height: 360, width: 540
});
}


function writeShockWaveOrig(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}


function writeQuickTime(p) {
document.write('<div id="column'+p.src+'"></div>');
	
			/**
			 * Visualize an HTML table using Highcharts. The top (horizontal) header 
			 * is used for series names, and the left (vertical) header is used 
			 * for category names. This function is based on jQuery.
			 * @param {Object} table The reference to the HTML table to visualize
			 * @param {Object} options Highcharts options
			 */
			Highcharts.visualize = function(table, options) {
				// the categories
				options.xAxis.categories = [];
	//			$('tbody th', table).each( function(i) {
	//				options.xAxis.categories.push(this.innerHTML);
	//			});
				
				$('caption', table).each( function(i) {
					options.title.text = this.innerHTML;
				});
				
				// the data series
				options.series = [];
				$('tr', table).each( function(i) {
					var tr = this;
					$('th, td', tr).each( function(j) {
						if (j > 0) { // skip first column
							if (i == 0) { // get the name and init the series
								options.series[j - 1] = { 
									name: this.innerHTML.replace(/^\s+/, "").replace(/\s+$/, ""),
									data: []
								};
							} else { // add values
							x=this.innerHTML;
							x=str_replace('&nbsp;', "",x);
							x=str_replace(',', ".",x);
								options.series[j - 1].data.push(parseFloat(x));
							}
						} else {
						if (i>0) options.xAxis.categories.push(this.innerHTML.replace(/^\s+/, "").replace(/\s+$/, ""));
						}
						
					});
				});
				
				var chart = new Highcharts.Chart(options);
			}
				
			// On document ready, call visualize on the datatable.
			$(document).ready(function() {			
				var table = document.getElementById(p.src),
				options = {
					   chart: {
					      renderTo: 'column'+p.src,
					      defaultSeriesType: 'column'
					   },
					   title: {
					      text: ''
					   },
					   xAxis: {
					   },
					   yAxis: {
					      title: {
					         text: 'Units'
					      }
					   },
					   tooltip: {
					      formatter: function() {
					         return '<b>'+ this.series.name +'</b><br/>'+
					            this.y +' '+ this.x.toLowerCase();
					      }
					   }
					};
				
			      					
				Highcharts.visualize(table, options);
			});
}




function writeQuickTimeOrig(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
document.write('<div id="line'+p.src+'"></div>');
	
			/**
			 * Visualize an HTML table using Highcharts. The top (horizontal) header 
			 * is used for series names, and the left (vertical) header is used 
			 * for category names. This function is based on jQuery.
			 * @param {Object} table The reference to the HTML table to visualize
			 * @param {Object} options Highcharts options
			 */
			Highcharts.visualize = function(table, options) {
				// the categories
				options.xAxis.categories = [];
				$('caption', table).each( function(i) {
					options.title.text = this.innerHTML;
				});
				
				// the data series
				options.series = [];
				$('tr', table).each( function(i) {
					var tr = this;
					$('th, td', tr).each( function(j) {
						if (j > 0) { // skip first column
							if (i == 0) { // get the name and init the series
								options.series[j - 1] = { 
									name: this.innerHTML.replace(/^\s+/, "").replace(/\s+$/, ""),
									data: []
								};
							} else { // add values
							x=this.innerHTML;
							x=str_replace('&nbsp;', "",x);
							x=str_replace(',', ".",x);
								options.series[j - 1].data.push(parseFloat(x));
							}
						} else {
						if (i>0) options.xAxis.categories.push(this.innerHTML.replace(/^\s+/, "").replace(/\s+$/, ""));
						}
						
					});
				});
				
				var chart = new Highcharts.Chart(options);
			}
				
			// On document ready, call visualize on the datatable.
			$(document).ready(function() {			
				var table = document.getElementById(p.src),
				options = {
					   chart: {
					      renderTo: 'line'+p.src,
					      defaultSeriesType: 'line'
					   },
					   title: {
					      text: ''
					   },
					   xAxis: {
					   },
					   yAxis: {
					      title: {
					         text: 'Units'
					      }
					   },
					   tooltip: {
					      formatter: function() {
					         return '<b>'+ this.series.name +'</b><br/>'+
					            this.y +' '+ this.x.toLowerCase();
					      }
					   }
					};
				
			      					
				Highcharts.visualize(table, options);
			});
}

function writeWindowsMedia(p) {
document.write('<div id="pie'+p.src+'"></div>');
sum=0;		
			/**
			 * Visualize an HTML table using Highcharts. The top (horizontal) header 
			 * is used for series names, and the left (vertical) header is used 
			 * for category names. This function is based on jQuery.
			 * @param {Object} table The reference to the HTML table to visualize
			 * @param {Object} options Highcharts options
			 */
			Highcharts.visualizepie = function(table, options) {
				// the categories
				$('caption', table).each( function(i) {
					options.title.text = this.innerHTML;
				});	
				// the data series
				options.series = [];
				$('tr', table).each( function(i) {
					var tr = this;
					$('th, td', tr).each( function(j) {
						if (i == 0) { // get the name and init the series
								options.series[j - 1] = { 
									data: []
								};
							} else { // add values
								
								if (j==0) cat=this.innerHTML; else 
								
								{
															x=this.innerHTML;
							x=str_replace('&nbsp;', "",x);
							x=str_replace(',', ".",x);
								options.series[j - 1].data.push([cat,parseFloat(x)]);
sum=sum+parseFloat(this.innerHTML);
								}
								                  						
						}
					});
				});
				
				var chart = new Highcharts.Chart(options);
			}
				
			// On document ready, call visualize on the datatable.
			$(document).ready(function() {			
				var table = document.getElementById(p.src),
				options = {
				chart: {
						renderTo: 'pie'+p.src,
						defaultSeriesType: 'pie'
					},
					title: {
						text: ''
					},
		plotArea: {
						shadow: null,
						borderWidth: null,
						backgroundColor: null
					},
					tooltip: {
						formatter: function() {
							return '<b>'+ this.point.name +'</b>: '+ this.y + ' ('+(this.y*100/sum).toFixed(2)+' %)';
						}
					},
					plotOptions: {
						pie: {
							allowPointSelect: true,
							cursor: 'pointer',
							dataLabels: {
								enabled: false
							},
							showInLegend: true
						}
					}
					};
				
			      			
				Highcharts.visualizepie(table, options, sum);

			});
}

function writeRealMediaOrig(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMediaOrig(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}


function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}

