if ($('fPassBtn') && $('loginBtn')) {
	$('fPassBtn').observe('click',function(e){
		e.stop();
		this.up('#login').setStyle({'display':'none'});
		this.up('#login').next('#fPass').setStyle({'display':'block'});
	});
	$('loginBtn').observe('click',function(e){
		e.stop();
		this.up('#fPass').setStyle({'display':'none'});
		this.up('#fPass').previous('#login').setStyle({'display':'block'});
	});
}

// Rounding numbers
function roundNum(number, digits) {
	var multiple = Math.pow(10, digits);
	var rndedNum = Math.round(number * multiple) / multiple;
	return rndedNum;
}

// See more in Statistic table
if($$('.statTable table')){
	$$('.tableContainer').each(function(e){
		var tblH = parseInt(e.down('table').getHeight());
		if (tblH > 92){
			e.setStyle({'overflow':'hidden','height':'92px'});
		}
		else {
			e.next('.seeMore').setStyle({'display':'none'});
		}
	});
}
if($$('.statTable .seeMore')[0]){
	$$('.statTable .seeMore a').invoke('observe','click',function(e){
		e.stop();
		var	tblH = parseInt(this.up('.seeMore').previous('.tableContainer').down('table').getHeight()),
			container = this.up('.seeMore').previous('.tableContainer'),
			contH = parseInt(this.up('.seeMore').previous('.tableContainer').getHeight());
		if (contH <= 92){
			new Effect.Morph(container, {style: {height:(tblH+10)+'px'},duration:0.2});
			this.update('Vis siste spill');
		}
		else {
			new Effect.Morph(container, {style: {height:'92px'},duration:0.2});
			this.update('Vis alle spill');
		}
	})
}

/*
	Formating numbers function
	--------------------------
	a - number
	b - numbers after floating poin
	c - floating point type (',','.',...)
	d - thousands point 
*/

function numberFormat(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if (!f[0]) {
		f[0] = '0';
	}
	if (!f[1]) {
		f[1] = '';
	}
	if (f[1].length < b) {
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

//
if($$('#bettingPage .amountInp')[0]){
	$$('#bettingPage .amountInp').invoke('observe','keyup',function(e){
		var	val1 = this.up('.bettingInfo').next('.total').down('.val1'),
				val2 = this.up('.bettingInfo').next('.total').down('.val2'),
				val3 = this.up('.bettingInfo').next('.total').down('.val3'),
				sharePrice = parseInt(this.next('.priceInp').getValue()),
				thisVal1 = this.getValue();//.replace(",","."),
				thisVal = parseFloat(thisVal1),
				totalPrice = sharePrice * thisVal;//roundNum(sharePrice * thisVal,10);
		if (isNaN(thisVal) == true) { thisVal = '0'; }
		val1.update(numberFormat(thisVal,0,',','.'));	// Ammount
		val2.update(sharePrice);								// Share price
		if (isNaN(totalPrice) == true) { totalPrice = '0'; }
		val3.update(numberFormat(totalPrice,0,',','.'));
	});
}
/*
document.delegate('a.cantBuy', 'click', function(e){
	alert('Det er ikke mulig å kjøpe andeler før du er innlogget. Er du ikke medlem fra før, MÅ du registrere deg.');
});
document.delegate('#fPassSubmit', 'click', function(e){
	alert('Ditt nye passord er sendt til '+$('fPassInput').value);
});
*/
