
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(".entitycomment-form-content #name").focus(function() {
		if (this.value == "Ditt navn") {
			this.value = "";
		}
	});
	$j(".entitycomment-form-content #name").blur(function() {
		if (this.value == "") {
			this.value = "Ditt navn";
		}
	});
	
	$j(".entitycomment-form-content #email").focus(function() {
		if (this.value == "Din e-postadresse") {
			this.value = "";
		}
	});
	$j(".entitycomment-form-content #email").blur(function() {
		if (this.value == "") {
			this.value = "Din e-postadresse";
		}
	});

	$j(".entitycomment-form-content #text").focus(function() {
		if (this.value == "Skriv din kommentar her") {
			this.value = "";
		}
	});
	$j(".entitycomment-form-content #text").blur(function() {
		if (this.value == "") {
			this.value = "Skriv din kommentar her";
		}
	});
	
	$j(".entitycomment-form-content #captcha").focus(function() {
		if (this.value == "Sikkerhetskode") {
			this.value = "";
		}
	});
	$j(".entitycomment-form-content #captcha").blur(function() {
		if (this.value == "") {
			this.value = "Sikkerhetskode";
		}
	});

	/* ĝl-/brusvelger forsida */
	$j(".frontpage .beerfinder #focusheaderid0").click(function() {
		// vis ĝlvelger ved klikk 
		$j("#brusvelger").hide();
		$j("#olvelger").show();
		$j(".frontpage .beerfinder #focusheaderid1").removeClass('active');
		$j(this).addClass('active');
	});
	
	$j(".frontpage .beerfinder #focusheaderid1").click(function() {
		// vis brusvelger 
		$j("#olvelger").hide();
		$j("#brusvelger").show();
		$j(".frontpage .beerfinder #focusheaderid0").removeClass('active');
		$j(this).addClass('active');
	});

	$j(".frontpage .beerfinder .tile-content ul li").each(function() {				

		// vis eller skjul ul-liste ved klikk 
		var ul = $j(this).find('ul');
		$j(this).click(function() {
			ul.toggle();
		});			
		
		// skjul ul-liste ved mouseleave
		ul.mouseleave(function() {		
			if (ul.css('display') == 'block') {
				$j(ul).css('display', 'none');
			}
		});		
	});
	
	$j("ul.produktliste").each(function() {
		//$j(this).find("li a").equalHeights();
		setEqualHeight($j(this).find("h3"));		
		//setEqualHeight($j(this).find("li a"));
	});

	$j(".bruslist ul").each(function() {
		setEqualHeight($j(this).find("h3"));
		//setEqualHeight($j(this).find("li"));
	});

	/* oppskrift velger */
	$j(".oppskrift-finder .dropdown ul li").each(function() {
		// vis eller skjul ul-liste ved klikk 
		var ul = $j(this).find('ul');
		$j(this).click(function() {
			ul.toggle();
		});			
		
		// skjul ul-liste ved mouseleave
		ul.mouseleave(function() {		
			if (ul.css('display') == 'block') {
				$j(ul).css('display', 'none');
			}
		});		
	});

	/* statistikk velger */
	$j(".statfinder ul li").each(function() {
		// vis eller skjul ul-liste ved klikk 
		var ul = $j(this).find('ul');
		$j(this).click(function() {
			ul.toggle();
		});			
		
		// skjul ul-liste ved mouseleave
		ul.mouseleave(function() {		
			if (ul.css('display') == 'block') {
				$j(ul).css('display', 'none');
			}
		});		
	});

	/* Ansatte toggle Les mer */
	$j(".ansatte .les-mer a").each(function() {
		// vis eller skjul ansattes Les mer ved klikk 
		var lesmer = $j(this).parents('li').find('div.ansatt-skjult');
		$j(this).click(function(e) {
			e.preventDefault()
			lesmer.toggle();
		});			
	});			
			
	/* Validering */
	$j("table.validering input:text").each(function() {
		checkForNumber($j(this));
	});
	
});
function setEqualHeight(columns) {
	var tallestcolumn = 0;
	columns.each(function() {
		currentHeight = $j(this).height();
		if(currentHeight > tallestcolumn) {
			tallestcolumn  = currentHeight;
		}
	});
	columns.height(tallestcolumn);
 }

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $j(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

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);
	});
}

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$j(this).each(function(){
		var currentTallest = 0;
		$j(this).children().each(function(i){
			if ($j(this).height() > currentTallest) { currentTallest = $j(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($j.browser.msie && $j.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$j(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};