/* 
 * dependencias
 * lib/jquery-1.3.2.js
 */

//plugin sanfona
jQuery.fn.sanfona = function(options){
	
	var defaults = {
		open : 'sOpen',
		close : null,
		href: 'javascript:;',
		time: 'slow',
		all : true,
		active: 'active-sanfona',
		obj: null,
		islabel: true,
		label: 'abrir',
		labelTemp: 'fechar',
		prevEl: true
	}
	
    var options = jQuery.extend(defaults, options);
		
    return this.each(function(){
        var obj = jQuery(this);
		
		obj.attr('href',options.href)
		
		if(jQuery('body *').hasClass(options.open)){
			jQuery(this).prev('.'+options.open).addClass(options.active);
		}
		closeAll(options);
		
        obj.click(function(){
			if(options.prevEl){
				var hideObj = jQuery(this).prev('.'+options.open);	
			}else{
				var hideObj = jQuery(this).parents('li').find('.'+options.open);
			}
			
			
			
			
			if(hideObj.hasClass(options.active)){
				up(options,hideObj);
				obj.removeClass('up');
				if(options.islabel){
					obj.html(options.label);
				}
			}else{
				down(options,hideObj);
				obj.addClass('up');
				if(options.islabel) {
					obj.html(options.labelTemp);
				}
			}
			//closeAllOthers(options,hideObj);
		});
    })
	
	function up(options,obj){
		obj.slideUp(options.time);
		delC(options,obj);
		
	}
	function down(options,obj){
		obj.slideDown(options.time);
		addC(options,obj);
	}
	function addC(options,obj){
		obj.addClass(options.active);
	}
	function delC(options,obj){
		obj.removeClass(options.active);
		
	}
	function closeAll(options){
		if(options.all){
			jQuery('.'+options.active).each(function(){
				var obj = jQuery(this);
				if(obj != options.obj){
					up(options,obj);	
				}
			});
		}
	}
	
	function closeAllOthers(options,showObj){
		if(options.all){
			jQuery('.'+options.active).each(function(){
				var obj = jQuery(this);
				
				if(obj != showObj){
					up(options,obj);	
				}
			});
		}
	}
   
}


