//------------------------------GENERAL--------------------------------------------------------------------
function gbid(x) { return ( document.getElementById(x) );}
function cleanVar(x) { xstr=x.toString(); if ((xstr.indexOf('em')>0)||(xstr.indexOf('px')>0)) { xstr=xstr.substring(0,xstr.length-2);	return parseInt(xstr); } else { return x; } }
function checkWidth(id) { if (gbid(id)) { return cleanVar(gbid(id).clientWidth); } else { return 0; } }
function checkHeight(id) { if (gbid(id)) { return cleanVar(gbid(id).clientHeight); } else { return 0; } }

function IE_fix(matter) {
	if (browser == "Internet Explorer") {
		switch (matter) {
			case 'vrml_frame':
				if (current_view=='view_vrml') {
					gbid('view_vrml').style.width=items_width.view_zone+'px';
				}
				break;
		}
	}
}

function fix_ie_title() {
	if (browser == "Internet Explorer") {
		document.title='FEFCO codes';
	}
}

//------------------------------GENERAL--------------------------------------------------------------------




//------------------------------GLOBALS--------------------------------------------------------------------

//related to menus
var menus_items=4;
var submenus_items=0;
var menu_status=new Array();

//related to catalogue
var current_id='';
var last_choice=new Array(); //this var remember the last choices of the user
last_choice['id']='none'; last_choice['CAT_TEXT']='none'; last_choice['CAT_SELECT']='none';
var current_view='';

//related to browse
var items_height=new Array();
var items_width=new Array();

//related to the browse
var content_type='welcome';

//related to translations
var trad = new Array();
trad.labels= new Array(); trad.pages= new Array();

//related to library
var library_open=true;

//------------------------------GLOBALS--------------------------------------------------------------------






//------------------------------MENUS--------------------------------------------------------------------

function generate_items(zone,buttons,id_prefix,items) {
	menu_status[zone].items=new Array();
	menu_status[zone].items_width=0;
	for (x=0; x<items;x++) {
		tmp=new Array();
		tmp.id=id_prefix+x;
		tmp.display='block';
		tmp.width=checkWidth(tmp.id);
		if ((config[browser])&&(config[browser][zone])) {
			if (config[browser][zone].items.margin_right) { tmp.width+=config[browser][zone].items.margin_right; }
			if (config[browser][zone].items.margin_left) { tmp.width+=config[browser][zone].items.margin_left; }
			if (config[browser][zone].items.padding_left) { tmp.width+=config[browser][zone].items.padding_left; }
		}
		menu_status[zone].items.push(tmp);
		menu_status[zone].items_width+=tmp.width;		
	}
	menu_status[zone].buttons_width=checkWidth(buttons);
}

function menus_display(zone,buttons,id_prefix,items) {

	if (!menu_status[zone]) { menu_status[zone]=new Array(); }
	//THIS SHOULD BE IN THE CONDITION ABOVE!!!
	menu_status[zone].items_start=0;
	menu_status[zone].items_end=0;
	//GENERATION OF ITEMS
	if ((!menu_status[zone].items)||(menu_status[zone].items.length==0)) { generate_items(zone,buttons,id_prefix,items); }
	menu_status[zone].display_width=checkWidth(zone);
	menu_status[zone].display_width-=menu_status[zone].buttons_width;
	tmp_length=0;
	for (x=0; x<menu_status[zone].items.length;x++) {
		if ((tmp_length+menu_status[zone].items[x].width)>menu_status[zone].display_width) {
			menu_status[zone].items[x].display='none';
			if ((tmp_length<menu_status[zone].display_width)&&(menu_status[zone].items_end==0)) { menu_status[zone].items_end=x-1; }
		} else if (menu_status[zone].items[x].display=='none') { menu_status[zone].items[x].display='block'; }
		gbid(menu_status[zone].items[x].id).style.display=menu_status[zone].items[x].display;
		tmp_length+=menu_status[zone].items[x].width;
	}
	if (menu_status[zone].display_width<menu_status[zone].items_width) {
		generate_menu_button(zone,buttons,'block',menu_status[zone].items_start,menu_status[zone].items_end,menu_status[zone].items.length);
	} else { 
		generate_menu_button(zone,buttons,'none',0,0,0);
	}
	
}

function generate_menu_button(zone,buttons,display,start,end,length) {
	html='';
	if (gbid(buttons).innerHTML) {
		if (display!='none') {
			if (start==0) { html+=config['menu_button_previous_off']; } 
			else { html+='<a href="javascript:menu_goto('+"'"+zone+"'"+','+"'"+buttons+"'"+',-1)">'+config['menu_button_previous']+'</a>'; }
			if (end==(length-1)) { html+=config['menu_button_next_off']; }
			else { html+='<a href="javascript:menu_goto('+"'"+zone+"'"+','+"'"+buttons+"'"+',1)">'+config['menu_button_next']+'</a>'; }
			gbid(buttons).innerHTML=html;
		}
		gbid(buttons).style.display=display;
	}
}

function menu_goto(zone,buttons,way) {
	if (menu_status[zone]) {
		menu_status[zone].items_start+=way;
		
		//IT MAY BE THAT THE ITEM TO SHOW IS TOO BIG => 2 OR MORE MUST BE HIDDEN;
		tmpW=0;
		for (x=menu_status[zone].items_start; x<menu_status[zone].items.length; x++) {
			if ((tmpW+menu_status[zone].items[x].width)<=menu_status[zone].display_width) {
				tmpW+=menu_status[zone].items[x].width;
				menu_status[zone].items_end=x;
			} else { break; }
		}
		for (x=0; x<menu_status[zone].items.length;x++) {
			if (x<menu_status[zone].items_start) { menu_status[zone].items[x].display='none'; }
			else if (x>menu_status[zone].items_end)  { menu_status[zone].items[x].display='none'; }
			else { menu_status[zone].items[x].display='block'; }
			gbid(menu_status[zone].items[x].id).style.display=menu_status[zone].items[x].display;		
		}
		generate_menu_button(zone,buttons,'block',menu_status[zone].items_start,menu_status[zone].items_end,menu_status[zone].items.length);
	}
}


//BASIC FUNCTIONS - USE 'menu_status' ARRAY

function switch_style(zone,id,styleSel,styleNormal,way) {
	if (!(menu_status[zone].sel)) { menu_status[zone].sel=''; } 
	if (!(menu_status[zone].styleSel)) { menu_status[zone].styleSel=styleSel; }
	if (!(menu_status[zone].styleNormal)) { menu_status[zone].styleNormal=styleNormal; }
	if (way==1) { gbid(id).className=menu_status[zone].styleSel; }
	if (way==0) { if (menu_status[zone].sel!=id) { gbid(id).className=menu_status[zone].styleNormal; }	}
}

function select_menu(zone,id,items_num,c_type) {
	if (menu_status[zone].sel!='') { gbid(menu_status[zone].sel).className=menu_status[zone].styleNormal; }
	if (id!='none') {
		menu_status[zone].sel=id;
		submenus_items=items_num;
		if (items_num>0) {
			subHtml=gbid('submenus_'+id).innerHTML;
			subHtml=subHtml.substring( (subHtml.indexOf('<!--START')+9),(subHtml.indexOf('END-->')) );
			gbid('submenus_zone').innerHTML=subHtml;
			if (menu_status['submenus_zone']) { menu_status['submenus_zone'].items=new Array(); }
			menus_display('submenus_zone','nextprevious_submenus','sm',submenus_items);
			
			if ((c_type=='catalogue')&&(library_open==false)) {
				open_close_library();
			}
			
		} else {
			gbid('submenus_zone').innerHTML='&nbsp;';
			if (c_type=='file') {
				if (content_type=='catalogue') { var prefix='CAT'; }
				else { var prefix='TEX'; }
				subHtml=gbid('submenus_'+id).innerHTML;
				tmpItemsNum=parseInt(subHtml.substring((subHtml.indexOf('<!--START_NUM_'+prefix+'-')+18),(subHtml.indexOf('-'+prefix+'_NUM_END-->'))));
				subHtml=subHtml.substring( (subHtml.indexOf('<!--START_'+prefix)+13),(subHtml.indexOf(prefix+'_END-->')) );
				gbid('submenus_zone').innerHTML=subHtml;
				if (menu_status['submenus_zone']) { menu_status['submenus_zone'].items=new Array(); }
				menus_display('submenus_zone','nextprevious_submenus','sm',tmpItemsNum);
			}
		}
	}
	if (c_type!='welcome') { 
		if (c_type!='file') { adapt_layout(c_type); }
		if (c_type!='file') { send_search('search_form'); }
	}
	fix_ie_title();
}


//------------------------------MENUS--------------------------------------------------------------------





//------------------------------LAYOUT--------------------------------------------------------------------
function adapt_layout(c_type) {

	switch (c_type) {
		case 'text':
			
			if (browser == "Internet Explorer") { document.getElementById('browse').style.display='block'; }
			else { document.getElementById('browse').style.display='table-cell'; }
			
			//CATALOGUE -> must be done before because the last choices of the user will be stored
			clean_catalogue();
						
			//SEARCH
			searchHtml=gbid('search_text').innerHTML;
			searchHtml=searchHtml.substring( (searchHtml.indexOf('<!--START')+9),(searchHtml.indexOf('END-->')) );
			gbid('search_display').innerHTML=searchHtml;
			gbid('list').innerHTML='';
			
			//send_search('search_form');
			
			//hide the browse view in all text pages
			if (library_open) { open_close_library(); }
			gbid('browse').style.display='none';
			
			break;
			
		case 'catalogue':
		
			//gbid('text_frame').src = '../pages/blank.php';
		
			if (browser == "Internet Explorer") { document.getElementById('browse').style.display='block'; }
			else { document.getElementById('browse').style.display='table-cell'; }
			
			if (browser == "Internet Explorer") { document.getElementById('footer').style.display='block'; }
			else { document.getElementById('footer').style.display='table-cell'; }
			
			searchHtml=gbid('search_catalogue').innerHTML;
			searchHtml=searchHtml.substring( (searchHtml.indexOf('<!--START')+9),(searchHtml.indexOf('END-->')) );
			gbid('search_display').innerHTML=searchHtml;
			
			gbid('list').innerHTML='';
			
			//send_search('search_form');
			
			if (library_open===false) { open_close_library(); }
			
			break;
	}
	
	content_type=c_type;
	resize_list();
	
}
//------------------------------LAYOUT--------------------------------------------------------------------





//------------------------------LIBRARY--------------------------------------------------------------------

function open_close_library() {
	if (gbid('library')) {
		if (library_open) {
			library_open=false;
			gbid('library').style.display='none';
			gbid('browse_open').innerHTML='<a href="javascript:open_close_library()">'+trad.labels['open']+'&nbsp;'+config['browse_open']+'</a>';
			//VIEW
			resize_list();
			gbid('view_zone').style.width=checkWidth('root')+'px';
			
		} else {
			library_open=true;
			if (browser == "Internet Explorer") { document.getElementById('library').style.display='block'; }
			else { document.getElementById('library').style.display='table-cell'; }
			gbid('browse_open').innerHTML='<a href="javascript:open_close_library()">'+trad.labels['close']+'&nbsp;'+config['browse_close']+'</a>';
			//VIEW
			resize_list();
			//LOAD ALL
			send_search('search_form');
			gbid('view_zone').style.width=(checkWidth('root')-items_width.library)+'px';
		}
		menus_display('menus_zone','nextprevious_menus','m',menus_items);
		if (submenus_items!=0) { menus_display('submenus_zone','nextprevious_submenus','sm',submenus_items); }
	}
}

function resize_list() {
	tmpH=checkHeight('root');
	if (items_height.length==0) { 
		items_height.header=checkHeight('header');
		items_height.menus=checkHeight('menus');
		//IF PADDING TOP ON THE MENUS -> CHECK IN config
		if ((config[browser])&&(config[browser]['menus_zone'])) {
			if (config[browser]['menus_zone'].items.padding_top) { items_height.menus+=config[browser]['menus_zone'].items.padding_top; }
		}
		items_height.submenus=checkHeight('submenus');
		items_height.search_zone=checkHeight('search_zone');
		if ((config[browser])&&(config[browser]['search_zone'])) {
			if (config[browser]['search_zone'].items.margin_top) { 
				items_height.search_zone+=config[browser]['search_zone'].items.margin_top; }
			if (config[browser]['search_zone'].items.margin_bottom) {
				items_height.search_zone+=config[browser]['search_zone'].items.margin_bottom; }
		}
		items_height.footer=checkHeight('footer');
	}
	
	//VIEW
	items_height.view_zone=tmpH-(items_height.header+items_height.menus+items_height.submenus);
	if (content_type=='catalogue') { items_height.view_zone-=items_height.footer; }
	gbid('view_zone').style.height=items_height.view_zone+'px';
	gbid('list').style.height=((items_height.view_zone+items_height.submenus)-items_height.search_zone)+'px';
	
	
	tmpW=checkWidth('root');
	if (library_open) {
		if (items_width.length==0) { items_width.library=checkWidth('library'); }
		items_width.view_zone=tmpW-items_width.library;
		gbid('view_zone').style.width=items_width.view_zone+'px';
	} else {
		items_width.view_zone=tmpW;
		gbid('view_zone').style.width=items_width.view_zone+'px';		
	}
	//will fix the IE display problem
	IE_fix('vrml_frame');
	
}

function clean_form(id) {
	if (id=='search_form') {
		gbid('CAT_SELECT').value='';
		gbid('CAT_TEXT').value='';
		send_search(id);
	}
}

function send_search(id) {
	if (gbid(id)) { gbid(id).submit(); }
	if ((content_type=='catalogue') || ((gbid('PAGE_TEXT'))&&(gbid('PAGE_TEXT').value!='')) ) {
		gbid('list').innerHTML='<div class="text"><div class="text_preview">'+trad.labels['loading']+'</div></div>';
	}
}

function load_details(id) {
	gbid('details_id').value=id;
	gbid('details_form').submit();
}

function load_details_from_menu(id) {
	//alert(last_choice); 
	if (last_choice['id']!='none') { id=last_choice['id']; }
	if (last_choice['CAT_TEXT']!='none') { gbid('CAT_TEXT').value=last_choice['CAT_TEXT']; }
	if (last_choice['CAT_SELECT']!='none') { gbid('CAT_SELECT').value=last_choice['CAT_SELECT']; }
	if ((last_choice['CAT_TEXT']!='none')||(last_choice['CAT_SELECT']!='none')) {
		send_search('search_form');
	}
	gbid('details_id').value=id;
	gbid('details_form').submit();
}

function openFile(type) {  }

//------------------------------LIBRARY--------------------------------------------------------------------










//------------------------------VIEW-CATALOGUE--------------------------------------------------------------------
function swicth_view(id,view) {
	if (gbid('view_text').style.display!='none') { 
		gbid('view_text').style.display='none';
		
	}
	if (!((current_view==view)&&(current_id==id))) {
		if (view=='current') {
			if (current_view!='') { view=current_view; }
			else { view='view_wireframe'; }
		}
		if (current_view!='') {
			gbid(current_view).style.display='none';
			gbid(current_view).innerHTML='';
			button='button_'+current_view;
			gbid(button).className=config['button_view_bg'];
		}
		gbid(view).style.display='block';
		button='button_'+view;
		
		gbid(button).className=config['button_view_bg_over'];

		switch (view) {
		
			case 'view_wireframe':
				gbid(view).innerHTML='<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" WIDTH="90%" HEIGHT="90%"><PARAM NAME=movie VALUE="biblio/wireframe/'+id+'.swf"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#ffffff><EMBED SRC="biblio/wireframe/'+id+'.swf" QUALITY=high BGCOLOR=#ffffff WIDTH="90%" HEIGHT="90%" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED></OBJECT>';
				break;
				
			case 'view_vrml':
				vhtml='';
				
				vhtml+='<object classid="clsid:86A88967-7A20-11d2-8EDA-00600818EDB1" id="vrml_frame" width="100%" height="100%" codebase="http://www.parallelgraphics.com/bin/cortvrml.cab#Version=4,2,0,93"> ';
				vhtml+='<param name="scene" value="biblio/wrl/'+id+'.WRL"> ';
				vhtml+='<param name="vrml_background_color" value="#FFFFFF"> ';
				vhtml+='<param name="vrml_splashscreen" value="false"> ';
				vhtml+='<param name="vrml_dashboard" value="true"> ';
				vhtml+='<param name="ColliderMode" value="0"> ';
				vhtml+='<param name="Skin" value="{46BB95BF-8EB4-481A-A1EF-50D43FC32B9D}; style/cad.zip"> ';
				
				vhtml+='<embed type="x-world/x-vrml" ';
				vhtml+='src="biblio/wrl/'+id+'.WRL" ';
				vhtml+='width="100%" ';
				vhtml+='height="100%" ';
				vhtml+='VRML_BACKGROUND_COLOR="#FFFFFF" ';
				vhtml+='VRML_SPLASHSCREEN="false" ';
				vhtml+='VRML_DASHBOARD="true" ';
				vhtml+='collidermode="0" ';
				vhtml+='Skin="{46BB95BF-8EB4-481A-A1EF-50D43FC32B9D}; style/cad.zip" ';
				vhtml+='align="middle" frameborder="no"> ';
				
				vhtml+='</object> ';
				gbid(view).innerHTML=vhtml;
											
				break;
				
			case 'view_2d':
				gbid(view).innerHTML='<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" WIDTH="90%" HEIGHT="90%"><PARAM NAME=movie VALUE="biblio/2d/'+id+'.swf"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#ffffff><EMBED SRC="biblio/2d/'+id+'.swf" QUALITY=high BGCOLOR=#ffffff WIDTH="90%" HEIGHT="90%" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED></OBJECT>';
				break;
		}
		current_id=id;
		current_view=view;
		IE_fix('vrml_frame');
	}
	fix_ie_title();
}
//------------------------------VIEW-CATALOGUE--------------------------------------------------------------------




//------------------------------VIEW-PAGE--------------------------------------------------------------------

function load_page(url) {
	if (gbid('view_text').style.display!='block') { gbid('view_text').style.display='block';  }
	gbid('text_frame').src=url;
	if (gbid('PAGE_ID')) { gbid('PAGE_ID').value=url; }
}

function clean_catalogue() {
	if (current_view!='') {
		gbid(current_view).style.display='none';
		gbid(current_view).innerHTML='';
		gbid(button).className=config['button_view_bg'];
	}
	if (current_id!='') { last_choice['id']=current_id; current_id=''; }
	if (gbid('CAT_TEXT')) { last_choice['CAT_TEXT']=gbid('CAT_TEXT').value; }
	if (gbid('CAT_SELECT')) { last_choice['CAT_SELECT']=gbid('CAT_SELECT').value; } 
	
	current_id='';
	gbid('footer').style.display='none';
	//gbid('cutout_content').innerHTML='';
	gbid('description_content').innerHTML='';
}

//------------------------------VIEW-PAGE--------------------------------------------------------------------




//------------------------------INIT FUNCTIONS--------------------------------------------------------------------

function init() {
	resize_list();
	menus_display('menus_zone','nextprevious_menus','m',menus_items);
	if (submenus_items!=0) { menus_display('submenus_zone','nextprevious_submenus','sm',submenus_items); }
	if (content_type=='welcome') {
		if (gbid('text_frame').src!=trad.pages['default']) { load_page(trad.pages['default']); }
		if (gbid('library').style.display!='none') { open_close_library(); }
		if (gbid('browse').style.display!='none') { gbid('browse').style.display='none'; }
		if (gbid('footer').style.display!='none') { gbid('footer').style.display='none'; }
	}
}

function reinit() {
	select_menu('menus_zone','none',0,'text');
	gbid('submenus_zone').innerHTML='';
	clean_catalogue();
	if (library_open) { 
		gbid('library').style.display='none';
		library_open=false;
	}
	if (gbid('browse').style.display!='none') { 
		gbid('browse').style.display='none';
		gbid('browse_open').innerHTML='<a href="javascript:open_close_library()">'+trad.labels['open']+'&nbsp;'+config['browse_open']+'</a>';
	}
	load_page(trad.pages['default']);
	//related to menus
	submenus_items=0;
	menu_status=new Array();
	//related to catalogue
	current_id='';
	last_choice=new Array();
	last_choice['id']='none'; last_choice['CAT_TEXT']='none'; last_choice['CAT_SELECT']='none';
	current_view='';
	//related to browse
	items_height=new Array();
	items_width=new Array();
	//related to the browse
	content_type='welcome';
	init();
}

//------------------------------INIT FUNCTIONS--------------------------------------------------------------------

//------------------------------CORTONA FUNCTIONS--------------------------------------------------------------------

function cortona_installed()
{
   var f = false;
   for (i = 0; i < navigator.plugins.length; i++)
   {
    	pl = navigator.plugins[i];
		for (j = 0; j < pl.length; j++)
		{
    		mime = pl[j];
	 		if (mime.type.indexOf('vrml') != -1 && 
		        mime.suffixes.toLowerCase().indexOf('wrl') != -1 &&
	   		    mime.enabledPlugin.name == pl.name &&
		   	    pl.name.toLowerCase().indexOf('cortona') != -1)
			    {
					cortonaversion = pl.description.substring(pl.description.indexOf("(") + 1, pl.description.indexOf(")"));
					if (!cortonaversion.length) {
						cortonaversion = "2.2 or lower";
						cortonabuild = 40;
					} else {
						cortonabuild = cortonaversion.substring(cortonaversion.lastIndexOf(" "), cortonaversion.length);
					}
					f = true; break;
				}
		}
		if (f) break; 
	}	
	return f;
}

//------------------------------CORTONA FUNCTIONS--------------------------------------------------------------------


//SEEK IE AND VBSCRIPT
/*
function timeLoop() {
	if (document.body) { document.title="BODY HERE!!!"; }
}
window.setInterval('timeLoop()',100);*/
