﻿/* http://www.codingforums.com/showthread.php?t=58814 
*/
function banner(params){
	if(!(params.width>0 && isFinite(params.width)))params.width=100;
	if(!(params.height>0 && isFinite(params.height)))params.height=100;
	if(!(params.step>0 && params.step<100 && isFinite(params.step)))params.step=20;
	if(!(params.speed>0 && isFinite(params.speed)))params.speed=200;
	if(!(params.brake_duration>=0 && isFinite(params.brake_duration)))params.brake_duration=0;
	if(!(params.brake_item>=0 && isFinite(params.brake_item)))params.brake_item=0;
	if(!(params.item_padding>=0 && isFinite(params.item_padding)))params.item_padding=0;
	if(!(params.item_border_width>=0 && isFinite(params.item_border_width)))params.item_border_width=0;
	if(!(params.item_width>0 && isFinite(params.item_width)))params.item_width=params.width;
	if(!(params.item_height>0 && isFinite(params.item_height)))params.item_height=params.height/params.params.images.length;
	if(isFinite(params.id)||!params.id)params.id='bad_id_given_'+Math.random();

	document.write("<div style='position:relative;overflow:hidden;' id='"+params.id.replace(/[^a-zA-Z0-9]+/g,'_')+"'></div>");
	var brdiv=document.getElementById(params.id.replace(/[^a-zA-Z0-9]+/g,'_'));
	brdiv.style.width=params.width+'px';
	brdiv.style.height=params.height+'px';
	brdiv.style.border=params.border;
	brdiv.style.position='relative';
	brdiv.style.overflow='hidden';
	brdiv.title=params.id;

	var spinning=true;
	var direction=params.direction, targ, attr, faraway;
	var images=[];
	var first_item=0, offset=0;
	var brake_item_count=0;
	
	function init() {
		faraway=(direction=="left" || direction=="top")?'-20000px':'20000px';
		
		// Image-Node - Template erstellen
		var img=new Image();
		img.style.position='absolute';
		img.style[direction]=faraway;
		img.style.width=params.item_width-2*params.item_border_width+'px';
		img.style.height=params.item_height-2*params.item_border_width+'px';
		img.style.border=(params.iten_border_width||0)+'px solid '+params.item_border_color;

		// loop über alle Images
		for (var i = 0; i < params.images.length; i++) {
			images[i]=img.cloneNode(true);
			images[i].src=params.images[i];
			// Link
			if(params.links && params.links[i] && params.links[i] != ''){
				targ=params.lnk_targets && params.lnk_targets[i]||params.lnk_base||'new';
				if(targ=="_blank"){
					attr=(params.lnk_attr && params.lnk_attr[i])?",'"+params.lnk_attr[i]+"'":"";
					images[i].onclick=new Function("window.open('"+params.links[i]+"','"+targ+"'"+attr+")");
				} else if(targ.substr(0,1)=="_") {
					images[i].onclick=new Function(targ.substr(1)+".location='"+params.links[i]+"'");
				} else {
					attr=(params.lnk_attr && params.lnk_attr[i])?",'"+params.lnk_attr[i]+"'":"";
					images[i].onclick=new Function("var t='"+targ+"';if(window[t]){try{window[t].close()}catch(z){}}window[t]=window.open('"+params.links[i]+"',t"+attr+");window[t].focus()");
				}
				images[i].style.cursor=document.all?'hand':'pointer';
			}
			// Title
			if (params.titles && params.titles[i] && params.titles[i]!='') {
				images[i].title=params.titles[i];
			}
			images[i].alt=images[i].title;
			brdiv.appendChild(images[i]);
      }
	}
	init();

	brdiv.rotate = function() {
		if (spinning) {
			// alle sichtbaren items positionieren			
			var current_item = first_item;
			var current_pos = offset;
			while (current_pos < params.height) {
				images[current_item].style[direction]=current_pos+'px';
				images[current_item].style.left=((params.width - params.item_width)/2)+'px';
				current_pos += params.item_height + params.item_padding;
				current_item++;
				if (current_item >= params.images.length) {
					current_item = 0;
				}
				// kein Item mehrfach
				if (current_item == first_item) {
					current_pos = params.height;
				}
			} 
			
			// alle anderen items unsichtbar
			while (current_item != first_item) {
				images[current_item].style[direction]=faraway;
				current_item++;
				if (current_item >= params.images.length) {
					current_item = 0;
				}
			} 
			
			// einen step weiter
			offset -= params.step;
			if (offset + params.item_height < 0) {
				offset += params.item_height + params.item_padding;
				first_item++;
				if (first_item >= params.images.length) {
					first_item = 0;
				} 
				if (params.brake_item > 0) {
					brake_item_count++;
				}
			}

		}
		if (brake_item_count > params.brake_item) {
			brake_item_count = 0;
			setTimeout("document.getElementById('"+brdiv.id+"').rotate()",params.brake_duration);
		} else {
			setTimeout("document.getElementById('"+brdiv.id+"').rotate()",params.speed);
		}
   }
   
	brdiv.onmouseover=function() {
		 spinning=false;
   }
   
	brdiv.onmouseout=function(){
		spinning=true;
   }
   
	setTimeout("document.getElementById('"+brdiv.id+"').rotate()",100);
}	


