
$().ready(function(){
	
	$.fn.extend({
		makeControls:function(){ //interact
			
			$('.textbox', this).each(function(){ 
				var $this = $(this);
				
				$this.input(); 
				
				if ($this.attr('ovalue') != undefined && $this.val() != $this.attr('ovalue')){
					$this.addClass('focused');
				}
			});
			
			$('.selectbox', this).unbind().live('focusin', function(){ 
				$(this).addClass('focused'); 
			}).live('focusout', function(){ 
				var $sel = $(':selected', this);
				if ($sel.val() && $sel.val() > 0){
					return;
				}
				
				$(this).removeClass('focused'); 
			}).each(function(){
				var $sel = $(':selected', this);
				if ($sel.val() && $sel.val() > 0){
					$(this).addClass('focused'); 
				}
			});
			
			/*$('.selectbox', this).each(function(){ 
				$(this).unbind().bind('focusin', function(){ $(this).addClass('focused'); })
					.bind('focusout', function(){ $(this).removeClass('focused'); }); 
			});*/
			
			$('.optbox', this).each(function(){ $(this).optbox(); });
			
			if ($().mbTooltip){
				$("a[title]", this).mbTooltip({ //a[title] //.not('form')
					live:false,
					opacity:1.0, //.90, //opacity
					wait:500, //before show
					ancor:"mouse", //"parent"
					cssClass:"default", // default = default
					timePerWord:70, //time to show in milliseconds per word
					hasArrow:false,
					color:"white",
					imgPath:"/res/plugins/tooltip/images/",
					shadowColor:"black",
					fade:500
				}).click(function(){ 
					if ($(this).attr('title')){
						return false; 
					}
				});
			}
			
		}
	});
	
	$(document.body).makeControls();
	
	var $more_handler = $('.ztop .menu a.more');
	var $more = $('.ztop .menu div.more')
	var $more_wrp = $('.ztop .menu div.more .wrp'); //$more_wrp.dropShadow({left: 1, top: 2, blur: 2, opacity: 0.4});
	
	$more_handler.click(function(e){
		//$more.toggle(100, function(){  });
		//$more.trigger('click', true);
		
		function hide_more(){
			$more.hide(); 
			$more_wrp.removeShadow();
		}
		
		if ($more_wrp.is(':visible')){ //$more
			hide_more();
		} else {
			$more.show(); //$(this).show('fold', {}, 2000, function(){ console.log('!'); }); 
			$more_wrp.dropShadow({left: 1, top: 2, blur: 2, opacity: 0.4}); //$more_wrp.redrawShadow();
			
			$(document).bind('click', function(e){ 
				var s = false; //stop "propagation" flag
				
				$(e.target).parents().each(function(){
					if (s) return;
					if ($(this).hasClass('more')) return (s = true);
				});
				
				if (s)
					return;
				
				e.stopPropagation();
				e.preventDefault();
				
				hide_more();
				$(document).unbind('click');
				
				return false; 
			});
		}
		
		/*$more.toggleClass('hidden');
		if (!$more.hasClass('hidden')){
			$more_wrp.redrawShadow();
			$(document).bind('click', function(e){ 
				var $target = $(e.target);
				var s = false; //stop propagation flag
				
				$target.parents().each(function(){
					if (s) return;
					if ($(this).hasClass('more')) return (s = true);
				});
				
				if (s)
					return;
				
				//if ($target.parent().hasClass('more') || $target.parent().parent().hasClass('more'))
				//	return false;
				
				$more.addClass('hidden'); 
				return false; 
			});
		} else {
			$(document).unbind('click');
		}*/
		
		return false;
	});
	
});

