$.config.override({
    forms: {
		spinBox: {
			maxNumber: 9999,
			minNumber: 1,
			step: 1,
			timeOut: 250,
			minusContent: "",
			plusContent: ""
		},
		textarea: {
			height: 50
		}
    }
})

function form(element, objectList)
{
	this.element = element;
	this.formObjectList = objectList;
}

form.prototype.element;
form.prototype.formObjectList;

form.prototype.pushObject = function(object) {	
	this.formObjectList.push(object);
}

form.prototype.getName = function() {	
	var nameArray = $(this.element).attr("name");
	return nameArray.split(".").pop();
}

form.prototype.getState = function() {	
	return $(this.element).attr("state");
}

form.prototype.listActions = function()
{
	var objects = new Array();
	for(var i = 0; i < this.formObjectList.length; i++)
		if(this.formObjectList[i].isAction())
			objects.push(this.formObjectList[i]);
	return objects;
}

function formObject(element)
{
	if ($(element).parents("div.field").length != 0) {
		this.element = $(element).parents("div.field").get(0);
		this.input = element;
	}
	else {
		this.element = element;	
		this.input = element;
	}
	
	if(!(this.isReadonly())) {
		$(this.element).find(":input").focus(this.onFocus);
		$(this.element).find(":input").focusout(this.onFocusOut);	
	}
	
	$(this.element).find("textarea").each(function() {
		$(this).height($.config.get("forms.textarea.height") + "px");
	});	
	
	var thisElement = this;
	
	$(this.element).find("input.spinBox").each(function() {		
		thisElement.spinBox.call(thisElement);
	});
	
	$(element).addClass("formObject");
}

formObject.prototype.element;
formObject.prototype.input;

formObject.prototype.onFocus = function() {
	var item = $(this).parents(".field").get(0);
	$(item).addClass("focused");
}

formObject.prototype.onFocusOut = function() {
	var item = $(this).parents(".field").get(0);
	$(item).removeClass("focused");
}

formObject.prototype.isBoolean = function() {
	
	if ($(this.element).hasClass("boolean")) return true;
	return false;
}

formObject.prototype.isAction = function() {
	
	if ($(this.element).hasClass("action")) return true;
	return false;
}

formObject.prototype.getActionName = function() {
	if (!this.isAction()) return;	
	return $(this.element).attr("class").split(" ").pop();
}

formObject.prototype.isReadonly = function() {
	if ($(this.element).attr("readonly") == "readonly") return true;
	return false;
}

formObject.prototype.getParentFormName = function() {	
	var nameArray = $(this.element).parents("form").attr("name");
	return nameArray.split(".").pop();
}

formObject.prototype.spinBox = function() {	
	
	var thisField = this.element;
	var input = $(this.input);	
	
	var minus = document.createElement("div");
	$(minus).html($.config.get("forms.spinBox.minusContent")).addClass('minus');
	
	var plus = document.createElement("div");
	$(plus).html($.config.get("forms.spinBox.plusContent")).addClass('plus');
	
	$(thisField).append(plus).prepend(minus);
	
	var value = parseInt(input.val());
	
	if (value <= $.config.get("forms.spinBox.minNumber")) {
		input.val($.config.get("forms.spinBox.minNumber"));
		$(minus).addClass("disabled");
	}
			
	if (value >= $.config.get("forms.spinBox.maxNumber")) {
		input.val($.config.get("forms.spinBox.maxNumber"));		
		$(plus).addClass("disabled");
	}
	
	var minusSandWiched = 0, plusSandWiched = 0;
	
	$(minus).bind("mousedown", function(event) {
		if (event.preventDefault) event.preventDefault; event.stopPropagation(); $(this).disableTextSelect();
		if ($(this).hasClass("disabled") || event.which == 2 || event.which == 3) return;
		
		value = parseInt(input.val());
		
		input.val(value-parseInt($.config.get("forms.spinBox.step")));
		if (value-1 == $.config.get("forms.spinBox.minNumber"))
		{
			$(minus).addClass("disabled");
		}		
		$(plus).removeClass("disabled");
		
		minusSandWiched = setTimeout(function() {
			$(minus).trigger("mousedown");
		}, $.config.get("forms.spinBox.timeOut"));
		
	});
	
	$(minus).bind("mouseup mouseleave", function() {
		if (minusSandWiched) clearTimeout(minusSandWiched);
	});

	$(plus).bind("mousedown", function(event) {
		if (event.preventDefault) event.preventDefault; event.stopPropagation(); $(this).disableTextSelect();
		if ($(this).hasClass("disabled") || event.which == 2 || event.which == 3) return;
				 
		value = parseInt(input.val());		
		input.val(value+parseInt($.config.get("forms.spinBox.step")));
		if (value + 1 == $.config.get("forms.spinBox.maxNumber"))
		{
			$(plus).addClass("disabled");
		}
		$(minus).removeClass("disabled");	
		
		plusSandWiched = setTimeout(function() {
			$(plus).trigger("mousedown");
		}, $.config.get("forms.spinBox.timeOut"));
	});

	$(plus).bind("mouseup mouseleave", function(){
		if (plusSandWiched) clearTimeout(plusSandWiched);
	});
		
}

function initForms()
{
	var formList = new Array();
	
	var forms = $("body").find("form:not(.jquery)").get();
	
	for(var i = 0; i < forms.length; i++)
	{	
		var inputList = $(forms[i]).find(":input[type!='hidden']:not(.formObject)").get();
		var objectList = new Array();
		for(var j = 0; j < inputList.length; j++) {
			 if($(inputList[j]).is(":visible")) {
					objectList.push(new formObject(inputList[j]));
				}
		}
		var newForm = new form(forms[i], objectList);
		$(forms[i]).addClass("jquery")
		formList.push(newForm);
	}
	
	var unFormInputList = $("body").find(":input[type!='hidden']:not(.formObject)").get();
	
	for(var j = 0; j < unFormInputList.length; j++) {
		if($(unFormInputList[j]).is(":visible")) {
			unFormInputList[i].formObject = new formObject(unFormInputList[j]);
		}
	}
	
	$(".radioItem").each(function(){
		var thisElem = $(this);
		if ($(this).find("input:radio").get(0).checked == 1)
		{
			$(thisElem).addClass("checked");
		}
	})
	
	$(".radioItem").bind("click", function() {
		if (!($(this).hasClass('checked')))
		{
			var thisElem = $(this)			
			$(this).parents("form").find("div.radioItem").each(function() { 
				$(this).removeClass("checked").removeAttr("checked");
				if (thisElem != $(this)) $(this).find("input:radio").get(0).checked = 0; 
			});
			$(this).addClass("checked").attr("checked", "checked");
			$(this).find("input:radio").get(0).checked = 1;
		}
	})	
}


$(document).ready(function() {
	initForms();
});

$(document).bind("update", function() {
	initForms();
});

