/* --------------------------
	jLook 0.2
	Nice form templates
	by Sebastian Romano / seba@envero.org 
-----------------------------*/
jQuery.fn.jLook = function(options){
	var self = this;
	
	var selectCount = 0;
	//
	$(this).addClass('jLookForm');
	// each form
	this.each(function(){
		//---------------------------------
		// select/s
		
		$('select', this).each(function(){
			var zIndex = 5000 - selectCount;
			var selectContent= '<div><span></span><a rel="selectlist'+ selectCount +'"href="#" class="jLookSelectOpen"></a></div><div class="mitsu holder select-border" style= "display: none"><div class="selectlist" id="selectlist'+ selectCount +'"><ul>'; // xhtml replace
			var nam = $(this).attr('name')+'_wrapper';
			$(this).wrap('<div class="jLookSelectWrapper" id="'+nam+'" style= "z-index: ' + zIndex + '"></div>');
			
			var optionCount = 0;
			
			$('option', this).each(function(){
				selectContent += '<li id="' + selectCount +""+ optionCount + '">'+$(this).html()+'</li>';
				optionCount++;
			});
			selectContent += '</ul></div></div>';
			$('#'+nam).append(selectContent);
			$('#'+nam).find('select').hide();
			selectCount++;
		});
		
		
		$('#selectlist').hide();
		$('.jLookSelectOpen').bind("click", onShow);
		
	});
	
	function onShow(){
		var id = ($(this).attr("rel"));
		$('#'+id).show();
		var zacual = $('ul', $(this).parent().parent()).css('z-index');		
		$('ul', $(this).parent().parent()).show();
		$('form .jScrollPaneContainer').hide();
		$('form .holder').hide();
		$(this).parent().parent().find('.holder').show();
		$(this).parent().parent().find('.jScrollPaneContainer').show();
		$(this).parent().parent().find('.selectlist').jScrollPane({showArrows: true, dragMinHeight: 30, dragMaxHeight: 127, scrollbarMargin:0, scrollbarWidth: 13, arrowSize: 20});
		$(this).parent().parent().find(".holder").addClass('select_border');
		$(this).unbind("click");
		$(this).bind("click", onHide);
		return false;		
	}
	
	function onHide(){
		var id = ($(this).attr("rel"));
		$(this).unbind("click");
		$('#'+id).hide();
		$('ul', $(this).parent().parent()).hide();
		$('form .jScrollPaneContainer').hide();
		$(this).parent().parent().find('.jScrollPaneContainer').hide();
		$(this).parent().parent().find('.holder').removeClass('select_border');
		$(this).bind("click", onShow);
		return false;		
	}
	
	return this;
}
