var x45050_activeCat = 0;
var x45050_firstCat = 0;
var prevUid = '';
var isOnload = true;
var blockNavigation = false;

//Eingabe der Titelfelder-Uid:
var titleFieldsUid = new Array('47');

function showCategory(uid) {
	if (!blockNavigation){
		if (x45050_activeCat != uid || isOnload) {
			isOnload = false;
			if (x45050_activeCat > 0) {
				Effect.BlindUp($('category_'+x45050_activeCat),{duration: 0.7});
				if($('act')){
					hideSaveButton()
					$('act').src = 'fileadmin/linie-e/img/inact-arrow.gif';
					$('act').id = prevUid;
				}
			}
			Effect.BlindDown($('category_'+uid),{duration: 0.7});
			x45050_activeCat = uid;
			$('title'+uid).src = 'fileadmin/linie-e/img/act-arrow.gif';
			prevUid = 'title'+uid;
			defineDynamicHeader(uid);
			$('title'+uid).id = 'act';
			$('actCat').value = uid;
			showSaveButton();
		}
	}
	return false;
}

function defineDynamicHeader(uid){
	if(uid == 1){
		$('bgImage').setStyle({
			backgroundImage: 'url(fileadmin/linie-e/img/header'+uid+'.gif)'
		});
	}else if(uid==2){
		$('bgImage').setStyle({
			backgroundImage: 'url(fileadmin/linie-e/img/header'+uid+'.gif)'
		});
	}else if(uid==5){
		$('bgImage').setStyle({
			backgroundImage: 'url(fileadmin/linie-e/img/header'+uid+'.gif)'
		});
	}else{
		$('bgImage').setStyle({
			backgroundImage: 'url(fileadmin/linie-e/img/header.gif)'
		});
	}
}
function hideSaveButton(){
	temp = $('act').ancestors();
	temp[0].setStyle({
		width: 690+"px"
	});
	temp = temp[1];
	temp = temp.childElements();
	button = temp[1];
	button.toggle();
}

function showSaveButton(){
	temp = $('act').ancestors();
	temp[0].setStyle({
		width: 582+"px"
	});
	temp = temp[1];
	temp = temp.childElements();
	button = temp[1];
	button.toggle();
}

function validate_and_calculate(min, max, obj, actionUid, categoryUid) {
	
	if(obj.readOnly == false){
		min = parseInt(min);
		max = parseInt(max);
		
		if (obj.value != '') {
			obj.value = parseInt(obj.value);
			
			if (obj.value == 'NaN') {
				obj.value = 0;
			}
		}else{
			obj.value = 0;
		}
		
		if (obj.value < min) {
			obj.value = min;
		}
		
		if (obj.value > max) {
			obj.value = max;
		}
		
		calculateSavings(actionUid, categoryUid);
	}
}

function calculateAllSavings() {
	cats = $('x4e5050_categories').getElementsByClassName('category');
	catsLength = cats.length;
	for(i=0;i<catsLength;i++) {
		actions = $(cats[i]).getElementsByClassName('action_uids');
		actionsLength = actions.length;
		id = cats[i].id.split('_')[1];
		for(j=0;j<actionsLength;j++) {
			
			calculateSavings(actions[j].value,id);
		}
	}
	x45050_firstCat = $('actCat').value;
	setTitleRows();
	showCategory(x45050_firstCat);
}

function setTitleRows(){
	for(i=0; i<titleFieldsUid.length; i++){
		uid = titleFieldsUid[i];
		action = $('action_'+uid+'_name').childElements();
		action[0].setStyle({
			fontWeight: "bold"
		});

		$('action_'+uid+'_perweek').hide();
		$('action_'+uid+'_saved_costs').hide();
		$('action_'+uid+'_saved_year').hide();
		$('doingYet_'+uid).hide();
		$('fr1_'+uid).hide();
		$('fr2_'+uid).hide();
		
	}
	
}

function calculateSavings(actionUid, categoryUid) {
	categories = $('catWrapper').getElementsByClassName('category');
	x45050_firstCat = categories[0].id.replace(/category_/,"");
	idPrefix = 'action_'+actionUid;
	val1 = $(idPrefix+'_perday').value;
	val2 = $(idPrefix+'_perweek').value;
	factor = $(idPrefix+'_saving').value;
	
	no_of_savings_per_year = $(idPrefix+'_no_of_savings_per_year').value;

	energyCostFactor = $F(idPrefix+'_factor');
	savedEnergy = val2*factor/1000;
	if (val1 != "null") {
		savedEnergy = savedEnergy*val1;
	}
	savedCosts = savedEnergy * energyCostFactor;
	yearFormula = $(idPrefix+'_year_formula').value;

	$(idPrefix+'_saved_energy').innerHTML = savedEnergy.toFixed(3);
	$(idPrefix+'_saved_costs').innerHTML = toCurrency(savedCosts);
	if (yearFormula != '') {
		$(idPrefix+'_saved_year').innerHTML = toCurrency(eval(yearFormula));
	} else if (val2>0) {
		$(idPrefix+'_saved_year').innerHTML = toCurrency((savedCosts*no_of_savings_per_year));
	} else {
		$(idPrefix+'_saved_year').innerHTML = 0;
	}
	calculateSubTotal(categoryUid);
	disableInputOnload(actionUid, categoryUid);
}

function resetTotal(actionUid, categoryUid){
	zero = 0.000;
	$('action_'+actionUid+'_saved_energy').innerHTML = zero.toFixed(3);
	$('action_'+actionUid+'_saved_costs').innerHTML = zero.toFixed(2);
	$('action_'+actionUid+'_saved_year').innerHTML = 0;
}

function toCurrency(number) {
	number = parseFloat(number).toFixed(2);
	number100 = Math.round(number*100);
	number100Mod5 = number100 % 5
	if (number100Mod5 == 0) {
		
	} else if (number100Mod5 > 2) {
		number100 += (5-number100Mod5);
	} else {
		number100 -= number100Mod5;
	}
	return (number100 / 100).toFixed(2);
}


function calculateSubTotal(categoryUid) {
	if(categoryUid != 5 && categoryUid != 6 ){
		$('category_'+categoryUid+'_total_saved_energy').innerHTML = subtotal(categoryUid,'saved_energy').toFixed(3);
	}
	$('category_'+categoryUid+'_total_saved_costs').innerHTML = subtotal(categoryUid,'saved_costs').toFixed(2);
	$('category_'+categoryUid+'_total_saved_year').innerHTML = subtotal(categoryUid,'saved_year').toFixed(2);
	calculateTotalSavings();
}

function calculateTotalSavings(categoryUid) {
	if(categoryUid != 5 && categoryUid != 6 ){
		$('week_total_saved_energy').innerHTML = totalSavings('subtotal_saved_energy').toFixed(3);
		$('field_week_total_saved_energy').value = $('week_total_saved_energy').innerHTML;
	}
	$('week_total_saved_costs').innerHTML = totalSavings('subtotal_saved_costs').toFixed(2);
	$('field_week_total_saved_costs').value = $('week_total_saved_costs').innerHTML;
	$('week_total_saved_year').innerHTML = totalSavings('subtotal_saved_year').toFixed(2);
}

function totalSavings(className) {
	var elems = $('x4e5050_categories').getElementsByClassName(className);
	var elems_length = elems.length
	var total = 0;
	for(t=0;t<elems_length;t++) {
		if (elems[t].innerHTML == '') {
			elems[t].innerHTML = 0;
		}
		total += parseFloat(elems[t].innerHTML);
	}
	return total;
}

function subtotal(categoryUid,className) {
	var elems = $('category_'+categoryUid).getElementsByClassName(className);
	var elems_length = elems.length;
	var total = 0;
	for(t=0;t<elems_length;t++) {
		if (elems[t].innerHTML == '') {
			elems[t].innerHTML = 0;
		}
		total += parseFloat(elems[t].innerHTML);
	}
	return total;
}

function displayBigGraph(){
	increaseOverlayOpacity();
	setGraphPosition();
}

function increaseOverlayOpacity(){
	var x = document.viewport.getDimensions().width;
	var y = document.viewport.getDimensions().height;
	$('overlay').setStyle({
		width: x+"px",
		height: y+"px"

	});
	$('overlay').show();
	new Effect.Opacity('overlay', {duration:0.5, from:0.0, to:0.6});
	
}

function setGraphPosition(){
	leftOff = $('previewGraphBox').cumulativeOffset()["left"];
	topOff =  $('previewGraphBox').cumulativeOffset()["top"];

	leftOff = 65;
	topOff = 250;
	
	$('graphBoxContainer').setStyle({
		top: topOff+"px",
		left: leftOff+"px"
	});
	$('graphBoxContainer').show();
	new Effect.Opacity('graphBoxContainer', {duration:0.5, from:0.0, to:1.0});
}

function hideGraph(){
	new Effect.Opacity('overlay', {duration:0.5, from:0.6, to:0.0});
	timeoutOverlay = setTimeout('overlayTimeout()', 700);
	
	new Effect.Opacity('graphBoxContainer', {duration:0.5, from:1.0, to:0.0});
	timeoutGraph = setTimeout('graphTimeout()', 700);
	
	
}

function graphTimeout(){
	$('graphBoxContainer').hide();
	clearTimeout(timeoutGraph);
}

function overlayTimeout(){
	$('overlay').hide();
	clearTimeout(timeoutOverlay);
}

function hideElements(){
	$('week-nav').hide();
}

function disableInput(uid, categoryUid){
	
	if (typeof $('action_'+uid+'_perday')!= "undefined"){
		if($('action_'+uid+'_perday').readOnly == false){
			$('action_'+uid+'_perday').readOnly = true;
			$('action_'+uid+'_perday').setStyle({
				backgroundColor: "#D4D0C8",
				color: "#D4D0C8"
			});
			
		}else{
			$('action_'+uid+'_perday').readOnly = false;
			$('action_'+uid+'_perday').setStyle({
				backgroundColor: "#FFFFFF",
				color: "#000000"
			});
		}
	}
	if (typeof $('action_'+uid+'_perweek')!= "undefined"){
		if($('action_'+uid+'_perweek').readOnly == false){
			$('action_'+uid+'_perweek').readOnly = true;
			$('action_'+uid+'_perweek').setStyle({
				backgroundColor: "#D4D0C8",
				color: "#D4D0C8"
			});
		}else{
			$('action_'+uid+'_perweek').readOnly = false;
			$('action_'+uid+'_perweek').setStyle({
				backgroundColor: "#FFFFFF",
				color: "#000000"
			});
		}
	}
	if (typeof $('action_'+uid+'_perday')!= "undefined" || typeof $('action_'+uid+'_perweek')!= "undefined"){
		if($('action_'+uid+'_perday').readOnly == true || $('action_'+uid+'_perweek').readOnly == true){
			resetTotal(uid, categoryUid);
			calculateSubTotal(categoryUid);
		}else{
			calculateSavings(uid, categoryUid);
		}
	}
}

function disableInputOnload(uid, categoryUid){
	if ($('doingYet_'+uid).checked == true){
		disableInput(uid, categoryUid);
	}
}

function disableInputsOnSave(catUid){
	inputs = $A($('category_'+catUid).getElementsByClassName('valueInputs'));
	inputsLength = inputs.length;
	for (i=0; i < inputsLength; i++){
		inputs[i].readOnly = true;
	}
}
