// Create a DPX
new DatPresenter();

DatPresenter.VERSION='1.01';
DatPresenter.prototype.player=new Player();
DatPresenter.prototype.sections=new Array();
DatPresenter.prototype.presentations=new Array();
DatPresenter.prototype.addNewPresentation=AddP;
//DatPresenter.prototype.addNewSection=AddS;
//DatPresenter.prototype.addNewCategory=AddC;

// Target object for raw presentation details
var dp = new DatPresenter(Unique_DP_id,client,user,key,skin,system_speed,bandwidth,offset_x,offset_y,w,h,debug,admin);
dp.sections_space.top=420;//rpx(source.style.top);
dp.sections_space.left=0;//rpx(source.style.left);
dp.sections_space.height=695;//rpx(source.style.height);
//dpxminimise();

function DatPresenter(dp_unique_id,dp_client,dp_user,dp_key,dp_skin,dp_system_speed,dp_bandwidth,dp_x,dp_y,dp_w,dp_h,dp_debug,admin){
	if(dp_unique_id){
		this.id=dp_unique_id;
		this.client=dp_client;
		this.user=dp_user;
		this.key=dp_key;
		this.skin=dp_skin;
		this.system_speed=dp_system_speed;
		this.bandwidth=dp_bandwidth;
		this.x=dp_x;
		this.y=dp_y;
		this.width=dp_w;
		this.height=dp_h;
	  //resizeTo(dp_w,dp_h);
	  //moveTo(dp_x,dp_y);
		this.debug=dp_debug;
		this.player=new Player();
		this.sections=new Sections();
		this.sections_space=new Area();
		this.control=admin;
	}
}
	var displayableSections=new Array();
	var displayableCategories=new Array();

// Loader
function load_presentations( target,presentationDetails ){
	var displayableSections=new Array();
	var displayableCategories=new Array();

	var DPS_cfl=0;
	var crrar;
	var disp_sec_index=1;
	var disp_sec_indexarr=new Array();

	// Walk through presentationDetails array for active presentations
	for ( var a in presentationDetails){
		var presentation= new Presentation(presentationDetails[a]);
		 //alert('Presentation status is:'+presentation.status+' id is: '+presentation.id);
		if(admin=='1' || presentation.status=='active'){
			target.addNewPresentation(presentation);

			// Try to find presentations that are in the specified embedded sections by walking the 'DPSections' array looking for a match with the section name attribute
			for ( var sectionid in DPSections){
				//alert(sectionid);

				// If
				if(presentation.section == DPSections[sectionid].name){



					// OK presentation has a section reference that matches this section's name so walk this section's categories
					for ( var categoryid in DPSections[sectionid].categorylist){

						// Check that the presentation is a match for the category too.
						if(presentation.category==DPSections[sectionid].categorylist[categoryid].name){




							// Construct the displayableSections array based on the position field of the section
							if(admin!='1'){
								if(!disp_sec_indexarr[DPSections[sectionid].position]){
									disp_sec_indexarr[DPSections[sectionid].position]=DPSections[sectionid].position;
									srrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									crrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									disp_sec_index++;
								}else{
									srrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									crrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
								}
							}else{

									//crrar=DPSections[sectionid].position;
								if(!disp_sec_indexarr[DPSections[sectionid].position]){
									//alert('nf presentation with section at position '+DPSections[sectionid].position+'pid '+presentation.id  );
									// set this disp_sec_indexarr for this position=disp_sec_index
									//alert('nf dsi '+disp_sec_index);
									disp_sec_indexarr[DPSections[sectionid].position]=DPSections[sectionid].position;

									srrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									crrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									disp_sec_index++;
								}else{
									srrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
									crrar=disp_sec_indexarr[DPSections[sectionid].position]-1;
								}




							//alert('Category match Presentation status is:'+presentation.status+' id is: '+presentation.id+' Cat:'+DPSections[sectionid].categorylist[categoryid].name+' Crrar:'+crrar+' sectionid:'+sectionid);
							}
							//alert('dispsec index is'+crrar+' pos is'+DPSections[sectionid].position+' section name is '+DPSections[sectionid].friendlyName);
							displayableSections[srrar]=DPSections[sectionid];

							// Get current total of categories found in this section
							DPS_cfl=DPSections[sectionid].cats_found.length;

							// Add this presentation to 'presentation_list' array attached to this DPSections
							DPSections[sectionid].categorylist[categoryid].presentation_list[presentation.id]=presentation;

							// If this category has NOT already had a presentation matched to it.....
							if(DPSections[sectionid].cats_once[presentation.category]!=true){

								// Then add this category to the cats_found array, thus extending it
								DPSections[sectionid].cats_found[DPS_cfl]=DPSections[sectionid].categorylist[categoryid];

								//Add the found category to the 'displayableCategories' array
								displayableCategories[srrar]=DPSections[sectionid].cats_found;

								// Now record that this category has been found in this section
								DPSections[sectionid].cats_once[presentation.category]=true;

							}


						}

					}

				}else{
				}
			}
	 	}
	}
	// Now populate this dp instance's 'disp_elements' attribute with a single object called 'Displayable' that has a 'sections' and 'categories' attribute each containing the found and hence displayable, sections and categories arrays indexed by position
	newe=0;
	brfa = new Array();
	//alert(displayableCategories.length);
	for(we=0;we<displayableCategories.length;we++){
		if(displayableCategories[we]){
			brfa[newe]=displayableCategories[we];
			newe++;
		}
	}
	displayableCategories=brfa;
	//alert(displayableCategories.length);
	for(we=0;we<displayableCategories.length;we++){

			fff=displayableCategories[we];
			//temp2_array= fff;
			brf = new Array();
			jbrf= new Array();
			for(pp=0;pp<fff.length;pp++){
				ddd=fff[pp].position-1;
				brf[ddd]=fff[pp]	;
			}
			var ibb =	0;
			for(ppl=0;ppl<brf.length;ppl++){
				if(brf[ppl]){
					jbrf[ibb]=brf[ppl];
		    	ibb++;
				}
			}
			displayableCategories[we]=jbrf;
	}

	snewe=0;
	sbrfa = new Array();
	//alert(displayableSections.length);
	for(we=0;we<displayableSections.length;we++){
		if(displayableSections[we]){
			sbrfa[snewe]=displayableSections[we];
			snewe++;
		}
	}
	displayableSections=sbrfa;
	//alert(displayableSections.length);

	dp.disp_elements=new Displayable(displayableSections,displayableCategories);
	return true;
}

function Displayable(s,c){
	this.sections=s;
	this.categories=c;
}

function Area( top,left,height ){
	this.top=top;
	this.left=left;
	this.height=height;
}

function Sections(){
}

function Presentation(DetailsArray){
	for ( var a in DetailsArray){
		this[a]=DetailsArray[a];
	}
}

function Section(position,description,type,name,friendlyName,categorylist,x,y,z,w,h,vis,id,row){
	this.position=position;
	this.description=description;
	this.type=type;
	this.name=name;
	this.friendlyName=friendlyName;
	this.categorylist=categorylist;
	this.cats_found=new Array();
	this.cats_once=new Object();
	this.foremost_cat_row=0;
	this.x=x;
	this.y=y;
	this.z=z;
	this.w=w;
	this.h=h;
	this.vis='d';
	this.id=id;
	this.row=row;
}

function Category(position,description,type,name,friendlyName,target_section,x,y,z,w,h,vis,id,row){
	this.position=position;
	this.description=description;
	this.type=type;
	this.name=name;
	this.friendlyName=friendlyName;
	this.target_section=target_section;
	this.presentation_list=new Array();
	this.x=x;
	this.y=y;
	this.z=z;
	this.w=w;
	this.h=h;
	this.vis=vis;
	this.id=id;
	this.row=row;
}

function mediaFormat(shortname,description,deliverymethod,pluginURL){
	this.type=shortname;
	this.description=description;
	this.delivery=deliverymethod;
	this.URL=pluginURL;
}

function Player(){
}



//Define available media formats
formats.realmedia=new mediaFormat('real','Realmedia from RealNetworks','stream','http://www.realnetworks.com');
formats.windowsmedia=new mediaFormat('wm','Windows media 9 for Windows media player','stream','http://www.microsoft.com');
formats.quicktime=new mediaFormat('qt','Quicktime','stream','http://www.quicktime.com');
formats.javastream=new mediaFormat('jstream','Java stream','stream','http://www.javastreaming.com');
formats.dvd=new mediaFormat('dvd','DVD','Digital Video Disc File','download','http://www.dvd.com');

function AddP(presentation){
		this.presentations[presentation.id]=presentation;
}

function get_items_in_focus(){
	section_in_focus=cft;
	category_in_focus=current_forward_tab[section_in_focus];
	presentation_in_focus=current_preview;
}

function sendtoamobile(){
	//alert('ahoy');

	var undef;
	get_items_in_focus();
	if(presentation_in_focus!=undef){
		mob_no=window.prompt('Enter the destination mobile number (eg. 447890123456) ','');
		if((mob_no!=null) && mob_no){
			message=dp.presentations[presentation_in_focus].title+' - A DPX Video presentation.';
			//controlpath='dpx_v1a/admin';
			theurl='http://'+hostname+'/'+playerpath+'/send_to_mobile.php?mob_no='+mob_no+'&stream='+escape(dp.presentations[presentation_in_focus].url3gpp)+'&message='+escape(message);
  		settings='status=yes,resizable=yes,width=100,height=40,left=10,top=10';
			callwin=window.open(theurl,'callwin',settings);
		}
	}

}
function go_website(){
			theurl='http://www.datmedia.co.uk?fromltplayer';
  		settings='scrollbars=yes,status=yes,resizable=yes,width=820,height=820,left=10,top=10';
			webwin=window.open(theurl,'webwin',settings);
}




function advsearch(){
			get_items_in_focus();
			theurl='advanced_search.php?category='+category_in_focus;
  		settings='status=yes,resizable=yes,width=430,height=439,left=770,top=425';
			callwin=window.open(theurl,'callwin',settings);
}




// Helper functions ///////////////////////////////////////////////////////////////////////////////////////

function write_formats(){
	// Write to document list of known formats
	document.write('List of known formats<br>');
	for (var a in formats){
		document.write('formats.'+a+'<br>');
		for (var b in formats[a]){
			document.write(b+' '+formats[a][b]+'<br>');
		}
	document.write('<br>');
	}
}

function search_content(){
	 //for ( var a in dp.presentations){
		searchterm='';
		for ( var ss in document.forms){
		 searchterm+=ss+' -- '+this[ss]+"\n";
		 }
		//searchterm=this.toString();
		alert(dp.presentations[1].title+'-'+searchterm);
		presentations[a].speaker;
	 	presentations[a].synopsis;
		return false;
	// }
	}

function build_dpx(){
	document.getElementById('sections').innerHTML='';
	if(load_presentations(dp,presentationDetails)){
		say('Presentation information has been scanned and successfully included in your DPX, ID#'+dp.id);
	}else{
		say('An error has occurred while scanning the presentation information.');
	}
}

function mode_control(){
	if(size=='mini'){
		if(player_open && type!='solo'){
			dpxminimise();
		}
	}
  if(cmd){
		if(cmd=='autoplay' && type=='solo' && pres){
		}
		if(cmd=='autoplay' && type=='autofocus' && pres){

			//alert('Please wait while building');
			for(var da=0;da<card_list.length;da++){
				if(card_list[da].name==dp.presentations[pres].section){
					sec_id=da;
					fre=dp.disp_elements.categories[da];
					for(var db=0;db<fre.length;db++){
						if(fre[db].name==dp.presentations[pres].category){
							cat_id=db;
						}
					}
				}
			}

			//required_section='archive';
			cardtype='section';
			cardrequired=cardtype+'_'+sec_id+'_'+dp.presentations[pres].section+'_tabname';

			//alert('pwl');
			cardtofront(cardtype,cardrequired);

			//alert('Press OK to continue');
			cardtype='category';
			cardrequired=cardtype+'_'+cat_id+'_'+dp.presentations[pres].section+'_'+dp.presentations[pres].category+'_tabname';
			cardtofront(cardtype,cardrequired);

			//alert('ready');
			if(media=='wm'){
				image_name='w_media9_'+pres;
				prev_image_name='w_media9_p'+pres;


				mediachoice=media;
				this_id=pres;
				go_mediachoice(mediachoice,this_id);
				setTimeout('build_preview(pres,card_list[sec_id].name,cat_id);',300);


			}
			if(media=='real'){
				image_name='real_one_'+pres;
				prev_image_name='real_one_p'+pres;


				mediachoice=media;
				this_id=pres;
				go_mediachoice(mediachoice,this_id);
				setTimeout('build_preview(pres,card_list[sec_id].name,cat_id);',300);


			}
		}
	}
}

function callmode_control(){
	if(sections_built){

		//alert(brrr);
		mode_control();
	}else{
		cmc_tim=setTimeout('callmode_control();',500);
		brrr++;
	}
}

