
var LanguageSelect = Class.create();
LanguageSelect.prototype = {

	// ******************************************************************************
	// Constants
	// ******************************************************************************
	Version: '1.0',

	selectDivId : 'languageSelectSelected',
	popupDivId : 'languageSelectItems',
	

	// ******************************************************************************
	// vars
	// ******************************************************************************

	isOpen : false,

	// ******************************************************************************
	// Constructor
	// ******************************************************************************
	initialize: function(options) {
		if((typeof Prototype=='undefined') || 
		   (typeof Element == 'undefined') || 
		   (typeof Element.Methods=='undefined') ||
		   parseFloat(Prototype.Version.split(".")[0] + "." +
			      Prototype.Version.split(".")[1]) < 1.5)
			throw("LanguageSwitch benötigt das Prototype JavaScript framework >= 1.6.0");


		Event.observe(document, 'dom:loaded', this.setup.bindAsEventListener(this), false);
   	},

	setup: function(){


//		$A($('languageSelectDropdown').options).each(function(elm){ console.info(elm) });
		
		pageId = $('activePageId').value;
		select = $A($('languageSelectDropdown').options).collect(function(elm){
				//return {'title' : elm.text, 'value' : elm.value, 'selected' : elm.selectedIndex};
				if(elm.selected) elm.active = 'active';
				var myTemplate = new Template('<div class="languageSelectItem" ><a class="#{active}" href="'+ $('languageSwitchURL_' + elm.value).value  +'" >#{text}</a></div>');
				return myTemplate.evaluate(elm);
			}).join(' ');

		activeLangTitle = $('languageSelectDropdown').options[$('languageSelectDropdown').options.selectedIndex].text;

		html = '<div id="languageSelectSelected">'+ activeLangTitle +'</div><div id="languageSelectItems" style="display:none;" >' + select + '</div>';

		$('languageSelect').update(html);



		Event.observe($(this.selectDivId), 'click', this.selectClickHandler.bindAsEventListener(this), false);

//		$$('.popdown').each(this.setup.bind(this));

		$(this.popupDivId).select('a').each(this.setupLinks.bind(this));

	},

	setupLinks : function(elm){
		Event.observe(elm, 'click', this.linkClickHandler.bindAsEventListener(this), false);
	},

	linkClickHandler : function(ev){
		ev.stop();

		elm = ev.element();
	
		document.location.href = elm.href;
	},
	

	selectClickHandler : function(ev){
		ev.stop();


		if(this.isOpen == false){
			this.openPopup();
		} else {
			this.closePopup();
		}
	},

	openPopup : function(){
		$(this.popupDivId).show();

		this.isOpen = true;

		bod = document.getElementsByTagName('body')[0];
		this.bodyClickHandlerPointer = this.bodyClickHandler.bindAsEventListener(this);

		Event.observe(bod , 'click', this.bodyClickHandlerPointer, false);
	},

	closePopup : function(){
		$(this.popupDivId).hide();

		this.isOpen = false;

		bod = document.getElementsByTagName('body')[0];
		Event.stopObserving(bod, 'click', this.bodyClickHandlerPointer);
	},


	bodyClickHandler : function(ev)
	{
		ev.stop();

		this.closePopup();
		
	}


};


myLanguageSelect = new LanguageSelect();