function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

function email($this) {
	var name   = $this.text();
	var domain = 'hellyhansen.no';
	var mail   = name.toLowerCase().replace(' ','.')+'@'+domain;
	
	$this.html('<a href="mailto:'+mail+'">'+name+'</a>');
}

//document.onkeypress = stopRKey; 
//document.write('<style>#filter input{position: absolute;left: -10000px;}</style>');

$(document).ready(function() {
	//tooltip();
	$('#error').hide();
	
	clearInput();
	
	$('span.mailto').each(function(){
		email($(this));
	});
	
	// emailsignup
	$('a.emailsignup').nyroModal({
		closeButton: '<a href="#" class="nyroModalClose closeLang" id="closeBut" title="close">Close</a>',
		type: 'form',
		minHeight: null,
		endShowContent: function(elts, settings){
			clearInput();
		}
	});
		
	// Language selector
	$('.country a').nyroModal({
		closeButton: '<a href="#" class="nyroModalClose closeLang" id="closeBut" title="close">Close</a>',
		endShowContent: function(elts, settings){
			$('a.country-link').click(function(){
				var $this = $(this);
				var href  = $this.attr('href');
				
				$.ajax({
					url: href,
					dataType: 'json',
					beforeSend: function(){
						$('#language ul').html('');
					},
					success: function(data) {
						$('#country a.country-link').removeClass('active');
						$this.addClass('active');
						$('.step1').removeClass('active');
						$('.step2').addClass('active');
						if(data.length >0) {
							$(data).each(function(i){
								$('#language ul').append('<li><a href="/language_selector/select/cid:' + data[i]['language_selector']['country_id'] + '/lid:' + data[i]['languages']['id'] + '">' + data[i]['languages']['native_name'] + '</a></li>');
							});
						} else {
							$('#language ul').append('<li><a href="/language_selector/select">Coming soon</a></li>');
						}
					}
				});
				return false;
			});
		}
	});

});

$(window).load(function(){
	containerHeight();
});

$(window).resize(function(){
	containerHeight();
});

function containerHeight() {

	var wHeight = $('#wrapper').height();
	var dHeight = $(document).height();

	if(wHeight < dHeight) {
		var cHeight = dHeight - ($('#container').offset().top + ($('#footer-wrapper').outerHeight()-14));
		$('#container').height(cHeight);
	}
}


function clearInput() {
	// Clear def value from input
	$("form:not(.notclear)").find("input[type='text']").each(function(){
		var defValue = $(this).val();
		if(defValue != '') {
			$(this).focus(function(){
				if($(this).val() == defValue) $(this).val('');
			}).blur(function(){
				if($(this).val() == '') $(this).val(defValue);
			});
		}
	});
}
