function inspect(obj) {
	for (var prop in obj) {
		$('body').append("" + prop + ": " + obj[prop] + "<br />");
	}
}

function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
  return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

$(document).ready(function(){
	// alle akkordeon inhalte initial verstecken
  $('div.accordion div.accordionContent').hide();
  $('div.accordion > div.accordionHeader').click(function(){
		var openNode = true;
    //$('div.accordion.open').find('div.accordionContent').slideUp('fast');
		if ($(this).parent().hasClass("open")) openNode = false;
		if (openNode) {
  		$(this).parent().find('>div.accordionContent').slideDown('slow'); 
  		$('div.accordion.open').find('>div.accordionContent').hide();
  		$(this).parent().find('>div.accordionContent').show(); 
  		$('div.accordion.open').removeClass("open");	
  		$(this).parent().addClass("open");
    } else {
  		$(this).parent().find('>div.accordionContent').slideUp('slow'); 
  		$(this).parent().removeClass("open");
    }	
	});
	$('div.accordion.open > div.accordionContent').slideDown('slow'); 
 
  // subaccordions
	$('div.accordion > div.accordionContent > div.subAccordion div.accordionHeader').click(function(){
		var openNode = true;
 		if ($(this).parent('div.subAccordion').hasClass("open")) openNode = false;
		if (openNode) {
  		$(this).parent('div.subAccordion').find('div.accordionContent').slideDown('slow'); 
  		$('div.subAccordion.open').find('div.accordionContent').hide();
  		$(this).parent('div.subAccordion').find('div.accordionContent').show(); 
  		$('div.subAccordion.open').removeClass("open");	
  		$(this).parent('div.subAccordion').addClass("open");
    } else {
  		$(this).parent('div.subAccordion').find('div.accordionContent').slideUp('slow'); 
  		$(this).parent('div.subAccordion').removeClass("open");
    }	
	});
	$('div.accordion div.accordionContent div.subAccordion.open div.accordionContent').slideDown('slow'); 
  
  // footer
  $('#footerInfoText').hide();
});


$(document).ready(function(){

	  $('div.suche').hover(sucheEnter);
	  $('div.suchForm').hover(sucheEnter);
	  $('div.kontakt').hover(kontaktEnter);
	  $('div.kontaktText').hover(kontaktEnter);
	  $('div.topPanel').hover(enter, out);
});
		function sucheEnter(){
			$('div.kontaktText').hide();
			$('div.suchForm').show();
		}

		function out(){
			$('div.kontaktText').hide();
			$('div.suchForm').hide();
		}
		function enter(){}
		function kontaktEnter(){
			$('div.suchForm').hide();
			$('div.kontaktText').show();
		}

function toggleFooterInfo() {
  if ($('#footerInfoText').is(':visible')) {
    $('#footerInfoText').hide();
    $('#footer .contentFooter .middle a').removeClass('hi');
  } else {
    $('#footer .contentFooter .middle a').addClass('hi');
    $('#footerInfoText').show();
  }
}

function getLeftPosToCenterElement(lengthFull, lengthElement) {
  var center = Math.round(lengthFull/2-lengthElement/2);
  return center;
}


function positioningCloudTop(arrTags, maxWidth) {
  arrTags[0].left = getLeftPosToCenterElement(maxWidth, arrTags[0].width);
  arrTags[0].row = 0;
  arrTags[0].top = 0;
  var rowsOnTop = 4;
  var lastFullRowWidth = arrTags[0].width;
  var shiftPerRow = (maxWidth-arrTags[0].width)/2/rowsOnTop;
  var row;
  var currTagNr = 1;
  // zeilen fuer oben durchlaufen
  for (row=1; row<rowsOnTop; row++) {
    if(currTagNr < arrTags.length) {
      lastFullRowWidth = lastFullRowWidth+shiftPerRow*2;
      currTagRowWidth = 0;
      countTagsInRow = 0;
      // letztes element der zeile ermitteln
      if (typeof arrTags[currTagNr+countTagsInRow] != 'undefined') {
        do {
          currTagRowWidth += arrTags[currTagNr+countTagsInRow].width;
          countTagsInRow++;
        } while(typeof arrTags[currTagNr+countTagsInRow] != 'undefined' && currTagRowWidth+arrTags[currTagNr+countTagsInRow].width < lastFullRowWidth);
      }
      // zwischenraum zwischen den tags berechnen
      if (countTagsInRow < 2) {
        arrTags[currTagNr].row = row;
        arrTags[currTagNr].left = getLeftPosToCenterElement(maxWidth, arrTags[currTagNr].width);
      } else {
        distanceBetweenTags = (lastFullRowWidth-currTagRowWidth)/(countTagsInRow-1);
        
        currPos = shiftPerRow*(rowsOnTop-row);
        for (i=0; i<countTagsInRow; i++) {
          arrTags[currTagNr+i].left = Math.round(currPos);
          arrTags[currTagNr+i].row = row;
          currPos = currPos+arrTags[currTagNr+i].width+ distanceBetweenTags;
        }
      }
    }
    currTagNr += countTagsInRow; 
  }
  return arrTags;
}
function positioningCloudMiddle(arrTags, maxWidth) {
  var currTagNr = 1;
  while (arrTags.length > currTagNr && arrTags[currTagNr].row != 0) {
    currTagNr++;
  }
  var row = arrTags[currTagNr-1].row+1;
  // zeilen fuer oben durchlaufen
  while (arrTags.length > currTagNr && arrTags[currTagNr].row == 0) {
    lastFullRowWidth = maxWidth;
    currTagRowWidth = 0;
    countTagsInRow = 0;
    // letztes element der zeile ermitteln
    do {
      currTagRowWidth += arrTags[currTagNr+countTagsInRow].width;
      countTagsInRow++;
    } while(currTagNr+countTagsInRow < arrTags.length && currTagRowWidth+arrTags[currTagNr+countTagsInRow].width < lastFullRowWidth);
    // zwischenraum zwischen den tags berechnen
    if (countTagsInRow < 2) {
      arrTags[currTagNr].row = row;
      arrTags[currTagNr].left = getLeftPosToCenterElement(maxWidth, arrTags[currTagNr].width);
    } else {
      distanceBetweenTags = (lastFullRowWidth-currTagRowWidth)/(countTagsInRow+1);
      
      currPos = distanceBetweenTags;
      for (i=0; i<countTagsInRow; i++) {
        arrTags[currTagNr+i].left = Math.round(currPos);
        arrTags[currTagNr+i].row = row;
        currPos = currPos+arrTags[currTagNr+i].width+ distanceBetweenTags;
      }
    }
    currTagNr += countTagsInRow; 
    row++;
  }
  return arrTags;
}

function positioningCloudBottom(arrTags, maxWidth) {
  arrTags[arrTags.length-1].left = getLeftPosToCenterElement(maxWidth, arrTags[arrTags.length-1].width);
  arrTags[arrTags.length-1].row = -1;
  arrTags[arrTags.length-1].top = 0;
  var rowsOnTop = 4;
  var lastFullRowWidth = arrTags[arrTags.length-1].width;
  var shiftPerRow = (maxWidth-arrTags[arrTags.length-1].width)/2/rowsOnTop;
  var row;
  var currTagNr = arrTags.length-2;
  if (currTagNr < 0) currTagNr = 0;
  // zeilen fuer oben durchlaufen
  for (row=1; row<rowsOnTop; row++) {
    if (currTagNr >= 0) {
      lastFullRowWidth = lastFullRowWidth+shiftPerRow*2;
      currTagRowWidth = 0;
      countTagsInRow = 0;
      // letztes element der zeile ermitteln
      if (typeof arrTags[currTagNr-countTagsInRow] != 'undefined') {
        do {
          currTagRowWidth += arrTags[currTagNr-countTagsInRow].width;
          countTagsInRow++;
        } while(typeof arrTags[currTagNr-countTagsInRow] != 'undefined' && currTagRowWidth+arrTags[currTagNr-countTagsInRow].width < lastFullRowWidth);
      }
      // zwischenraum zwischen den tags berechnen
      if (countTagsInRow < 2) {
        arrTags[currTagNr].row = row*-1-1;
        arrTags[currTagNr].left = getLeftPosToCenterElement(maxWidth, arrTags[currTagNr].width);
      } else {
        distanceBetweenTags = (lastFullRowWidth-currTagRowWidth)/(countTagsInRow-1);
        
        currPos = shiftPerRow*(rowsOnTop-row);
        for (i=0; i<countTagsInRow; i++) {
          arrTags[currTagNr-i].left = Math.round(currPos);
          arrTags[currTagNr-i].row = row*-1-1;
          currPos = currPos+arrTags[currTagNr-i].width+ distanceBetweenTags;
        }
      }
    }
    currTagNr = currTagNr-countTagsInRow; 
  }
  return arrTags;
}

function doTagCloudProcess (tagCloudDivId){
  var arrTags = new Array();
  var currRow = 0;
  var counter = 0;
  var maxWidth = $('#'+tagCloudDivId).width();

  $('#'+tagCloudDivId+' div.tag').each(function(index){
    var currTag = new Object();
    currTag.id = $(this).attr('id');
    currTag.top = 500;
    currTag.left = 0;
    currTag.row = 0;
    currTag.width = $(this).width();
    currTag.height = $(this).outerHeight();
    arrTags.push(currTag);
    counter++;
  });
  var countTags = arrTags.length;

  var maxBottom = 0;
  var i,j;

  // erstes und letztes tag in die mitte
  arrTags = positioningCloudTop(arrTags, maxWidth);
  arrTags = positioningCloudMiddle(arrTags, maxWidth);
  arrTags = positioningCloudBottom(arrTags, maxWidth);
  
  // rendern der tags
  currRow = 0;
  lastRow = 0;
  lastTop = 0;
  maxHeightInRow = 0;
  for (i=0; i<countTags; i++) {
    if (lastRow != arrTags[i].row) {
      currRow++;
      lastTop += maxHeightInRow+5;
      maxHeightInRow = 0;
    }
    lastRow = arrTags[i].row;
    if (arrTags[i].height > maxHeightInRow) maxHeightInRow = arrTags[i].height;
    if (arrTags[i].row > 0) {
      arrTags[i].row = currRow;
      arrTags[i].top = lastTop+GetRandom(0,5);
      //currRow = arrTags[i].row;
    } else if (arrTags[i].row < 0) {
      arrTags[i].row = currRow;
      arrTags[i].top = lastTop+GetRandom(0,5); 
      //arrTags[i].row*30;
    }
    if (arrTags[i].top+arrTags[i].height > maxBottom)  maxBottom = arrTags[i].top+arrTags[i].height+5;
    $('#'+tagCloudDivId+' #'+arrTags[i].id).css('top', arrTags[i].top);
    $('#'+tagCloudDivId+' #'+arrTags[i].id).css('left', arrTags[i].left);
  }
  
//  for(i=0; i<arrTags.length; i++) {
//    inspect(arrTags[i]);
//    $('body').append("####<br />");
//  }
  
  $('#'+tagCloudDivId).css('height', maxBottom+5);
}
 

