function maximize(div_to_max,div_max_icon,div_min_icon,speed,height)
{

	$('#contentwrap').width($('#main').width()-parseInt($('#contentwrap').css("padding-left"))-parseInt($('#contentwrap').css("padding-right")));
	//todo fare in modo che ricordi l'altezza prima di cambiarla (stile tooltip che si ricorda il testo)
	if (height != null && height!="")
	{
		$('#'+div_to_max).height(height+'px');
	}
	$('#'+div_min_icon).show();
	$('#'+div_max_icon).hide();
	/*$('#head').hide(speed);
	 * $('#footer').hide(speed);
	 */
	$('#sidebarwrap').hide(speed);

	$.cookie('maximized_for_'+div_to_max,div_to_max+' '+div_max_icon+' '+div_min_icon+' 1 '+height);
	return false;
}

function minimize(div_to_max,div_max_icon,div_min_icon,speed,height)
{
	$('#contentwrap').width('');
	//todo fare in modo che recuperi l'altezza (stile tooltip che si ricorda ricarica il testo)
	$('#'+div_min_icon).hide();
	$('#'+div_max_icon).show();
	/*$('#head').show(speed);
	$('#footer').show(speed);*/
	$('#sidebarwrap').show(speed);
	$.cookie('maximized_for_'+div_to_max,'');
	return false;
}

function max_min_imize(div_to_max,div_max_icon,div_min_icon,speed,height)
{
	var maximized=false;
	if (speed==null)
	{
		speed='slow';
	}
	if ($('#'+div_min_icon).css('display') == 'none' )
	{
		maximized=true;
	}
	else
	{
		maximized=false;
	}
	if (maximized)
	{
		maximize(div_to_max,div_max_icon,div_min_icon,speed,height);
	}
	else
	{
		minimize(div_to_max,div_max_icon,div_min_icon,speed,height);
	}
	return false;
}

function toggle(controller,id,cookie_name,speed)
{
	if(controller)
	{
		$(controller).toggleClass("toggle_opened");
		$(controller).toggleClass("toggle_closed");
	}
	if ($('#'+id).is(':visible'))
	{
		$.cookie(cookie_name,'closed');
	}
	else
	{
		$.cookie(cookie_name,'opened');
	}
	if(!speed) speed="slow";
	$('#'+id).toggle(speed);
	return false;
}

/**
   var defaults = {
               class_name: "tooltip",
               position: "local",            // local | fixed | growl
               max_width: 400,
               max_height: 300,
               anchor: "",                    //if position is fixed, the ID of the anchor element
               vertical_alignment: "bottom",
               horizontal_alignment: "right",
               vertical_offset: 10,
               horizontal_offset: -10,
               opacity: 0.75,
               speed: 300,
               content: "",
               ajax: false,
               url: ""                        //if ajax=true
           };
   */
function showTooltip(target,config)
{
   if(target.tooltip) return;

   if(config.ajax)
   {
       if(target.tooltipText || target.tooltipText=="")
       {
       	config.content=target.tooltipText;
       }
       else
       {
			config.content = $.ajax({
	           url: config.url,
	           async: false,
	           type: "GET"
	           }).responseText;
			target.tooltipText=config.content;
	   }
   }

	if(config.content=="") return;

	var tooltip=document.createElement("div");
   tooltip.config=config;
   $(tooltip).addClass(config.class_name);
   $(tooltip).css("opacity",config.opacity);
   $(tooltip).css("position","absolute");
   $(tooltip).css("display","none");

   $("body").append(tooltip);

   $(tooltip).hover(function(){target.stopHideTooltip=true;},function(){hideTooltip(target,true)});

   var content=document.createElement("div");
   $(content).addClass("tooltip_content");
   $(content).html(config.content);
   $(tooltip).append(content);

   var pointer=document.createElement("div");
   $(pointer).addClass("tooltip_pointer");
   $(tooltip).append(pointer);

   if($(tooltip).outerHeight()>config.max_height) $(tooltip).height(config.max_height);
   if($(tooltip).outerWidth()>config.max_width) $(tooltip).width(config.max_width);

   switch(config.position)
   {
           case "fixed":
       var top=0;
       var left=0;
       switch(config.vertical_alignment)
       {
               case "bottom":
           top=$("#"+config.anchor).offset().top+$("#"+config.anchor).outerHeight()+config.vertical_offset;
           break;
               case "top":
           top=$("#"+config.anchor).offset().top-$(tooltip).outerHeight()+config.vertical_offset;
           break;
       }
       switch(config.horizontal_alignment)
       {
               case "left":
           left=$("#"+config.anchor).offset().left+config.horizontal_offset-$(tooltip).outerWidth();
           break;
               case "right":
           left=$("#"+config.anchor).offset().left+$("#"+config.anchor).outerWidth()+config.horizontal_offset-$(tooltip).outerWidth();
           break;
       }
       $(tooltip).css("top",top+"px");
       $(tooltip).css("left",left+"px");
       break;

           case "local":
       $(tooltip).css("top",($(target).offset().top - $(tooltip).outerHeight() - config.vertical_offset) + "px");
       $(tooltip).css("left",($(target).offset().left + config.horizontal_offset) + "px");
       break;

           case "growl":
       $(tooltip).css("position","fixed");
       $(tooltip).css("top",10);
       $(tooltip).css("left",$("body").innerWidth()+config.horizontal_offset-30-$(tooltip).outerWidth());
       break;
   }

   if(parseInt($(tooltip).css("top"))<0) $(tooltip).css("top",0);
   if(parseInt($(tooltip).css("left"))<0) $(tooltip).css("left",0);
   var diff=$(tooltip).offset().left+$(tooltip).outerWidth-$("body").innerWidth();
   if(diff>0)  $(tooltip).css("left",parseInt($(tooltip).css("left"))-diff);
   //TODO: controllare anche se finisce in giu' fuori dalla pagina - come si fa?

   $(tooltip).fadeIn(config.speed);

   target.tooltip=tooltip;
}

function hideTooltip(target,force)
{
   var tooltip=target.tooltip;
   if(tooltip && (!target.stopHideTooltip || force))
   {
       $(tooltip).fadeOut(tooltip.config.speed, function(){$(tooltip).remove();});
       target.stopHideTooltip=false;
       target.tooltip=null;
   }
}

function activateAutocomplete(id,address)
{
	function format(row,rowIndex,totalRows,textSearched)
	{
		var ret=row[0];
		if(row.length>1)
		{
			ret+="(";
			for(var i=1;i<row.length;i++) {ret+=row[i]+",";}
			ret=ret.substring(0,ret.length-1)+")";
		};
		return ret;
	}

	var element=document.getElementById(id);
	$(element).autocomplete(
			 address,
			 {
			 minChars:2,
			 formatItem:format
			 });
}


function ajaxSubmit(from,target,replace,address,target_result,showMessage,messageCode,callback)
{
	var reply="";

	var $message=$(messageCode);

	if(showMessage)
	{
		if(replace)
			$("#"+target).replaceWith($message);
		else
			$("#"+target).html(messageCode);
	}

	if($(from)[0].tagName.toLowerCase()=="a")
	{
		reply=$.ajax({
	           url: address,
	           async: false,
	           type: "GET"
	           }).responseText;
	}
	else	//form
	{
		var dati=$(from).serialize();
		reply=$.ajax({
	           url: address,
	           async: false,
	           data: dati,
	           type: $(from).attr("method")
	           }).responseText;
	}


	var $result=null;
	if (target_result)
	{
		var $reply=$(reply);
		$result=$('#'+target_result,$reply);
	}
	else
	{
		$result=$(reply);
	}
	if(replace)
	{
		if(showMessage)
			$message.replaceWith($result);
		else
			$("#"+target).replaceWith($result);
	}
	else
		$("#"+target).html($result);

	if(callback) callback();

	return false;
}

function openPopup(from,title,address,width,height,draggable,ok_button)
{
	var reply="";

	if(address.match("/detail_edit")) address=address.replace("/detail_edit","/popup_detail_edit");

	if($(from)[0].tagName.toLowerCase()=="a")
	{
		reply=$.ajax({
	           url: address,
	           async: false,
	           type: "GET"
	           }).responseText;
	}
	else	//form
	{
		var dati=$(from).serialize();
		reply=$.ajax({
	           url: address,
	           async: false,
	           data: dati,
	           type: $(from).attr("method")
	           }).responseText;
	}

	reply=reply.replace(/detailEdit\./g,"popupDetailEdit.");

	var buttons={};
	if(ok_button)
	{
		buttons={'Ok': function() {$(this).dialog('close');}};
	}

	$dialog=$('<div title="'+title+'"><a href="#" style="height: 0;font-size: 0em; width: 0px; display: block; float: left;" id="ancora_popup">(aperto popup)</a>'+reply+"</div>");

	$dialog.dialog({
			bgiframe: true,
			autoOpen: true,
			width: width,
			height: height,
			modal: true,
			draggable: draggable,
			buttons: buttons,
			open: function(event, ui) { if(typeof(onopen_dialog)=="function") onopen_dialog($dialog); else $('#ancora_popup').focus(); },
			close: function() { if(typeof(onclose_dialog)=="function") onclose_dialog($dialog);  else $(this).remove(); }
		});


	return false;
}

/*--------------MAIN--------------*/

$(document).ready(function()
{
	if(window.location.href.match("activate="))
	{
		var id=window.location.href.match(/activate=([^&]*)/)[1];
		$('#'+id).click();
	}
});
