/**
 * @author tucci
 * Rotator - small
 * Version 0.0.1	
 */
var myInterval;
var myPause;

var User = function () {
	this.Image	= "";
	this.Name	= "";
	this.URL	= "";
};

var Rotator = function () {

	this.Users			= []	;	
	this.displayArea	= ""	;
	this.mode			= 0		;
	this.myLeft			= []	;
	this.liWidth		= 90	;
	this.waittime 		= 1	;
	this.speed			= 30	;
	this.stepwith		= 1		;
	this.stepwait		= 0		;
	this.margin			= 5		;
	this.shownpics		= 5		;
	
	this.totalwidth		= this.liWidth + (2 * this.margin);
	
	this.run = function () {

		this.buildWorld();
		myPause = setInterval("window.rotator.startScrolling()",this.waittime);
	
	};

	this.buildWorld = function () {
		var _ul = this.createElement('ul',{"attributes":{"id":"rotatorul"}});
		_ul.style.width = ((this.Users.length + 4) * this.liWidth)+"px";
		_ul.style.zIndex = 0;
		_ul.onmouseover = function() {
			window.rotator.stop();
		};
		_ul.onmouseout = function() {
			window.rotator.go();
		};
		
		var _rotatorcontainer = this.createElement('div',{"attributes":{"id":"rotatorcontainer"},"append":[_ul]});		
		_rotatorcontainer.style.overflow = "hidden";
		_rotatorcontainer.style.zIndex = 1;
		//_rotatorcontainer.style.width = this.shownpics * this.liWidth + "px";			
				
		this.$(this.displayArea).appendChild(_rotatorcontainer);		
		this.buildList();
	};
	this.buildList = function() {
		for ( var i=0; i < this.Users.length; i++ ) {
			try {
				this.buildUser( this.Users[i] );
			} catch (e) {alert('Error name: ' + e.name + '. Error message: ' + e.message);}			
		}
	}
 	this.autoScroll = function () {
 		this.mode = 1;
 		var _users = this.$$$('rotatorli');
    	for (var counter=0; counter < this.Users.length; counter++){
			this.myLeft[counter] = this.myLeft[counter] - this.stepwith;
			if ( this.myLeft[counter] <= - ( counter * this.totalwidth + this.totalwidth ) ) {
				var leftpos = 0;
				leftpos = this.Users.length * this.totalwidth + Math.abs( this.myLeft[ this.Users.length - 1 ] ) - ( counter * 2 * this.totalwidth ) - (2 * this.totalwidth ) + this.stepwith;
				this.myLeft[counter] = ( this.myLeft[counter] != this.myLeft[ this.Users.length - 1] ) ? ( leftpos ) : 0;
				if (this.stepwait > 0) {
					this.stop();
					myPause = setInterval("window.rotator.startScrolling()",this.stepwait);
				}
			}
			_users[counter].style.left = this.myLeft[counter] + "px";			
		}
		this.mode = 1;
    }
	this.startScrolling = function (){
		if (this.ready() == true){		
			window.clearInterval(myPause)
			myInterval = setInterval("window.rotator.autoScroll()",this.speed);
		}
	}	
    this.stop = function (){
		if (this.mode == 1) {
			window.clearInterval(myInterval);
		}
		if (this.mode == 0) {
			window.clearTimeout(myPause);
		}	
	}
	
	this.go = function (){
		if (this.mode == 1){
			myInterval = setInterval("window.rotator.autoScroll()",this.speed);
		}
		if (this.mode == 0){
			myPause = setTimeout("window.rotator.startScrolling()",this.waittime);
		}	
	}
	this.ready = function () {
		var _user = this.$('rotatorul').getElementsByTagName('img');					
		for (var i in _user) {
			if (_user[i].complete == false) {
				return false;break;
			}
		}
		return true;
	}
	this.buildUser = function(user) {
    	var _usrimg = this.createElement(
    		'img',	{
    				"attributes":{"src":user.image}
    				}
    	);
    	_usrimg.verticalAlign = "bottom";
		_usrimg.onerror = function() {
			this.src = "http://www.jux.de/images/unknownuser_90.jpg";
		};
		var _imgdiv = this.createElement(
    		'div',	{    				
    				"append" : [_usrimg]
    				}
    	);		
		var _user = this.createElement(
    			'li', {
    					"attributes"	:{"class":"rotatorli"},
    					"append"	:[_imgdiv,this.createElement('span',{"innerHTML":user.name})]
    					}
    		);
    		_user.link = user.link;
    		_user.style.width = this.liWidth + "px";   
    		_user.style.zIndex = 0;   
    		  		
    		_user.onclick = function() {
    			window.rotator.stop();
    			window.location.href = this.link;
    		};
    		_user.onmouseover = function() {
    			this.style.cursor = "pointer";
    			this.style.backgroundColor  = "#C1DEFB";//"#7FC3F0";
    			window.rotator.stop();
    			window.clearTimeout(myPause);
    		};
    		_user.onmouseout = function() {
    			this.style.cursor = "";
    			this.style.backgroundColor  = "";
    		};
    		this.myLeft[this.$$$('rotatorli').length] = 0;    		
    		this.$('rotatorul').appendChild(_user); 
    }
	this.createElement  = function () {
	
		if (!arguments[0]) { alert('function createElem::no type was set'); }		
		
		var newElement = document.createElement(arguments[0]);
	
		if (arguments[1]) {
			if (arguments[1].attributes) {
				for (var i in arguments[1].attributes) {
					newElement.setAttribute(i,arguments[1].attributes[i]);
					if (i == 'class')	{ newElement.className	= arguments[1].attributes[i]; }
					if (i == 'id') 		{ newElement.id 		= arguments[1].attributes[i]; }
				}
			}
			if (arguments[1].innerHTML) {
				newElement.innerHTML = arguments[1].innerHTML;
			}
			if (arguments[1].append) {
				for (var i in arguments[1].append ) {
					newElement.appendChild(arguments[1].append[i]);
				}
			}
		}
		return newElement;
	};
	this.$ =  function() {
		var elements = [];
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string') {
				element = document.getElementById(element);
			}
			if (arguments.length == 1) {
				return element;
			}
			elements.push(element);
		}
		return elements;
	};
	this.$$$ = function(class_name) {
		var all_obj, ret_obj = new Array(),j=0,teststr;
		if(document.all) {
			all_obj=document.all; 
		} else {
			if(document.getElementsByTagName && !document.all) {
	    		all_obj=document.getElementsByTagName("*");
			}
		}
		for(i=0; i < all_obj.length; i++) {
			if(all_obj[i].className.indexOf(class_name)!=-1) {
				teststr=","+all_obj[i].className.split(" ").join(",")+",";
				if(teststr.indexOf(","+class_name+",")!=-1) {
					ret_obj[j]=all_obj[i];
					j++;
				}
			}
		}
		return ret_obj;
	};
};