var Gallery = new Object();
var leftmost_image = 0;

var LEFT = 0;
var RIGHT = 1;

Gallery.images = new Array();
Gallery.is_playing = null;
Gallery.num_preview_images = 3;
Gallery.current_image = null;

Gallery.register_image = function(img_url) {
  img = new Image();
  img.src = img_url;
  Gallery.images.push(img);
  leftmost_image = 1;
}

Gallery.populate_divs = function() {
  var big = $("bigImage");
  big.innerHTML = ''
  
  Gallery.images.length.times(function(i) {
    var img_tag = '<div id="thumb-' + (i+1) + '" class="thumbnail" style="display:none">';
    img_tag    += '<a href="#" onclick="return(Gallery.show(' + (i+1) + '))">';
    img_tag    += '<img src="' + Gallery.images[i].src + '" />';
    img_tag		 += '</a></div>';
  	new Insertion.Bottom('thumbs', img_tag);
  	
  	var big_img_tag = '<div class="bigImg" id="bigImage-' + (i+1) + '" style="display:none">';
  	big_img_tag    += '<img src="' + Gallery.images[i].src + '" />';
  	big_img_tag		 += '</div>';
  	new Insertion.Bottom('bigImage', big_img_tag);
  });
  
  Gallery.show(2);
  
  //create extra images to allow circular scrolling
  var n = Gallery.num_preview_images - 1;
  n.times(function(i) {
    var img_tag = '<div id="thumb-' + (i+1) + 'a" class="thumbnail" style="display:none">';
    img_tag    += '<a href="#" onclick="Gallery.show(' + (i+1) + '); return false;">';
    img_tag    += '<img src="' + Gallery.images[i].src + '" />';
    img_tag		 += '</a></div>';
  	new Insertion.Bottom('thumbs', img_tag);
  });
  
  //show first images
  Gallery.num_preview_images.times(function(i) {
    var ele = $("thumb-" + (i+1));
    
    if (ele != null) {
      ele.style.display = 'block';
    }
  });
}

Gallery.show = function(image_id) {
  if (Gallery.images[image_id-1] != null) {
    Gallery.images.length.times(function(i) {
      if ((i+1) != Gallery.current_image) {
        $("bigImage-" + (i+1)).style.display = 'none';
      }
    });

    if (Gallery.current_image != null) {
      Effect.Fade('bigImage-'+Gallery.current_image, {queue: 'front', duration: 0.2});
    }
    
    Effect.Appear('bigImage-'+image_id, {queue: 'end', duration: 0.2});
    Gallery.current_image = image_id;
  }  
}

Gallery.scroll_images = function(direction) {
 
  if (Gallery.is_playing) {
    var imgNum = ((leftmost_image+2) % Gallery.images.length);
    if (imgNum == 0) { imgNum = Gallery.images.length}
    Gallery.show(imgNum);
  } else {
    if (direction == null) return;
  }
  
  if (direction == LEFT) {
    leftmost_image = ((Gallery.images.length + leftmost_image - 1) % Gallery.images.length);
  } else {
    leftmost_image = ((Gallery.images.length + leftmost_image + 1) % Gallery.images.length);
  }
  
  if (leftmost_image == 0) {leftmost_image = Gallery.images.length }
   
  Gallery.images.length.times(function(i) {
    var ele = $("thumb-" + (i+1));
    ele.style.display = 'none';
  });
  
  var n = Gallery.num_preview_images - 1;
  n.times(function(i) {
    var ele = $("thumb-" + (i+1) + 'a');
    ele.style.display = 'none';
  });
  
  if (leftmost_image + (Gallery.num_preview_images - 1) > Gallery.images.length) {
    var num_additional = (leftmost_image + (Gallery.num_preview_images - 1) - Gallery.images.length);
    var normal = Gallery.num_preview_images - num_additional;
       
    normal.times(function(i) {
      var ele = $("thumb-" + (leftmost_image+i));
      ele.style.display = 'block';
    });
    
    num_additional.times(function(i) {
      var ele = $("thumb-" + (i+1) + 'a');
      ele.style.display = 'block';
    });
    
  } else {
    Gallery.num_preview_images.times(function(i) {
		  var ele = $("thumb-" + (leftmost_image + i));
		  ele.style.display = 'block';
  	});  
  }
}

Gallery.play = function(speed) {
  if (speed == null) { speed = 5 } //5 seconds per image by default
  if (Gallery.is_playing == null) {
    updater = new PeriodicalExecuter(Gallery.scroll_images, speed);
  }
  Gallery.is_playing = true;
  $('play').style.backgroundPosition = '0 -27px';
  $('pause').style.backgroundPosition = '0 0';
}

Gallery.pause = function() {
  Gallery.is_playing = false;
  $('play').style.backgroundPosition = '0 0';
  $('pause').style.backgroundPosition = '0 -27px';
}

function popup(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=1,resizable=1,width=350,height=400,left=400,top=200');");
}

curBigImg = 0;

function showBigImage(id, nxt) {
  if (id == curBigImg) return;
  
  mv = false;
  
  if (nxt == true && curBigImg < maxBigImg) {
  	id = curBigImg + 1;
  	mv = true;
  } else if (nxt == false && curBigImg > 0) {
  	id = curBigImg - 1;
  	mv = true;
  }
  
  if (mv) {
		Effect.Fade('big'+curBigImg, {queue: 'front', duration: 0.2});
	  Effect.Appear('big'+id, {queue: 'end', duration: 0.2});
	  curBigImg = id;
  }
}

