/* -----------------05/06/2006 11:47-----------------

Copyright Datmedia Limited 2006

Reproduction or duplication in whole or part without the permission of DATMedia Limited is illegal.

Filename: construction.js

Description
Javascript library of functions for building and cloning DATPresenter DOM objects.

Notes
Javascript version is 1.2 compatible, some commands such as 'with' etc are NOT used, to provide compatibility with the widest range of browsers

Revision History
fixed cdstripbutton_img reference not pointing to the admin folder.
added && admin for the mdu_extras allowing player to hide movie duration
in build_striplist and build_preview added if admin show .. as empty
general code indentation housekeeping
seperated the construction defaults into new library constructiondefs.js
re-written build_preview
added 'player only' test in function 'build'
moved sleditor back in
added sl panel store
--------------------------------------------------*/
if(presentationDetails.length==0){
	alert('Player not ready, no active public archives found.\n\nPlease make at least one programme active \n\n via the administrator\'s interface.');
}

function build(){
	if(preStartUpCheck()){
		say('Onload event, panel store is ready.');
		if(isFirefox){
			the_sl_panel=document.getElementById('striplist_store').innerHTML;
			the_sl_panel=the_sl_panel.replace(/(%5B)/g,'[' );
			the_sl_panel=the_sl_panel.replace(/(%5D)/g,']' );
		}else{
			the_sl_panel=document.getElementById('striplist_store').innerHTML;
		}
		if(type!='solo'){
			build_sections('sections',dp);
		}else{
			cft													=	dp.disp_elements.sections[0].description+'_0_'+dp.disp_elements.sections[0].name;
			current_forward_tab[cft]		=	dp.disp_elements.categories[0].description+'_0_'+dp.disp_elements.sections[0].name+'_'+dp.disp_elements.categories[0].name;
			current_category						=	dp.disp_elements.categories[0].name;
			sections_built		=	true;
			mediachoice				=	media;
			current_preview		=	pres;
			this_id						=	pres;
			id								=	pres;
			if(admin){
				document.getElementById('adminstatustext').innerHTML						=	'Presentation: '+pres;
				document.getElementById('adminpresentationtitletxt').innerHTML	=	dp.presentations[pres].title;
			}
			supdocplayerbuttonvis=(dp.presentations[id].pdflink && (dp.presentations[id].pdflink != 'undefined'))?'visible':'hidden';
			if(document.getElementById('pdfbutton')){
				document.getElementById('pdfbutton').style.visibility	=	supdocplayerbuttonvis;
			}
			go_mediachoice(mediachoice,this_id);
			//init_player(mediachoice,this_id);
		}
		build_finished	=	true;
		say('Finished building cards index');
	}else{
		setTimeout('build();',330);
	}
}

function build_sections(target_id,dp_obj){
  date_sort_presentations('reverse');
	card_target	  	= target_id;
	de					  	= dp_obj.disp_elements;
	sections_area  	= dp_obj.sections_space
	var card_list 	= de.sections;
	no_of_cards			= card_list.length;
	card_top				= sections_area.top+adminslidecontrolheight -15;
	card_height			= sections_area.height;
	if(no_of_cards==0){return;}
	for(var i=0;i<no_of_cards;i++){
		var card_description	=	card_list[i].description;

		// Clone the card template
		var the_clone					=	card_source.cloneNode(true);
		var cardname					=	card_list[i].description+'_'+i+'_'+card_list[i].name;

		// Add the card into the main document via the section target
		document.getElementById(card_target).appendChild(the_clone);
		document.getElementById(card_target).childNodes[i].setAttribute('id',cardname);

		// Assemble section card
		assemble_section_card(cardname,i,card_list);
	}

 	// Initialise the row total and last processed element values before finally writing the dealt deck's row control arrays
 	s_rtotal				=	0;
 	elementname_old	=	'';

	for(var i=0;i<no_of_cards;i++){
 		var card_description	=	card_list[i].description;
 		elementname						=	card_description+'_'+i+'_'+card_list[i].name

 		// Start totting up the count of section cards on this row
 		s_rtotal++;

 		// If this is the first card OR this card's rowcontrol entry IS NOT EQUAL TO ....
 		if(i==0 || (positions[elementname].r!=positions[elementname_old].r)){
 			s_rstart[positions[elementname].r]=i;

			//alert('Store the rstart value: '+i+' for: '+elementname+' on row: '+positions[elementname].r+' to: '+s_rstart[positions[elementname].r]);
 			// This is the begining of a new row so set the total to 1 again
			if(i!=0){

				// Set row total for previous row now we know it has ended
				// s_mm = row num of previous row
				s_mm					=	positions[elementname].r+1;
				s_rtot[s_mm]	=	s_rtotal-1;

				//alert('Row: '+s_mm+' has: '+s_rtot[s_mm]+' cards present.');
				// Reset totting to 1 for subsequent row
				s_rtotal	=	1;
 			}
 	 	}
 		elementname_old	=	elementname;
 	} // Loop ends


	s_mm					=	positions[elementname].r;
 	s_rtot[s_mm]	=	s_rtotal;

	//  alert('Row: '+s_mm+' has: '+s_rtot[s_mm]+' cards present.');
  //	alert('Constructing s_rtot store for: '+elementname);
 	for(var i=0;i<no_of_cards;i++){
 		var card_description=card_list[i].description;
 		elementname									=	card_description+'_'+i+'_'+card_list[i].name
	  s_c_rowtots[elementname]		=	s_rtot;
 		s_c_rowstarts[elementname]	=	s_rstart;
 	}

	s_rtot					=	[];
 	s_rstart				=	[];
	sections_built	=	true;
}

function get_true_string_length(string_item){
	friendly				=	string_item.replace(/(&nbsp;)|(&amp;)/gi," ");
	c_text_length		=	friendly.length*(character_width);
	return c_text_length;
}

function assemble_section_card(elementname,i,card_list){

	//alert(elementname);
	var no_of_cards					=	card_list.length;
	var s_thiscard_top			=	card_top+0;//((no_of_cards-i-1)*4);
	var tra									=	card_left+0+((i)*0);
	positions[elementname]	=	new position(tra,s_thiscard_top,no_of_cards-i,null,null,0,i,'visible');
	position_element(elementname,positions[elementname]);

	// Produce the tab and background
	var c_text_length = get_true_string_length(card_list[i].friendlyName);
	document.getElementById(elementname).childNodes[1].setAttribute('id',elementname+card_tab_bg_suffix);
	document.getElementById(elementname+card_tab_bg_suffix).style.left						=	tab_offset_pos+'px';
	document.getElementById(elementname+card_tab_bg_suffix).childNodes[1].width		=	c_text_length;
	if(i==0){
		cft=elementname;
		current_section=card_list[i].name;
		document.getElementById(elementname+card_tab_bg_suffix).childNodes[0].src=cardimgpath+'/_'+card_list[i].description+'_tab_sel_start.gif';
		document.getElementById(elementname+card_tab_bg_suffix).childNodes[1].src=cardimgpath+'/_'+card_list[i].description+'_tab_sel_mid.gif';
		document.getElementById(elementname+card_tab_bg_suffix).childNodes[2].src=cardimgpath+'/_'+card_list[i].description+'_tab_sel_end.gif';
	  document.getElementById(elementname).childNodes[2].style.backgroundColor=selected_section_card_colour;
	}

	// Position and add text to the tabname element
	document.getElementById(elementname).firstChild.firstChild.setAttribute('id',elementname+card_tab_name_suffix);

	//document.getElementById(elementname+card_tab_name_suffix).innerHTML='<nobr onmouseover="showit(\''+elementname+'\');" >'+card_list[i].friendlyName+'</nobr>';
	document.getElementById(elementname+card_tab_name_suffix).innerHTML='<nobr>'+card_list[i].friendlyName+'</nobr>';
	document.getElementById(elementname+card_tab_name_suffix).parentNode.style.top=tabtext_y+'px';
	tab_toffset=tab_offset_pos+tabtext_x;
	document.getElementById(elementname+card_tab_name_suffix).parentNode.style.left=tab_toffset+'px';
	document.getElementById(elementname+card_tab_name_suffix).parentNode.style.zIndex=no_of_cards-i+2;

	// Now it's safe to calculate the horizontal offset for the tab (and background)
	tab_offset_pos+=(c_text_length)+lh_section_tab_width+lh_section_tab_width+2;
	tabwidth=c_text_length+(lh_section_tab_width + rh_section_tab_width)+0;
	document.getElementById(elementname+card_tab_bg_suffix).style.width=tabwidth+'px';
	document.getElementById(elementname).childNodes[2].setAttribute('id',elementname+'_'+card_list[i].type);
  var s_m=no_of_cards-i;
	var s_thiscard_top=card_top+((s_m-1)*0);
	card_list[i].y=s_thiscard_top;

	// This section's row max initial value
	psrowcontrol[elementname]=(psrowcontrol[elementname])?psrowcontrol[elementname]:0;

	// alert('Setting: '+elementname+' initial row to: '+psrowcontrol[elementname]);
	positions[elementname]=new position(tra,s_thiscard_top,no_of_cards-i,null,null,psrowcontrol[elementname],i,'visible');

	// ********* place the card now ***************
	position_element(elementname,positions[elementname]);

	// If no horizontal space left and this is not the last card, move existing cards down

	if((tab_offset_pos>tab_rh_max) && (s_m!=1)){
		// Increment this section's total row count (-1) for next card starting next row
		//	alert('Row: '+psrowcontrol[elementname]+' has no more horizontal space as tab_offset_pos is now: '+tab_offset_pos);
		//psrowcontrol[elementname]+=1;
		//move all preceeding cards down 1 row
		for(var im=0;im<i+1;im++){
			card_description=card_list[im].description;
			elementname			=card_description+'_'+im+'_'+card_list[im].name;
			s_new_top=rpx(document.getElementById(elementname).style.top)+row_vert;
			card_list[im].y=s_new_top;
			psrowcontrol[elementname]+=1;
			card_list[im].row+=1;
			//alert('1. moving down one row: '+elementname+' new row is to be: '+psrowcontrol[elementname]);
			//alert('2. moving down one row: '+elementname+' new card_list row is to be: '+card_list[im].row);
			positions[elementname]=new position(positions[elementname].x,s_new_top,positions[elementname].z,positions[elementname].w,positions[elementname].h,psrowcontrol[elementname],im,'visible');
			position_element(elementname,positions[elementname]);
		}
	tab_offset_pos=0;
	}
	card_list[i].id=elementname;
	card_list[i].x=tra; //XXL
	card_list[i].y=s_thiscard_top;
	card_list[i].z=i+1;
	card_list[i].w=rpx(document.getElementById(elementname).style.width);
	card_list[i].h=rpx(document.getElementById(elementname).style.height);
	card_list[i].vis=document.getElementById(elementname).style.visibility;
	make_section_card_content(elementname,i,card_list,dp.disp_elements.categories,s_thiscard_top,card_left);
}

function make_section_card_content(card_id,card_index,section_card_list,category_card_list,thiscard_top,card_left){
  elementname			=	card_id;
	i								=	card_index;
	card_list				=	section_card_list;
	var no_of_cards	=	card_list.length;

	if(card_list[i].type =='previewandcards'){
		var category_cards_placeholder_suffix	=	'_cards';
		var preview_panel_placeholder_suffix	=	'_preview';

		// Add preview and cards as single unnamed internal panel
		dr		=	90-adminslidecontrolheight;
		trb		=	515-thiscard_top-dr;
		trc		=	card_left+5;

		// APPEND PREVIEW PANEL PLACEHOLDER
		var preview_source		=	document.getElementById('prepreview_store').firstChild;
		var preview_panel			=	preview_source.cloneNode(true);

		SectionCardHolderNode	=	document.getElementById(elementname);

		SectionCardHolderNode.childNodes[2].appendChild(preview_panel);
		SectionCardHolderNode.childNodes[2].childNodes[0].setAttribute('id',card_list[i].name+preview_panel_placeholder_suffix);
		SectionCardHolderNode.childNodes[2].childNodes[0].zIndex=no_of_cards-i;

		// APPEND CARDS PLACEHOLDER
		var cpanel_source	=	document.getElementById('cpanel_store').firstChild;
		var cards_panel		=	cpanel_source.cloneNode(true);

		SectionCardHolderNode.childNodes[2].appendChild(cards_panel);
		SectionCardHolderNode.childNodes[2].childNodes[1].setAttribute('id',elementname+'_'+card_list[i].type+category_cards_placeholder_suffix);
		SectionCardHolderNode.childNodes[2].childNodes[1].zIndex=no_of_cards-i;

		// LINK TO CARDS SOURCE
		var category_card_source	=	document.getElementById('categorycard_store').firstChild;

		// ADD CATEGORY CARDS INTO TARGET PANEL
		var c_card_target				=	elementname+'_'+card_list[i].type+category_cards_placeholder_suffix;
		var c_card_list_temp		=	category_card_list[i];
		var c_card_list					=	category_card_list[i];//displayableCategories[i];
		ca_card_list						=	category_card_list[i];
		var no_of_c_cards				=	c_card_list.length;
		cardtotal[elementname]	=	no_of_c_cards;
		 	c_card_droplist					=	'<select name="drop_'+elementname+'" class="search_input">';

		//positions the category cards within the section cards
		for(var k=0;k<no_of_c_cards;k++){
			var the_c_clone					=	category_card_source.cloneNode(true);
			var c_card_description	=	c_card_list[k].description;
			text_length 						= get_true_string_length(c_card_list[k].friendlyName);
			c_elementname						=	c_card_description+'_'+k+'_'+card_list[i].name+'_'+c_card_list[k].name;
		 	//alert(c_elementname);
			document.getElementById(c_card_target).appendChild(the_c_clone);
			document.getElementById(c_card_target).childNodes[k].setAttribute('id',c_elementname);
			document.getElementById(c_card_target).childNodes[k].childNodes[2].firstChild.setAttribute('name','search_'+c_elementname);
			assemble_category_card(c_elementname,k,c_card_list);
		}
		c_card_droplist+='</select>';
		cats_for_section[elementname]=c_card_droplist;
		//SectionCardHolderNode.childNodes[3].innerHTML=cats_for_section[elementname];

		// Initialise the row total
		rtotal=0;
		c_elementname_old='';
		for(var k=0;k<no_of_c_cards;k++){
				var c_card_description=c_card_list[k].description;
				c_elementname			=c_card_description+'_'+k+'_'+card_list[i].name+'_'+c_card_list[k].name;
				rtotal++;

				// If this is the first card OR this card's rowcontrol entry IS NOT EQUAL TO ....
				if(k==0 || (positions[c_elementname].r!=positions[c_elementname_old].r)){
					rstart[positions[c_elementname].r]=k;
					if(k!=0){
						mm=positions[c_elementname].r+1;
						rtot[mm]=rtotal-1;

						//txt('setting row:',mm+' to '+rtot[mm]);
						rtotal=1;
			  	}

					//txt('rstart',rstart[positions[c_elementname].r]+'-'+positions[c_elementname].r);
		 		}
				c_elementname_old=c_elementname;
			} // Loop ends
			mm=positions[c_elementname].r;
			rtot[mm]=rtotal;
		} // It's a 'preview panel and a card deck'  ends

		//      alert('tot '+elementname+' -- '+rtot);
		//			alert('starts '+elementname+' -- '+rstart);
		c_rowtots[elementname]=rtot;
		c_rowstarts[elementname]=rstart;
		rtot=[];
		rstart=[];
		c_tab_offset_pos=0;
} // Function ends







function assemble_category_card(c_elementname,k,c_card_list){
	var no_of_c_cards																=	c_card_list.length;
	CategoryCardHolderNode 													= document.getElementById(c_elementname);

	CategoryCardHolderNode.childNodes[1].setAttribute('id',c_elementname+card_tab_bg_suffix);

	CategoryCardTabBGHolderNode											=	document.getElementById(c_elementname+card_tab_bg_suffix);
	CategoryCardTabBGHolderNode.style.left					=	c_tab_offset_pos+'px';
	CategoryCardTabBGHolderNode.childNodes[1].width	=	text_length;
	if(k==0){
		current_forward_tab[elementname]														=	c_elementname;
		current_category																						=	c_card_list[k].name;
		CategoryCardTabBGHolderNode.childNodes[0].src								=	cardimgpath+'/_'+c_card_list[k].description+'_tab_sel_start.gif';
 	 	CategoryCardTabBGHolderNode.childNodes[1].src								=	cardimgpath+'/_'+c_card_list[k].description+'_tab_sel_mid.gif';
 	 	CategoryCardTabBGHolderNode.childNodes[2].src								=	cardimgpath+'/_'+c_card_list[k].description+'_tab_sel_end.gif';
 	 	CategoryCardHolderNode.childNodes[2].style.backgroundColor	=	selected_category_card_colour;
	}else{
		// hide the search feature for cards at the rear
		CategoryCardHolderNode.childNodes[2].firstChild.firstChild.firstChild.style.visibility	=	'hidden';
	}
	CategoryCardHolderNode.firstChild.firstChild.setAttribute('id',c_elementname+card_tab_name_suffix);
	ctabnametemp			=		c_elementname+card_tab_name_suffix;
	c_card_droplist		+=	'<option value="'+ctabnametemp+'">'+c_card_list[k].friendlyName+'</option>';
	CategoryCardTabNameMUHolderNode													=	document.getElementById(c_elementname+card_tab_name_suffix);
	CategoryCardTabNameMUHolderNode.innerHTML								=	'<nobr>'+c_card_list[k].friendlyName+'</nobr>';
	CategoryCardTabNameMUHolderNode.parentNode.style.top		=	c_tabtext_y+'px';

	trd								=		c_tab_offset_pos+c_tabtext_x;

	CategoryCardTabNameMUHolderNode.parentNode.style.left		=	trd+'px';
	CategoryCardTabNameMUHolderNode.parentNode.style.zIndex	=	no_of_c_cards-k+2;

	c_tab_offset_pos	+=	(text_length)+lh_category_tab_width+lh_category_tab_width-8 //previously 22;
	tre								=		c_card_left+0+(k*0);

	//document.getElementById(c_elementname).style.left=tre+'px';//140*(k-1);//c_card_left+0;
	c_card_list[k].x			=	tre;
	c_tab_width						=	(text_length)+(lh_category_tab_width + rh_category_tab_width)+3;//previously 28;

	CategoryCardTabBGHolderNode.style.width	=	c_tab_width+'px';

	var m=no_of_c_cards-k;

	var thisccard_top=c_card_top+((m-1)*0);
	c_card_list[k].y=thisccard_top;

	// This section's category deck, row max initial value
	srowcontrol[elementname]=(srowcontrol[elementname])?srowcontrol[elementname]:0;

	// This category's row initial value
	rowcontrol[c_elementname]=(rowcontrol[c_elementname])?rowcontrol[c_elementname]:0;


	positions[c_elementname]=new position(tre,thisccard_top,no_of_c_cards-k,null,null,rowcontrol[c_elementname],k,'visible');

	// ********* place the card now ***************
	position_element(c_elementname,positions[c_elementname]);

	// If no horizontal space left and this is not the last card, move existing cards down
	if((c_tab_offset_pos>c_tab_rh_max) && (m!=1)){

		// Increment this sections total row count (-1) for next card starting next row
		srowcontrol[elementname]+=1;

		//move all preceeding cards down 1 row
		for(var km=0;km<k+1;km++){
			c_card_descriptionm =c_card_list[km].description;
			c_elementnamem			=c_card_descriptionm+'_'+km+'_'+card_list[i].name+'_'+c_card_list[km].name;
			new_top=rpx(document.getElementById(c_elementnamem).style.top)+row_vert;

			//alert(new_top);
			//document.getElementById(c_elementnamem).style.top=new_top+'px';
			c_card_list[km].y=new_top;
			rowcontrol[c_elementnamem]+=1;
			c_card_list[km].row+=1;

			//alert(c_elementnamem);
			positions[c_elementnamem]=new position(positions[c_elementnamem].x,new_top,positions[c_elementnamem].z,positions[c_elementnamem].w,positions[c_elementnamem].h,rowcontrol[c_elementnamem],km,'visible');
			position_element(c_elementnamem,positions[c_elementnamem]);
		}
		c_tab_offset_pos=0;
	}
	c_card_list[k].z=k+1;
	c_card_list[k].w=rpx(CategoryCardHolderNode.style.width);
	c_card_list[k].h=rpx(CategoryCardHolderNode.style.height);
	c_card_list[k].id=c_elementname;
	if(c_card_list[k].type =='striplist'){
		document.getElementById(c_elementname).childNodes[2].childNodes[1].id=c_elementname+'_'+c_card_list[k].type;

		// Striplist BUILD
		document.getElementById(c_elementname+'_'+c_card_list[k].type).innerHTML=build_striplist(card_list[i].name+','+c_card_list[k].name,k);
	}

	//c_card_list[k].vis=CategoryCardHolderNode.style.visibility;
}

function build_preview(id,section,categ){
	run_when_preview_built(id,section,categ);
	if(current_preview){
		if(document.getElementById('selbox_'+current_preview)){
			document.getElementById('selbox_'+current_preview).className = 'blank';
		}
	}
	document.getElementById('selbox_'+id).className = (admin)?'admin_selectedbox':'selectedbox';

	// Clone preview template
	document.getElementById(section+'_preview').innerHTML = document.getElementById('preview_store').innerHTML;

	// Record current preview
	current_section					=	section;
	current_category				=	categ;
	current_preview					=	id;
	url2mediadirectory    	=	'http://'+mediahostname+'/'+pmediapath+dp.presentations[id].slidedirectory+'/'+id+'/';

	// Get required node references for the preview panel
	PreviewPanelHolderNode  	=	document.getElementById(section+'_preview');
	RealButtonNode						=	PreviewPanelHolderNode.childNodes[1];
	WmButtonNode							=	PreviewPanelHolderNode.childNodes[2];
	PreviewImgHolderNode			=	PreviewPanelHolderNode.childNodes[3];
	TitleTextHolderNode				=	PreviewPanelHolderNode.childNodes[4];
	SupDocButtonNode					=	PreviewPanelHolderNode.childNodes[5];
	SynopsisDivHolderNode			=	PreviewPanelHolderNode.childNodes[7];
	AboutSpeakerLabelText			=	PreviewPanelHolderNode.childNodes[8];
	AboutSpeakerDivHolderNode = PreviewPanelHolderNode.childNodes[9];
	SpeakerDivHolderNode			= PreviewPanelHolderNode.childNodes[10];
	DateTextHolderNode				=	PreviewPanelHolderNode.childNodes[11];
	MobileImgHolderNode				=	PreviewPanelHolderNode.childNodes[12];
	SendToFriendDivNode				=	PreviewPanelHolderNode.childNodes[13];
	WebsiteAnchorHolderNode		=	PreviewPanelHolderNode.childNodes[14];
	ChatAnchorHolderNode			=	PreviewPanelHolderNode.childNodes[15];

	QtButtonNode							=	PreviewPanelHolderNode.childNodes[18];
	FlashButtonNode						=	PreviewPanelHolderNode.childNodes[19];
	// Hide Send to friend feature if we are a CD
	if(cd){
		SendToFriendDivNode.style.visibility	=	'hidden';
	}

	// Insert streaming media option icons
	RealButtonNode.innerHTML	=	(dp.presentations[id].urlreal)?'<IMG name="real_one_p_'+id+'" id="real_one_p_'+id+'" onclick="button_ctrl(\'real_one_p_'+id+'\',\'down\');mediachoice=\'real\';this_id='+id+';go_mediachoice(mediachoice,this_id);" src="'+realpreview_img_src+'" onmouseover="button_ctrl(\'real_one_p_'+id+'\',\'over\')" alt="Play now in realmedia format" onmouseout="button_ctrl(\'real_one_p_'+id+'\',\'\')" class="hand"/>':'';
	WmButtonNode.innerHTML		=	(dp.presentations[id].urlwm)?'<IMG name="w_media9_p_'+id+'" id="w_media9_p_'+id+'" onclick="button_ctrl(\'w_media9_p_'+id+'\',\'down\');mediachoice=\'wm\';this_id='+id+';go_mediachoice(mediachoice,this_id);" src="'+wmpreview_img_src+'" onmouseover="button_ctrl(\'w_media9_p_'+id+'\',\'over\')" alt="Play now in windows media format"  onmouseout="button_ctrl(\'w_media9_p_'+id+'\',\'\')" class="hand"/>':'';
	QtButtonNode.innerHTML		=	(dp.presentations[id].urlqt)?'<IMG name="quicktime_p_'+id+'" id="quicktime_p_'+id+'" onclick="button_ctrl(\'quicktime_p_'+id+'\',\'down\');mediachoice=\'qt\';this_id='+id+';go_mediachoice(mediachoice,this_id);" src="'+qtpreview_img_src+'" onmouseover="button_ctrl(\'quicktime_p_'+id+'\',\'over\')" alt="Play now in quicktime format"  onmouseout="button_ctrl(\'quicktime_p_'+id+'\',\'\')" class="hand"/>':'';
	FlashButtonNode.innerHTML		=	(dp.presentations[id].urlflash)?'<IMG name="flash_p_'+id+'" id="flash_p_'+id+'" onclick="button_ctrl(\'flash_p_'+id+'\',\'down\');mediachoice=\'flash\';this_id='+id+';go_mediachoice(mediachoice,this_id);" src="'+flashpreview_img_src+'" onmouseover="button_ctrl(\'flash_p_'+id+'\',\'over\')" alt="Play now in flash format"  onmouseout="button_ctrl(\'flash_p_'+id+'\',\'\')" class="hand"/>':'';

	// Insert support doc icon
	if(dp.presentations[id].pdflink && (dp.presentations[id].pdflink != 'undefined')){
		supdoclink									=	(cd=='1')?id+'/'+dp.presentations[id].pdflink:url2mediadirectory+dp.presentations[id].pdflink;
		SupDocButtonNode.innerHTML	=	'<A href="'+supdoclink+'" target="_blank"><IMG border="0" name="supdoc_p'+id+'" id="sup_doc_'+id+'" onclick="button_ctrl(\'sup_doc_p'+id+'\',\'down\');this_id='+id+';" src="'+supdocpreview_img_src+'" onmouseover="button_ctrl(\'sup_doc_p'+id+'\',\'over\')" alt="Click to download support document" onmouseout="button_ctrl(\'sup_doc_p'+id+'\',\'\')" class="hand"/></A>';
		supdocplayerbuttonvis				= 'visible';
	}else{
		SupDocButtonNode.innerHTML	=	'';
		supdocplayerbuttonvis				= 'hidden'
	}
	SupDocButtonNode.style.visibility	=	'visible';
	if(document.getElementById('pdfbutton')){
		document.getElementById('pdfbutton').style.visibility	=	supdocplayerbuttonvis;
	}

	// Insert preview image
	spkrpreview_img_alt	=	dp.presentations[id].speaker.replace(/&#039;/g,"'");
	if(cd){
		spkrpreview_img_src	=	id+'/'+dp.presentations[id].previewimage;
	}else{
		if(admin){
			spkrpreview_img_src		=	(dp.presentations[id].previewimage)?url2mediadirectory+dp.presentations[id].previewimage:add_preview_img_src;
			spkrpreview_img_alt		=	(spkrpreview_img_alt=='Click here to add your speakers name.')?'Use "upload speaker\'s image" toolbar option to change this image.':spkrpreview_img_alt;
		}else{
			spkrpreview_img_src		=	(dp.presentations[id].previewimage)?url2mediadirectory+dp.presentations[id].previewimage:empty_gif_img_src;
		}
	}
	PreviewImgHolderNode.firstChild.setAttribute('src',spkrpreview_img_src);
	PreviewImgHolderNode.firstChild.setAttribute('alt',spkrpreview_img_alt);


	// Get references to content and set visibilities.
	the_synopsisfull	=	dp.presentations[id].synopsisfull;
	the_aboutSpeaker	=	dp.presentations[id].aboutSpeaker;
	the_speaker				=	dp.presentations[id].speaker;
	the_title					=	dp.presentations[id].title;
	websitelinkvis		=	(dp.presentations[id].websiteurl)?'visible':'hidden';
	chatlinkvis				=	(dp.presentations[id].chatchoice)?'visible':'hidden';
	mobilelinkvis			=	(dp.presentations[id].url3gpp)?'visible':'hidden';

	if(admin){
		document.getElementById('adminstatustext').innerHTML						=	'Presentation: '+id;
		document.getElementById('adminpresentationtitletxt').innerHTML	=	dp.presentations[id].title;
	}else{
		the_synopsisfull	=	clear_if_2dots(the_synopsisfull);
		the_aboutSpeaker	=	clear_if_2dots(the_aboutSpeaker);
		the_title					=	clear_if_2dots(the_title);
		the_speaker				=	clear_if_2dots(the_speaker);
		if(!the_aboutSpeaker){
			// In player mode remove the title label from about the speaker/ Further details if there are no details
			AboutSpeakerLabelText.innerHTML='';
		}
	}

	SynopsisDivHolderNode.innerHTML			=	'<div id="synopsisfull_'+id+'" onclick="sleditor(this);">'+the_synopsisfull+'</div>';
	AboutSpeakerDivHolderNode.innerHTML	=	'<div id="ats_'+id+'" onclick="sleditor(this);">'+the_aboutSpeaker+'</div>';
	TitleTextHolderNode.innerHTML				=	the_title;
	SpeakerDivHolderNode.innerHTML			=	'<div id="spk_'+id+'" onclick="sleditor(this);">'+the_speaker+'</div>';
	DateTextHolderNode.innerHTML				=	dp.presentations[id].daterecorded;

	// Website
	if(WebsiteAnchorHolderNode){
		WebsiteAnchorHolderNode.firstChild.setAttribute('href',dp.presentations[id].websiteurl);
		WebsiteAnchorHolderNode.style.visibility	=	websitelinkvis;
	}
	if(document.getElementById('websitebutton')){
		document.getElementById('websitebutton').style.visibility	=	websitelinkvis;
	}

	// Chat
	if(ChatAnchorHolderNode){
		ChatAnchorHolderNode.firstChild.setAttribute('href',chatroom+'?chatid='+id);
		ChatAnchorHolderNode.style.visibility	=	chatlinkvis;
	}
	if(document.getElementById('chatbutton')){
		chatlinkvis=(dp.presentations[id].chatchoice=='yes')?'visible':'hidden';
		document.getElementById('chatbutton').style.visibility=chatlinkvis;
	}

	// Send to Mobile
	if(MobileImgHolderNode){
		MobileImgHolderNode.style.visibility	=	mobilelinkvis;
	}
	if(document.getElementById('mobilebutton')){
		document.getElementById('mobilebutton').style.visibility=mobilelinkvis;
	}
}

function clear_if_2dots( text2clear ){
	var clearedtext	=	(text2clear=='..')?'':text2clear;
	return clearedtext;
}

function launchwebsite(){
	var	desiredWidth=800;
	var desiredHeight=600;
	var left=(screen.width-desiredWidth-10)/2;
	var top=40;
	theurl=dp.presentations[current_preview].websiteurl;
  settings='status=yes,resizable=yes,width='+desiredWidth+',height='+desiredHeight+',left='+left+',top='+top;
	websiteWin=window.open(theurl,'websiteWin',settings);
}

function launchchat(){
	var	desiredWidth=800;
	var desiredHeight=600;
	var left=(screen.width-desiredWidth-10)/2;
	var top=40;
	theurl='http://www.keyone.co.uk/dmchat';
	settings='status=yes,resizable=yes,width='+desiredWidth+',height='+desiredHeight+',left='+left+',top='+top;
	chatWin=window.open(theurl,'chatWin',settings);
}

function launchhelp(){
	var	desiredWidth=800;
	var desiredHeight=600;
	var left=(screen.width-desiredWidth-10)/2;
	var top=40;
	theurl='http://www.datmedia.co.uk/dp_help/help.htm';
	settings='status=yes,resizable=yes,width='+desiredWidth+',height='+desiredHeight+',left='+left+',top='+top;
	chatWin=window.open(theurl,'chatWin',settings);
}

function get_stream_urls(aad,aac){

}

function build_striplist(sec_and_cat,kk){
	if(isSearch){
		entry_num			=	0;
	}
	slides_found			=	'';
	adminslprefx			=	(admin=='1')?'admin_':'';
	apair							=	sec_and_cat.split(',');
	var section				=	apair[0];
	var category			=	apair[1];
	striplist					=	'<table width="100%" border="0" cellpadding=1 cellspacing=1><tr><td>';
	cat_total_entries	=	0;

	//alert(twidth);
	for(var iii=0;iii<sorted_presentations.length;iii++){

		id	=	sorted_presentations[iii];
		url2mediadirectory    	=	'http://'+mediahostname+'/'+pmediapath+dp.presentations[id].slidedirectory+'/'+id+'/';
		if((isSearch && found[id])||!isSearch){
			if((!isSearch && dp.presentations[id].section==section && dp.presentations[id].category==category)||(isSearch)){
				if(isSearch){entry_num++}

				wmbutton  	=	(dp.presentations[id].urlwm)?'<img src="'+wmstripbutton_img+'" onclick="button_ctrl(\'w_media9_'+id+'\',\'down\');mediachoice=\'wm\';this_id='+id+';go_mediachoice(mediachoice,this_id);build_preview(\''+id+'\',\''+section+'\','+kk+');" name="w_media9_'+id+'" width="20" height="20" border="0" id="w_media9_'+id+'" onmouseout="button_ctrl(\'w_media9_'+id+'\',\'\')" onmouseover="button_ctrl(\'w_media9_'+id+'\',\'over\')" alt="Play now in Windows Media format" align="absmiddle" class="hand" />':'';
				realbutton	=	(dp.presentations[id].urlreal)?'<img src="'+realstripbutton_img+'" onclick="button_ctrl(\'real_one_'+id+'\',\'down\');mediachoice=\'real\';this_id='+id+';go_mediachoice(mediachoice,this_id);build_preview(\''+id+'\',\''+section+'\','+kk+');" name="real_one_'+id+'" width="20" height="20" border="0" id="real_one_'+id+'"  onmouseout="button_ctrl(\'real_one_'+id+'\',\'\')" onmouseover="button_ctrl(\'real_one_'+id+'\',\'over\')"  alt="Play now in Realmedia format" align="absmiddle" class="hand" />':'';
				qtbutton		=	(dp.presentations[id].urlqt)?'<img src="'+qtstripbutton_img+'" onclick="button_ctrl(\'quicktime_'+id+'\',\'down\');mediachoice=\'qt\';this_id='+id+';go_mediachoice(mediachoice,this_id);build_preview(\''+id+'\',\''+section+'\','+kk+');" name="quicktime_'+id+'" width="20" height="20" border="0" id="quicktime_'+id+'"  onmouseout="button_ctrl(\'quicktime_'+id+'\',\'\')" onmouseover="button_ctrl(\'quicktime_'+id+'\',\'over\')"  alt="Play now in Quicktime format" align="absmiddle" class="hand" />':'';
				flashbutton		=	(dp.presentations[id].urlflash)?'<img src="'+flashstripbutton_img+'" onclick="button_ctrl(\'flash_'+id+'\',\'down\');mediachoice=\'flash\';this_id='+id+';go_mediachoice(mediachoice,this_id);build_preview(\''+id+'\',\''+section+'\','+kk+');" name="flash_'+id+'" width="20" height="20" border="0" id="flash_'+id+'"  onmouseout="button_ctrl(\'flash_'+id+'\',\'\')" onmouseover="button_ctrl(\'flash_'+id+'\',\'over\')"  alt="Play now in Flash format" align="absmiddle" class="hand" />':'';

				cdsetbutton				=	(dp.presentations[id].cdtitle && admin)?'<img src="'+cdstripbutton_img+'" onclick="button_ctrl(\'add_cd_'+id+'\',\'down\');" name="add_cd_'+id+'" width="20" height="20" border="0" id="add_cd_'+id+'"  onmouseout="button_ctrl(\'add_cd_'+id+'\',\'\')" onmouseover="button_ctrl(\'add_cd_'+id+'\',\'over\')"  alt="'+dp.presentations[id].cdtitle+'" align="absmiddle" class="hand" />':'';
				the_title	 				=	dp.presentations[id].title;
				the_synopsis			=	dp.presentations[id].synopsis;
				the_aboutspeaker	=	dp.presentations[id].aboutSpeaker;


				if(the_title=='..' && !admin){
					the_title			=	'';
				}

				if(the_synopsis=='..' && !admin){
					the_synopsis	=	'';
				}

				price	=	dp.presentations[id].streamprice;
		  	if (price>0){
					ec_extras	=		'&nbsp;<a class="cc_buy" target="_cc" href="'+commerce_linkprefix+'cart.php?ClientID=102&cb=1&de='+escape(the_title)+'&pr='+price+'&co='+id+'"><img src="'+commercebuybutton_img+'"  border="0" alt="add to basket" align="absmiddle">&pound;<span id=price'+id+' >'+price+'</span></a>&nbsp;';
		  		ec_extras	+=	'<a target="_cc" href="'+commerce_linkprefix+'chk.php?ClientID=102"><img src="'+commercechkbutton_img+'"  border="0" alt="go to checkout" align="absmiddle"></a><div id=type'+id+' style="display:none">stream</div>';
		  	}else{
					ec_extras	=	'';
				}

				if (dp.presentations[id].playoffset && admin){
					pbo_extras	=	'&nbsp;+'+dp.presentations[id].playoffset+'ms';
				}else{
					pbo_extras	=	'';
				}

				if ( dp.presentations[id].mediadurations !='' && dp.presentations[id].mediadurations !=' '&& admin){
					var mdets			=	dp.presentations[id].mediadurations.split("|");
					var mdtt			=	mdets[5].split(":");
					var moviedur	=	parseInt(mdtt[1]);
					movietim			=	cooltime(moviedur);
					mdu_extras		=	'&nbsp;'+movietim;
				}else{
					mdu_extras		=	'';
				}


				live_extras					=	( dp.presentations[id].live=="yes" && admin)?'&nbsp;[LIVE]&nbsp;':'';
				checked							=	(dp.presentations[id].status=='active')?'checked="checked"':'';
				admin_active_box		=	(admin=='1')?'<input type="checkbox" class=checkbox value="active" onclick="set_status(this.name,this.checked)" name="pres_status_'+id+'" '+checked+'>':'';
				admin_presid_label	=	(admin=='1')?''+id+'&nbsp;':'&nbsp;';

				if(isSearch && (typeof s_found[id]!='undefined')){
					sf_length=s_found[id].length;
					if( sf_length>0){
						//plural=(sf_length>1)?'s':'';
						for(var sd in s_found[id]){
							fr=s_found[id];
							ttsd=fr[sd];
							sdtt=ttsd-1;
							//var ttsd=parseInt(sd)+1;
							slides_found+='<span onclick="mediachoice=\'real\';this_id='+id+';playfromslide=true;slidetoplay='+sdtt+';go_mediachoice(mediachoice,this_id);" class="hand" title="Play from slide '+ttsd+'" >&nbsp;'+ttsd+'</span>';
				   	}
					}
					slides_found='<nobr>'+slides_found+'</nobr>';
				}else{
					slides_found='';
				}

				striplist						+=the_sl_panel.replace(/TEMPLATE([0-9a-z\[\]\.\_]+)TEMPLATE/g,function(m){return replacechar(m,section,kk)} );
				slides_found='';
				cat_total_entries++;
			}
		}
	}
	striplist+='</td></tr></table>';
	//alert(striplist);
	var result_summary=(isSearch)?entry_num+' Results. <br /><a href="javascript:void(0);" onclick="parent.sl_reset(\''+c_elementname+'\')" >click here to exit results view</a>':'';
	striplist=result_summary+striplist;
	isSearch=false;
	return striplist;
}

function replacechar(thematch,section,kk){
	//alert(thematch+' -- '+id);
	thematch2=thematch.replace(/TEMPLATE/g,'');
	//alert('POST '+thematch2+' -- '+id);
	eval("the_result="+thematch2);
	//alert(thematch2+'IS TR '+the_result);
	return the_result;
	}


function setinner(target,content){
	document.getElementById(target).innerHTML=content;
}

function position_element(id,e_pos){

	//alert(id);
	if(e_pos.x!=null){
		document.getElementById(id).style.left=e_pos.x+'px';
	}
	if(e_pos.y!=null){
		document.getElementById(id).style.top =e_pos.y+'px';
	}
	if(e_pos.w!=null){
		document.getElementById(id).style.width=e_pos.w+'px'
	}
	if(e_pos.h!=null){
		document.getElementById(id).style.height=e_pos.h+'px'
	}
	if(e_pos.z!=null){
		document.getElementById(id).style.zIndex=e_pos.z;
	}
}

function position(x,y,z,w,h,r,pos,vs){
	if(x!=null){
		this.x=x;
	}
	if(y!=null){
		this.y=y;
	}
	if(w!=null){
		this.w=w;
	}
	if(h!=null){
		this.h=h;
	}
	if(z!=null){
		this.z=z;
	}
	if(r!=null){
		this.r=r;
	}
	if(pos!=null){
		this.pos=pos;
	}
	if(vs!=null){
		this.vs=vs;
	}
}

function sleditor(comp){
	if(admin ){
		if(peditorWin){
			peditorWin.close();
		}
		if(pedit){
			gebi=comp.id;
			sp=comp.id.split('_');
			element=sp[0];
			pr=sp[1];
			category='x';
			pp.element=element;
			pp.pres=pr;
			pp.inner=comp.innerHTML;
			pp.fromdb=dp.presentations[pr].title;
			pp.component=comp;
			pp.gebi=gebi;
			pre_conv_title=dp.presentations[pr].title;
			if(an_id){
				desiredTop=an_eventX;
				desiredLeft=an_eventY;
			}else{
				desiredTop=top;
				desiredLeft=left;
			}
			desiredWidth=610;
			desiredHeight=250;

			//pct=pre_conv_title.replace(/&#(\d+);/g, String.CharCode("$1") );
			//alert(unescape(pre_conv_title));
			theurl='admin/sleditor.php?id='+comp.id+'&element='+element+'&category='+category+'&pres='+pr;
      settings='status=no,resizable=yes,width='+desiredWidth+',height='+desiredHeight+',left='+desiredLeft+',top='+desiredTop;
			if(sleditorWin){
				 sleditorWin.close();
				}
			sleditorWin=window.open(theurl,'sleditorWin',settings);
      sleditorWin.creator=self;
			sleditorWin=sleditorWin;
		}
		return true;
	}
}

function dpx_run(){
	txt('dpx_run called, is build finished?',build_finished);
	if(build_finished){
		say('DPX Ready');
		dp_top=rpx(document.getElementById('dp').style.top);
		if(admin){
			dp_top+=40;
			document.getElementById('dp').style.top=dp_top+'px';
		}
		txt('starting status reporter now ',id);
		status();
		callmode_control();
	}else{
		setTimeout('dpx_run();',330);
	}
}

