Math._round = Math.round;
Math.round = function(number, precision)
{
	precision = Math.abs(parseInt(precision)) || 0;
	var coefficient = Math.pow(10, precision);
	return Math._round(number*coefficient)/coefficient;
}
$(document).ready(function(){
	
	$('.team-img').hover(function() {
		$('#col1').find('.team-text').remove();
		var obj = $(this);
		var txt = obj.find('.team-text').clone().show();
		$('#col1 > div').hide();
		$('#col1').append(txt);
	},
	function() {
		/*needs to be here because hover needs to functions to be implemented*/
	});
	
	/* 
	 * Only relevant for photopress 
	 */
	function imageResizeNoSkew() {
		var dh =  $(document).height();
		var dw = $(document).width();
		var wh = $(window).height();
		var ih =  $('#bg').attr("height");
		var iw =  $('#bg').attr("width");
		
		var iratio = ih/iw;
		var wratio = dh/dw;
		
		if (Math.round(wratio, 5) > iratio ){
			$('#bg').height(dh);
			$('#bg').width('auto');
		} else if (Math.round(wratio, 5) < iratio ) {
			$('#bg').height('auto');
			$('#bg').width('100%');
		} else {
			$('#bg').height(dh);
			$('#bg').width(dw);
		}
		
		if($('#outerwrapper').height() >= wh) {
			$('#scrollfix').css({'height': ($('#outerwrapper').height()+80) }).addClass('scrollfixer');
			$('html').css('overflow-y','scroll');
		} else {
			$('html').css('overflow-y','hidden');
			$('#scrollfix').removeAttr('style').removeClass('scrollfixer');
		}
	}
	
	console.log($('#outerwrapper').length);
	
	/* 
	 * Only relevant for photopress 
	 * outerwrapper only exists in photopress
	 */
	if($('#outerwrapper').length) {
		//console.log('trigger1');
		imageResizeNoSkew();
		
		$(window).resize(function() {
			imageResizeNoSkew();
		});
	}
	
	$("#newsletter").formvalidate();

	// Align right column with left column content.
	if ($('#col2 .text-box').length) {
		var titleHeight = $('#pagetitle').height();
		$('#col2').css('margin-top', titleHeight + 'px')
	}
});

/**
 * Formvalidation
 */
(function($) {
	$.fn.formvalidate = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			$this.submit(function() {

				var valid = true;
				
				$this.find('.notempty').each(function() {
					if ($.trim($(this).val()) == '') {
						$(this).addClass('error');
						valid = false;
					}
					else {
						$(this).removeClass('error');
					}
				});
				
				$this.find('.email').each(function() {
					if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($(this).val())) {
						$(this).removeClass('error');
					}
					else {
						$(this).addClass('error');
						valid = false;
					}
				});
				
				if (!valid) {
					return false;
				}
			})
		});
	}
}) (jQuery);

