// JavaScript Document

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

function formlicious_init(){
	
	if($("form.formlicious").length>0){
		if($("form.formlicious input[type=text]").length>0){
			$("form.formlicious input[type=text]").each(function(){
				init_inputText(this);
			});
		}
		if($("form.formlicious input[type=radio]").length>0){
			$("form.formlicious input[type=radio]").each(function(){
				init_radioButtons(this);
			});
		}
		if($("form.formlicious input[type=checkbox]").length>0){
			$("form.formlicious input[type=checkbox]").each(function(){
			});
		}
		if($("form.formlicious select").length>0){
			$("form.formlicious select").each(function(){
				init_selectBox(this);
			});
		}
		if($("form.formlicious textarea").length>0){
			
		}
	}
	
	if($("input[type=text].formlicious").length>0){
		$("input[type=text].formlicious").each(function(){
			init_inputText(this);
		});
	}
	if($("input[type=radio].formlicious").length>0){
		$("input[type=radio].formlicious").each(function(){
			init_radioButtons(this);
		});
	}
	if($("input[type=checkbox].formlicious").length>0){
		
	}
	if($("select.formlicious").length>0){
		$("select.formlicious").each(function(){
			init_selectBox(this);
		});
	}
	if($("textarea.formlicious").length>0){
		
	}
}

function init_inputText(element){
	if($(element).parents(".frmlcs_text").length<=0){
		$(element).wrap('<span class="objText" />');
		$(element).parents(".objText").wrap('<span class="frmlcs_text" />');
	}
	$(element).focusin(function(){
		$(element).parents(".objText").addClass("objTextFocus");
	});
	$(element).focusout(function(){
		$(element).parents(".objText").removeClass("objTextFocus");
	});
}


function init_radioButtons(element){
	if($(element).parents(".frmlcs_radio").length<=0){
		$(element).hide();
		$(element).wrap('<span class="frmlcs_radio" />');
		$('<a href="javascript:;" class="objRadio"></a>').appendTo($(element).parents(".frmlcs_radio"));
		$(element).parents(".frmlcs_radio").find(".objRadio").click(function(){
			$(element).parents(".frmlcs_radio").find("input[type=radio]").attr("checked","checked");
			$(element).parents(".frmlcs_radio").find("input[type=radio]").trigger("change");
			formlicious_init();
		});
	}
	$(element).focusin(function(){
		$(element).parents(".frmlcs_radio").find(".objRadio").trigger("focus");
	});
	if($(element).attr("checked")){
		$(element).parents(".frmlcs_radio").find(".objRadio").css("background-image","url("+path+"forms/radio_on.png)");
	}else{
		$(element).parents(".frmlcs_radio").find(".objRadio").css("background-image","url("+path+"forms/radio_off.png)");
	}
	$(".objRadio").focusin(function(){
		if($(element).parents(".frmlcs_radio").find("input[type=radio]").attr("checked")){
			$(element).css("background-image","url("+path+"forms/radio_on_focus.png)");
		}else{
			$(element).css("background-image","url("+path+"forms/radio_off_focus.png)");
		}
		$(element).keypress(function(event){
			if(event.keyCode=='0'){
				$(element).parents(".frmlcs_radio").find("input[type=radio]").attr("checked","checked");
				formlicious_init();
			}
		});
	});
	$(".objRadio").focusout(function(){
		if($(element).parents(".frmlcs_radio").find("input[type=radio]").attr("checked")){
			$(element).css("background-image","url("+path+"forms/radio_on.png)");
		}else{
			$(element).css("background-image","url("+path+"forms/radio_off.png)");
		}
	});
}
function init_selectBox(element){
	if($(element).parents(".frmlcs_select").length<=0){
		$(element).hide();
		$(element).wrap('<span class="frmlcs_select" />');
		$('<span class="objSelect"><a href="javascript:;"><span>'+$(element).find("option:selected").html()+'</span></a></span><ul></ul>').appendTo($(element).parents(".frmlcs_select"));
		$(element).parents(".frmlcs_select").find(".objSelect").toggle(function(){
			if($(".frmlcs_select ul:visible").length > 0){
				$(".frmlcs_select ul:visible").parents(".frmlcs_select").find(".objSelect").trigger("click");
				$(".frmlcs_select ul:visible").hide();
			}
			$(element).parents(".frmlcs_select").find("ul").empty();
			for(i=0;i<$(element).find("option").length;i++){
				$("<li>"+$(element).find("option:eq("+i+")").html()+"</li>").appendTo($(element).parents(".frmlcs_select").find("ul")).click(function(){
					$(this).parents(".frmlcs_select").find("select option:eq("+$(this).index()+")").attr("selected", "selected").trigger("change");
					$(this).parents(".frmlcs_select").find(".objSelect span").empty().html($(this).html());
					$(this).parents(".frmlcs_select").find(".objSelect a").trigger("focus");
					$(this).parents("ul").hide();
					$(this).parents(".frmlcs_select").find(".objSelect").trigger("click");
				})
			}
			
			$(element).parents(".frmlcs_select").find("ul").show();
		},function(){
			$(element).parents(".frmlcs_select").find("ul").hide();
		});
	}
	$(".objSelect a").unbind("focusin");
	$(".objSelect a").focusin(function(){
		$(this).parents(".objSelect").addClass("objSelectFocus");
	});
	$(".objSelect a").unbind("focusout");
	$(".objSelect a").focusout(function(){
		$(this).parents(".objSelect").removeClass("objSelectFocus");
	});
}
$("*").live("click", function(e){
	if($(this).parents(".frmlcs_select").length == 0){
		if($(".frmlcs_select ul:visible").length > 0){
			$(".frmlcs_select ul:visible").parents(".frmlcs_select").find(".objSelect").trigger("click");
			$(".frmlcs_select ul:visible").hide();
		}
	}else{
		if($(".frmlcs_select ul:visible").length>0){
			$(".frmlcs_select ul:visible").hide();
		}
	}
});
