/*
*@package EnvisioNext javascript library
*@subpackage Input helper
*@copyright (C) 2007 EnvisioNext.com  <support@EnvisioNext.com>
*@author Anton Zavidov <anton.zavidov@envisionext.com.ua>
*/

// Register base class
InputHelper = new Object;
// Register configuration object
var input_helper = new Object;
 
// Base class
InputHelper = function ()
{
	////
	addedClass = "clearfix";
	////
	
	if(typeof input_helper != "object") {
		return false;
	}
	
	// Get parent node
	this.p = function(element, count)
	{
		while (count > 0) {
			count--;
			element = element.parentNode;
		}
		return element;
	}
	
	// Strip spacers
	this.strip = function(element)
	{
		tmp = $(input_helper[element].textFieldId).value;
		return tmp;
		
		while(tmp.substr(tmp.length-1,tmp.length) == " ") {
			tmp = tmp.substr(0,tmp.length-1);
		}
		while(tmp.substr(0,1) == " ") {
			tmp = tmp.substr(1,tmp.length);
		}
		var reg1 = '  ';
		tmp = tmp.replace(reg1, ' ');
		return tmp;
	}
	
	// Generate Keywords
	this.generateKeywords = function (element)
	{
		while (element.nodeName != "DIV") {
			element = element.parentNode;
		}
		var inputField = $(input_helper[element.id].textFieldId);
		var checkboxes = $$("#" + element.id + " input");
		for (c = 0; c < checkboxes.length; c++) {
			if (checkboxes[c].type == "checkbox" && checkboxes[c].className != "checkAll" && checkboxes[c].className != "checkAll checkFull") {
				var text = (navigator.appName == "Microsoft Internet Explorer") ? this.p(checkboxes[c],1).innerText.substr(0, this.p(checkboxes[c],1).innerText.length-1) : this.p(checkboxes[c],1).textContent;
				text = (text.indexOf(" ") > 0) ? '"' + text + '"' : text;
				if (checkboxes[c].checked == true) {
					if (inputField.value.indexOf(text) < 0) {
						inputField.value += (inputField.value.length > 0) ? " " + text : text;
						inputField.value = this.strip(element.id);
					}
				} else {
					var reg1 = " "+text;
					var v = (" "+inputField.value).replace(reg1, '');
					inputField.value = v.substr(1,v.length);
					inputField.value = this.strip(element.id);
				}
			}
		}
	}
	
	// Check opened
	this.checkOpened = function(element)
	{
		for(var c=0; c<element.childNodes.length; c++) {
			if(element.childNodes[c].nodeName == "LI") {
				for (var n = 0; n < element.childNodes[c].childNodes.length; n++) {
					if (element.childNodes[c].childNodes[0].checked == true) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	// Registration event handlers
	this.checks = {
  		keywordEvent: function(event, element)
		{
			var rootUl = $$("#" + element + " ." + element + "_root")[0];
			if (!input_helper[i].setOpen) {
				input_helper[i].setOpen = true;
				var ff = $(input_helper[i].formField)
				ff.className = "active open_list "+addedClass;
				rootUl.style.display = "block";
				var checkboxes = $$("#" + element + " input");
				var tmp = "*" + $(input_helper[element].textFieldId).value;
				for (c = 0; c<checkboxes.length; c++) {
					if (checkboxes[c].type == "checkbox" && checkboxes[c].className != "checkAll") {
						var reg = (navigator.appName == "Microsoft Internet Explorer") ? this.p(checkboxes[c],1).innerText.substr(0, this.p(checkboxes[c],1).innerText.length-1) : this.p(checkboxes[c],1).textContent;
						reg = escape(reg);
						checkboxes[c].checked = (tmp.toLowerCase().search(reg.toLowerCase())>0) ? true : false;
					}
				}
				var tmpA = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 2;
				for (r=0; r<rootUl.childNodes.length; r++) {
					if(rootUl.childNodes[r].tagName == "LI") {
						rootUl.childNodes[r].style.display = "block";
						if(rootUl.childNodes[r].childNodes.length > tmpA) {
							if (rootUl.childNodes[r].childNodes[1].tagName == "UL") {
								if (rootUl.childNodes[r].childNodes[1].style.display == "none") {
									rootUl.childNodes[r].className = rootUl.childNodes[r].className.replace(/expand/, "collapse");
								}
							}
						}
					}
				}
			} else {
				input_helper[i].setOpen = false;
				$(input_helper[i].formField).className = "active "+addedClass;
				rootUl.style.display = "none";
				for (r=0; r<rootUl.childNodes.length; r++) {
					if(rootUl.childNodes[r].tagName == "LI") {
						rootUl.childNodes[r].style.display = "none";	
					}
				}
			}
		},
		aEvent: function(event, element)
		{
			var parent = this.p(element,1).childNodes[1];
			if (!window[this.p(element,1).childNodes[1].id] || typeof window[this.p(element,1).childNodes[1].id] == "object") {
				window[this.p(element,1).childNodes[1].id] = true;
				parent.style.display = "block";
				this.p(element,1).className = this.p(element,1).className.replace(/collapse/, "expand");
				this.p(element,1).className = this.p(element,1).className.replace(/opened/, "");
				for (r=0; r<parent.childNodes.length; r++) {
					if(parent.childNodes[r].tagName == "LI") {
						parent.childNodes[r].style.display = "block";
					}
				}
				
			} else {
				window[this.p(element,1).childNodes[1].id] = false;
				parent.style.display = "none";
				this.p(element,1).className = this.p(element,1).className.replace(/expand/, "collapse");
				for (r=0; r<parent.childNodes.length; r++) {
					if(parent.childNodes[r].tagName == "LI") {
						parent.childNodes[r].style.display = "none";
					}
				}
				var checkboxes = $$("#" + element.parentNode.childNodes[1].id + " input");
				for (c = 0; c<checkboxes.length; c++) {
					if (checkboxes[c].type == "checkbox" && checkboxes[c].className != "checkAll" && checkboxes[c].checked == true) {
						this.p(checkboxes[c],1).style.display = "block";
						this.p(checkboxes[c],2).style.display = "block";
						this.p(checkboxes[c],3).style.display = "block";
						this.p(checkboxes[c],3).className = this.p(checkboxes[c],3).className.replace(/collapse/, "expand");
						if(checkboxes[c].checked){
							this.p(checkboxes[c],4).style.display = "block";
							if(this.p(element,1).className.indexOf("opened")<0) {
								this.p(element,1).className += " opened";
							}
						} else {
							this.p(checkboxes[c],4).style.display = "none";
						}
					}
				}
			}
			Event.stop(event);
		},
		checkLi: function (event, element)
		{
			element.checked = (element.checked) ? false : true;
			var thisAll = $$("#" + this.p(element,2).id + " .checkAll");
			var full = $$(".checkFull");
			if(element.checked != true)
			{
				thisAll[0].checked = false;
				full[0].checked = false;
				if (this.p(element,3).className.indexOf("opened")>0) {
					this.p(element,1).style.display = "none";
					if(!this.checkOpened(this.p(element,2))) {
						var tmp = this.p(element,3).className.split(" ");
						this.p(element,3).className = tmp[0] + " collapse";
						this.p(element,2).style.display = "none";
					}
				}
			}
			this.generateKeywords(element);
		},
		checkAllLi: function (event,element)
		{
			element.checked = (element.checked) ? false : true;
			if(!element.checked) {
				$$(".checkFull")[0].checked = false;
			}
			var ul = this.p(element,2);
			if (ul.id != "null") {
				var checkboxes = $$("#" + ul.id + " input");
				for (c = 0; c<checkboxes.length; c++) {
					if (checkboxes[c].type == "checkbox" && (checkboxes[c].className != "checkAll" || this.p(element,2).className == ul.parentNode.id + "_root")) {
						checkboxes[c].checked = element.checked;
						if (element.checked) {
							if (this.p(checkboxes[c],1).style.display != "block") {
								if(this.p(checkboxes[c],3).className.indexOf("opened")<0) {
									this.p(checkboxes[c],3).className += " opened";
								}
								this.p(checkboxes[c],2).style.display = "block";
								if(checkboxes[c].className != "checkAll"){
									this.p(checkboxes[c],1).style.display = "block";
								}
							}
						} else {
							if (this.p(checkboxes[c],1).style.display == "block") {
								if(this.p(checkboxes[c],3).className.indexOf("opened")>0) {
									this.p(checkboxes[c],1).style.display = "none";
									if(!this.checkOpened(this.p(checkboxes[c],2))) {
										this.p(checkboxes[c],3).className = this.p(checkboxes[c],3).className.split(" ")[0] + " collapse";
										this.p(checkboxes[c],2).style.display = "none";
									}
								}
							}
							if(element.className == "checkAll checkFull")
							{
								var splited = this.p(checkboxes[c],3).className.split(" ");
								if(splited[splited.length-1] == "opened")
								{
									this.p(checkboxes[c],3).className = splited[0] + " collapse";
									this.p(checkboxes[c],2).style.display = "none";
								}
							}
						}
					}
				}
				this.generateKeywords(element);
			} 
		},
		changeEvent: function (event,element)
		{
			var checkboxes = $$("#" + element + " input");
			var tmp = "*" + this.strip(element);
			for (c = 0; c<checkboxes.length; c++) {
				if (checkboxes[c].type == "checkbox" && checkboxes[c].className != "checkAll" && checkboxes[c].className != "checkAll checkFull") {
					var reg = (navigator.appName == "Microsoft Internet Explorer") ? this.p(checkboxes[c],1).innerText.substr(0, this.p(checkboxes[c],1).innerText.length-1) : this.p(checkboxes[c],1).textContent;
					if(tmp.toLowerCase().search(reg.toLowerCase())>0) {
						checkboxes[c].checked = true;
						if (this.p(checkboxes[c],1).style.display != "block") {
							if(this.p(checkboxes[c],3).className.indexOf("opened")<0) {
								this.p(checkboxes[c],3).className += " opened";
							}
							this.p(checkboxes[c],2).style.display = "block";
							this.p(checkboxes[c],1).style.display = "block";
						}
					} else {
						checkboxes[c].checked = false;
						if (this.p(checkboxes[c],1).style.display == "block") {
							if(this.p(checkboxes[c],3).className.indexOf("opened")>0) {
								this.p(checkboxes[c],1).style.display = "none";
								if(!this.checkOpened(this.p(checkboxes[c],2))) {
									this.p(checkboxes[c],3).className = this.p(checkboxes[c],3).className.split(" ")[0] + " collapse";
									this.p(checkboxes[c],2).style.display = "none";
								}
							}
						}
					}
				}
			}
		}
	}
	// Registration support object
	for (i in input_helper) {
		input_helper[i].setFocus = false;
		input_helper[i].setOpen = false;
		Event.observe($(input_helper[i].keywordButtonId),'click',this.checks.keywordEvent.bindAsEventListener(this,i));
		Event.observe($(input_helper[i].formField),'change',this.checks.changeEvent.bindAsEventListener(this,i));
		Event.observe($(input_helper[i].formField),'mouseover',function(){
			try{
				if($(input_helper[i].formField).className == "passive "+addedClass) {
					$(input_helper[i].formField).className = "active "+addedClass;
				}
			}catch(e){}
		});
		Event.observe($(input_helper[i].formField),'mouseout',function(){
			try{
				if(!input_helper[i].setFocus && !input_helper[i].setOpen){
				$(input_helper[i].formField).className = "passive "+addedClass;
			}
			}catch(e){}
		});
		Event.observe($(input_helper[i].textFieldId),'focus',function(){
			try{
				$(input_helper[i].formField).className = "active";
			input_helper[i].setFocus = true;
			
			}catch(e){}
		});
		Event.observe($(input_helper[i].textFieldId),'blur',function(){
			try{
				if (!input_helper[i].setOpen) {
				$(input_helper[i].formField).className = "passive";
			}
			input_helper[i].setFocus = false;
			
			}catch(e){}
		});
		var expanders = $$(" .toggler");
		for (a = 0; a<expanders.length; a++) {
			Event.observe(expanders[a],'click',this.checks.aEvent.bindAsEventListener(this,expanders[a]));
		}
		var uls = $$("#" + i + " ul");
		for (u = 0; u<uls.length; u++) {
			if (uls[u].parentNode.id == i) {
				uls[u].className = i + "_root";
				uls[u].id = i + "_" + u;
				for(ils = 0; ils<uls[u].childNodes.length; ils++)
				{
					if(uls[u].childNodes[ils].tagName == "LI") {
						uls[u].childNodes[ils].className = i + "_first_item collapse";
					}
				}
				new Insertion.Bottom(uls[u],'<li class="' + i + '_first_item all collapse"><input type="checkbox" class="checkAll checkFull" rel="noChange">Check All</li>');
				Event.observe(uls[u].childNodes[ils],'click',this.checks.checkAllLi.bindAsEventListener(this,uls[u].childNodes[ils].childNodes[0]));
			} else {
				uls[u].className = i + "_folder";
				uls[u].id = i + "_" + u;
				for(ils = 0; ils<uls[u].childNodes.length; ils++)
				{
					if(uls[u].childNodes[ils].tagName == "LI") {
						uls[u].childNodes[ils].className = i + "_element";
						new Insertion.Top(uls[u].childNodes[ils],'<input type="checkbox" rel="noChange">');
						Event.observe(uls[u].childNodes[ils],'click',this.checks.checkLi.bindAsEventListener(this,uls[u].childNodes[ils].childNodes[0]));
						Event.observe(uls[u].childNodes[ils].childNodes[0],'click',function (event){
							element = Event.element(event);
							element.checked = (element.checked) ? false : true;
						});
					}
				}
				new Insertion.Bottom(uls[u],'<li class="' + i + '_element all"><input type="checkbox" class="checkAll" rel="noChange">Check All</li>');
				Event.observe(uls[u].childNodes[ils],'click',this.checks.checkAllLi.bindAsEventListener(this,uls[u].childNodes[ils].childNodes[0]));
			}
		}
		var checkAll = $$("#" + i + " input.checkAll");
		for (c = 0; c<checkAll.length; c++) {
			Event.observe(checkAll[c],'click',function (event){
				element = Event.element(event);
				element.checked = (element.checked) ? false : true;
			});
		}
		setTimeout(function()
		{
			try{
				myCbir = new Cbir(
				{
					def:[designPath+'/images/checkbox_act.gif', designPath+'/images/checkbox_def.gif']
				},i);
			} catch(e){}
		},100);
	}
	
}
var InputHelperInit = function()
{
	setTimeout(InputHelper,300);
}
Event.observe(window, 'load', InputHelperInit, false);
