function getelement(id) { return document.getElementById(id); }

function notifybrowsers() 
{
	var wm = window._yass_wmediator;
	var wenum = wm.getEnumerator(null);
	while(wenum.hasMoreElements()){
		var w = wenum.getNext();
		if(("yass" in w) && ("refresh_preferences" in w.yass)) { w.yass.refresh_preferences(); }
	}
}

function updatebar(id)
{
	getelement(id+"_s").setAttribute("value", getelement(id+"_t").value);
	// this automatically call slideshcange function
}

function sliderchange(event)
{
	updatetextbyid(event.target.id.slice(0,-2));

	if(window._yass_initializing == false)
	{
		updategraph(getelement("inputmethodtabbox").selectedTab.id);	
		onaccept();
	}
}


function updategraph(prefix)
{
	if((new Date()).getTime() - window._yass_lastupdategraph < 80) return;
	window._yass_lastupdategraph = (new Date()).getTime();

	var step = getelement(prefix + "step_t").value;
	var bdump = getelement(prefix + "bdumping_t").value/890 + 0.01;
	var edump = (900 - getelement(prefix + "dumping_t").value)/1000;
	var prefversion = getelement("prefversion").value;
	
	var c = getelement("grapharea").getContext("2d");
	c.fillStyle="#fff";
	c.fillRect(0,0,240,100);

	// draw guid line
	c.fillStyle="#aaa";
	for(var t = 40; t < 240; t+=40){
		c.fillRect(t,0,1,100);
	}
	
	c.fillStyle="#999";
	c.strokeStyle="#000";
	c.beginPath();
	c.moveTo(500,500);
	c.lineTo(0,500);

	var dest = (60.0 / 300.0) * step + 40.0 ;
	var d = 0;
	var bdumpbase = bdump * 110.0;
	var tipw = 4;
	for(var t = 0; t <= 60; t++)
	{
		if(d + 0.3 > dest) { break;}
		var bd;
		if(prefversion == 2)
			bd = Math.min(Math.max(((t+1)/(bdumpbase))+0.2,0.05),1.0);
		else
			bd = Math.min(Math.max(((t+1)/(bdumpbase)),0.05),1.0);
		d += (dest - d) * edump * bd ;
		c.lineTo(t*tipw,100-(dest-d));
	}
	c.stroke();
	c.fill();
}

function updatetextbyid(id)
{
	getelement(id+"_t").value = getelement(id+"_s").getAttribute("value");

	var elm = null;

	elm = getelement(id+"_tr");
	if(elm) elm.value = getelement(id+"_t").value / 100; // tr

	elm = getelement(id+"_ts");
	if(elm) elm.value = (getelement(id+"_t").value * (100.0 / 890.0)).toPrecision(3); // ts
}

function inittab(id)
{
	var prefs = getelement("preset_"+id).value.split(",");

	window._yass_initializing = true;
	for(var i in idlist) 
	{
		getelement(idlist[i]+"_t").value = prefs[i];
 		updatebar(idlist[i]); 
	}
	updategraph(getelement("inputmethodtabbox").selectedTab.id);
	window._yass_initializing = false;
}

function selecttab(id)
{
	getelement("tabpanel").style.backgroundImage = "url('chrome://yass/content/"+id+"bg.png')";

	getelement("wheelcaption").style.color = colorlist[id];
	getelement("keyboardcaption").style.color = colorlist[id];

	// restore values
	inittab(id);

	getelement("selectedpreset").value = id;
	getelement("pane").writePreferences(true);
	notifybrowsers();
}

function selectmethodtab(prefixmain,prefixreset)
{
	updategraph(prefixmain);
}

function onaccept()
{
	var pref = new Array();
	for(var i in idlist) 
	{ 
		pref.push(getelement(idlist[i]+"_t").value);
	}
	getelement("selectedpreset").value = getelement("tabbox").selectedTab.id;
	getelement("preset_" + getelement("selectedpreset").value).value = pref.toString();
	getelement("pane").writePreferences(true);
	notifybrowsers();

	return true;
}

function onapplymisc()
{
	getelement("usekbd").value = getelement("usekbd_c").checked;
	getelement("usepagejump").value = getelement("usepagejump_c").checked;
	getelement("usewholejump").value = getelement("usewholejump_c").checked;
	getelement("edgetype").value = getelement("edgetypemenu").value;

	getelement("panedisplay").writePreferences(true);
	notifybrowsers();
}

function onchangesmoothalg()
{
	getelement("prefversion").value = getelement("prefversionmenu").value;

	getelement("panedisplay").writePreferences(true);
	notifybrowsers();
	updategraph(getelement("inputmethodtabbox").selectedTab.id);
}

function ondialogok()
{
	getelement("panedisplay").writePreferences(true);
	notifybrowsers();
	getelement("prefwindow_yass").lastSelected="pane";
}

function ondialogcancel()
{
	// restore backup value 
	for(i in paramlist){
		getelement(paramlist[i]).value = window._yass_backup[paramlist[i]];
	}
	// save it 
	getelement("panedisplay").writePreferences(true);
	getelement("pane").writePreferences(true);
	
	notifybrowsers();
	getelement("prefwindow_yass").lastSelected="pane";
}

function closehelptips()
{
	var tips = document.getElementsByTagName("tooltip");
	try{for(i in tips){tips[i].hidePopup();}}catch(e){}
}

function onhelp(e)
{
	closehelptips();
	getelement((getelement("prefwindow_yass").currentPane.id == "pane")?"helpmain":"helpmisc").openPopup(e.target,"before_start",0,0,false,null);
}

// onload //

var idlist = [
"wheelstep","wheeldumping","wheelbdumping","wheelaccel","kbdstep","kbddumping","kbdbdumping","kbdaccel"
];

var paramlist = [
"preset_red","preset_green","preset_blue","selectedpreset","usekbd","usepagejump","usewholejump","edgetype","prefversion","usepixelscroll"
];

var colorlist = {red: "#cc0000", green: "#008800", blue: "#0000cc"};


function onpaneload0(p)
{
	// spacer for mac os
	var PS = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPref);
	if(PS.GetBoolPref("browser.preferences.animateFadeIn") == false) {
		getelement("spacer4mac1").setAttribute("height","0");
		getelement("spacer4mac2").setAttribute("height","0");
	}

	// global parameters
	window._yass_lastupdategraph = 0;
	window._yass_wmediator = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);

	// save current parameters
	var obj = {}
	for(i in paramlist){ obj[paramlist[i]] = getelement(paramlist[i]).value; }
	window._yass_backup = obj;

	// regist help button behavior
	getelement("buttonhelp").addEventListener("click",onhelp,false);
	getelement("buttonhelp").addEventListener("blur",closehelptips,false);

	// listener on slider
	for(i in idlist) {
		getelement(idlist[i] + "_s").addEventListener("change",sliderchange,false);
	}

	// select preset tab
	var stab = getelement("selectedpreset").value;
	if(!stab) stab = getelement("tabbox").selectedTab.id;
	getelement("tabbox").selectedTab = getelement(stab);
	selecttab(stab);
}
