/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 lyarkitekter.no forwarder 2011
 website by eDIR/ Atelier Espen Schjelderup © All Rights Reserved 2011-2111

 scripts initialization

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 document ready functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
$(function () {
	/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 add target="_blank" to relevant links while maintaining xhtml validity
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$("a").each(function() {
		if( $.attr($(this).get(0),'rel') == "ext" ) {
			$(this).attr("target", "_blank");
		}
	});

	/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 credits pulse
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	$("#credits a").hover(function () {
		$(this).css("background-position", '0 -25px');
		var $fade = $(this);
		if( $fade.is(':animated') ) {
			$fade.stop().fadeTo(200, '1.0');
		}
		else {
			$(this).fadeTo(200, '1.0');
		}
	}, function () {
		$(this).css("background-position", '0 -25px');
		var $fade = $(this);
		if( $fade.is(':animated') ){
			$fade.stop().fadeTo(600,'0.2');
		}
		else {
			$(this).fadeTo(600, '0.2');
		}
	});

	/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 distribute contact-columns across available space
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
	fit();

	function fit() {
		var $f_box = $('#forwarder_field');
		var $p_box = $('.pb');
		var fb_w_total = $f_box.outerWidth();
		var pb_w_total = 0;
		var count = 0;
		var divide = 0;


		$p_box.each(function(i, e) {
			$(e).css('margin-right', ''); // important reset
			margin_r = parseInt($(e).css('margin-right'));
			pb_w = $(e).width();
			pb_w_total = pb_w_total + pb_w + margin_r;
			count = i;
		});

		divide = (fb_w_total - pb_w_total) / count;

		divide = parseInt(divide) - 10;

		$p_box.each(function(i, e) {
			if( i < count && divide >= 0 ) {
				$(e).css('margin-right', divide);
			}
		});
	}
});
