// JavaScript Document

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function viewmember(id,a) {
	
	elm = $('#' + id);
	if(elm[0].style.display == 'block') {
		$(a).html('Click here for more information');
		elm.slideUp('slow');
	} else {
		$(a).html('Click here to hide information');
		elm.slideDown('slow');
	}
	return false;
}
//-->

	$(function() {
			
			  //remove js-disabled class
				$("#viewer").removeClass("js-disabled");
			
			  //create new imagecontainer for images
				$("<div>").attr("id", "imagecontainer").css({ position:"absolute"}).width($(".wrapper").length * 170).height(170).appendTo("div#viewer");
			  	
				//add images to imagecontainer
				$(".wrapper").each(function() {
					$(this).appendTo("div#imagecontainer");
				});
				
				//work out duration of anim based on number of images (1 second for each image)
				var duration = $(".wrapper").length * 2000;
				
				//store speed for later (distance / time)
				var speed = (parseInt($("div#imagecontainer").width()) + parseInt($("div#viewer").width())) / duration;
				
				//set direction
				var direction = "rtl";
				
				//set initial position and class based on direction
				(direction == "rtl") ? $("div#imagecontainer").css("left", $("div#viewer").width()).addClass("rtl") : $("div#imagecontainer").css("left", 0 - $("div#imagecontainer").width()).addClass("ltr") ;
				
				//animator function
				var neg = 120;
				var animator = function(el, time, dir) {
				// time = 12000;
					//which direction to scroll
					if(dir == "rtl") {
					  
					  //add direction class
						el.removeClass("ltr").addClass("rtl");
					 		
						//animate the el
						
						//neg += $('#' + el[0].id + ' a:first').width();
						el.css({left : '0px'});
						el.animate({ left : - neg + 'px' },4000, "linear", function() {
										
							$(this).append($('#' + $(this)[0].id + ' a:first'));
							neg = $('#' + el[0].id + ' a:first').width();
							
							if(neg == 0) {
								neg = 120;	
							}
							
							$(this).css( {left : '0px'});
																								
							//	alert($('#' + $(this)[0].id + ' a:last').width());
							//$(this).css({left : $(this).position().left() - 		$('#' + $(this)[0].id + ' a:last').width()});									
							//reset imagecontainer position
						//	$(this).css({ left:$("div#imageScroller").width(), right:"" });
							//$(this).append($('#' + $(this)[0].id + ' a:first'));
														
												  
							//restart animation
							animator($(this), duration, "rtl");
							
							//hide controls if visible
							($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null ;			
											
						});
					} else {
					
					  //add direction class
						el.removeClass("rtl").addClass("ltr");
					
						//animate the el
						el.animate({ left:$("div#viewer").width() + "px" }, time, "linear", function() {
												
							//reset imagecontainer position
							$(this).css({ left:0 - $("div#imagecontainer").width() });
							
							//restart animation
							animator($(this), duration, "ltr");
							
							//hide controls if visible
							($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null ;			
						});
					}
				}
				
				//start anim
				animator($("div#imagecontainer"), duration, direction);
				
				//pause on mouseover
				$("a.wrapper").live("mouseover", function() {
				  	return;
					//stop anim
					$("div#imagecontainer").stop(true);
					
					//show controls
					($("div#controls").length == 0) ? $("<div>").attr("id", "controls").appendTo("div#outerimagecontainer").css({ opacity:0.7 }).slideDown("slow") : null ;
					($("a#rtl").length == 0) ? $("<a>").attr({ id:"rtl", href:"#", title:"rtl" }).appendTo("#controls") : null ;
					($("a#ltr").length == 0) ? $("<a>").attr({ id:"ltr", href:"#", title:"ltr" }).appendTo("#controls") : null ;
					
					//variable to hold trigger element
					var title = $(this).attr("title");
					
					//add p if doesn't exist, update it if it does
					($("p#title").length == 0) ? $("<p>").attr("id", "title").text(title).appendTo("div#controls") : $("p#title").text(title) ;
				});
				
				//restart on mouseout
				$("a.wrapper").live("mouseout", function(e) {
				  	return;
					//hide controls if not hovering on them
					(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $("div#controls").slideUp("slow").remove() : null ;
					
					//work out total travel distance
					var totalDistance = parseInt($("div#imagecontainer").width()) + parseInt($("div#viewer").width());
														
					//work out distance left to travel
					var distanceLeft = ($("div#imagecontainer").hasClass("ltr")) ? totalDistance - (parseInt($("div#imagecontainer").css("left")) + parseInt($("div#imagecontainer").width())) : totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#imagecontainer").css("left")))) ;
					
					//new duration is distance left / speed)
					var newDuration = (distanceLeft + 250) / speed;
					
					//restart anim
					animator($("div#imagecontainer"), newDuration, $("div#imagecontainer").attr("class"));
 
				});
												
				//handler for ltr button
				$("#ltr").live("click", function() {
				 					
					//stop anim
					$("div#imagecontainer").stop(true);
				
					//swap class names
					$("div#imagecontainer").removeClass("rtl").addClass("ltr");
										
					//work out total travel distance
					var totalDistance = parseInt($("div#imagecontainer").width()) + parseInt($("div#viewer").width());
					
					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($("div#imagecontainer").css("left")) + parseInt($("div#imagecontainer").width()));
					
					//new duration is distance left / speed)
					var newDuration = (distanceLeft + 250) / speed;
					
					//restart anim
					animator($("div#imagecontainer"), newDuration, "ltr");
				});
				
				//handler for rtl button
				$("#rtl").live("click", function() {
										
					//stop anim
					$("div#imagecontainer").stop(true);
					
					//swap class names
					$("div#imagecontainer").removeClass("ltr").addClass("rtl");
					
					//work out total travel distance
					var totalDistance = parseInt($("div#imagecontainer").width()) + parseInt($("div#viewer").width());
 
					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#imagecontainer").css("left"))));
					
					//new duration is distance left / speed)
					var newDuration = (distanceLeft + 250) / speed;
				
					//restart anim
					animator($("div#imagecontainer"), newDuration, "rtl");
				});
			});
