var MAX_INFOBOX_HEIGHT = 448;
var INFOBOX_INITAL_WAIT = 3000; //time in ms
var infoboxIsOpen = false;

//adapt all columns' heights to their wrapper container
function autoAdaptColumnHeights() {
	var container = $('#container1',$('#content-gast'))[0];
	if (!container) //no wrapper container on this page -> do nothing
	 return;
	var columnJQuery = $('#column1, #column2, #column3');
	if (columnJQuery.length!=3) //the expected three columns couldn't be found on this page -> better do nothing
	 return;
	columnJQuery.css('height',container.clientHeight+"px");
}

function initBigBox(box_width_1, box_width_2, box_height) {
    var max_width = box_width_1 > box_width_2 ? box_width_1 : box_width_2;

  //Migration to new style boxes (with vertical resizing) fix:
	box_width_1+=20;
  box_width_2+=20;

	$("#box").css( { 
		"height": box_height, 
		"left": -max_width-50});
	
	$("#box-content").css({width: box_width_1});
	
	var isTabbed= ($('#content-haus')[0] && $('#content-gast')[0]);

	if (!isTabbed)
	  $("#box").css( {"width": box_width_1}); //width is not set by tab switching so we need to set it here...
	else
	{
  	var heightHaus = Math.min($('#content-haus')[0].clientHeight, MAX_INFOBOX_HEIGHT);
  	var heightGast = null; //this can only be calculated correctly later on
	  var heightSpeed = null;
		
		$("#haus-tab").click( function(event) {
      $("#box-content").css({'overflow-x': 'hidden', 'overflow-y':'hidden'});
      $('#content-gast').animate(
              {"opacity": 0.0 },
          {duration: "slow", queue: false, complete: function() {
            $("#box-content").animate({"height": heightHaus},{duration: heightSpeed,queue: false,complete: function(){
              $('#content-gast').hide();
              $('#content-haus').show();
              $('#content-haus').css({opacity: 1});
              $("#box-content").css({'overflow-x': 'hidden', 'overflow-y':'auto'});
              }
            }); 
          } 
        }
      );
      $('#box-content').animate( { "width": box_width_1 }, {duration: "slow", queue: false});
      $('#haus-tab').removeClass('haus-inactive');
      $('#haus-tab').addClass('haus-active');
      $('#gast-tab').removeClass('gast-active');
      $('#gast-tab').addClass('gast-inactive');
    });
  
    $("#gast-tab").click( function(event) {
      $("#box-content").css({'overflow-x': 'hidden', 'overflow-y':'hidden'}); 
      $('#content-haus').animate(
              {"opacity": 0.0 },
          {duration: "slow", queue: false, complete: function() {
              if (!heightGast) {
                $('#content-gast').show();
                heightGast = Math.min($('#content-gast')[0].clientHeight,MAX_INFOBOX_HEIGHT);
                heightSpeed = Math.min(600,10*Math.abs(heightGast-heightHaus));
                autoAdaptColumnHeights();
                $('#content-gast').hide();
              }
              $("#box-content").animate({"height": heightGast},{duration: heightSpeed, queue:false, complete: function(){
                  $('#content-haus').hide(); 
                  $('#content-gast').show();
                  $('#content-gast').css({opacity: 1});
                  $("#box-content").css({'overflow-x': 'hidden', 'overflow-y':'auto'}); 
                }
              }); 
            } 
          }
      );
      $('#box-content').animate( { "width": box_width_2 }, {duration: "slow", queue: false});
      $('#haus-tab').removeClass('haus-active');
      $('#haus-tab').addClass('haus-inactive');
      $('#gast-tab').removeClass('gast-inactive');
      $('#gast-tab').addClass('gast-active');
    });   
	}	

	 	

	var box_open = false;
		
	$(document).ready(function() {
		$('#openbutton').css('left','-'+$('#openbutton').css('width'));
    
		if (isTabbed)
		  $("#haus-tab").click();
		
		$('#closebutton').click(
      function() {
        hideContents(max_width);
        return false;
      }
    
    );
    
    $('#openbutton').click(function() {
        showContents(max_width);      
        return false;
      }
    );
		
		//Make a click on anything (except open button, the navi, the home button, the language menu, and the box itself) close the box:
		var mainContainerJQuery = $("#container");
		if (mainContainerJQuery.length==1)
		{
			var mainContainer = mainContainerJQuery[0];
			for (var i=0; i< mainContainer.childNodes.length; i++)
			{
				var childNode = mainContainer.childNodes[i];
				var nodeId = childNode.id ? childNode.id.toLowerCase() : null;
				var nodeClass = childNode.className ? childNode.className.toLowerCase() : null;
				if (nodeId!="openbutton" && nodeId!="navi" && nodeId!="box" && nodeClass!="menu-box" && nodeId!="logo" && nodeId!="langpress")
				{
					$(childNode).click(function() {
             hideContents(max_width);
          });
				}					  
			}
		}
		
		setTimeout(function(){
			$('#box').css("visibility","visible");
	  	showContents(max_width);
	  },INFOBOX_INITAL_WAIT);
	});
}


function showContents(max_width)
{
	if (infoboxIsOpen)
	  return;
		
	infoboxIsOpen=true;
	$('#openbutton').animate({left:'-'+$('#openbutton').css('width')},{duration: "fast", complete: function() {
		$('#box').animate( {"left": "+=" + (max_width+100)  }, "slow");
    $('#closebutton').show();
	}});
}

function hideContents(max_width)
{
	if (!infoboxIsOpen)
    return;
    
  infoboxIsOpen=false;
	
	$('#box').animate( {"left": "-=" + (max_width+100)  }, {duration: "slow", complete: function() {
		$('#openbutton').animate({left: 0},"slow");
	}});
  $('#closebutton').hide();
}

