/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function Partnerslide() {

	var self = this;
	var canvasId = null;
	var canvasDiv = null;

	var div = null;
	var div2 = null;
	var div3 = null;

	var contentArray = null;
	var index = 0;
	var currentIndex = 0;
	var iterRichtung = true;
	var firstLoad = true;

	var interval = null;
	var inMove = false;

	var pos0 = null;
	var pos1 = null;
	var pos2 = null;
	var pos3 = null;

	this.setCanvasId = function(id) {
		canvasId = id;
	}

	this.setContent = function(arr) {
		contentArray = arr;
	}

	this.init = function() {


		div = document.createElement("div");
		var id = document.createAttribute("id");
		id.nodeValue = "item1_"+canvasId;
		div.setAttributeNode(id);
		div.className = "testim_item";

		div2 = document.createElement("div");
		var id2 = document.createAttribute("id");
		id2.nodeValue = "item2_"+canvasId;
		div2.setAttributeNode(id2);
		div2.className = "testim_item";
		
		div3 = document.createElement("div");
		var id3 = document.createAttribute("id");
		id3.nodeValue = "item3_"+canvasId;
		div3.setAttributeNode(id3);
		div3.className = "testim_item";


		canvasDiv = $('partnerslide_'+canvasId);
		canvasDiv.appendChild(div);
		canvasDiv.appendChild(div2);
		canvasDiv.appendChild(div3);
		div = $("item1_"+canvasId);
		div2 = $("item2_"+canvasId);
		div3 = $("item3_"+canvasId);

		div.style.width = Math.round(canvasDiv.offsetWidth/2) +"px";
		div.style.left = (canvasDiv.offsetWidth*-1)+"px";

		div2.style.width = Math.round(canvasDiv.offsetWidth/2) +"px";
		div2.style.left = (canvasDiv.offsetWidth*-1)+"px";

		div3.style.width = Math.round(canvasDiv.offsetWidth/2) +"px";
		div3.style.left = (canvasDiv.offsetWidth*-1)+"px";

		div.style.display = "block";
		div2.style.display = "block";
		div3.style.display = "block";

		generateContent(div, contentArray[0]);
		generateContent(div2, contentArray[1]);
		
		index = 1;
		currentIndex = 1;

		$('tm_br_'+canvasId).onclick = function() {
			clearInterval(interval);
			goRight();
		}
		$('tm_bl_'+canvasId).onclick = function() {
			clearInterval(interval);
			goLeft();
		}


		$('tm_br_'+canvasId).onmouseover = function() {
			$('tm_button_r_'+canvasId).className = "tm_button_over";
		}
		$('tm_br_'+canvasId).onmouseout = function() {
			$('tm_button_r_'+canvasId).className = "tm_button";
		}


		$('tm_bl_'+canvasId).onmouseover = function() {
			$('tm_button_l_'+canvasId).className = "tm_button_over";
		}
		$('tm_bl_'+canvasId).onmouseout = function() {
			$('tm_button_l_'+canvasId).className = "tm_button";
		}

		action();
		
		/*
		interval = setInterval(function() {
			goRight();
		}, 6000);
		*/

	}

	function action() {
		if(inMove) return;
		inMove = true;


		if(iterRichtung) {
				if(firstLoad) {
					new Effect.Morph(div3, {
						style: 'left:'+Math.round(canvasDiv.offsetWidth/2*-1)+'px',
						duration:0.7
					});
					pos0 = div3;
					pos3 = pos0;
				
					new Effect.Morph(div, {
						style: 'left:40px',
						duration:0.7
					});
					pos1 = div;
					
					new Effect.Morph(div2, {
						style: 'left:'+Math.round(canvasDiv.offsetWidth/2)+'px',
						duration:0.7
					});
					pos2 = div2;
									
					
				}else {				

					//alert(currentIndex+"\nm:"+currentIndex%3);

					generateContent(pos0, contentArray[index]);
					pos0.style.left = Math.round(canvasDiv.offsetWidth/2*-1)+"px";
					new Effect.Parallel([									
						new Effect.Morph(pos0, {
							style: 'left:40px',
							duration:0.7
						}),
						
						new Effect.Morph(pos1, {
							style: 'left:'+Math.round(canvasDiv.offsetWidth/2)+'px',
							duration:0.7
						}),
				
						new Effect.Morph(pos2, {
							style: 'left:'+canvasDiv.offsetWidth+'px',
							duration:0.7
						})
						],
				
						{
							duration: 0.7
						} );
						
						pos3 = pos2;						
						
						pos2 = pos1;
						pos1 = pos0;							
						
						pos0 = pos3;					
					
					currentIndex++;					
				}
		}else {

				if(firstLoad) {
					new Effect.Morph(div, {
						style: 'left:40px',
						duration:0.7
					});
					pos1 = div;
					
					new Effect.Morph(div2, {
						style: 'left:'+Math.round(canvasDiv.offsetWidth/2)+'px',
						duration:0.7
					});
					pos2 = div2;
					
					pos3 = div3;
					pos0 = pos3;
					
				}else {

					//alert(currentIndex+"\nm:"+currentIndex%3);

					generateContent(pos3, contentArray[index]);
					pos3.style.left = canvasDiv.offsetWidth+"px";
					new Effect.Parallel([

						new Effect.Morph(pos1, {
							style: 'left:'+Math.round(canvasDiv.offsetWidth/2*-1)+'px',
							duration:0.7
						}),
						
						new Effect.Morph(pos2, {
							style: 'left:40px',
							duration:0.7
						}),
						
						new Effect.Morph(pos3, {
							style: 'left:'+Math.round(canvasDiv.offsetWidth/2)+'px',
							duration:0.7
						})	
						],

						{
							duration: 0.7
						} );
						
						pos0 = pos1;
						
						pos1 = pos2;
						pos2 = pos3;
						
						pos3 = pos0;
										
					currentIndex++;

				}
				
				
		}


		setTimeout(function(){
			inMove=false;
		},800);


		firstLoad = false;
        
	}

	function generateContent(obj, arr) {
		var cont = "";

		cont = '<div class="tm_content">'+
		//'   <div style="padding-bottom:20px;">'+arr[0]+'</div>'+
		'   <div>'+
		'		<div class="psItem" style="left:15px;">'+
		'			<img src="'+arr[1]+'" title="'+arr[0]+'"/>'+
		'			<div style="text-align:center; padding-top:5px;"><a href="'+arr[2]+'">'+arr[3]+'</a></div>'+
		'		</div>'+
		'</div></div>';

		obj.innerHTML = cont;

	}


	function goRight() {
		if(iterRichtung == false) index++;
	
		iterRichtung = true;
        

		index++;
		if(index >= contentArray.length)
			index=0;

		action();
	}

	function goLeft() {
		if(iterRichtung == true) index--;
	
		iterRichtung = false;
       
		index--;
		if(index < 0)
			index = contentArray.length-1;

		action();
	}

}


