if (!ndp) ndp = {} // TO AVOID NAMING CONFLICTS

// CALLED ONCE THE PAGE HAS LOADED
$(document).ready(function(){
	if ($('body').attr('id') == 'page-detail') {
		ndp.checkLoadedTimer = window.setInterval('ndp.checkLoaded()', 250);
	}
});

// WAITS UNTIL THE VIEWPORT HAS BEEN LOADED
ndp.checkLoaded = function() {
	if (viewerBean != null) {
		window.clearInterval(ndp.checkLoadedTimer);
		ndp.iniResizer();
	}
}

// SETS UP THE FRAME RESIZER BEHAVIOUR
ndp.iniResizer = function() {
	$('#container').append('<div id="resizer"></div><div id="resizer-guide"></div>');
	$('#resizer')
		.css('left', $('#column').width())
		.draggable({
			axis: 'x',
			containment: '#resizer-guide',
			start: function(e, ui) {},
			drag: function(e, ui) {
				ndp.resizeFrames();
			},
			stop: function(e, ui) {
				ndp.redrawViewport();
			}
		}).hover(function() {
			$(this).addClass('active');
		}, function() {
			$(this).removeClass('active');
		});
	ndp.redrawViewport(ndp.findWidth());
	// HIDE/SHOW BUTTON
	$('#resizer').append('<p id="hidecol"><a href="#" title="show/hide the left column">Show/hide</a></p>');
	$('#hidecol').toggle(function() {
		$(this).addClass('hidden');
		var left = parseInt($('#resizer').css('left'))
		if (left < ndp.minWidth) left = ndp.minWidth; // TO STOP PEOPLE SHRINKING IT ACCIDENTALLY AND GETTING CONFUSED
		ndp.lastWidth = left;
		ndp.redrawViewport(1);
	}, function() {
		$(this).removeClass('hidden');
		ndp.redrawViewport(ndp.lastWidth);
	});
}

// SORTS NUMBERS DESCENDING
ndp.sortDesc = function(a, b) {
	return b-a;
}

// CALCULATES THE DEFAULT WIDTH FOR THE LEFT COLUMN //TODO make more efficient
ndp.findWidth = function() {
	ndp.minWidth = 310; // THE MINIMUM DEFAULT WIDTH
	var maxWidth = $('html').width() / 2; // THE MAX DEFAULT OCR SHOULD NEVER BEEN MORE THAN HALF THE WIDTH OF THE SCREEN
	var offset = 62; // FOR SCROLLBARS, PADDING AND A LITTLE OFFSET FOR REMOVED LINES

//var ts = new Date().getTime(); 

	$('.ocr').addClass('check-width');
/*	var lineWidths = [];
	$('.ocr').addClass('check-width');
	// CALCULATES THE LONGEST LINE
	$('.ocr p span').each(function(){
		lineWidths.push($(this).width());
	});
	lineWidths.sort(ndp.sortDesc);
	var trimThreshold = lineWidths.length * 0.02; // IGNORES THE TOP FEW LINES, TO AVOID BLOWING OUT THE LAYOUT FOR ONE OR TWO REALLY LONG LINES
	if (trimThreshold < 2) trimThreshold = 2; else if (trimThreshold > 10) trimThreshold = 10;
	for (i=0; i<trimThreshold; i++) {
		lineWidths.shift();
	}
	var articleWidth = lineWidths[0] + offset;
*/
	// kkf 27nov09 - not storing and sorting and only looking at first 50 saves 0.5sec load time on IE for medium article
	var maxlinewidth = 100 ;

	$('.ocr p span').each(function(c){
		var xx = $(this).width();
		if (xx > maxlinewidth) maxlinewidth = xx ;
		if (c > 50) return false;
	});
	var articleWidth = maxlinewidth + offset;

	// CHECKS MIN/MAX VALUES
	$('.ocr').removeClass('check-width');
	if (articleWidth > maxWidth) {
		articleWidth = articleWidth/2 + offset/2; // IF THE LENGTH EXCEEDS MAXIMUM WIDTH, IT TRIES TO USE HALF THE LINE LENGTH FOR BETTER WRAPPING
		if (articleWidth > maxWidth) articleWidth = maxWidth; // IF IT'S STILL TOO LONG, CROP IT
	}
	if (!$('body').hasClass('article') || articleWidth < ndp.minWidth) articleWidth = ndp.minWidth; // MINIMUM WIDTH ALSO FOR PAGE VIEW
//var elap = new Date().getTime() - ts ;
//alert(elap) ;
	return articleWidth;
}

// REDRAWS THE VIEWPORT
ndp.redrawViewport = function(x) {
	ndp.resizeFrames(x);
	viewerBean.k1pos = findPos(document.getElementById('viewer')); // OVERRIDES THE POSITION
	reinitializeGraphic();
	if ($(".ocr.power-mode").length>0) {
		//$(".ocr-input .input").triggerHandler("change");
		$.each($(".ocr-input textarea"), 
			function() {
				refreshTextAreaSize(this);				
			}
		);
	}
}

// RESIZES THE FRAMES
ndp.resizeFrames = function(x) {
	if (x) $('#resizer').css('left', x); // SET A DEFAULT
	var left = parseInt($('#resizer').css('left'));
	var windowW = $('html').width();
	var resizerW = $('#resizer').width();
	var contentW = windowW - left - resizerW - 2;
	var buttonW = $('.scroll-y .down').width();
	var minShrink = 250; // THE MINIMUM VALUE TO SHRINK BEFORE IT STARTS TO PUSH OFF THE SCREEN
	// RESIZE/REPOSITION THE COLUMN AND CONTENT
	if (left < minShrink) {
		$('#column').width(minShrink - 20); // 20 FOR PADDING
		$('#column').css('left', left - minShrink);
	} else {
		$('#column').width(left - 20); // 20 FOR PADDING
		$('#column').css('left', 0);
	}
	$('#content').css('left', left + resizerW);
	// RESIZE THE VIEWPORT
	$('#viewer, .scroll-x').width(contentW);
	$('.scroll-x .slider').width(contentW - buttonW);
	viewerBean.width = contentW; // OVERRIDES THE WIDTH
//alert("resizer height="+$('#resizer').height() + " column height=" + $('#column').height() + " viewrht="+$('#viewer').height()) ;
var kfx = $('#viewer').height() ;
if ($('#column').height() > kfx) $('#column').height(kfx-30) ;
if ($('#resizer').height() > kfx) $('#resizer').height(kfx-30) ;
//alert("resizer height="+$('#resizer').height() + " column height=" + $('#column').height() + " viewrht="+$('#viewer').height()) ;


}

