//zwinięte z http://ka.wikipedia.orghttps://wikines.com/pl/მედიავიკი:Common.js

function tableInit()
{
	if(document.getElementById('wpTextbox1'))
	{
		var tableBoxCode =
'<h5>Generowanie tabeli</h5>'+
'<form onsubmit="tableGenerate(parseInt(document.paramForm.inputCol.value), parseInt(document.paramForm.inputRow.value), parseInt(document.paramForm.inputBorder.value), document.paramForm.inputHeader.checked, document.paramForm.inputLine.checked, document.paramForm.inputItems.checked, document.paramForm.inputAlign.value, document.paramForm.inputPad.checked);return false" name="paramForm">'+
'<label for="inputAlign">Wyrównanie (<tt>align</tt>):</label> '+
	'<input type="text" name="inputAlign" id="inputAlign" value="center"><br />'+
'<label for="inputRow">Wiersze:</label> '+
	'<input type="text" name="inputRow" id="inputRow" value="3"><br />'+
'<label for="inputCol">Kolumny:</label> '+
	'<input type="text" name="inputCol" id="inputCol" value="3"><br />'+
'<label for="inputHeader">Tytuł:</label> '+
	'<input type="checkbox" name="inputHeader" id="inputHeader" checked="1"><br />'+
'<label for="inputBorder">Obramowanie:</label> '+
	'<input type="text" name="inputBorder" id="inputBorder" value="1"><br />'+
'<label for="inputPad">Padding:</label> '+
	'<input type="checkbox" name="inputPad" id="inputPad"><br />'+
'<label for="inputLine">Kolorowanie wierszy:</label> '+
	'<input type="checkbox" name="inputLine" id="inputLine" checked="1"><br />'+
'<label for="inputItems">Nagłówki w pionie:</label> '+
	'<input type="checkbox" name="inputItems" id="inputItems">'+
'</form">'+
'<p><input type="submit" value="Wstaw kod" /></p>'

		csslink=document.createElement('link')
		csslink.rel="stylesheet"
		csslink.type="text/css"
		csslink.href="http://pl.wikipedia.org/w/index.php?title=Wikipedysta:Matma_Rex/table.css&action=raw&ctype=text/css&dontcountme=s"
		document.getElementsByTagName('head').appendChild(csslink)

		var tableBox=document.createElement('div');
		tableBox.style.cssText='border:1px solid #ccc; background:#eee; font-size:80%; display:none'
		tableBox.id='insertingTable'
		tableBox.innerHTML=tableBoxCode
 
		el=document.getElementById('editform');
		el.parentNode.insertBefore(tableBox,el);
	}
}

function tableShowHide()
{
obj=document.getElementById('insertingTable')
if(obj.style.display=='none') obj.style.display=''
else obj.style.display='none'
}

function tableGenerate(nbCol, nbRow, border, styleHeader, styleLine, exfield, align, padding)
{
	var code=''
	
	if(styleHeader)
	{
		code += '{| style="'+(align=='center'?'margin:0 auto':'float:'+align)+';border:'+border+'px solid black;text-align:center;background:white"\n';
		code += "|'''Tytuł'''\n";
		
		if(padding)
		{
			code += '{| class="wikitable" cellpadding="0" cellspacing="0" style="margin:1em"'+"\n"
		}
		else
		{
			code += '{| class="wikitable" cellpadding="0" cellspacing="0" style="margin:0;margin-top:1em"'+"\n"
		}
	}
	else
	{
		code += '{| style="'+(align=='center'?'margin:0 auto':'float:'+align)+';border:'+border+';text-align:center;background:white"\n';
		code += '|+\n';
	}
	
	if(exfield) code += '!\n';
	
	for (i=0;i<nbCol;i++)
	{
		code += '!style="background:#dfd"|Nagłówek '+(i+1)+'\n'
	}
	
	for (j=0,items=0;j<nbRow;j++)
	{
		if((j+1)%2==0 && styleLine) 
		{
			code += '|- style="background:#efefef"\n'
		}
		else
		{
			code += '|-\n'
		}
		
		if(exfield)
		{
			items++
			code += '!style="background:#fdd"|Nagłówek '+items+'\n'
		}
		
		for(i=0;i<nbCol;i++)
		{
			code += '|Komórka\n'
		}
	}
	
	if (styleHeader) code += '|}\n'
	
	code += '|}\n'
	
	insertTags('','', code)
}

function tableButton()
{
 var toolbar = document.getElementById('toolbar');
 if (!toolbar) return false;
 
 var textbox = document.getElementById('wpTextbox1');
 if (!textbox) return false;
 
 if (!document.selection && textbox.selectionStart == null)
 return false;
 
 var a=document.createElement('a')
 a.innerHTML='<img src="http://upload.wikimedia.org/wikipedia/commons/0/04/Button_array.png" alt="Tabela" title="Wstaw tabelę" width="23" height="22" style="cursor:pointer" />'
 a.href='javascript:tableShowHide()'
 toolbar.appendChild(a)
}
 
$(tableButton)
$(tableInit)