/* File: /includes/js/framework/selectmanager.js (Modified: 3. september 2010 12:30:56) */

var SelectManager = {
  selects: new Hash(),
  
  createSelect: function(container, objects, options){
    var sel = new SelectList(container, objects, options);
    this.selects.set(container, sel);
  },
  
  showTeaser: function(container){
    this.selects.get(container).showTeaser();
  },
  
  showTeasers: function(){
    this.selects.each(function(pair){
      pair.value.showTeaser();
    });
  },
  
  updateSelect: function(container, objects){
    this.selects.get(container).updateObjects(objects);
  }
};

var SelectList = Class.create();
SelectList.prototype = {
	initialize: function(container, objects, options) {
		this.options = Object.extend({
			selectClass: '',
			teaserClass: 'navLinkBottom',
			teaserLabel: '',
			selectTimeout: 30000,
			defaultObjects: new Array(),
			onMouseover: Prototype.emptyFunction,
			destinationWindow: window
		}, options);
		this.container = $(container);
		this.objects = objects;
		this.container.update();
		this.renderTeaser();
		this.renderSelect();
	},

	renderTeaser: function() {
		this.teaser = new Element('a', { 'class': this.options.teaserClass });
		this.teaser.appendChild(document.createTextNode(this.options.teaserLabel));
		this.teaser.observe('mouseover', this.teaserMouseover.bindAsEventListener(this));
		this.container.appendChild(this.teaser);
	},

	teaserMouseover: function() {
		this.options.onMouseover();
		clearTimeout(this.teaserTimer);
		SelectManager.showTeasers();
		this.teaser.hide();
		this.select.show();
		this.select.focus();
		this.teaserTimer = setTimeout(this.showTeaser.bindAsEventListener(this), this.options.selectTimeout);
	},

	showTeaser: function() {
		clearTimeout(this.teaserTimer);
		this.select.hide();
		this.teaser.show();
	},

	renderSelect: function() {
		this.select = new Element('select', { 'class': this.options.selectClass }).setStyle({ display: 'none' });
		this.select.observe('change', this.selectChanged.bindAsEventListener(this));
		this.setObjects();
		this.container.appendChild(this.select);
	},

	setObjects: function() {
		this.select.update();
		var obj;
		var bottomObjects = new Array();
		for (var i = 0; i < this.options.defaultObjects.length; i++) {
			obj = this.options.defaultObjects[i];
			if (obj.top) {
				this.select.options.add(new Option(obj.text, obj.value));
			}
			else {
				bottomObjects[bottomObjects.length] = obj;
			}
		}
		for (var i = 0; i < this.objects.length; i++) {
			obj = this.objects[i];
			this.select.options.add(new Option(obj.text, obj.value));
		}
		for (var i = 0; i < bottomObjects.length; i++) {
			obj = bottomObjects[i];
			this.select.options.add(new Option(obj.text, obj.value));
		}
	},

	updateObjects: function(objects) {
		this.objects = objects;
		this.setObjects();
	},

	selectChanged: function() {
		if (this.select.value && typeof this.select.value != 'undefined' && this.select.value != '' && this.select.value != 'undefined') {
			var url = this.select.value.indexOf('javascript:') != -1 ? eval(this.select.value) : this.select.value;
			if (url != null && typeof url != 'undefined') {
				if (url.startsWith('/')) {
					this.options.destinationWindow.location.href = url;
				}
				else {
					window.open(url);
				}
			}
		}
		this.select.value = this.select.options[0].value;
		this.showTeaser();
	}
};


try {
} catch (e) {}
