 /**
  * JavaScript for the jaron.de main navigation.
  *
  * @author Steffen Friedrich (steffen.friedrich@pingping.ag, www.pingping.ag)
  ***/

function get( id )
{ return document.getElementById( id ); }

function startUp()
{ new TagCloud(get('tags'));
}


function TagCloud(root)
{ if(!root) return;

  var paNo = root.parentNode,sklave;
//  paNo.removeChild(root);
//  root.style.position='absolute';

  var as=root.getElementsByTagName('a'),maxS=0;

  for(sklave=0;sklave<as.length;sklave++)
  { as[sklave].style.position='absolute';
    maxS=Math.max(maxS,as[sklave].offsetWidth);
  }

  var maxW=206;
  
  if(maxS>maxW)
    _TagCloud_scale(as, maxW/maxS);
  
 var alis=['left','center','right'];
 
 for(sklave=0;sklave<as.length;sklave++)
    as[sklave].style.position='static';
 //for(sklave=0;sklave<as.length;sklave++)
//      as[sklave].style.textAlign=alis[Math.round(Math.random()*3)];
 root.style.width='206px';
  // to 
 
  if(parseInt(root.offsetHeight)>110)
  { var ta = root.parentNode;
    while(ta.parentNode && ta.parentNode.tagName.toLowerCase() != 'table' )
      ta = ta.parentNode;
    var maxH = ta.parentNode.getAttribute('class').indexOf('small')==-1 ? 219 : 110;
    if(parseInt(root.offsetHeight)>maxH)
      _TagCloud_scale(as, maxH/root.offsetHeight);

    
  }
}

function _TagCloud_scale(as, factor)
{   var fos, foo;
    for(sklave=0;sklave<as.length;sklave++)
    { fos='';
      foo=as[sklave].style.fontSize;
      if(typeof foo=='string')
        fos = foo.substr(0,foo.indexOf('p'));
      else
        for(sth in foo)
          if(foo[sth]=='p') break;
          else fos+=foo[sth];

      as[sklave].style.fontSize = Math.round(fos*factor)+'px';
    }
}

 /**
  * JavaScript for changing images
  *
  * @author Steffen Friedrich - steffen.friedrich@pingping.ag, Yan Dehner - yan.dehner@pingping.ag
  * @copy pingping.ag
  ***/

function imageChanger(_id, _path, _name, _images)
{
	this.name = _name;
	this.path = _path;
	this.element = get(_id);
	this.images = new Object();
	this.queue = null;
	this.act_image_nr = 0;
	this.isCool = !document.all;

	this.complete = function(_image)
	{
		var image;
		image = document.createElement("IMG");
		image.src = _image.src;
		this.element.appendChild(image);

		image.style.top = Math.round(45 - image.height/2) + "px";
		image.style.left = Math.round(99-image.width/2) + "px";

		if(this.isCool) image.style.opacity = 0.0;
			else image.style.filter='alpha(opacity=0)';
		if ( this.queue )
		{
			this.queue.push(image);
		} else {
			this.queue = new Array(image);
			this.animate(0);
			this.blend(0, 0, 10);
		}
	}

	this.animate = function( image_nr )
	{
		if ( this.act_image_nr != image_nr )
		{
			this.blend(this.act_image_nr, 10, 0);

		}

		if ( this.act_image_nr != image_nr )
		{
			this.blend(image_nr, 0, 10);
			this.act_image_nr = image_nr;
		}

		this.queue[image_nr].style.visibility = "visible";
		window.setTimeout(this.name + ".animate("+ ((image_nr+1)%this.queue.length) +")", 3000);
	}

	this.blend = function( image_nr, from_value, to_value )
	{
		var step = (to_value - from_value) / 10;

		if(!to_value)
			window.setTimeout(this.name+'.queue['+image_nr+'].style.visibility="hidden"',from_value*100);
		else this.queue[image_nr].style.visibility="visible";

		for(var sklave=from_value;sklave<=to_value&&step>0||sklave>=to_value&&step<0;sklave+=step)
		{
			if(this.isCool) window.setTimeout(this.name+'.queue['+image_nr+'].style.opacity='+sklave*.1,step>0?sklave*100:1000-sklave*100);
			else window.setTimeout(this.name+'.queue['+image_nr+'].style.filter="alpha(opacity='+sklave*10+')"',step>0?sklave*100:1000-sklave*100);

			// filter:alpha(opacity=50);
		}
	}

	for(var i=0; i<_images.length; i++)
	{
		this.images[_images[i]] = new Image();
		this.images[_images[i]].changer = this;
		this.images[_images[i]].onload = function () {this.changer.complete(this);}
		this.images[_images[i]].src = this.path + _images[i];
	}


}

 /**
  * JavaScript for paginating through google results.
  *
  * @author Steffen Friedrich, steffen.friedrich@pingping.ag
  * @copy pingping.ag
  ***/
function GoogleSearchResultBrowser(elementPrefix,resultsNumber,resultsPerPage,startAtResult)
{
	this.prefix = elementPrefix;
	this.current = startAtResult ? startAtResult : 0;
	this.count = resultsNumber;
	this.rpp = resultsPerPage;
	this.boxes = new Array();

	var sklave=0;

	var resF = get(this.prefix+'_resultsFrom'),
		resT = get(this.prefix+'_resultsTo');

	while(get(this.prefix+sklave) && get(this.prefix+sklave+'_button'))
	{	this.boxes[sklave] = new Object();

		this.boxes[sklave] = get(this.prefix+sklave);
		this.boxes[sklave].button = get(this.prefix+sklave+'_button');
		this.boxes[sklave].num = sklave;
		this.boxes[sklave].ePrefix = elementPrefix;

		this.boxes[sklave].fromElement = resF;
		this.boxes[sklave].from = sklave*this.rpp+1;

		this.boxes[sklave].toElement = resT;
		this.boxes[sklave].to = Math.min((sklave+1)*this.rpp,this.count);

			this.boxes[sklave].hide = function() {
				this.style.display = 'none';
				this.button.className = 'tx-jppageteaser-pi1-browsebox-inactive-link';
				if(!this.num)
					{	get(this.ePrefix+'_toStart_button').className = 'tx-jppageteaser-pi1-browsebox-inactive-link';
						get(this.ePrefix+'_retreat_button').className = 'tx-jppageteaser-pi1-browsebox-inactive-link';
					}else
				if(this.num==this.max)
					{	get(this.ePrefix+'_toEnd_button').className = 'tx-jppageteaser-pi1-browsebox-inactive-link';
						get(this.ePrefix+'_advance_button').className = 'tx-jppageteaser-pi1-browsebox-inactive-link';
					}

			}

			this.boxes[sklave].show = function() {
				this.style.display = 'block';
				this.button.className = 'tx-jppageteaser-pi1-browsebox-active-link';
				this.fromElement.innerHTML = this.from;
				this.toElement.innerHTML = this.to;

				if(!this.num)
					{	get(this.ePrefix+'_toStart_button').className = 'tx-jppageteaser-pi1-browsebox-active-link';
						get(this.ePrefix+'_retreat_button').className = 'tx-jppageteaser-pi1-browsebox-active-link';
					}else
				if(this.num==this.max)
					{	get(this.ePrefix+'_toEnd_button').className = 'tx-jppageteaser-pi1-browsebox-active-link';
						get(this.ePrefix+'_advance_button').className = 'tx-jppageteaser-pi1-browsebox-active-link';
					}

			}

		sklave++;
	}

	this.max = sklave-1;

	for(box in this.boxes)
		this.boxes[box].max = this.max;

	this.open = function(nr)
	{
		if(this.boxes[nr])
		{	this.boxes[this.current].hide();
			this.boxes[nr].show();
			this.current=nr;
		}

	}

	this.toStart = function()
	{	this.open(0);	}

	this.advance = function()
	{	if(this.current<this.max) this.open(this.current+1);	}

	this.retreat = function()
	{	if(this.current>0) this.open(this.current-1);	}

	this.toEnd = function()
	{	this.open(this.max);	}
}

