/* #############################################
	# Module: TextBild-Popup (2Spalten)
*/
function showPopup(file,width,height,maxW)
{	file=file.src;	
	file=file.split("w__");
	file=file[1];
	
	newwidth=width;
	newheight=height;
	
	ratio=maxW/newwidth;
		
	if (newwidth>maxW)
	{	newwidth=maxW;
		newheight=height*ratio;
	}


	var x=window.open('', 'bigPrev', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+(newwidth)+',height='+(newheight));
	
	x.document.write("<html><head><title>Bild</title>");
	x.document.write('<style type="text/css">');
	x.document.write("html,body{margin:0;padding:0;overflow:hidden;}img{cursor:pointer;}");
	x.document.write("</style></head><body>");

	x.document.write('<img src="index.php?rex_resize='+newwidth+'w__'+file+'" alt="" onclick="window.close();">');
	x.document.write("</body></html>");
	x.document.close();
	x.focus();


}	
/*	#############################################*/

/* ############################################
Slideshow Functions
*/

var reverse=0;

function slideBack()
{	clearTimeout(slideSwap);
	
	reverse=1;
	slideSwap=setTimeout("slider("+currentSlide+")",0);
	
	config['stop']="yes";
	document.getElementById('slidePlayPause').className="stop";
}

function slidePlayPause()
{	if (config['stop']!="yes")
	{	config['stop']="yes";
		clearTimeout(slideSwap);
		document.getElementById('slidePlayPause').className="play";
	}
	else
	{	config['stop']="no";
		slideSwap=setTimeout("slider("+currentSlide+")",0);
		document.getElementById('slidePlayPause').className="stop";
	}
	
}	
	
function slideNext()
{	clearTimeout(slideSwap);
	currentSlide=currentSlide++;
	slideSwap=setTimeout("slider("+currentSlide+")",0);
	config['stop']="yes";
	document.getElementById('slidePlayPause').className="play";
}	
	
function Left(STRING,CHARACTER_COUNT){
return STRING.substring(0,CHARACTER_COUNT);
}


//Opacity
// http://www.brainerror.net/scripts_js_blendtrans.php
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
//change the opacity for different browsers

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function slider()
{	var args = slider.arguments;
	

	//If the first iteration, set up the variables
	if (args.length==0)
	{	start=0;
	}
	else
	{	//Future iteration
		start=args[0];
	}
	//If only one image
	if ($slideshow.length==0)
	{	return true;
	}	

	if (reverse==1)
	{	start=currentSlide;
		if (currentSlide==0)
		{	next=slides-1;
		}
		else
		{	next=currentSlide-1;
		}
	}
	else
	{	next=start+1;
	}
	
	//On the last image, restart
	if ((start==($slideshow.length-1)) && (reverse!=1))
	{	next=0;
	}

	if (reverse==1)
	{	reverse=0;
	}
	
	opacity('slide'+start,100,0,$speed);
	opacity('slide'+next,0,100,$speed);
	
	currentSlide=next;
	
	document.getElementById('slide'+start).style.zIndex=80;
	document.getElementById('slide'+next).style.zIndex=81;
	
	if (config['text']!="no")
	{	document.getElementById('desc'+start).style.display="none";
		document.getElementById('desc'+next).style.display="block";
		
		
		
		document.getElementById('desc'+start).style.zIndex=80;
		document.getElementById('desc'+start).style.position="absolute";
		document.getElementById('desc'+next).style.zIndex=81;
			
	}
	
	if (config['stop']!="yes")
	{	slideSwap=setTimeout("slider("+next+")",$delay);
	}
}






function startShow()
{	if (document.getElementById('slideshow'))
	{	
		var slideshow=document.getElementById('slideshow');
	
		slideshow.style.position="relative";
		slideshow.innerHTML="";
		
		$counter=0;
		
		slides=$slideshow.length;
		
		
		for (var i=0;i<$slideshow.length;i++)
		{	var slide=document.createElement("DIV");
			slide.setAttribute('id','slide'+i);
			slide.className="slide";
			slide.style.position="absolute";
			slide.style.overflow="visible";
			
							
			if (i>0)
			{	slide.style.display="none";
			}
			
			var div=document.createElement("DIV");
			div.className="bildContainer";
			
			var imgLink=document.createElement("A");
			
			if (config['text']!="no")
			{	var desc=document.createElement("P");
				desc.setAttribute('id','desc'+i);
				desc.innerHTML=Left($beschreibung[i],70);
				desc.style.className="slidedesc";
				desc.style.position="absolute";
			}
			if (($link[i]!="") && ($link[i]!=undefined))
			{	
				imgLink.setAttribute('href',$link[i]);
				
				if (Left($link[i],4)=="http")
				{	imgLink.setAttribute('target','_blank');
				}
				
				if (config['text']!="no")
				{	desc.innerHTML='<a target="_blank" href="'+$link[i]+'">'+desc.innerHTML+'</a>';
				
				}
			}
			else
			{	//imgLink.setAttribute('href','javascript:;');
				
			}
			
			var img=document.createElement("IMG");
			img.src=$path+"/"+$slideshow[i];
			
			/*
			if (img.width=="0")	
			{	setTimeout(	function()
							{	img.style.marginTop=(($boxheight-img.height)/2)+"px";
								img.style.marginLeft=(($boxwidth-img.width)/2)+"px";						
							},2000);
			}
			else
			{	img.style.marginTop=(($boxheight-img.height)/2)+"px";
				img.style.marginLeft=(($boxwidth-img.width)/2)+"px";
			}*/
			
			
			
			imgLink.appendChild(img);
					
			
			div.appendChild(imgLink);		
			slide.appendChild(div);
			if (config['text']!="no")
			{	slideshow.appendChild(desc);
				
			}
			
			slideshow.appendChild(slide);
			
			document.getElementById('slide0').style.zIndex=100;
			if (config['text']!="no")
			{	document.getElementById('desc0').style.zIndex=81;
			}
				
			if (i>0)
			{	changeOpac(0,'slide'+i);
				document.getElementById('slide'+i).style.display="block";
			}
			
		} //eoFor
		
		if (config['navi']=="yes")
		{	var controls=document.createElement("SPAN");
			controls.id="controls";
			controls.style.display="block";
			var back=document.createElement("A");
			back.id="slideBack";
			back.href="javascript:slideBack();";
			back.innerHTML="&lt;";
			back.title = "vorheriges Bild";
			
			var playpause=document.createElement("A");
			playpause.id="slidePlayPause";
			playpause.href="javascript:slidePlayPause();";
			if (config['stop']!="yes")
			{	playpause.className="stop";
				playpause.title="Pause"
			}
			else
			{	playpause.className="play";
				playpause.title="Play"
			}
			playpause.innerHTML="x";
			
			var next=document.createElement("A");
			next.id="slideNext";
			next.href="javascript:slideNext();";
			next.innerHTML="&gt;";
			next.title="nchstes Bild";
			
			controls.appendChild(back);
			controls.appendChild(playpause);
			controls.appendChild(next);
			slideshow.appendChild(controls);
		}
		
		start=0;
		
		currentSlide=0;
		if (config['stop']!="yes")
		{	slideSwap=setTimeout("slider()",$delay);
		}
		
		
		
	}
}

/* ##########################
BEHAVIOUR
*/


Behaviour.addLoadEvent 
(	function()
	{   if (window['$slideshow']!=undefined)
		{	//Load config
			if (window['$slideconfig']!=undefined)
			{	config=window['$slideconfig'];
				
			}	else {config=new Array();}
			startShow();
			
		}
		
	}
);

var Rules = {
	'.extern' : function(el)
	{	el.onclick = function()
		{    this.setAttribute('target','_blank');
		}
	}	
};

Behaviour.register(Rules);



//Experimental getVar
function getVar(name)
 {
 get_string = document.location.search;         
 return_value = '';
 
 do { //This loop is made to catch all instances of any get variable.
	name_index = get_string.indexOf(name + '=');
	
	if(name_index != -1)
	  {
	  get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
	  
	  end_of_value = get_string.indexOf('&');
	  if(end_of_value != -1)                
		value = get_string.substr(0, end_of_value);                
	  else                
		value = get_string;                
		
	  if(return_value == '' || value == '')
		 return_value += value;
	  else
		 return_value += ', ' + value;
	  }
	} while(name_index != -1)
	
 //Restores all the blank spaces.
 space = return_value.indexOf('+');
 while(space != -1)
	  { 
	  return_value = return_value.substr(0, space) + ' ' + 
	  return_value.substr(space + 1, return_value.length);
					 
	  space = return_value.indexOf('+');
	  }
  
 return(return_value);        
 }




Behaviour.register(Rules);
