var ext2id = [];
ext2id['mp3'] = 'mp3';
ext2id['jpg'] = 'bg';
ext2id['gif'] = 'fg';
ext2id['png'] = 'fg';
ext2id['swf'] = 'swf';
ext2id['pdf'] = 'newwindow';
ext2id['mov'] = 'qt';
var media_ids = ['mp3','bg','fg','swf','qt'];
var media_bgC = {mp3:'none',bg:'#f60',fg:'none',swf:'#000',qt:'#000'};
var clean_props = ['innerHTML','src','src','innerHTML','innerHTML'];
//var clean_bgColor = [false,true,true,false,false];
function in_array (arr, str) {
	for (var n in arr) if (arr[n] == str) return true;
	return false;
}

function in_array_keys (arr, key) {
	for (var n in arr) if (n == key) return true;
	return false;
}

function clean_all_except (excl_id) {
	if (excl_id.indexOf('mp3') < 0) {
		excl_id = excl_id.split(",");
		for (var i=0; i<media_ids.length; i++) {
			if ( document.getElementById(media_ids[i])) {
			if (!in_array(excl_id, media_ids[i])) {// && media_ids[i] != 'mp3') {
				// -- kan niet kiezen ... ;) ... lastig!
				// voor mp3 innerHtml ongemoeid
				if (clean_props[i] != 'src') document.getElementById(media_ids[i])[clean_props[i]] = "";
				document.getElementById(media_ids[i]).style['visibility'] = 'hidden';
				//document.getElementById(media_ids[i]).style['background-color'] = 'none';
			} else {
				document.getElementById(media_ids[i]).style['visibility'] = 'visible';
				//document.getElementById(media_ids[i]).style['background-color'] = media_bgC[media_ids[i]];
			}
			}
		}
	} else {
		//alert ('is mp3, geen inhoud vervangen');
	}
}

var slide_count = 0;
var img_src = new Array ();
var img_id = new Array ();
var img_col = new Array ();
function doSlideShow (src, show_size, col) {
	if (arguments.length == 3) {
		img_src = src; 
		img_id = show_size; 
		img_col = col;
	} else {
		slide_count = (slide_count+1) % img_src.length;
		//alert(img_src[slide_count]+'\n'+img_id[slide_count]+'\n'+img_col[slide_count]);
		getMedia (img_src[slide_count],img_id[slide_count],img_col[slide_count]);
	}
	document.Timer = setTimeout("doSlideShow ()", 7000);
}
//show_size = 'fg' of 'bg' 
function getMedia (src, show_size, col) {
	//if (col) alert(col); else alert ('no color');
	var ext = src.substr((src.lastIndexOf('.')+1)).toLowerCase();
	if (!in_array_keys (ext2id, ext)) {
		alert('Invalid extension '+src+'('+ext+'): .'+show_size);
		return;
	}
	var id = ext2id[ext];
	if(ext == 'jpg')
	{
		if(show_size == 'fg' || show_size == 'bg' )
		{
			id = show_size;
		}
		else
		{
			show_size = 'fg';
		}
	}
	new_media (id, src, col);
}
function new_media (id, src, col) {
	if (id) var id_arr = id.split(",");
	if (src) var src_arr = src.split(",");
	if (col) var col_arr = col.split(","); else var col_arr = new Array();
	for (var i=0; i<id_arr.length; i++) {
		col_arr[i] = (!col_arr[i]) ? '#000000' : col_arr[i];
	}
	if (id_arr.length == src_arr.length == col_arr.length) {
		for (var n in id_arr) {
			//alert (n);
			id_swap_src (id_arr[n], src_arr[n], col_arr[n]);
		}
		clean_all_except (id);
	} else {
		alert ('new_media ('+id+','+src+','+col+')\nfout in media defenitie')
	}
}
function id_swap_src (id, src, col) {
	clearTimeout(document.Timer);
	//alert('id = ' + id + '\nsrc = ' + src + '\ncol = ' + col);
	if (!col) col = '#000000';
	if (document.getElementById(id)) {
	//if(id) {
		var obj_str = "";
		switch (id) {
			case 'newwindow':
				window.open(src , 'mediaWindow', 'resizable=yes,scrollbars=yes,status=yes,width=600,height=700,LEFT=0,TOP=0');
				break;
      
			case 'swf':
				//alert('loading Flash Movie: '+src);
				obj_str += ""
					+"\n\t <object id=\"swf_obj\" "
					+((navigator.appVersion.indexOf('Win')>0 && navigator.appVersion.indexOf('MSIE')>0) ? "\n\t\t classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\""
							+"\n\t\t codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\">" : "")

					+"\n\t\t type=\"application/x-shockwave-flash\" "
					+"\n\t\t data=\"c.swf?swf_pad="+src+"\">"
					+"\n\t\t <param name=\"bgcolor\" value=\""+col+"\">"
					+"\n\t\t <param name=\"scaleMode\" value=\"noScale\" \/>"
					+"\n\t\t <param name=\"movie\" value=\"c.swf?swf_pad="+src+"\" \/>"
					+"\n\t\t <img src=\"no_swf_plug.gif\" width=\"100\" height=\"100\" alt=\"The required plugin for this object was not found on your system\" \/>"
					+"\n\t <\/object>";
				break;
/*
			case 'bg':
				alert('loading background pic: '+src);
				obj_str += ""
					+"\n\t <object id=\"swf_obj\" "
					+((navigator.appVersion.indexOf('Win')>0 && navigator.appVersion.indexOf('MSIE')>0) ? "\n\t\t classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\""
							+"\n\t\t codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\">" : "")

					+"\n\t\t type=\"application/x-shockwave-flash\" "
					+"\n\t\t data=\"c.swf?swf_pad="+src+"\">"
					+"\n\t\t <param name=\"bgcolor\" value=\"#000000\">"
					+"\n\t\t <param name=\"scaleMode\" value=\"noScale\" \/>"
					+"\n\t\t <param name=\"movie\" value=\"c.swf?swf_pad="+src+"\" \/>"
					+"\n\t\t <img src=\"no_swf_plug.gif\" width=\"100\" height=\"100\" alt=\"The required plugin for this object was not found on your system\" \/>"
					+"\n\t <\/object>";
				break;
*/
			case 'qt':
				//alert('loading Quicktime movie: '+src);
				obj_str += ""
					+"\n\t <object id=\"qt_obj\" "
/*					+((navigator.appVersion.indexOf('Win')>0 && navigator.appVersion.indexOf('MSIE')>0) ? "\n\t\t classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\""
							+"\n\t\t codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\">"
							+"\n\t\t src=\""+src+"\" >" : "")
					+"\n\t\t type=\"application/x-quicktime\" "
					+"\n\t\t data=\""+src+"\" >"
					+"\n\t\t <param name=autoplay value=\"true\" \/>"
					+"\n\t\t <param name=bgcolor value=\"#000000\">"
					+"\n\t\t <param name=controller value=false \/>"
					+"\n\t\t <param name=pluginspage value=\"http://quicktime.apple.com/\">"
					+"\n\t\t <a href=\"http://quicktime.apple.com/\" target=\"blank\"><img src=\"no_qt_plug.gif\" width=\"100\" height=\"100\" alt=\"The required plugin for this object was not found on your system\" \/></a>"
*/					
					+"\n\t\t width=\"100%\" height=\"100%\" "
					+"\n\t\t classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\""
					+"\n\t\t codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\">"
					+"\n\t\t <param name=\"src\" value=\""+src+"\">"
					+"\n\t\t <param name=\"autoplay\" value=\"true\">"
					+"\n\t\t <param name=\"controller\" value=\"false\">"
					+"\n\t\t <param name=bgcolor value=\""+col+"\">"
					+"\n\t\t "
					+"\n\t\t <embed src=\""+src+"\" autoplay=\"true\" controller=\"false\" width=\"100%\" height=\"100%\" bgcolor=\""+col+"\""
					+"\n\t\t pluginspage=\"http://www.apple.com/quicktime/download/\">"
					+"\n\t\t </embed>"
					+"\n\t <\/object>";
				break;
			
			case 'mp3':
				//alert('loading mp3: '+src);
				obj_str += ""
					+"\n\t <OBJECT CLASSID=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" "
					+"\n\t\t WIDTH=\"160\" HEIGHT=\"16\" "
					+"\n\t\t CODEBASE=\"http://www.apple.com/qtactivex/qtplugin.cab\">"
					+"\n\t\t\t <PARAM name=\"src\"        VALUE=\""+src+"\">"
					+"\n\t\t\t <PARAM name=\"loop\"       VALUE=\"true\">"
					+"\n\t\t\t <PARAM name=\"autoplay\"   VALUE=\"true\">"
					+"\n\t\t\t <PARAM name=\"controller\" VALUE=\"false\">"
					+"\n\t\t\t <PARAM NAME=\"volume\"     VALUE=\"90\">"
					+"\n\t\t\t <PARAM NAME=\"type\"       VALUE=\"audio/x-mpeg\">"
					+"\n\t\t <EMBED SRC=\""+src+"\" " 
					+"\n\t\t\t TYPE=\"audio/x-mpeg\" "
					+"\n\t\t\t WIDTH=\"160\" HEIGHT=\"16\" " 
					+"\n\t\t\t VOLUME=\"90\" "
					+"\n\t\t\t LOOP=\"true\" " 
					+"\n\t\t\t AUTOPLAY=\"true\" " 
					+"\n\t\t\t CONTROLLER=\"false\" "
					+"\n\t\t\t PLUGINSPAGE=\"http://www.apple.com/quicktime/download/\">" 
					+"\n\t\t <\/EMBED>"
					+"\n\t <\/OBJECT>";

					//+"\n\t <object id=\"mp3_obj\" "
					//+"\n\t\t type=\"audio/x-mpeg\" "
					//+"\n\t\t data=\""+src+"\" "
					//+"\n\t\t <param name=\"autoplay\" value=\"true\" \/>"
					//+"\n\t\t <param name=\"movie\" value=\"c.swf?swf_pad="+src+"\" \/>"
					//+"\n\t\t <img src=\"no_mp3_plug.gif\" width=\"100\" height=\"100\" alt=\"The required plugin for this object was not found on your system\" \/>"
					//+"\n\t <\/object>";
				break;

			default:
				if (id == 'bg' || id == 'fg') {
				//alert('loading foreground pic: '+src);
					if (src.substring(0,1) == '#') {
						document.getElementById(id).style['background-color'] = src;
						document.getElementById(id).innerHTML = '';
						return;
					} else {
						if (id == 'fg') { 
							obj_str += "\n\t <table style=\"height:100%; width:100%\">"
								+"\n\t\t <tr style=\"height:100%; width:100%\">"
								+"\n\t\t\t <td style=\"height:100%; width:100%; text-align:center\">";
						}
						obj_str += "\n\t\t\t <img id=\""+id+"_img\" src=\""+src+"\" \/>";
						if (id == 'fg') { 
							obj_str += "\n\t\t\t </td>"
								+"\n\t\t </tr>"
								+"\n\t </table>";
							document.getElementById('media').style.backgroundColor = col;
						} else {
							document.getElementById('media').style.backgroundColor = '#000';
							//document.getElementById('media').style.backgroundColor = '#FFF';
						}
					}
				} else {
					alert ('error loading '+src+', server error or your browser does not support getElementById()');
				}
		}

		//alert (obj_str);
		if (obj_str) {
			document.getElementById(id).innerHTML = obj_str;
			//clean_all_except (id);
		}
		//alertstr='';
		//for (var n in document.getElementById(id)) alertstr += n+", ";
		//alert (alertstr);

	} else {
		
		alert( id + ' is an invalid id. define a div with the id in str_media_view.tpl');
	
	}
}

