
var $j = jQuery;
$j(document).ready(function() {
	
	rotatePics(1);

	$j("input#epost").focus(function() {
		if (this.value == "E-post") {
			this.value = "";
		}
	});
	$j("input#epost").blur(function() {
		if (this.value == "") {
			this.value = "E-post";
		}
	});
	$j("input#passord").focus(function() {
		if (this.value == "Passord") {
			this.value = "";
		}
	});
	$j("input#passord").blur(function() {
		if (this.value == "") {
			this.value = "Passord";
		}
	});

	$j("#lost-email").focus(function() {
		if (this.value == "E-post") {
			this.value = "";
		}
	});
	$j("#lost-email").blur(function() {
		if (this.value == "") {
			this.value = "E-post";
		}
	});
	
	$j("#fornavn").focus(function() {
		if (this.value == "Fornavn") {
			this.value = "";
		}
	});
	$j("#fornavn").blur(function() {
		if (this.value == "") {
			this.value = "Fornavn";
		}
	});
	$j("#etternavn").focus(function() {
		if (this.value == "Etternavn") {
			this.value = "";
		}
	});
	$j("#etternavn").blur(function() {
		if (this.value == "") {
			this.value = "Etternavn";
		}
	});
/*
	$j("input.po-nr").each(function() {
		$j(this).focus(function() {
			if (this.value == "Skriv inn PO. nr") {
				this.value = "";
			}
		});
		$j(this).blur(function() {
			if (this.value == "") {
				this.value = "Skriv inn PO. nr";
			}
		});
	});

*/
	/* Validering */
	$j("table.validering input:text").each(function() {
		checkForNumber($j(this));
	});
		
		

	/* Lag total sum for hver tr i maanedlig-rapport */
	$j("table.maanedlig-rapport tr").not('th').each(function() {
		var  d = $j(this).find('td.c1 input:text').attr('disabled');
		// først, hent verdier når du kommer inn på sida
		var $tot = $j(this).find('td.ctotal input:text');
		var a_val = $j(this).find('td.c1 input:text').val();
		var a = $j(this).find('td.c1 input:text');
		var b_val = $j(this).find('td.c2 input:text').val();
		var b = $j(this).find('td.c2 input:text');
		var c_val = $j(this).find('td.c3 input:text').val();
		var c = $j(this).find('td.c3 input:text');
		
		if (a_val) {
		
			var sum = parseInt(a_val.replace(/\s+/g,''))+parseInt(b_val.replace(/\s+/g,''))+parseInt(c_val.replace(/\s+/g,''));
			// og skriv ut totalen samtidig som vi sørger for at den er disablet
			sum = addSpace(sum)
		//	$tot.val(sum);
			$tot.attr("disabled","disabled");

			// deretter forbereder vi et live scenario hvor total oppdateres onblur
			var cells = $j(this).find('td.c1 > input,td.c2 > input,td.c3 > input');
			cells.each(function (i,em) {
				var _cell = $j(em);
				_cell.blur(function() {
					var tall = _cell.val();
					tall = tall.replace(/\s+/g,'');
					var newtotal;
					if (!isNaN(tall)) {
						av = a.val();
						av = av.replace(/\s+/g,'');
						bv = b.val();
						bv = bv.replace(/\s+/g,'');
						cv = c.val();
						cv = cv.replace(/\s+/g,'');
						if (i == 0) {
							newtotal = parseInt(tall) +  parseInt(bv) + parseInt(cv);
						} else if (i == 1) {
							newtotal = parseInt(tall) +  parseInt(av) + parseInt(cv);						
						} else {
							newtotal = parseInt(tall) +  parseInt(av) + parseInt(bv);						
						}	
						newtotal = addSpace(newtotal);
						$tot.val(newtotal);
					}
				});			
			});		
		}
	});
	
});
function addSpace(str) {
	var newstr = str.toString();
	if (newstr.length > 3) {
		var len = newstr.length-3;
		newstr = newstr.substr(0,len) + ' ' + newstr.substr(len);		
		if (newstr.length > 7) {
			len = newstr.length-7;
			newstr = newstr.substr(0,len) + ' ' + newstr.substr(len);
		}
	}
	return newstr;
}

function checkForNumber(em) {
	$j(em).blur(function() {
		var ok = /^[0-9]+$/.test($j(em).val());
		if (!ok) {
			var newitem = $j(this).parent().append('<p class="nummer-feil">OBS: Bare tall her!</p>').hide();
			newitem.fadeIn('slow');
			$j(em).focus(function() {
				var olditem = $j(em).next("p.nummer-feil").fadeOut('slow');
				olditem.remove();
			});
		}
	});		

}

function rotatePics(currentPhoto) {

	var numberOfPhotos = $j('#slideshow .image').length;
	currentPhoto = currentPhoto % numberOfPhotos;
		
	$j('#slideshow .image').eq(currentPhoto).fadeOut(function() {
		// re-order the z-index
		$j('#slideshow .image').each(function(i) {
			$j(this).css(
		        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
			);
		});
		$j(this).show();
		setTimeout(function() {rotatePics(++currentPhoto);}, 5000);
	});
}

