var ExpandedList = Class.create();

ExpandedList.prototype = {
  /*
    Initializes an expanded list. The variable +element_id+ must be the id of an HTML element
  */
  initialize: function(element_id, event_id) {
    this.control_id = element_id;
    this.event_id = event_id;
    this.element = $(element_id);
    Event.observe(this.event_id, 'click', this.onMouseClickToggle.bindAsEventListener(this), false);
    var expand = this.getCookie(this.control_id);
    if (expand != null && expand == "true") {
    	this._toggle();
    }
  },
  
  /*
    Saves a cookie for the expanded list
  */
  saveCookie: function(c_name, expanded) {
    document.cookie = c_name + "=" + expanded + "; path=" + this.extractPath();
  }, 
  
  /*
  	toggles the expanded list
  */
  onMouseClickToggle: function(event) {
  	var element = Event.element(event);
  	this._toggle();
  },
  
  _toggle: function() {
  	Element.toggle(this.control_id);
  	this.saveCookie(this.control_id, this.element.visible().toString());
  },
  
  /*
    Extracts the current path from the current url
  */
  extractPath: function() {
    var fullpath = location.href.substring(0,location.href.lastIndexOf("/")+1);
    return fullpath.replace("http://www.fta.co.uk", "");
  },
  
  getCookie: function(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) { 
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return null;
  }
  
};