$(document).ready(function(){

	$("#leftcontroller").hide();
	$("#rightcontroller").hide();

	var movedintoimage		= false;
	var currentimage			= 0;
	var numberofimages		= 2;
	
	//var currentimagearray = '01';
	//var imagefolder				= '/development/';

 	var inithidearrows = function () {
 		if(movedintoimage == false) {
			$("#rightcontroller").fadeOut(1000);
			$("#leftcontroller").fadeOut(1000);
		}
	}

 	var hidearrows = function () {
		$("#rightcontroller").fadeOut(1000);
		$("#leftcontroller").fadeOut(1000);
	}
	
 	var showarrows = function () {
 		// moved into image, this stops the autofading out of the arrows on page load
 		movedintoimage = true;
 		
		if(currentimage < numberofimages) { $("#rightcontroller").fadeIn(1000); }
		if(currentimage > 0) { $("#leftcontroller").fadeIn(1000); }
	}
		
	$("#serviceimages").hover(showarrows,hidearrows);
	
	var stop = function () {
		$("#rightcontroller").hide();
		$("#rightcontroller").css("opacity","");
		$("#rightcontroller").stop();

		$("#leftcontroller").hide();
		$("#leftcontroller").css("opacity","");
		$("#leftcontroller").stop();
	}
  
  $("#rightcontroller").click(function() {
  	firstslide = (Number(currentimage)+1)*850;
  	
	  $("#slideme").animate({ marginLeft: '-'+firstslide }, 800);
	  
	  // Increment the current image
	  currentimage++;
	  
	  // We went right, so the left one now needs to show
	  $("#leftcontroller").fadeIn(1000);
	  
	  // If this is the last image, remove the right arrow
	  if(currentimage == numberofimages) { $("#rightcontroller").hide(); }
  });
  
  $("#leftcontroller").click(function() {
  	firstslide = (Number(currentimage)-1)*850;
  	
	  $("#slideme").animate({ marginLeft: '-'+firstslide }, 800);
	  
	  // Decrement the current image
	  currentimage--;
	  
	  // We went left, so the right one now needs to show
	  $("#rightcontroller").fadeIn(1000);
	  
	  // If this is the last image, remove the right arrow
	  if(currentimage == 0) { $("#leftcontroller").hide(); }
  });

	$.post("/services/loadimages/",{folder:imagefolder+currentimagearray}, loadImages);
  $("#"+currentimagearray).attr("class","selected");
		
  setTimeout(inithidearrows,3000);
  
  $("#slideshow a").click(function() {
	  $.post("/services/loadimages/",{folder:imagefolder+this.id}, loadImages);

	  $("#"+this.id).attr("class","selected");
		$("#"+currentimagearray).attr("class","");		
		currentimagearray = this.id;
		
//		if(numberofimages > 0) {
//			$("#rightcontroller").fadeIn(1000);
//		}
		
	  return false;
	});
	
	function loadImages(imagestring) {
		imagestring = imagestring.split('::');
		
		currentimage = 0;
		numberofimages = imagestring[0];
		
		$("#slideme").css("margin-left",0);
		$("#slideme").html(imagestring[1]);
		
		if(numberofimages == 0) {
			$("#rightcontroller").hide();
		}
		
		if(numberofimages > 0) {
			$("#rightcontroller").show();
		}
		
		setTimeout(inithidearrows,3000);
	}
	
  $("#workselect a").click(function() {
	  $.post("/services/loadimages/",{folder:'/'+this.id+'/01'}, loadImages);
	  
		$("#"+currentimagearray).attr("class","");
	  $("#01").attr("class","selected");
		currentimagearray = '01';
		
	  $("#workselect a").attr("class","");
		$("#"+this.id).attr("class","current");
		
		imagefolder = '/'+this.id+'/';
		
	  return false;
	});

  
});