/*#############################################################
Name: NiceJForms
Version: 2.0
Author: Lucian Lature
WTF: Plugin for jQuery

Feel free to use and modify but please keep this copyright intact.
#################################################################*/

(function($){
	$.fn.NiceJFormerRefresh = function(options) {
		
		var selectedFieldId = options;
		
		var global = this;
		//Theme Variables - edit these to match your theme
		var defaults = {  
			imagesPath: "http://www.bilinfocms.dk/js/jquery/nicejforms2/img/",  
			selectRightWidthSimple: 4,  
			selectRightWidthScroll: 2,  
			selectMaxHeight: 500,  
			textareaTopPadding: 10,  
			textareaSidePadding: 10  
		};  
  
		var options = $.extend(defaults, options); 

		//Global Variables
		var resizeTest = 1;
		
		if($.browser.msie && $.browser.version < "7.0") return false; //exit script if IE6 or below
			
		
		
		this.niceSelect = function(el) { //extend Selects
			
			el.unload();
			
			el.oldClassName = el.className;
			
			el.dummy = document.createElement('div');
			el.dummy.ref = el;
			$(el.dummy).addClass("NFSelect").width($(el).width()).css({ left: $(el).position().left + 'px', top: $(el).position().top + 'px' });;
			
			el.left = document.createElement('img');
			$(el.left).attr({src: options.imagesPath + "0.png"}).addClass("NFSelectLeft");
			el.right = document.createElement('div');
			$(el.right).addClass("NFSelectRight");
			
			el.txt = document.createTextNode(el.options[0].text);
			el.bg = document.createElement('div');
			$(el.bg).addClass("NFSelectTarget").css({'display': 'none'});
			
			el.opt = document.createElement('ul');
			$(el.opt).addClass("NFSelectOptions");
			
			el.opts = new Array(el.options.length);
			
			el.init = function(pos) {
				
				this.dummy.appendChild(this.left);
				this.right.appendChild(this.txt);
				this.dummy.appendChild(this.right);
				this.bg.appendChild(this.opt);
				this.dummy.appendChild(this.bg);
				
				for(var q = 0; q < this.options.length; q++) {
					this.opts[q] = new global.option(this.options[q], q);
					this.opt.appendChild(this.options[q].li);
					this.options[q].lnk.onclick = function() {
						//el.onchange.call(this);
						
						this._onclick();
						$('div', this.ref.dummy)[0].innerHTML = this.ref.options[this.pos].text;
						// this.ref.dummy.getElementsByTagName('div')[0].innerHTML = this.ref.options[this.pos].text;
						this.ref.options[this.pos].selected = "selected";
						for(var w = 0; w < this.ref.options.length; w++) {this.ref.options[w].lnk.className = "";}
						this.ref.options[this.pos].lnk.className = "NFOptionActive";
						
						updateForm(el.form,el.name);
						if (el.title != ""){
							window[el.title](el);
						}
					}
				}
				
				/*var AdjustTopPos = 0;
				//options.selectMaxHeight;
				if(this.options.length > 11) {
					AdjustTopPos = options.selectMaxHeight;
					AdjustTopPos += 40;
				} else {
					AdjustTopPos = this.options.length*30;
					AdjustTopPos += 1000;
				}
				$(el.opt).css({ top: ($(el).position().top-AdjustTopPos) + 'px' });
				*/
				
				if(this.options.selectedIndex) {
					$('div', this.dummy)[0].innerHTML = this.options[this.options.selectedIndex].text;					
					this.options[this.options.selectedIndex].lnk.className = "NFOptionActive";
				}
				this.dummy.style.zIndex = 799 - pos;
				document.maxZ = 999;
				this.parentNode.insertBefore(this.dummy, this);
				this.className = "NFhidden";
			}
			
			el.unload = function() {
				this.parentNode.removeChild(this.dummy);
				this.className = this.oldClassName;
			}
			el.dummy.onclick = function() {
				var allDivs = document.getElementsByTagName('div'); for(var q = 0; q < allDivs.length; q++) {if((allDivs[q].className == "NFSelectTarget") && (allDivs[q] != this.ref.bg)) {allDivs[q].style.display = "none";}}
				
				if(this.ref.bg.style.display == "none") {
					this.ref.bg.style.display = "block";
					
					myHeight = $(window).height();
					if($(this.ref.opt).offset().top + this.ref.opt.offsetHeight > myHeight) {
						this.ref.opt.style.top = "-"+ (this.ref.opt.offsetHeight + 18) +"px";
					}
					document.maxZ++;
					this.style.zIndex = document.maxZ;
				} else {
					this.ref.bg.style.display = "none";
				}
				//alert($(this).offset().top);
				//alert($(this).top() +", "+ this.offsetTop +", "+ this.ref.offsetTop +", "+ this.ref.opt.offsetTop +", "+ this.ref.opt.offsetHeight +" : "+document.height);
				if(this.ref.opt.offsetHeight > options.selectMaxHeight) {
					this.ref.bg.style.width = this.ref.offsetWidth - options.selectRightWidthScroll + 33 + 'px';
					this.ref.opt.style.width = this.ref.offsetWidth - options.selectRightWidthScroll + 'px';
				}
				else {
					this.ref.bg.style.width = this.ref.offsetWidth - options.selectRightWidthSimple + 33 + 'px';
					this.ref.opt.style.width = this.ref.offsetWidth - options.selectRightWidthSimple + 'px';
				}
			}
			el.bg.onmouseout = function(e) {
				if (!e) var e = window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				if((reltg.nodeName == 'A') || (reltg.nodeName == 'LI') || (reltg.nodeName == 'UL')) return;
				if((reltg.nodeName == 'DIV') || (reltg.className == 'NFSelectTarget')) return;
				else{this.style.display = "none";}
			}
			el.dummy.onmouseout = function(e) {
				if (!e) var e = window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				if((reltg.nodeName == 'A') || (reltg.nodeName == 'LI') || (reltg.nodeName == 'UL')) return;
				if((reltg.nodeName == 'DIV') || (reltg.className == 'NFSelectTarget')) return;
				else{this.ref.bg.style.display = "none";}
			}
			el.onfocus = function() {this.dummy.className += " NFfocused";}
			el.onblur = function() {this.dummy.className = this.dummy.className.replace(/ NFfocused/g, "");}
			el.onkeydown = function(e) {
				if (!e) var e = window.event;
				var thecode = e.keyCode;
				var active = this.selectedIndex;
				switch(thecode){
					case 40: //down
						if(active < this.options.length - 1) {
							for(var w = 0; w < this.options.length; w++) {this.options[w].lnk.className = "";}
							var newOne = active + 1;
							this.options[newOne].selected = "selected";
							this.options[newOne].lnk.className = "NFOptionActive";
							this.dummy.getElementsByTagName('div')[0].innerHTML = this.options[newOne].text;
						}
						return false;
						break;
					case 38: //up
						if(active > 0) {
							for(var w = 0; w < this.options.length; w++) {this.options[w].lnk.className = "";}
							var newOne = active - 1;
							this.options[newOne].selected = "selected";
							this.options[newOne].lnk.className = "NFOptionActive";
							this.dummy.getElementsByTagName('div')[0].innerHTML = this.options[newOne].text;
						}
						return false;
						break;
					default:
						break;
				}
			}
			
			return el;
		}
		
		
		this.option = function(el, no) { //extend Options
			el.li = document.createElement('li');
			el.lnk = document.createElement('a');
			el.lnk.href = "javascript:;";
			el.lnk.ref = el.parentNode;
			el.lnk.pos = no;
			el.lnk._onclick = el.onclick || function () {};
			el.txt = document.createTextNode(el.text);
			el.lnk.appendChild(el.txt);
			el.li.appendChild(el.lnk);
			
			return el;
		}
		
		return this.each(function() {
			form = $(this);
			
			$('input[@type=text]', form).add($('input[@type=password]', form)).each(function() {
				global.niceInputText(this).init();
			});
			
			$('input[@type=radio]', form).each(function() {
				global.niceInputRadio(this).init();
			});
			
			$('input[@type=checkbox]', form).each(function() {
				global.niceInputCheck(this).init();
			});
			
			/*$('input[@type=submit]', form).add($('input[@type=reset]', form)).add($('button', form)).each(function() {
				global.niceInputSubmit(this).init();
			});*/
			
			
			$('input[@type=file]', form).each(function() {
				global.niceInputFile(this).init();
			});
			
			
			$('textarea', form).each(function() {
				global.niceTextarea(this).init();
			});
			
			
			$('select', form).each(function(index) {
				if(this.size > 1) {
					global.niceMultipleSelect(this).init();
				} else {
					if (this.id == selectedFieldId){
						global.niceSelect(this).init(index);
					}
				}
			});
			
		});
		
	};
	
})(jQuery);
