// collapsible fieldsets  
var $j = jQuery.noConflict(); 
$j(document).ready( function (){
		//collapsible body
	    $j("div.collapsible").each(function(){
	    	if ($j(this).find(".error").size()>0 || $j(this).find(".fieldRequired").size()>0){	    		
	    		$j(this).show();
	    	}else {	    		
	    		$j(this).hide();				
	    	}	    	
	    });
	    //collapsible fieldset title
		$j("legend.collapsible").each(function(){
			$j(this).prepend('<img class="collapsible-expand" src="/media/img/plus.gif" />'   );
			$j(this).prepend('<img class="collapsible-collapse" src="/media/img/minus.gif" />');
			if ($j(this).next().next().is(':hidden') ){
				$j(this).find("img.collapsible-expand").show();
				$j(this).find("img.collapsible-collapse").hide();				
			}else{
				$j(this).find("img.collapsible-expand").hide();
				$j(this).find("img.collapsible-collapse").show();
			}
			$j(this).click(function() {
				$j(this).find("img.collapsible-expand").toggle();
				$j(this).find("img.collapsible-collapse").toggle();
				$j(this).next().toggle(); // the next is helptext
				$j(this).next().next().toggle();//next.next is collapsible body (div)
			})			
		});	
		 $j("div.fieldsetHelp").each(function(){
		    	if ($j(this).next().is(':hidden')){	    		
		    		$j(this).show();
		    	}else {	    		
		    		$j(this).hide();				
		    	}	
		    	$j(this).click(function() {
					$j(this).prev().find("img.collapsible-expand").toggle();
					$j(this).prev().find("img.collapsible-collapse").toggle();
					$j(this).toggle(); 
					$j(this).next().toggle();//next is collapsible body (div)
				})	
		    });  
		 //select filter
		 $j("select").each(function(){
			 if (! $j(this).attr('multiple') && $j(this).children().size() > 15){
				 var field_id =$j(this).attr('id')				 
				 $j(this).before('<p class="selector-filter"><img class="collapsible-expand" src="/admin_media/img/admin/selector-search.gif"/> <input type="input" id="'+field_id+'_input" value=""/></p>');
				 $j(this).parent().find("#"+field_id+"_input").bigoFilter($j(this));				 
			 }
		 });  
});
var $k = jQuery.noConflict(); 
$k(document).ready( function (){
    // field help lo oculto y lo pongo como tooltip
    $k(".formHelp").hide();	
    $k("label").each(function() {
        if (!$k(this).parent().find("div.formHelp").text().replace(" ","")) return;
        $k(this).tooltip({
            showURL: false,
            top: -25, 
            left: 0, 
            bodyHandler: function(){return $k('<div>'+$k(this).parent().find("div.formHelp").text()+'<div>');}    
        });   
    });   
	
    // tooltip para el fieldset
    $k("legend.collapsible").tooltip({
    	showURL: false,
        top: -25, 
        left: 0, 
    	bodyHandler: function(){return $k('<div>Click here to expand/collapse<div>');}    
    });    
    $k("div.fieldsetHelp").tooltip({
    	showURL: false,        
    	bodyHandler: function(){return $k('<div>Click here to expand/collapse<div>');}    
    });     
    //tooltip para el botón de añadir
    $k(".add-another img").tooltip({ 
        delay: 0, 
        showURL: false, 
        bodyHandler: function(){return $k('<div>'+$k(this).parent().attr("title")+'<div>');} 
    });
    // tooltip para selects
    $k("option").tooltip({ 
        delay: 0, 
        showURL: false, 
        bodyHandler: function(){return $k("<div>"+$k(this).text()+"<div>");} 
    });    
});


// ayuda extraordinaria. TODO: cambiar el alert por un jquery dialog mas elaborado
var $ext = jQuery.noConflict();
$ext(document).ready(function(){
    $ext("span.help_ext").each(function(){
        var helper = $ext(this);
        var help_text_dialog = $ext(this).find('div.help_ext_dialog');
        if (help_text_dialog.text() != ''){						
            helper.find('a.help_ext_link').show();				  
            help_text_dialog.dialog({
                autoOpen: false,
                closeText: 'X',
                width: 500,
                height: 300,
                resizable: false
            });
            helper.find('a.help_ext_link').click(function() {						    
                $ext('div.help_ext_dialog').dialog('close');
                help_text_dialog.dialog('open');
                return false;
            });
        }					
    });
}); 

