/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function Testimonial() {

	var self = this;
	var canvasId = null;
	var canvasDiv = null;

	var div = null;
	var div2 = null;

	var contentArray = null;
	var index = 0;
	var currentIndex = 0;
	var iterRichtung = true;
	var firstLoad = true;

	var interval = null;
	var inMove = false;

	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";

		canvasDiv = $('testtimonial_'+canvasId);
		canvasDiv.appendChild(div);
		canvasDiv.appendChild(div2);
		div = $("item1_"+canvasId);
		div2 = $("item2_"+canvasId);

		div.style.width = canvasDiv.offsetWidth+"px";
		div.style.left = (canvasDiv.offsetWidth*-1)+"px";

		div2.style.width = canvasDiv.offsetWidth+"px";
		div2.style.left = (canvasDiv.offsetWidth*-1)+"px";

		div.style.display = "block";
		div2.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(currentIndex%2 == 0) {
				if(firstLoad) {
					new Effect.Morph(div, {
						style: 'left:0px',
						duration:0.7
					});
				}else {
					generateContent(div, contentArray[index]);
					div.style.left = (canvasDiv.offsetWidth*-1)+'px';
					new Effect.Parallel([
						new Effect.Morph(div, {
							style: 'left:0px',
							duration:0.7
						}),
						new Effect.Morph(div2, {
							style: 'left:'+canvasDiv.offsetWidth+'px',
							duration:0.7
						})
						],

						{
							duration: 0.7
						} );
				}

			}else {
				if(firstLoad) {
					new Effect.Morph(div2, {
						style: 'left:0px',
						duration:0.7
					});
				}else {
					generateContent(div2, contentArray[index]);
					div2.style.left = (canvasDiv.offsetWidth*-1)+'px';
					new Effect.Parallel([
						new Effect.Morph(div2, {
							style: 'left:0px',
							duration:0.7
						}),
						new Effect.Morph(div, {
							style: 'left:'+canvasDiv.offsetWidth+'px',
							duration:0.7
						})
						],

						{
							duration: 0.7
						} );
				}
			}
		}else {
			if(currentIndex%2 == 0) {
				if(firstLoad) {
					new Effect.Morph(div, {
						style: 'left:0px',
						duration:0.7
					});
				}else {
					generateContent(div, contentArray[index]);
					div.style.left = (canvasDiv.offsetWidth)+'px';
					new Effect.Parallel([
						new Effect.Morph(div2, {
							style: 'left:'+(canvasDiv.offsetWidth*-1)+'px',
							duration:0.7
						}),
						new Effect.Morph(div, {
							style: 'left:0px',
							duration:0.7
						})
						],

						{
							duration: 0.7
						} );
				}

			}else {
				if(firstLoad) {
					new Effect.Morph(div2, {
						style: 'left:0px',
						duration:0.7
					});
				}else {
					generateContent(div2, contentArray[index]);
					div2.style.left = (canvasDiv.offsetWidth)+'px';
					new Effect.Parallel([
						new Effect.Morph(div, {
							style: 'left:'+(canvasDiv.offsetWidth*-1)+'px',
							duration:0.7
						}),
						new Effect.Morph(div2, {
							style: 'left:0px',
							duration:0.7
						})
						],

						{
							duration: 0.7
						} );
				}
			}
		}

		setTimeout(function(){
			inMove=false;
		},800);


		currentIndex++;
		firstLoad = false;
        
	}

	function generateContent(obj, arr) {
		obj.innerHTML = '<div class="tm_content">'+
		'   <div>'+arr[0]+'</div>'+
		'   <h1>'+arr[1]+'</h1>'+
		'   <div>'+
		'       <div style="float:left; width:180px; text-align:center;">'+
		'			<img style="cursor:pointer;" title="'+arr[5]+'" src="'+arr[4]+'" width="180" onclick="'+( (arr[5]!="")? "window.open('"+arr[5]+"', 'partner');":"")+'"/>'+
		'		</div>'+
		'       <div style="float:right; width:'+( canvasDiv.offsetWidth - 270 )+'px" >'+
		'           <div style="font-weight:bold; padding-bottom:6px;">'+arr[2]+'</div>'+
		'           <div>'+arr[3]+'</div>'+
		'			'+( (arr[5]!="")? '<a class="link" href="'+arr[5]+'">'+arr[6]+'</a>':'')+''+
		'       </div>'+
		'   </div>'+
		'</div>';
	}


	function goRight() {
		iterRichtung = true;
        

		index++;
		if(index >= contentArray.length)
			index=0;

		action();
	}

	function goLeft() {
		iterRichtung = false;
       
		index--;
		if(index < 0)
			index = contentArray.length-1;

		action();
	}

}


