var active=null;
var action='paste';
var textbox;
var prevValue;
var prevAction;

if (navigator.appName.indexOf('Microsoft')!=-1)
	alert('zmien przegladarke ;/');

function resize() {
	var width = window.innerWidth;
	var height = window.innerHeight;
	var optWidth;

	if (width*.3 > 200) optWidth = 190;
	else optWidth = width*.3 - 10;

	var options = document.getElementById('options');
	options.style.width = optWidth + 'px';
	options.style.height = height-125 + 'px';

	var textbox = document.getElementById('textbox');
	textbox.style.width = width-optWidth-25 + 'px';
	textbox.style.height = height-55 + 'px';

	var button = document.getElementById('button');
	button.style.width = options.style.width;
}

window.onresize = resize;

function hideAll() { // ukrywanie wszystkich pozycji na liscie.
	trace('hideall');
	var tags = document.getElementById('main-ul').getElementsByTagName('ul');
	for (var i=0; i<tags.length; i++) {
		//tags[i].style.color='red';
		tags[i].style.display='none';
		tags[i].parentNode.className='';
	}
}

function trace(txt) { // dopisywanie do texboxa
	//textbox.value+=txt+"\r\n";
}

function undo() {
	textbox=document.getElementById('textbox');
	var tmp = textbox.value;
	textbox.value = prevValue;
	prevValue = tmp;
}

function clear() {
	prevValue = textbox.value;
	textbox.value = '';
}

function setAction(a) {
	if (a=='') {
		document.getElementById('button').disabled=true;
		document.getElementById('button').value='[ select an action ]';
	}
	else {
		document.getElementById('button').disabled=false;
		document.getElementById('button').value='Go! ('+a+')';
		document.getElementById('action').value=a;
	}
	action=a;
}

function select (obj) {
	trace('select '+obj.id);

	var prev=active;
	active=obj;

	setAction(obj.id);

	//ukrywanie poprzedniego zaznaczenia
	if (prev) {
		prev.parentNode.className='';
	}
	hideAll();
	var tagi=obj.parentNode.getElementsByTagName('ul')
	if (tagi.length!=0) {
		tagi[0].style.display='block';
	}

	//pokazywanie wszystkich elementow nadrzednych
	var p=obj.parentNode;
	while (p.id!='main-ul') {
		if (p.style.display=='none')
			p.style.display='block';
			p.className='active2';
		var p=p.parentNode;
	}
	obj.parentNode.className='active';

}

function show() { // pokazywanie wybranego elementu.
	select (this);
}

function init() {
	textbox=document.getElementById('textbox');
	trace ('init...');
	textbox.focus();

	advAJAX.assign(document.getElementById("form"), {
	    onInitialization : function(obj) {
	    	prevValue = textbox.value;
	    },
	    onSuccess : function(obj) {
	    	textbox.value=obj.responseText;
	    },
	    onError : function(obj) { textbox.value="Error: " + obj.status; },
	    disableForm : false
	});

	hideAll();
	var tags = document.getElementById('options').getElementsByTagName('div');
	for (var i=0; i<tags.length; i++) {
		tags[i].onclick=show;
		// w sumie mozna by na css to zrobic, ale na IE nie zadziala
		//tags[i].onmouseover=function() {this.style.border='1px solid red';};
		//tags[i].onmouseout=function() {this.style.border='';};
		//tags[i].style.display='none';
	}

	tags = document.getElementById('options').getElementsByTagName('input');
	for (var i=0; i<tags.length; i++)
		tags[i].parentNode.onclick=function() {
			var a=this.getElementsByTagName('input');
			if (a.length!=0)
				a[0].checked=true; // dla radiobuttonow, dla reszty moze byc zle...
		};

	resize();

	select(document.getElementById(action));

	trace ('init done.');
}

window.onload=init;

