$(document).ready( function() {

	$('.bomTable .pno.wpic').live( 'click', function() {					
			var img = Omega.imageForPart( $(this).text() );
			if( img ) {
				var title = $(this).text() + ": " + $(this).siblings('.partDesc').text();
				
				
				// don't display the dialog until the image is loaded,
				// as this can throw off the vertical centering. if this proves
				// unreliable we could just do $('#popup').dialog( {position:[null,50]...
				$('<img src="' + img + '"/>').load( function() {
					$('#popup').html( this );
					$('#popup').dialog( { modal:true,width:650,title:title } );
				} );
				
			//	$('#popup').html('<img src="' + img + '"/>');
			//	$('#popup').dialog( { modal:true,width:650,title:title } );
			}
		}
	);
	
});

$(window).load( function() {

	if( $('#fixedHeadings').length == 0 ) {
		return;
	}

	// set up fixed table headings
	var original = $('#originalBom .bomTable thead:first');
	var copy = original.clone();
	copy.appendTo('#fixedHeadings');
//	copy = $('#fixedHeadings .bomTable thead:first');
	$('#fixedHeadings').width( original.width() );
	syncWidths( original, copy );
	
	// set up fixed left column
	$('#originalBom .bomTable').clone().appendTo( '#fixedFirstColumn' );
	$('#fixedFirstColumn').width( $('#originalBom .bomTable td:first').width() );
} );

$(window).scroll( function() {
	if( $('#fixedHeadings').length == 0 ) {
		return;
	}
	$('#fixedHeadings').css( 'left', -( $( window ).scrollLeft() ) + "px" );
	
	var original = $('.bomTable thead:first');
	if( original.position().top - $(window).scrollTop() < 0 ) {
		$('#fixedHeadings').show();
	}
	else {
		$('#fixedHeadings').hide();
	}
	
	if( $(window).scrollLeft() > 0 ) {
		$('#fixedFirstColumn').css( 'top', original.position().top - $(window).scrollTop() );
		$('#fixedFirstColumn').show( 200 );
	}
	else {
		$('#fixedFirstColumn').hide( 200 );
	}
	
} );

function syncWidths( src, dest ) {
	src.find( 'td' ).each( function( index, elem ) {
		// grr.. ie7.
		var newWidth = $.browser.msie && $.browser.version.charAt(0) == '7' ? ($(elem).outerWidth() - 1) : $(elem).width();
		$( '#fixedHeadings td:eq(' + (index) + ')' ).width( newWidth );
	} );
	
}

