Element.addMethods({
  scrollTo: function(element, left, top){
    var element = $(element);
    if (arguments.length == 1){
      var pos = element.cumulativeOffset();
      window.scrollTo(pos[0], pos[1]);
    } else {
      element.scrollLeft = left;
      element.scrollTop  = top;
    }
    return element;
  }
});
Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    this.start(Object.extend({x: 0, y: 0}, arguments[1] || {}));
  },
  setup: function() {
    var scrollOffsets = (this.element == window) 
                ? document.viewport.getScrollOffsets() 
                : Element._returnOffset(this.element.scrollLeft, this.element.scrollTop) ;
    this.originalScrollLeft = scrollOffsets.left;
    this.originalScrollTop  = scrollOffsets.top;
  },
  update: function(pos) {
    this.element.scrollTo(Math.round(this.options.x * pos + this.originalScrollLeft), Math.round(this.options.y * pos + this.originalScrollTop));
  }
});

function layout_thumbnails() {
	$$('#gallery-thumbnails li').each(function(e,i){
		left = i * 119
		e.setStyle('float:none; margin:0; left:0; top:0; position:absolute')
		e.setStyle('left:' + left + 'px')
	})
}

function cycle_image(e) {
	set_big_image(this.getAttribute('href'))
	Event.stop(e)
}

function set_big_image(uri) {
	$('bimg').src = uri
	$$('#wallpaper-download a').first().setAttribute('href', "big?i=" + uri.gsub('.jpg', '-w.jpg'))
}

function change_gallery(e) {
	target_gallery = this.getAttribute('href').gsub('#', '')
	$('gallery-thumbnails').innerHTML = $(target_gallery).innerHTML
	set_big_image( $$('#gallery-thumbnails a').first().getAttribute('href') )
	// if ($$('#wallpaper-download a').first().hasClassName('no-download')){ alert("You've got crabs ass face")}
	if (this.hasClassName('no-download')) { $$('#wallpaper-download').first().hide() } else { $$('#wallpaper-download').first().show() }
	layout_thumbnails()
	trap_thumbnail_clicks()
	Event.stop(e)
	return false
}

function trap_thumbnail_clicks() {
	$$('#gallery-thumbnails a').each(function(i){
		Event.observe(i, 'click', cycle_image)
	})
}

function trap_gallery_list_clicks() {
	$$('#gallery-list a').each(function(i){
		Event.observe(i, 'click', change_gallery)
	})
}

function trap_gallery_scrolling() {
	Event.observe('scroll_next', 'click', function(){ scroll_it('next') })
	Event.observe('scroll_previous', 'click', function(){ scroll_it('previous') })
	Event.observe('scroll_up', 'click', function(){ scroll_gals('previous') })
	Event.observe('scroll_down', 'click', function(){ scroll_gals('next') })
}

function scroll_it(direction) {
	amount = $('gallery-thumbnails').getWidth() * (direction == 'previous' ? -1 : 1)
	new Effect.Scroll('gallery-thumbnails', {x: amount, duration:0.5});
}

function scroll_gals(direction) {
	amount = $('#gallery-list').getHeight() * (direction == 'previous' ? -1 : 1)
	new Effect.Scroll('#gallery-list', {y: amount, duration:0.5});
}

function init() {
	layout_thumbnails()
	trap_thumbnail_clicks()
	trap_gallery_list_clicks()
	trap_gallery_scrolling()
}

document.observe('dom:loaded', init)
