//Image switch
Event.observe(document, 'dom:loaded', function(event) {
  var image_container = $("image_container");
  if(image_container && image_container.immediateDescendants().size() > 1) {
    new PeriodicalExecuter(function(pe) {
      var active = this.getElementsBySelector(".active")[0];
      var next = active.next();
      if(!next) {
        next = $(active.parentNode).down();
      }
      next.style.zIndex = 2;
      active.style.zIndex = 0;
      next.style.display = "none";
      next.addClassName("active");
      new Effect.Appear(next, {afterFinish: function() {active.removeClassName('active')}});
    }.bind(image_container), 3);
  }
});


//Image-Enlargement
Event.observe(document, 'dom:loaded', function(event) {
  var enlargement_links = $$("a[rel=lightbox]");
  if(enlargement_links.length === 0) {
    return;
  }
  
  var handle_enlargement = function(event) {
    var right_container = $('right_container');
    var link = this;
    $$('#left_container a.active').invoke('removeClassName', 'active');
    link.addClassName('active');
    right_container.immediateDescendants().each(function(image) {
      right_container.removeChild(image);
    });
    var image = document.createElement('img');
    image.setAttribute('src', link.getAttribute("href"));
    image.setAttribute('title', link.getAttribute("title"));
    image.setAttribute('alt', link.getAttribute("title"));
    right_container.appendChild(image);
    if(show_image_legends) {
      var p = document.createElement('p');
      p.appendChild(document.createTextNode(link.getAttribute("title")));
      right_container.appendChild(p);
    }
    link.blur();
    event.cancelBubble = true;
    event.preventDefault();
    Event.stop(event);
  };
  
  enlargement_links.each(function(link) {
    Event.observe(link, "click", handle_enlargement.bindAsEventListener(link));
  });
});


//Scrolling
Event.observe(window, 'load', function(event) {
  var left_container = $("left_container");
  if(left_container.scrollHeight > left_container.clientHeight) {
    $$('#arrow_up, #arrow_down').invoke('setStyle', {display: "block"});
    var arrow_up = $('arrow_up');
    var arrow_down = $('arrow_down');
    var scroll = function(event, is_up) {
      var offset = 160;
      if(is_up) {
        offset = 0-offset;
      }
      new Effect.ScrollElement(this, offset, {duration: 0.5});
    };
    Event.observe(arrow_down, "click", scroll.bindAsEventListener(left_container, false));
    Event.observe(arrow_up, "click", scroll.bindAsEventListener(left_container, true));
  }
});


//Scroll js
if(window.Effect) {
  Effect.ScrollElement = Class.create();
  Object.extend(Object.extend(Effect.ScrollElement.prototype, Effect.Base.prototype), {
    initialize: function(element, offset) {
      this.element = $(element);
      this.offset = offset;
      this.start(arguments[2] || {});
    },
    setup: function() {
    	this.begin_position = this.element.scrollTop;
    	this.end_position = this.begin_position + this.offset;
  	  if(this.end_position < 0) {
  	    this.duration_factor = 1.0*(0-this.end_position)/this.offset
    		this.end_position = 0;
    	}
  	  if(this.end_position > (this.element.scrollHeight - this.element.clientHeight)) {
  		  this.end_position = (this.element.scrollHeight - this.element.clientHeight);
    	}
      var duration_factor = 1.0*(this.end_position-this.begin_position)/this.offset;
      this.options.duration = this.options.duration*duration_factor;
    },
    update: function(position) {
    	this.element.scrollTop = this.begin_position + (position * (this.end_position-this.begin_position));
    }
  });
  
  Effect.ScrollToItem = Class.create();
  Object.extend(Object.extend(Effect.ScrollToItem.prototype, Effect.ScrollElement.prototype), {
    initialize: function(element, parent) {
      this.element = parent;
      this.offset = element.offsetTop;
      if(element.offsetParent !== parent) {
        this.offset -= parent.offsetTop;
      }
//      this.offset = this.offset - this.element.offsetTop;
      this.start(arguments[2] || {duration: 0.2});
    }
  });
}

var MapEventHandler = Class.create();

MapEventHandler.prototype = {  
  initialize: function() {
    this.queue = $A();
    this.previous_scroll_position = $("left_container").scrollTop;
  },
  
  add_to_queue: function(city, is_out, wait_for_next_event) {
    var old_map_event = this.queue.find(function(map_event) {
      return map_event.is(city, is_out) && !map_event.is_running();
    });
    
    if(old_map_event) {
      this.queue = this.queue.without(old_map_event);
      old_map_event.stop();
    }
    
    if(!is_out) {
      this.previous_scroll_position = $("left_container").scrollTop;
    }
    this.queue[this.queue.length] = new MapEvent(city, is_out, this.end.bind(this), this.previous_scroll_position, wait_for_next_event);
    this.start();
  },
  
  end: function(map_event) {
    this.queue = this.queue.without(map_event);
    this.start();
  },
  
  start: function() {
    if(this.queue.length > 0) {
      if(this.queue[0].wait_for_next_event === true && this.queue.length < 2) {
        return;
      }
    
      if(!this.queue[0].is_running()) {
        this.queue[0].start();
      }
    }
  }
}

var MapEvent = Class.create();

MapEvent.prototype = {
  initialize: function(city, is_out, finish_callback, scroll_position, wait_for_next_event) {
    this.city = city;
    this.is_out = is_out;
    this.scroll_position = scroll_position;
    this.list_items = $$("#tour_list .item").findAll(function(list_item) {
      return !list_item.match("."+city);
    });
    this.finish_callback = finish_callback;
    this.effects = $A();
    this.wait_for_next_event = wait_for_next_event;
  },
  
  start: function() {
    var map_event = this;
    if(this.list_items.length === 0) {
      this.finish_callback(this);
      return;
    }
    this.list_items.each(function(list_item) {
     var effect = map_event.is_out ? Effect.BlindDown : Effect.BlindUp;
     map_event.effects[map_event.effects.length] = new effect(list_item, {afterFinish: map_event.end.bind(map_event), duration: 0.2});
    });
    if(!this.is_out) {
      this.scroll();
    }
  },
  
  stop: function() {
    this.effects.each(function(effect) {
      effect.cancel();
    });
  },
  
  is: function(city, is_out) {
    return this.city === city && this.is_out === is_out;
  },
  
  end: function(effect) {
    this.effects = this.effects.without(effect);
    if(this.is_out) {
      this.scroll();
    }
    if(this.effects.length < 1) {
      this.finish_callback(this);
    }
  },
  
  scroll: function() {
    var desired_scroll_position = this.is_out ? this.scroll_position : 0;
    var scroll_offset = desired_scroll_position - $("left_container").scrollTop;
    if(scroll_offset !== 0) {
      new Effect.ScrollElement($("left_container"), scroll_offset, {duration: 0.2});
    }
  },
  
  is_running: function() {
    return this.effects.length > 0;
  }
}