Event.observe(window, 'load', function() {
	pluginVomHof.init();	
});

var pluginVomHof = {

	rubric: undefined,
	category: undefined,

	init : function (selectedRubric) {
		this.rubric = $('rubric');
		this.category = $('category');
		this.initCategory(selectedRubric)
		//rubric.onchange(this.rubricOnChange(this.value));
	},

	populateSelect : function (elt, arr){
		len = arr.length;
		elt.options[0] = new Option(JSFE['tx_vomhof_pi1']['pleaseChooseText'],'');

		for(j=0;j<len;j++) {		
			elt.options[j+1] = new Option(arr[j][0], arr[j][1]);
		}
		
		try {
			if(vomHofPi1SelectedCategories) this.checkIfCurrentOptionIsSelected(elt.options);
		}
		catch(err){
			elt.selectedIndex = 0;
		}
		
		
	},
	
	checkIfCurrentOptionIsSelected : function (options) {
		var optionNum = 0;
		$A(options).each(function(option) {
			vomHofPi1SelectedCategories.each(function(catId) {
					// ugly hack but the only way with internet explorer...
				if (option.value == catId) {
					window.setTimeout("$('category').options["+optionNum+"].selected = true,1");
				}
			});
			optionNum ++;
		});
	},

	clearSelect : function (elt){
		elt.options.length = 0;
		elt.selectedIndex = 0;
	},

	rubricOnChange : function (value) {
		if (value == 0) {
			this.disableCategory();
		} else {
			this.enableCategory(value, 0);
		}

		return false;
	},

	disableCategory : function (){
		this.category.options.length = 0;
		this.category.options[0] = new Option(JSFE['tx_vomhof_pi1']['defaultText'], 0);
		this.category.disabled = 0;
	},

	enableCategory : function (mainRubricUid, selected){
			//reset current options
		this.category.innerHTML = '';

		this.populateSelect(this.category, subArray[mainRubricUid]);
		this.category.value = selected;
		this.category.disabled = 0;
	},

	initCategory : function (selectedRubric){
		if (selectedRubric==undefined || selectedRubric=='') {
			selectedRubric = 0;
		}

		if (this.rubric) {
			if (this.rubric.value == 0) {
				this.disableCategory();
			} else {
				this.enableCategory(this.rubric.value, selectedRubric);
			}
		}
	},

	listShowOverlay : function (element) {

			//create a uniqe id we need this id for ie
		var rId = helper.uniqueId();

			//fetch overlay box
		var tempBoxOverlay = element.parentNode.parentNode.previousSibling;
		tempBoxOverlay.id = 'b'+rId;

			//now getElementBy id so we can do the nice functions with ie too.
		var boxOverlay = $('b'+rId);
		var boxOverlayHeight = boxOverlay.getHeight();
		var boxParent =  boxOverlay.parentNode;
		var overlayClass = 'overlay';

			//last element in list
		var boxOverlayLast = boxOverlay.next().next();

			//does the overlay overlap the footer?
		if($('footer')) var footerOffset = Position.cumulativeOffset($('footer'))[1];
		var elementOffset = Position.cumulativeOffset(element)[1];
		var overlayOffset = elementOffset+boxOverlayHeight;

			// is footer overlap is true set a overlayMarginTop var
		if(elementOffset+boxOverlayHeight > footerOffset) {
			var overlayMarginTop = '-'+(overlayOffset-footerOffset)+'px';
		}

		if (!boxOverlay.visible()) {

				// creat a dynamic margin
			element.id = 'a'+rId;
			var aTagWidth = $('a'+rId).getWidth();

			boxOverlay.setStyle({
				marginLeft: 20+aTagWidth+'px'
			});

			if(overlayMarginTop) {
				boxOverlay.setStyle({
					marginTop: overlayMarginTop
				});
			}

			helper.hideElementByClassName(boxParent,overlayClass);

				//close open overlayBoxes
			Effect.Appear(boxOverlay);

		}

	},


	detailToggleLabels : function (ulElement) {

		var container = $(ulElement);
		var childElements = container.childElements();
		var moreLink = document.getElementsByClassName('vomHofMore', container.up())[0];
		var backLink = document.getElementsByClassName('vomHofOne', container.up())[0];

		childElements.each(function(element) {

			if(element.style.display == 'none') {

				element.setStyle ({
					display: 'block',
					marginTop: '10px'
				});
			}

			if(backLink.visible()) {

				if(!element.hasClassName('active')) {
					element.setStyle ({
						display: 'none',
						marginTop: '0'
					});
				}

			}

		});

		moreLink.toggle();
		backLink.toggle();

	}
}