$(document).ready(function(){

  
  $(".slideholder").click(function() {
  	alert('clicked');
  	return false;
  });
  
	$("#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); }
	}

	$("#mediaimages").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)*640;

	  $("#slideme").animate({ marginLeft: '-'+firstslide }, 690);

	  // 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)*640;

	  $("#slideme").animate({ marginLeft: '-'+firstslide }, 690);

	  // 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("/mediacentre/loadimages/",{folder:imagefolder}, loadImages);
        $("#"+currentimagearray).attr("class","selected");

  setTimeout(inithidearrows,3000);

  $("#selector img").click(function() {
	  $.post("/mediacentre/loadimages/",{folder:this.id}, loadImages);
	  $("#"+this.id).attr("class","selected");
		$("#"+currentimagearray).attr("class","");
		currentimagearray = this.id;

//		if(numberofimages > 0) {
//			$("#rightcontroller").fadeIn(1000);
//		}

    setDisplay("mediaimages","block");
    setDisplay("videowrapper","none");
    setDisplay("problems", "none");

	  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);
		
		$('.slideholder').lightBox();
	}

  $("#workselect a").click(function() {
	  $.post("/mediacentre/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;
	});


});