/*
	ORK MANAGER: FANTABRAWL
	(c) 2006-2010 Lorenzo Petrone, tutti i diritti riservati (all rights reserved).
	http://looris.net , looris@gmail.com
*/

/*	TODO:
	°° unificare apvalue e apinner
*/

function apdispv (name, disp, vis) {
	var el=document.getElementById(name);
	if (!el) return false;
	el.style.display=disp;
	el.style.visibility=vis;
	return true;
}
function swi (name) {
	swii(name,false);
}
function swii (name, do_button, class1, class2) {
	do_button = typeof(do_button) != 'undefined' ? do_button : true;
	class1 = typeof(class1) != 'undefined' ? class1 : 'menu_title_collapsed';
	class2 = typeof(class2) != 'undefined' ? class2 : 'menu_title';
	
	var el=document.getElementById(name);
	if (!el) return;
	
	if (do_button) {
		var elb=document.getElementById(name+'_button');
		if (!elb) return;
	}
	
	if (el.style.display=='') {
		el.style.display='none';
		if (do_button) {
			elb.className=class1;
		}
	}
	else {
		el.style.display='';
		if (do_button) {
			elb.className=class2;
		}
	}
}
function doubleswi (one, two) {
	swi(one);
	swi(two);
}
function plusmin (id, val) {
	var el=document.getElementById(id);
	el.value=parseInt(el.value)+parseInt(val);
	if (parseInt(val)>0) {
		apdisp('sbutton','none');
	}
}
function show_x (x) {
	// TODO°° rimuoverlo in favore di button_*
	show_none();
	apdisp(x,'');
	apclass(x+'_button','pbutton');
	location.replace('#'+x);
}

function button_hide (thing) {
	apdisp(thing,'none');
	apclass(thing+'_button','button');
}
function button_show (thing) {
	button_none();
	apdisp(thing,'');
	apclass(thing+'_button','pbutton');
}
function buttonx_hide (thing, class0) {
	apdisp(thing,'none');
	apclass(thing+'_button',class0);
}
function buttonx_show (thing, class0) {
	button_none();
	apdisp(thing,'');
	apclass(thing+'_button',class0);
}

function load_help (name, tutorial) {
	help_wait();
	
	req=new XMLHttpRequest();
	req.onreadystatechange=load_help_handler;
	req.open("GET",AJURL+"load_help?page="+name+"&tutorial="+tutorial);
	req.send(null);
}
function load_help_handler () {
	if (req.readyState!=4 || req.status!=200)
		return;
	
	rrt=req.responseText;
	status=rrt.substr(0,2);
	message=rrt.substr(3); // NOTA: salta un carattere che è un \n
	
	if (status=="OK") {
		document.getElementById('help').innerHTML='<div class="help pd6">'+message+'</div>';
	}
	else {
		// errore
		document.getElementById('help').innerHTML='<div class="small error">'+message+'</div>';
	}
}

function selg (g,pre) {
	pre = typeof(pre) != 'undefined' ? pre : '';
	
	document.getElementById(pre+'gold_type').value=g;
	
	apdisp(pre+'sel_v_shiny','none');
	apdisp(pre+'sel_v_dirty','none');
	apdisp(pre+'sel_v_liquid','none');
	apdisp(pre+'sel_x_shiny','');
	apdisp(pre+'sel_x_dirty','');
	apdisp(pre+'sel_x_liquid','');
	apclass(pre+'sel_shiny','');
	apclass(pre+'sel_dirty','');
	apclass(pre+'sel_liquid','');
	
	apdisp(pre+'sel_x_'+g,'none');
	apdisp(pre+'sel_v_'+g,'');
	apclass(pre+'sel_'+g,'seln');
}

function adjheights (id) {
	var hish=apheight(id,'offset');
	var toth=apheight('body');
	var winh=parseInt(window.innerHeight);
	
	var maxh=winh-toth+hish;
	if (maxh+toth>winh) maxh-=bodyborder;
	apmaxheight(id,maxh);
	;
}
function adj_heights () {
	var hmax=0;
	for (var i=1; ; i++) {
		var el=document.getElementById('maxh'+i);
		if (!el) break;
		var id='maxh'+i;
		
		var hish=apheight(id,'offset');
		var toth=apheight('body');
		var winh=parseInt(window.innerHeight);
		
		var maxh=winh-toth+hish;
		if (maxh+toth>winh) maxh-=bodyborder;
		
		if (maxh>hmax) hmax=maxh;
	}
	for (var ii=1; ii<i; ii++) {
		apmaxheight('maxh'+ii,hmax);
	}
}

function maxhfull (base, maxhdiv) {
	maxhdiv = typeof(maxhdiv) != 'undefined' ? maxhdiv : 'maxh';
	
	var creh=apheight('credits_bar');
	if (document.getElementById('mantainance_div') && maxhdiv!='menu_maxh') creh+=apheight('mantainance_div');
	var winh=parseInt(window.innerHeight);
	var tooh=winh-creh-base-12;
	//alert('creh:'+creh+' — winh:'+winh+' — base:'+base+' — tooh:'+tooh);
	
	apmaxheight(maxhdiv,tooh);
}
function maxwfull (base) {
	var crew=apwidth('menu_td');
	var winw=parseInt(window.innerWidth);
	var toow=winw-crew-base-16;
	//alert('crew:'+crew+' — winw:'+winw+' — base:'+base+' — toow:'+toow);
	apmaxwidth('maxh',toow);
}

document.onkeypress = kill_enter;
if (document.layers) document.captureEvents(Event.KEYPRESS);
