/* --------------------------
	jLook 0.2
	Nice form templates
	by Sebastian Romano / seba@envero.org 
-----------------------------*/
jQuery.fn.jLook = function(options){
	var self = this;
	//
	$(this).addClass('jLookForm');
	// each form
	this.each(function(){
		// form link
		var actualForm = this;
		// labels
		$('label', this).addClass("jLookLabel");
		// inputs
		$('input:text', this).addClass("jLookInput");
		// textareas
		$('textarea', this).addClass("jLookTextarea");
		/*
			wrappers
		*/
		// inputs
		$('input:text', this).each(function(){
			$(this).focus(function(){
				$(this).addClass("jLookInput_hover");
			});
			$(this).blur(function(){
				$(this).removeClass("jLookInput_hover");
			});
		});
		//----------------------------------------------------------
		// radio/s
		var lastRadio = '';
		$('input:radio', this).each(function(){
			// link
			$(this).before('<a href="#" id="'+$(this).attr("name")+'-'+$(this).attr("value")+'" class="jLookRadio"></a>');
			$(this).remove();
			// hidden necesary fields
			if(lastRadio != $(this).attr("name")){
				$(actualForm).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="null" />');
				lastRadio = $(this).attr("name");
			}
		});
		// click event
		$('.jLookRadio').click(function(){
			$(this).addClass("jLookRadio_checked");
			// hidden generator
			valu = $(this).attr("id").split('-');
			$('#'+$('#'+valu[0]).attr("name")+'-'+$('#'+valu[0]).attr("value")).removeClass('jLookRadio_checked'); // uncheck
			$('#'+valu[0]).attr("value", valu[1]); // selectedValue
			return false;
		});
		//----------------------------------------------------------
		// checkbox/s
		var lastCheckbox = '';
		$('input:checkbox', this).each(function(){
			// link
			$(this).before('<a href="#" id="'+$(this).attr("name")+'-'+$(this).attr("value")+'" class="jLookCheckbox"></a>');
			$(this).remove();
			// necesary hidden fields
			if(lastCheckbox != $(this).attr("name")){
				$(actualForm).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="null" />');
				lastCheckbox = $(this).attr("name");
			}
		});
		// click event
		$('.jLookCheckbox').click(function(){
			valu = $(this).attr("id").split('-');
			if($('#'+valu[0]).attr("value") == valu[1]){
				$(this).removeClass('jLookCheckbox_checked'); // uncheck
				$('#'+valu[0]).attr("value", 'null');
			}else{
				$(this).addClass("jLookCheckbox_checked");
				$('#'+valu[0]).attr("value", valu[1]); // selectedValue
			}
			return false;
		});
		//---------------------------------
		// select/s
		$('select', this).each(function(){
			var selectContent= '<div><span></span><a href="#" class="jLookSelectOpen"></a></div><ul>'; // xhtml replace
			var nam = $(this).attr('name')+'_wrapper';
			$(this).wrap('<div class="jLookSelectWrapper" id="'+nam+'"></div>');
			$('option', this).each(function(){
				selectContent += '<li><a href="#">'+$(this).html()+'</a></li>';
				$(this).remove();
			});
			selectContent += '</ul>';
			$('#'+nam).html(selectContent);
		});
		// display select options
		$('.jLookSelectOpen').click(function(){
			var zacual = $('ul', $(this).parent().parent()).css('z-index');
			$('ul', $(this).parent().parent()).slideToggle();
			return false;
		});
		//--------------------------------------
		//textareas
		$('textarea', this).each(function(){
			$(this).focus(function(){
				$(this).addClass("jLookTextarea_hover");
			});
			$(this).blur(function(){
				$(this).removeClass("jLookTextarea_hover");
			});
		});

	});
}
