var imgPath='typo3conf/ext/electricitycostcalculator/pi1/images/';
var tinyscrollbarInit=false;
var itemsCounter=0;
var itemsDivHeight=4;
var items=new Array();
var itemImgPath='uploads/tx_electricitycostcalculator/';
var defQuantity='10';
var defPrice='0,2';
var defHours='8';
var actWatt1='0';
var actWatt2='0';

function initCalculator(){
	initDefaults();
	addItems();
	setItem(0);
	setDivHeight();
}

function calculate(){
	var quantity=getFloat($('#formAnzahl').val());
	var price=getFloat($('#formStrompreis').val());
	var hours=getFloat($('#formBetriebsstunden').val());
	var totalPrice= (getFloat(actWatt1)-getFloat(actWatt2))*price*hours*quantity*0.001*365;
	$('#textErsparnis').html(getPrice(totalPrice));
	$('#textAnzahl').html($('#formAnzahl').val());
	$('#textStrompreis').html($('#formStrompreis').val());
	$('#textBetriebsstunden').html($('#formBetriebsstunden').val());
}

function initDefaults(){
	$('#formAnzahl').val(defQuantity);
	$('#formStrompreis').val(defPrice);
	$('#formBetriebsstunden').val(defHours);
	$('#textAnzahl').html(defQuantity);
	$('#textStrompreis').html(defPrice);
	$('#textBetriebsstunden').html(defHours);
	$('#defAnzahl').html(defQuantity);
	$('#defStrompreis').html(defPrice);
	$('#defBetriebsstunden').html(defHours);
}

function setDefaults(quantity,price,hours){
	defQuantity=quantity;
	defPrice=price;
	defHours=hours;
}

function getFloat(num){
	num=num.replace(',','.');
	num=parseFloat(num);
	if(!num>0) num=0;
	return num;
}

function getPrice(num){
	num=$().number_format(num, { numberOfDecimals:2, decimalSeparator: ',', thousandSeparator: '.' });
	num=num.replace(',00',',-');
	return num;
}

function addItem(watt1,name1,num1,img1,imgTh1,watt2,name2,num2,img2,imgTh2){
	var item=new Object();
	item.watt1=watt1;
	item.name1=name1;
	item.num1=num1;
	item.img1=img1;
	item.imgTh1=imgTh1;
	item.watt2=watt2;
	item.name2=name2;
	item.num2=num2;
	item.img2=img2;
	item.imgTh2=imgTh2;
	items.push(item);
}

function addItems(){
	for(var i=0;i<items.length;i++)
		$('#overviewLeft').append('<div class="item"><table><tr><td class="itemTh"><img src="'+itemImgPath+items[i].imgTh1+'" /></td></tr><tr><td>'+items[i].watt1+'</td></tr><tr><td>'+items[i].name1+'</td></tr><tr><td class="itemNum">'+items[i].num1+'</td></tr></table></div>');
}

function setItem(num){
	var item=items[num];
	actWatt1=item.watt1;
	actWatt2=item.watt2;
	$('#itemImgLeft').attr('src', itemImgPath+item.img1);
	$('#itemImgThLeft').attr('src', itemImgPath+item.imgTh1);
	$('#itemWattLeft').html(item.watt1);
	$('#itemNameLeft').html(item.name1);
	$('#itemNumLeft').html(item.num1);
	$('#itemImgRight').attr('src', itemImgPath+item.img2);
	$('#itemImgThRight').attr('src', itemImgPath+item.imgTh2);
	$('#itemWattRight').html(item.watt2);
	$('#itemNameRight').html(item.name2);
	$('#itemNumRight').html(item.num2);
	calculate();
}

function setItemsDivHeight(num){
	itemsDivHeight=num;
}

function setDivHeight(){
	$('#viewportLeft').css('height', ''+(110*itemsDivHeight)+'px');
	$('#contentRight').css('height', ''+(300+110*itemsDivHeight)+'px');
}

$(document).ready(function(){
	if($('#allItemsArea').length) {
		initCalculator();

		$('#contentLeft').hover(
			function () {
				$('#allItemsArea').show();
				if(!tinyscrollbarInit){
					$('#allItemsArea').tinyscrollbar({ sizethumb:75 });
					tinyscrollbarInit=true;
				}
			},
			function () {
				$('#allItemsArea').hide();
			}
		);

		$('#overviewLeft').children().each(function() {
			$(this).attr('rel', itemsCounter);
			itemsCounter++;
		});

		$('#overviewLeft').children().hover(
			function () {
				$(this).css('background-image', 'url('+imgPath+'bg_on.gif)');
			},
			function () {
				$(this).css('background-image', 'url('+imgPath+'bg_off.gif)');
			}
		);

		$('#overviewLeft').children().click(
			function () {
				setItem($(this).attr('rel'));
			}
		);

		$('#arrow').click(
			function () {
				if($('#addFields').css('display')=='block'){
					$('#addFields').hide();
					$(this).css('background-image', 'url('+imgPath+'arrow_down.gif)');
				} else {
					$(this).css('background-image', 'url('+imgPath+'arrow_up.gif)');
					$('#addFields').show();
				}
			}
		);

		$('#contentCenter :input').each(
			function() {
				$(this).change(
					function () {
						calculate();
					}
				);			
				$(this).click(
					function () {
						calculate();
					}
				);
				$(this).mouseup(
					function () {
						calculate();
					}
				);
				$(this).mouseup(
					function () {
						calculate();
					}
				);
				$(this).mouseenter(
					function () {
						calculate();
					}
				);
				$(this).keypress(
					function () {
						calculate();
					}
				);
				$(this).keypress(
					function () {
						calculate();
					}
				);
				$(this).keyup(
					function () {
						calculate();
					}
				);
			}
		);
	}
});

