var zone_adjust = 0;
var currency_convertor_url = '';

function preload_images(images, pointer, callback) {
	if (images.length > pointer) {
		$('<img />').load(function() {
			preload_images(images, pointer + 1, callback);
		}).attr('src', images[pointer]);
	} else {
		callback();
	}
}

function update_oslo_time() {
	var localDate = new Date();
	var localTime = localDate.getTime();
	var localOffset = localDate.getTimezoneOffset() * 60000;
	var utcTime = localTime + localOffset;
	var osloTime = new Date(utcTime + zone_adjust * 3600000);
	var hours = osloTime.getHours();
	var minutes = osloTime.getMinutes();
	$("#oslo_time").text(((hours > 9) ? '' : '0') + hours + ':' + ((minutes > 9) ? '' : '0') + minutes);
	setTimeout(update_oslo_time, 30000);
}

function convert_currency_to_nok() {
	$.getJSON(currency_convertor_url, {currency:$("#to_cur").val(),amount:$("#from_amount").val()}, function(data) {
		$("#conv_amount").val(data.result);
	});
}

function mk_t1_article_columns() {
	if (!$('#article').hasClass('template_normal') || $('#col1').text() != '') return;
	var side_bar_height = $('#rightsidebar').height();
	var article_header_height = $('#article_top').height();
	var min_body_height = Math.max(0, side_bar_height - article_header_height);
	var min_column_height = 14 * Math.floor(min_body_height / 14);
	var body_full_height = $('#col0').height();
	var total_lines = body_full_height / 14;
	var column_min_lines = Math.ceil(total_lines / 2);
	var column_min_height = column_min_lines * 14;
	var col1_height;
	if (column_min_height <= min_column_height) {
		var col1_height = min_column_height;
	} else {
		var col1_height = column_min_height;
	}
	$('#col1').height(col1_height);
	$('#col2').css('margin-top', -col1_height);
	$('#col1, #col2').html($('#col0').html());
}

function mk_t5_article_columns() {
	if (!$('#article').hasClass('template_glossary')) return;
	var side_bar_height = $('#rightsidebar').height();
	var article_header_height = $('#article_top').height();
	var min_body_height = Math.max(0, side_bar_height - article_header_height);
	var col0 = $('#col0');
	var body_full_height = col0.height();
	col0.addClass('inv');
	var col1_height = 0;
	var add_to_col1 = true;
	$('#col0 .block').each(function() {
		if (add_to_col1) {
			var col1_new_height = col1_height + $(this).height();
			if (col1_new_height > min_body_height) {
				if (col1_new_height <= body_full_height - col1_new_height) {
					// add to 1st column
					col1_height = col1_new_height;  
				} else {
					// rest goes to column 2
					add_to_col1 = false;
				}
			} else {
				// add to 1st column
				col1_height = col1_new_height;  
			}
		}
	});
	col0.removeClass('inv');
	$('#col1').height(col1_height);
	$('#col2').css('margin-top', -col1_height);
	$('#col1, #col2').html($('#col0').html());
}

function adjust_content_height() {
	var side_bar_height = $('#rightsidebar').height();
	var article_height = $('#article').height();
	if (article_height < side_bar_height) {
		$('#article').height(side_bar_height);
	}
}

var lock = false;
$(function() {
	if (lock) return;
	lock = true;
	update_oslo_time();
	$("#to_cur").change(convert_currency_to_nok);
	$('#from_amount').keypress(function(event) {
		if (event.keyCode == '13') {
			convert_currency_to_nok();
		}
	});
	mk_t1_article_columns();
	mk_t5_article_columns();
	adjust_content_height();
});
