/*#############################################################
Name: Niceforms
Version: 2.0
Author: Lucian Slatineanu
URL: http://www.emblematiq.com/projects/niceforms/

Feel free to use and modify but please keep this copyright intact.
#################################################################*/

//Theme Variables - edit these to match your theme
var imagesPath = "img/";

//Global Variables
var NF = new Array();
var resizeTest = 1;

var fileInputCounter = 1;

//Initialization function
function NFInit()
{
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	if(!document.getElementById) {return false;}
	//alert("click me first");
	NFDo('start');
}
function NFDo(what)
{
	var niceforms = document.getElementsByTagName('form');
	var identifier = new RegExp('(^| )'+'niceform'+'( |$)');

	for(var q = 0; q < niceforms.length; q++)
	{
		if(identifier.test(niceforms[q].className))
		{
			if(what == "start")
			{ //Load Niceforms
				NF[q] = new niceform(niceforms[q]);
				niceforms[q].start();
			}
			else
			{ //Unload Niceforms
				niceforms[q].unload();
				NF[q] = "";
			}
	    }
    }
}
function NFFix() {
	NFDo('stop');
	NFDo('start');
}
function niceform(nf)
{
	nf._inputFile = new Array();
	
	nf.add_inputFile = function(obj)
	{
		this._inputFile[this._inputFile.length] = obj;
		inputFile(obj);
	}

	nf.start = function()
	{
		//Separate and assign elements
		var allInputs = this.getElementsByTagName('input');

		for(var w = 0; w < allInputs.length; w++)
		{
			switch(allInputs[w].type)
			{
				case "file": {
					this.add_inputFile(allInputs[w]);
					break;
				}
			}
		}
		for(w = 0; w < this._inputFile.length; w++) {this._inputFile[w].init();}
	}
	nf.unload = function() {
		//Stop
		for(w = 0; w < this._inputFile.length; w++) {this._inputFile[w].unload();}
	}
}
/*
 * extend File inputs
 */
function inputFile(el)
{
	el.oldClassName        = el.className;
	el.dummy               = document.createElement('div');
	el.dummy.className     = 'NFFile';
	el.addbutton           = document.createElement('img');
	el.addbutton.src       = imagesPath + 'add_plus.gif';
	el.addbutton.className = 'add';
	el.addbutton.onclick   = function()
	{
		if(fileInputCounter < 3)
		{
		   /*
		    * Zmień obrazek klikniętej ikonki
		    */
			this.src = imagesPath + 'add_minus.gif';
	// 	    this.style.opacity = '0.4';
	// 	    this.style.filter = "alpha(opacity=40)";
	
		   /*
		    * Zmień zdarzenie po kliknięciu - usunięcie kontrolki
		    */
		    this.onclick = function()
		    {
		        document.getElementById('upload').removeChild(this.parentNode);
		        fileInputCounter -= 1;
			};
	
		   /*
		    * Utwórz nowego inputa
		    */
		    var ni = document.createElement('input');
	
		   /*
		    * Nadaj atrybuty inputa
		    */
			ni.type      = 'file';
			ni.name      = 'upload[]';
			ni.className = 'NFhidden';
	
	 		document.getElementById('upload').appendChild(ni);
	 		
	 		fileInputCounter += 1;
	 		
	 		inputFile(ni);
	 		ni.init();
		}
		else
		{
			alert('Można dodać maksymalnie 3 załączniki!');
		}
	}
	el.file = document.createElement('div');
	el.file.className = "NFFileNew";
	el.center = document.createElement('div');
	el.center.className = "NFTextCenter";
	el.clone = document.createElement('input');
	el.clone.type = "text";
	el.clone.className = "NFText";
	el.clone.ref = el;
	el.left = document.createElement('img');
	el.left.src = imagesPath + "blank.gif";
	el.left.className = "NFTextLeft";
	el.button = document.createElement('img');
	el.button.src = imagesPath + "blank.gif";
	el.button.className = "NFFileButton";
	el.button.ref = el;
	el.button.onclick = function() {this.ref.click();}
	el.init = function() {
		var top = this.parentNode;
		if(this.previousSibling) {var where = this.previousSibling;}
		else {var where = top.childNodes[0];}
		top.insertBefore(this.dummy, where);
		this.dummy.appendChild(this.addbutton);
		this.dummy.appendChild(this);
		this.center.appendChild(this.clone);
		this.file.appendChild(this.center);
		this.file.insertBefore(this.left, this.center);
		this.file.appendChild(this.button);
		this.dummy.appendChild(this.file);
		this.className = "NFhidden";
		this.relatedElement = this.clone;
	}
	el.unload = function() {
		this.parentNode.parentNode.appendChild(this);
		this.parentNode.removeChild(this.dummy);
		this.className = this.oldClassName;
	}
	el.onchange = el.onmouseout = function() {this.relatedElement.value = this.value;}
	el.onfocus = function() {
		this.left.className = "NFTextLeft NFh";
		this.center.className = "NFTextCenter NFh";
		this.button.className = "NFFileButton NFh";
	}
	el.onblur = function() {
		this.left.className = "NFTextLeft";
		this.center.className = "NFTextCenter";
		this.button.className = "NFFileButton";
	}
	el.onselect = function() {
		this.relatedElement.select();
		this.value = '';
	}
}

//Get Position
function findPosY(obj) {
	var posTop = 0;
	do {posTop += obj.offsetTop;} while (obj = obj.offsetParent);
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	do {posLeft += obj.offsetLeft;} while (obj = obj.offsetParent);
	return posLeft;
}

//Get Siblings
function getInputsByName(name) {
	var inputs = document.getElementsByTagName("input");
	var w = 0; var results = new Array();
	for(var q = 0; q < inputs.length; q++) {if(inputs[q].name == name) {results[w] = inputs[q]; ++w;}}
	return results;
}

//Add events
var existingLoadEvent = window.onload || function () {};
var existingResizeEvent = window.onresize || function() {};

/*
window.onload = function () {
    existingLoadEvent();
    NFInit();
}
*/

window.onresize = function() {
	if(resizeTest != document.documentElement.clientHeight) {
		existingResizeEvent();
// 		NFFix();
	}
	resizeTest = document.documentElement.clientHeight;
}

