function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {oldonload();}
      func();
    }
  }
}




var ids=[110,49,51,50];
var realSize = ids.length - 1;
var firstId = ids[0];

// var timerID = null
// var timerRunning = false

function loadArrows() {
				
	$("left_btn").rel = ids.last();
	$("right_btn").rel = ids[1];	
			
}

// function StopTheClock() {
//     if(timerRunning) {
//         clearTimeout(timerID);
//         timerRunning = false;
//     }
// }

function prepCrossFader() {
	
	// Make sure the browser understands the DOM methods
	if (!document.getElementById) return false;
	
	$$('#left_btn').each ( function(element) {

				// set a click event on the links
				Event.observe(element, 'click', function(event) {
					Event.stop(event);
					
					// StopTheClock();
					
					currentId = element.rel;
					currentIndex = ids.indexOf(currentId);					
					
					if(currentIndex == realSize) {
						var prevId = ids[realSize - 1];
						var nextId = ids[1];
					}else if(currentIndex == 0) {
						var prevId = ids.last();
						var nextId = ids.first();
					}else{
						var prevId =  ids[currentIndex - 1];
					 	var nextId =  ids[currentIndex + 1];
					}
					
					
					// alert("Index: "+currentIndex+" Previous: "+prevId+" Next: "+nextId);
					
					
					
					$("left_btn").rel = prevId;
					$('right_btn').rel = nextId;
					
					new Ajax.Updater('testimonial', '/embeds/getTestimonial/' +currentId+ '', {
					  		method: 'get',
							onLoading:function() {$('progress').show(), $('testimonial').hide(), $('paginate').hide();},
							onComplete:function(){$('progress').hide(), $('testimonial').show(), $('paginate').show();}
					});
					
					// alert(prevId)

	            }); // event observe
			}
		);
		
		
		
		$$('#right_btn').each ( function(element) {

					// set a click event on the links
					Event.observe(element, 'click', function(event) {
						Event.stop(event);

						// StopTheClock();
						
						currentId = element.rel;
						currentIndex = ids.indexOf(currentId);
						
						// alert(currentIndex)

						if(currentIndex == 0) {
						 var prevId = ids.last();
						 var nextId = ids[currentIndex + 1];
						}else if(currentIndex == realSize) {
						 var nextId = ids.first();
						 var prevId = ids[currentIndex - 1];
						}else{
						 var nextId =  ids[currentIndex + 1];
						 var prevId =  ids[currentIndex - 1];
						}
						
						// alert("Index: "+currentIndex+" Previous: "+prevId+" Next: "+nextId);
						
						$('right_btn').rel = nextId;
						$("left_btn").rel = prevId;
							

						new Ajax.Updater('testimonial', '/embeds/getTestimonial/' +currentId+ '', {
						  		method: 'get',
								onLoading:function() {$('progress').show(), $('testimonial').hide(), $('paginate').hide();},
								onComplete:function(){$('progress').hide(), $('testimonial').show(), $('paginate').show();}
						});


		            }); // event observe
				}
			);
	
}

function ajaxInitialize() {
	new Ajax.Updater('testimonial', '/embeds/getTestimonial/' +firstId+ '', {
	  		method: 'get',
			onLoading:function() {$('progress').show(), $('testimonial').hide(), $('paginate').hide();},
			onComplete:function(){$('progress').hide(), $('testimonial').show(), $('paginate').show();}
	});
}
// function timedGrab() {
// 	
// 		currentId = $('right_btn').rel;
// 		currentIndex = ids.indexOf(currentId);
// 			
//      	if(currentIndex == 0) {
// 		 var prevId = ids.last();
// 		 var nextId = ids[currentIndex + 1];
// 		}else if(currentIndex == realSize) {
// 		 var nextId = ids.first();
// 		 var prevId = ids[currentIndex - 1];
// 		}else{
// 		 var nextId =  ids[currentIndex + 1];
// 		 var prevId =  ids[currentIndex - 1];
// 		}
// 		
// 		$('right_btn').rel = nextId;
// 		$("left_btn").rel = prevId;
// 		
// 		new Ajax.Updater('testimonial', '/embeds/getTestimonial/' +currentId+ '', {
// 		  		method: 'get',
// 				onLoading:function() {$('progress').show(), $('testimonial').hide(), $('paginate').hide();},
// 				onComplete:function(){$('progress').hide(), $('testimonial').show(), $('paginate').show();}
// 		});
// 		
// 		timerRunning = true
// 		timerID = self.setTimeout("timedGrab()", 10000);
// 			
// }

addLoadEvent(ajaxInitialize);
addLoadEvent(loadArrows);
addLoadEvent(prepCrossFader);
// addLoadEvent(timedGrab);