// a simple function to turn a number into a 3-character string
// this is needed to get the right picture files etc.
function int_to_string(i){

  var out_string;
  out_string = "";
  if (i<0)
  {
  out_string = "-";
  i = -i;
  }
  if (i<10)
    out_string += ("00" + i);
  else if (i<100)
    out_string += ("0" + i);
  return out_string;
}

var x = 0;
var desired_x = 0;
var x_speed = 0;
var acceleration = 5;
var current_image_number = 1;
var number_of_images = 5; ////////////// TODO: set this dynamically

current_image = window.location.hash;
//document.write(current_image);
if (current_image == "")
  current_image= "001";
//current_image_number = current_image.substr(-3,3);
current_image_number = parseInt(current_image.substr(-3,3),10);
x = 0-(current_image_number-1)*660;
//document.write(('image'+int_to_string(current_image_number)));
//document.write(location.pathname);
var category = "";
function initialise_everything(){
  if (location.pathname=="/bigglesart-new/drawing"){
    category = "drawings";
    number_of_images = 2;
    document.getElementById("drawings-button").className='selected';
  }
  else if (location.pathname=="/bigglesart-new/cartoon"){
    category = "cartoons";
    number_of_images = 9;
    //document.getElementById("cartoons-button").className='selected';
  }
  else if (location.pathname=="/bigglesart-new/blog"){
      category = "blog";
      number_of_images = 0;
      document.getElementById("blog-button").className='selected';
  }
  else if (location.pathname=="/bigglesart-new/contact"){
      category = "contact";
      number_of_images = 0;
      document.getElementById("contact-button").className='selected';
  }
  else if (location.pathname=="/bigglesart-new/painting"){ 
    category = "paintings";
    number_of_images = 5;
    document.getElementById("paintings-button").className='selected';
  }
  else if (location.pathname=="/bigglesart-new/exhibition"){ 
    category = "exhibition";
    number_of_images = 0;
    document.getElementById("exhibition-button").className='selected';
  }  
  else { // default page is now the exhibition page
    category = "exhibition";
    number_of_images = 0;
    document.getElementById("exhibition-button").className='selected';
  }
  set_current_image(current_image_number);
  t = setInterval('move_images_around()',10);
}

function set_desired_x(new_x){
    document.write(new_x);
  desired_x = new_x;
}
function set_current_image(i){
  i = i<1 ? number_of_images: i;
  i = i>number_of_images ? 1 : i;
  
  current_image_string = int_to_string(current_image_number);
  document.getElementById("image-button-"+current_image_string).className='normal';
  /////// IDEA: replace this with switching to the next/previous image category
  current_image_number = i;

  current_image_string = int_to_string(current_image_number);
  document.getElementById("image-button-"+current_image_string).className='selected';
  //document.write("image-button-"+current_image_string);
  document.getElementById("image-button-"+current_image_string).className='nav-button selected';
  window.location.hash = "#"+current_image_string;
}

function move_images_around(){
  desired_x = 0 - (current_image_number - 1) * 660;
  i = document.getElementById(category+"-pane");
  if (desired_x > x)
    direction = 1;
  else
    direction = -1;

  var distance = direction * (desired_x - x);
  
  //Snap to location
  if (distance * distance <= (x_speed * x_speed)){
    x = desired_x;
    x_speed = 0;
  }
  else {
    if ((x_speed * x_speed / 2) > (direction*(desired_x - x))){
      x_speed = x_speed + (direction * acceleration);
    }
    else
      x_speed = x_speed + (direction * acceleration);
    x = x + x_speed;
  }
  i.style.left = x + 'px';

}



