



var boolDragIsActive = false;
var buttonDD = null;
var targetUrl = '/registration.html?linkId='+_agency+'|GUEST_Homepage|SingleRoller|PIC|';
var _imagePath = "";
var _imgSpace = "http://i.friendscout24.de/datingr5/img/rps/8/4/4/0.gif";
var _imgButton = "http://i.friendscout24.de/datingr5/img/rps/8/4/3/9.gif";
var _imgSlotBg = "http://i.friendscout24.de/datingr5/img/rps/8/4/3/8.gif";


var objArray = function(){}
objArray.prototype = {
	len : function(arr){
		var result = 0;
		if(typeof(arr) != "undefined"){ if(typeof(arr) == "object"){ for(var e in arr){ result++; } } }
		return result;
	},
	hasKey : function(key,arr){
		var result=false;
		if(this.len(arr)>0){for(var e in arr){if(e==key){result=true;break;}}}
		return result;
	},
	hasValue : function(value,arr){
		var result=false;
		if(this.len(arr)>0){for(var e in arr){if(arr[e]==value){result=true;break;}}}
		return result;
	}
}
var _arr = new objArray();

function Slotmashine(_id, _dataObj, _speed, _maxspeed, _timeoutMsecs){
	this.globals={'absMinStepwidth':_speed, 'slotcount':3, 'slotheight':54, 'slotmargin': 10, 'timeoutMsecs':_timeoutMsecs, 'stepwidth':-1*_speed, 'nextStepwidth':-1*_speed, 'maxStepwidth':_maxspeed, 'containerPos':{},'boxHeight':184,'boxWidth':457, 'askedStepwidthCounter':0, 'firstRowStopped':-1}
	this.objId = _id;
	this.chunkData = _dataObj;
	this.newChunk = {'succeed':null,'preceding':null};
	this.newChunkQuest = {'succeed':null,'preceding':null};
	this.slotData = new Array();
	this.slotDataFilter = {};
	this.slotDataFilterControl = {'userId':true};
	this.slotControl = {'actualId':this.globals.slotcount,'firstId':0,'lastId':this.globals.slotcount};
	this.requestControl = {'succeed':false,'preceding':false};
	this.rows=[];
	this.boolSetRowPos = false;
	var textCutTestContainerId = "textwidthTestContainer";
	anker=this;
	
	this.returnCuttedText = function(_txt, _targetContainerId, _cloneCss){
		if(_txt != ""){
			if(_cloneCss == true){
				var sc = document.getElementById(_targetContainerId);
				var tc = document.getElementById(textCutTestContainerId);
				tc.style.fontFamily = ""+getStyle(sc, 'fontFamily');
				tc.style.fontSize = getStyle(sc, 'fontSize');
				tc.style.fontWeight = getStyle(sc, 'fontWeight');
				tc.style.paddingLeft = getStyle(sc, 'paddingLeft');
				tc.style.paddingRight = getStyle(sc, 'paddingRight');
				tc.style.marginLeft = getStyle(sc, 'marginLeft');
				tc.style.marginRight = getStyle(sc, 'marginRight');
			}
			document.getElementById(textCutTestContainerId).innerHTML=_txt;
			if(parseInt(getStyle(_targetContainerId, 'width')) >= getDimensions(textCutTestContainerId).width){
				if(_txt.charAt(_txt.length-1)==",")_txt=_txt.substring(0,_txt.length-1);
				return _txt;
			}else if(getDimensions(_targetContainerId).width < getDimensions(textCutTestContainerId).width){
				return this.returnCuttedText(_txt.substring(0,_txt.lastIndexOf(" "))+"...", _targetContainerId, false);
			}
		}
		return "";
	}
	
	this.setConstruct = function(){
		if(_arr.len(this.chunkData) > 0){
			this.setSlotData();
			if(_arr.len(this.slotData) > 0){
				this.setAnkerEvents();
				this.setDefaultSlotControl();
				this.setDefaultSlot();
				this.setSlotTimeout();
			}
		}
	}
	this.setAnkerEvents = function(){
		document.getElementById(_id).anker=anker;
		document.getElementById(_id).onmouseover=function(e){if(!boolDragIsActive)my_JumpDragToStart();}
		document.getElementById(_id).onmouseout=function(e){
			if(!boolDragIsActive){
				if(typeof(e)=="undefined"){ e = window.event;}
				if( typeof(e)!="undefined"){
					var cPos =  startseitenrolle.getPosition(document.getElementById(startseitenrolle.objId));
					if(e.clientY <= cPos.y+9 || e.clientY >=  cPos.y+startseitenrolle.globals.boxHeight-9){
						startseitenrolle.globals.nextStepwidth = -1*startseitenrolle.globals.absMinStepwidth;
					}else if(e.clientX <= cPos.x+1 || e.clientX >=  cPos.x+startseitenrolle.globals.boxWidth-1){
						startseitenrolle.globals.nextStepwidth = -1*startseitenrolle.globals.absMinStepwidth;
					}
				}
			}
		}
	}
	this.setDefaultSlotControl = function(){
		var startId = Math.round(_arr.len(this.slotData)/2);
		this.slotControl.actualId = startId+this.globals.slotcount;
		this.slotControl.firstId = startId;
		this.slotControl.lastId = startId+this.globals.slotcount;
	}
	this.setDefaultSlot = function(){
		for(var i=0;i<=this.globals.slotcount;i++){this.rows["rolle_"+i]=new row(_id, i, this.slotData[(this.slotControl.firstId+i)]);}
		for(var i=0;i<=this.globals.slotcount;i++){
			this.rows["rolle_"+i].topSibling=this.rows["rolle_"+(i==0?this.globals.slotcount:i-1)];
			this.rows["rolle_"+i].bottomSibling=this.rows["rolle_"+(i==this.globals.slotcount?0:i+1)];
		}
	}
	this.setSlotTimeout=function(){
		for(var i=0;i<=this.globals.slotcount;i++){
			if(!this.globals.stopMovement && ((this.globals.firstRowStopped!=-1 && this.globals.firstRowStopped==this.rows["rolle_"+i].info.pos) || this.globals.firstRowStopped==-1)){
				this.globals.firstRowStopped=-1;
				var newYPos=(parseInt(this.rows["rolle_"+i].r.style.top)+this.getStepWidth(this.rows["rolle_"+i].info.pos));
				if(!this.reachedEndPos(newYPos)){
					this.rows["rolle_"+i].r.style.top=newYPos+"px";
				}else{
					anker.setActualId();
					this.rows["rolle_"+i].setNextContent();
					this.rows["rolle_"+i].setAtStartpos();
				}
			}else{
				if(this.globals.firstRowStopped==-1)this.globals.firstRowStopped=this.rows["rolle_"+i].info.pos;
			}
		}
		self.setTimeout('anker.setSlotTimeout()', startseitenrolle.globals.timeoutMsecs);
	}
	this.setActualId = function(){
		var richtung = this.getDirection();
		var dataLen=_arr.len(this.slotData);
		var chunkId = (this.globals.nextStepwidth < 0) ? this.chunkData[2].succeedingChunkId : this.chunkData[0].precedingchunkId;
		if(this.globals.nextStepwidth < 0 && this.slotControl.lastId+1 == dataLen || this.globals.nextStepwidth > 0 && this.slotControl.firstId-1 == -1){
			if(this.newChunk[richtung] != null){
				this.setRefreshChunk(this.newChunk[richtung]);
				this.setSlotData();
				if(this.globals.nextStepwidth < 0){
					this.slotControl.firstId = (_arr.len(this.chunkData[0].singleRollerBeans)+_arr.len(this.chunkData[1].singleRollerBeans)-1)-this.globals.slotcount;
				}else{
					this.slotControl.firstId = _arr.len(this.chunkData[0].singleRollerBeans);
				}
				this.newChunk[richtung] = null;
		
			}else{
				if( ! this.requestControl[richtung]){ 
					if(this.newChunkQuest[richtung]==null){
						this.setChunkRequest(chunkId,richtung);
					}else{
						this.setChunkRequest(this.newChunkQuest[richtung],richtung);
					} 
				}
			}
		}else if(this.globals.nextStepwidth < 0 && this.slotControl.lastId == dataLen-4 || this.globals.nextStepwidth > 0 && this.slotControl.firstId == 2){
			if( ! this.requestControl[richtung] && this.newChunk[richtung] == null){ 
				if(this.newChunkQuest[richtung]==null){
					this.setChunkRequest(chunkId,richtung);
				}else{
					this.setChunkRequest(this.newChunkQuest[richtung],richtung);
				} 
			}
		}
		this.setSlotControl();
	}

	this.setRefreshChunk = function(arr){
		if(this.globals.nextStepwidth < 0){for(var i=1;i<this.chunkData.length;i++){this.chunkData[i-1]=this.chunkData[i];}this.chunkData[(_arr.len(this.chunkData)-1)]=arr;}
		if(this.globals.nextStepwidth > 0){for(var i=this.chunkData.length-1;i>=0;i--){this.chunkData[i]=this.chunkData[i-1];}this.chunkData[0]=arr;}
	}
	this.setSlotControl = function(){
		var dataLen=_arr.len(this.slotData);
		if(this.globals.nextStepwidth < 0){
			this.slotControl.firstId = (this.slotControl.firstId+1 < dataLen) ? this.slotControl.firstId + 1 : 0;
		}else{
			this.slotControl.firstId = (this.slotControl.firstId-1 >= 0) ? this.slotControl.firstId - 1 : dataLen-1;
		}
		this.slotControl.lastId = (this.slotControl.firstId+this.globals.slotcount < dataLen) ? this.slotControl.firstId+this.globals.slotcount : (this.slotControl.firstId+this.globals.slotcount)-(dataLen);
		this.slotControl.actualId = (this.globals.nextStepwidth > 0) ? this.slotControl.firstId : this.slotControl.lastId;
	}
	this.setChunkRequest=function(dataId,richtung){
		var x=this;
		this.requestControl[richtung] = true;
	 	singleroller.cb_getChunk(function(arr){if(_arr.len(arr) > 0){ x.setNewChunk(arr[0],richtung);}}, ""+dataId);
	}
	this.setSlotData = function(){
		if(_arr.len(this.chunkData) > 0){
			this.slotData = new Array();
			this.setRefreshDataFilter();
			for(var e in this.chunkData){
				if(_arr.len(this.chunkData[e].singleRollerBeans) > 0){
					for(var x in this.chunkData[e].singleRollerBeans){ 
						if(this.getFilterAccess(this.chunkData[e].singleRollerBeans[x])){
							this.slotData[this.slotData.length] = this.chunkData[e].singleRollerBeans[x];
						}
					}
				}
			}
		}
	}
	this.setNewChunk = function(arr,richtung){ 
		var isOk = false;
		if(_arr.len(arr) > 0){
			if( _arr.len(arr.singleRollerBeans) > 0 ){
				this.newChunk[richtung] = arr;
				this.requestControl[richtung] = false;
				this.newChunkQuest[richtung] = null;
				isOk = true;
			}
		}
		if( ! isOk){
			this.newChunk[richtung] = null;
			this.requestControl[richtung] = false;
			this.newChunkQuest[richtung] = (richtung=='succeed') ? arr.succeedingChunkId:arr.precedingchunkId; 
			/*if(typeof(arr.succeedingChunkId) !="undefined" && typeof(arr.precedingchunkId) !="undefined"){
				this.nextChunkQuestId = (richtung=='succeed')?arr.succeedingChunkId:arr.precedingchunkId; 
				this.setChunkRequest(nextId,richtung);
			}else{
				this.requestControl[richtung] = false;
			}*/
		}
	}
	this.setRefreshDataFilter = function(){if(_arr.len(this.slotDataFilter)>0){for(var e in this.slotDataFilter){this.slotDataFilter[e]=[];}}}
	this.reachedEndPos=function(_pos){
		var endPos = (anker.globals.stepwidth > 0) ? ((anker.globals.slotheight+anker.globals.slotmargin)*anker.globals.slotcount) : 0-(anker.globals.slotheight+anker.globals.slotmargin);
		return (anker.globals.stepwidth > 0 && _pos >= endPos || anker.globals.stepwidth < 0 && _pos <= endPos) ?  true : false;
	}
	this.setOpacity=function(objId,filterVal){
		if(typeof(document.getElementById(objId))!="undefined"){
			var _o =document.getElementById(objId).style;
		    _o.opacity = (filterVal / 10);
		    _o.MozOpacity = (filterVal / 10);
		    _o.KhtmlOpacity = (filterVal / 10);
		    _o.filter = "alpha(opacity=" + (filterVal*10) + ")";
		}
	}
	this.setSlideDirection = function(cY){
		this.globals.conatinerPos = this.getPosition(document.getElementById(_id));
		this.globals.conatinerPos.y+=document.getElementById('content').scrollTop;
		this.globals.conatinerPos.x+=document.getElementById('content').scrollLeft;
		if(typeof(cY)!="undefined" && typeof(this.globals.conatinerPos.y)!="undefined"){
			if(cY > this.globals.conatinerPos.y){ this.globals.nextStepwidth = this.getStepValue(cY-this.globals.conatinerPos.y); }
		}
	}
	this.setRowPos = function(rowId,direction){
		if(startseitenrolle.boolSetRowPos){
			var rId = rowId;
			var dirct = direction;
			var pos = parseInt(document.getElementById(rId).style.top);
			if(dirct == "down" && pos >= 0 || dirct == "up" && pos <= (startseitenrolle.globals.boxHeight-startseitenrolle.globals.slotheight)){
				startseitenrolle.globals.nextStepwidth = 0;
				startseitenrolle.boolSetRowPos = false;
			}else{
				self.setTimeout('startseitenrolle.setRowPos("'+rId+'","'+dirct+'")',50);
			}
		}
	}
	this.getFilterAccess = function(arr){
		var result = true;
		if(result){
			for(var e in this.slotDataFilterControl){
				if(this.slotDataFilterControl[e] == true && _arr.hasKey(e,arr)){
					if(typeof(this.slotDataFilter[e])=="undefined"){this.slotDataFilter[e]=[];}
					if( ! _arr.hasValue(arr[e],this.slotDataFilter[e])){
						this.slotDataFilter[e][this.slotDataFilter[e].length]=arr[e];
					}else{
						result=false;break;
					}
				}
			}
		}
		return result;
	}
	this.getDirection = function(){
		return (this.globals.nextStepwidth < 0) ? 'succeed' : 'preceding';
	}
	this.getStepValue = function(val){
		var ret = val==this.globals.boxHeight/2?0:((val>this.globals.boxHeight/2?Math.floor(-1*(val-(this.globals.boxHeight/2))/((this.globals.boxHeight)/2)*(this.globals.maxStepwidth-this.globals.absMinStepwidth)):Math.ceil((((this.globals.boxHeight)/2)-val)/((this.globals.boxHeight)/2)*(this.globals.maxStepwidth-this.globals.absMinStepwidth))));
		if(ret>0)ret+=this.globals.absMinStepwidth;
		else if(ret<0)ret-=this.globals.absMinStepwidth;
		return ret;
	}
	this.getStepWidth=function(_id){
		var spacer="";
		var result=anker.globals.stepwidth;
		anker.globals.askedStepwidthCounter++;
		if(anker.globals.askedStepwidthCounter > anker.globals.slotcount){
			anker.globals.stepwidth=anker.globals.nextStepwidth;
			anker.globals.askedStepwidthCounter=0;
		}
		return result;
	}
	this.getPosition = function (elem){
		var x=0; var y=0;
		try{
			while(typeof(elem)=="object" && typeof(elem.tagName)!="undefined"){
				tagname=elem.tagName.toUpperCase();
				if(tagname=="BODY" || tagname=="HTML")break;
				y+=elem.offsetTop;
				x+=elem.offsetLeft;
				if(typeof elem=="object")if(typeof elem.offsetParent=="object")elem=elem.offsetParent;
			}
		}catch(e){}
			y-=document.getElementById('content').scrollTop;
			x-=document.getElementById('content').scrollLeft;
			return {'x':x, 'y':y}
	}
	this.getContentObject=function(){ return (typeof(this.slotData[this.slotControl.actualId])!="undefined") ? this.slotData[this.slotControl.actualId] : "";}
	var row=function(_parentId, _pos, _data){
		this.info={'id':'row'+_pos, 'pos':_pos, 'userId':_data.userId};
		this.r;
		this.setNextContent=function(){try{this.changeContent(anker.getContentObject());}catch(e){/*error*/}}
		this.setAtStartpos=function(){this.r.style.top= (anker.globals.stepwidth > 0) ? (parseInt(this.bottomSibling.r.style.top)-anker.globals.slotmargin-anker.globals.slotheight+(_pos!=anker.globals.slotcount?anker.globals.stepwidth:0))+"px" : (parseInt(this.topSibling.r.style.top)+anker.globals.slotheight+anker.globals.slotmargin+(_pos==0?anker.globals.stepwidth:0))+"px";}
		this.dataIndex = _data.index;
		
		this.changeContent=function(_data){
			if(typeof(_data) == "object"){
				this.info.userId=_data.userId;
				document.getElementById("rphoto_"+_pos).src=_data.photoUrl;
				document.getElementById("rtxt1_"+_pos).innerHTML=anker.returnCuttedText(_data.firstLine, "rtxt1_"+this.info.pos, true);
				document.getElementById("rtxt2_"+_pos).innerHTML=anker.returnCuttedText(_data.secondLine, "rtxt2_"+this.info.pos, true);
			}
		}
		this.insertElementNodes=function(){
			this.r=document.createElement("div");
			this.r.style.width="457px";
			this.r.id="rolle_"+_pos;
			this.r.className="rolle";
			this.r.style.position="absolute";
			this.r.style.top=(_pos*(anker.globals.slotheight+anker.globals.slotmargin))+"px";
			
			var rc=document.createElement("div");
			rc.className="rollencontent";
			
			var bg=new Image(457,54);
			bg.src=_imagePath+""+_imgSlotBg;
			
			var photo=new Image();
			photo.src=_data.photoUrl;
			photo.className="rollenphoto";
			photo.id="rphoto_"+_pos;
			
			var linkImage=new Image(29,29);
			linkImage.src=_imagePath+""+_imgButton;
			linkImage.border="0";
			
			var bgSlot=document.createElement("div");
			bgSlot.className="container";
			bgSlot.id="bgSlot_"+_pos;
			bgSlot.appendChild(bg);
			
			var rct1=document.createElement("div");
			rct1.className="rollentext1";
			rct1.id="rtxt1_"+_pos;
			rct1.innerHTML=_data.firstLine;
			
			var rct2=document.createElement("div");
			rct2.className="rollentext2";
			rct2.id="rtxt2_"+_pos;
			rct2.innerHTML=_data.secondLine;
			
			var linkwrapper=document.createElement("div");
			linkwrapper.className="linkimg";
			linkwrapper.id = "linkwrapper_"+_pos;
			linkwrapper.style.visibility = "hidden";
			
			linkwrapper.appendChild(linkImage);
			
			var spacerImage=new Image(457,54);
			spacerImage.src=_imagePath+""+_imgSpace;
			spacerImage.border="0";
			
			var eventPad = document.createElement("div");
			eventPad.className = "container";
			eventPad.id = "eventPad_"+_pos;
			eventPad.topId = this.r.id;
			eventPad.buttonId = linkwrapper.id;
			eventPad.bgSlotId = bgSlot.id;
			eventPad.appendChild(spacerImage);
			eventPad.onmouseover=function(){
				this.style.cursor="pointer";
				document.getElementById(this.buttonId).style.visibility = "visible";
				if(!boolDragIsActive){
					var pos = parseInt(document.getElementById(this.topId).style.top);
					startseitenrolle.boolSetRowPos = true;
					if(pos < 0 || pos > (startseitenrolle.globals.boxHeight-startseitenrolle.globals.slotheight)){
						var direction = (pos < 0) ? "down" : "up";
						startseitenrolle.globals.nextStepwidth = (pos < 0) ? 1 : -1;
						startseitenrolle.setRowPos(this.topId,direction);
					}else{
						startseitenrolle.globals.nextStepwidth = 0;
					}
				}
				startseitenrolle.setOpacity(this.bgSlotId,10);
			}
			eventPad.onmouseout=function(){
				if(!boolDragIsActive){
					startseitenrolle.globals.nextStepwidth = 0;
					startseitenrolle.boolSetRowPos = false;
				}
				document.getElementById(this.buttonId).style.visibility = "hidden";
				startseitenrolle.setOpacity(this.bgSlotId,5);
			} 
			eventPad.onclick=function(){
				if(typeof(startseitenrolle.rows[this.topId]) != "undefined"){
					document.location.href = targetUrl+"&pId="+startseitenrolle.rows[this.topId].info.userId;
				}else{
					document.location.href = targetUrl;
				}
			}
			
			rc.appendChild(bgSlot);
			rc.appendChild(photo);
			rc.appendChild(rct1);
			rc.appendChild(rct2);
			rc.appendChild(linkwrapper);
			rc.appendChild(eventPad);
			
			this.r.appendChild(rc);
			document.getElementById(_parentId).appendChild(this.r);
			anker.setOpacity(bgSlot.id,5);
			document.getElementById("rtxt1_"+_pos).innerHTML=anker.returnCuttedText(_data.firstLine, "rtxt1_"+_pos, true);
			document.getElementById("rtxt2_"+_pos).innerHTML=anker.returnCuttedText(_data.secondLine, "rtxt2_"+_pos, true);
			anker.cloneCss=false;
		}
		this.insertElementNodes();
	}
}