// ********************************************************************************************************************************************************************************
// **************************************************************************CREATED BY VILA SEBASTIAN*****************************************************************************
// ********************************************************************************************************************************************************************************
// ********************************************************************************************************************************************************************************
// ********************************************************************************************************************************************************************************

// SET A CLASS 'SLIDE' FOR OBJECT

var accordion = new Object();

accordion.head = ".head";
accordion.focus = "focus";
accordion.selected = "sel";
accordion.block = "block";
accordion.multiple = true;
accordion.vel = 300;
accordion.min_height_head = 0;
accordion.min_height = 0;
accordion.onChange = "";

accordion.render = function(class_id){    // Define Method
	var thisobj = this;
	var solaps = 0;
	var wps = $(class_id).width()-15;
	$(class_id+" "+this.head).each(function(){
		solaps++;
		var head = $(this);
		head.attr("head",solaps);
		head.addClass("my_head_accordion");
		head.addClass("my_head_"+solaps);
	});
	solaps = 0;
	$(class_id).children().each(function(){
		if ($(this).attr("head")==undefined){
			solaps++;
			$(this).attr("content",solaps);
			$(this).addClass("my_content_"+solaps);
		}
	});
	var new_html = '';
	for (var i=1;i<=solaps;i++){
		var est = ($(class_id+" .my_head_"+i).attr("active")==undefined?true:$(class_id+" .my_head_"+i).attr("active"));
		var height_head = $(class_id+" .my_head_"+i).height();
		var height_cont = $(class_id+" .my_content_"+i).height();
		
		var id_head = $(class_id+" .my_head_"+i).attr("id");
		var id_cont = $(class_id+" .my_content_"+i).attr("id");
		
		if (height_head<thisobj.min_height_head && thisobj.min_height_head!=0) height_head = thisobj.min_height_head;
		new_html+="<div class='xsolap' style='overflow:hidden;height:"+height_head+"px' minheight='"+height_head+"' maxheight='"+(height_head+height_cont)+"' solap='"+i+"' active='"+est+"' open='false'>";
		new_html+="<div id='"+id_head+"' style='float:left;clear:both;width:"+wps+"px' class='my_head_accordion'>"+$(class_id+" .my_head_"+i).html()+"<img src='/images/loader.gif' width='20' class='rr' style='display:none'/>";
		new_html+="</div><div  id='"+id_cont+"' style='float:left;clear:both;width:"+wps+"px' class='my_content_accordion'>"+$(class_id+" .my_content_"+i).html()+"</div>";
		new_html+="</div>";
	}
	$(class_id).html(new_html);
	$(class_id+" .xsolap").each(function(){
		thisobj.action($(this));
	});
}
accordion.showload = function(class_id,p,est){

	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			if (est) obj.children(".my_head_accordion").children(".rr").show(); else obj.children(".my_head_accordion").children(".rr").hide();
		}
	});
}
accordion.closeall = function(class_id){
	var thisobj = this;
	class_id.children(".xsolap").each(function(){
		var minheight = $(this).attr("minheight");
		
		var open = $(this).attr("open");
		var est = $(this).attr("active");
		if ((est==true || est=="true") && (open==true || open=="true")){
			$(this).children(".my_head_accordion").removeClass(thisobj.focus);
			$(this).children(".my_head_accordion").removeClass(thisobj.selected);
			$(this).attr("open",false);
			$(this).animate({
			height: minheight+'px'
			}, thisobj.vel, function() {}
			);			
		}
	})
}

accordion.active = function(class_id, p){
	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			obj.attr("active",true);
			obj.children(".my_head_accordion").removeClass(thisobj.block);
			thisobj.action(obj);
		}
	});
}
accordion.hide = function(class_id, p){
	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			obj.hide();
		}
	});
}
accordion.show = function(class_id, p){
	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			obj.show();
		}
	});
}
accordion.desactive = function(class_id, p){
	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			obj.attr("active",false);
			thisobj.action(obj);
		}
	});
}
accordion.open = function(class_id, p){
	var thisobj = this;
	$(class_id+" .xsolap").each(function(){
		var obj = $(this);
		if (obj.attr("solap")==p){
			thisobj.click(obj);
		}
	});
}

accordion.action = function(obj){
	var thisobj = this;
	if (obj.attr("active")=="false"){
		obj.children(".my_head_accordion").unbind('mouseover');
		obj.children(".my_head_accordion").unbind('mouseout');
		obj.children(".my_head_accordion").unbind('click');
		obj.children(".my_head_accordion").removeClass(thisobj.block);
		obj.children(".my_head_accordion").addClass(thisobj.block);
	}else{
		obj.children(".my_head_accordion").mouseover(function(){
			$(this).removeClass(thisobj.focus);
			$(this).addClass(thisobj.focus);
		});
		obj.children(".my_head_accordion").mouseout(function(){
			$(this).children(".my_head_accordion").removeClass(thisobj.focus);
		});
		obj.children(".my_head_accordion").click(function(){
			thisobj.click($(this).parent("div"));
		});
	}
}

accordion.click = function(obj){
	var thisobj = this;
	var maxheight = obj.attr("maxheight");
	var minheight = obj.attr("minheight");
	if (maxheight<thisobj.min_height && thisobj.min_height!=0) maxheight=thisobj.min_height;
	var id = obj.parent("div");
	if (thisobj.multiple){
		if (obj.attr("open")=="false" || obj.attr("open")==false){
			obj.children(".my_head_accordion").addClass(thisobj.selected);
			obj.attr("open",true);
			obj.animate({
			height: maxheight+'px'
			}, thisobj.vel, function() {}
			);			
		}else{
			obj.children(".my_head_accordion").removeClass(thisobj.selected);
			obj.attr("open",false);
			obj.animate({
			height: minheight+'px'
			}, thisobj.vel, function() {}
			);							
		}
	}else{
		id.children(".my_head_accordion").removeClass(thisobj.selected);
		if (obj.attr("open")=="false" || obj.attr("open")==false){
			thisobj.closeall(obj.parent("div"));
			obj.attr("open",true);
			obj.children(".my_head_accordion").addClass(thisobj.selected);
			obj.animate({
				height: maxheight+'px'
				}, thisobj.vel, function() {}
			);
		}else{
			thisobj.closeall(obj.parent("div"));				
		}
	}
	if (thisobj.onChange!=""){
		thisobj.onChange(obj.attr("solap"),obj.attr("open"));
	}
}
