function is_numeric (mixed_var) {
	if (mixed_var === '') {
	    return false;
	}

	return !isNaN(mixed_var * 1);
}

function stripslashes (str) {
    return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':
                return '\0';
            case '':
                return '';
            default:
                return n1;
        }
    });
}

/* ------------------------------------------------------------------------------------------*/

function bringBackBlinky(na) {
	if (swi == 1) {
		sho='visible';
		swi=0;
	}
	else {
		sho='hidden';
		swi=1;
	}

	for(i=0;i<na.length;i++) {
		na[i].style.visibility=sho;
	}

	setTimeout('bringBackBlinky(na)', spe);
}

/* ------------------------------------------------------------------------------------------*/

function show_button(id) {
	$(id).css('visibility', 'visible');
    return;
}

function hide(id) {
	var $element = document.getElementById(id);
	if (!$element) {return;}
	var $display = $element.style.display;
	if ( !$display ) {return;}
	if ( $display == 'none' ) {
		$display = 'block';
	} else {
		$display = 'none';
	}
   	$element.style.display = $display;
    return;
}

/* ------------------------------------------------------------------------------------------*/

function blg_get_shrink_height(id, id_top, maxHeight) {
	var ww = $(window).width()-32;
	var tw = $(id).width();
	var wh = $(window).height();
	var tt = $(id_top).position().top;
	var e = (tw>ww) ? 20 : 0;
	var h =  Math.min(wh-tt-e, maxHeight);

    return h;
}

function shrink_height(id, maxHeight, id_top, pad_bottom, width) {
	var $target = $(id);
	if ( !$target || !$target.length) {return;}
//	var tw = $target.width();
	var wh = $(window).height();
	var tt = $(id_top).position().top;
	var e = (is_numeric(pad_bottom)) ? pad_bottom : $(pad_bottom).height();
	var h =  Math.min(wh-tt-e, maxHeight);
	$target.height(h);
	if ( width ) {$target.width(width);}
//alert(h);
	return true;
}

function browser_adjust_height(id, maxHeight, id_top, id_footer) {
//	var ww = $(window).width()-32;
//	var tw = $(id).width();
	var wh = $(window).height();
	var tt = $(id_top).position().top;
	var e = 0;
	var $footer = $('#' + id_footer);
	if ($footer.length) {
//		e += $footer.height() + 2;;
e += 2;
    }
	var $menuho = $('#menuho');
	if ( $menuho.length ) {
		e += $menuho.height();
	}
	var h =  Math.min(wh-tt-e, maxHeight);
	$(id).height(h);
}

function scrollable_resize(id, id_top, table_height, delta) {
	var wh = $(window).height();
	var $top = $(id_top);
	var tt = $top.position().top;
	var tl = $top.position().left;
	var ww = $(window).width();

	var h =  Math.min(wh-tt, table_height);
	if(delta) {h -= delta;}
    var w = ww - tl;
	var $target = $('table'+id);
	$target.scrollable({tableWidth:w, tableHeight:h});
//alert( 'w=' + w + ', h=' + h);
    return;
}

function tablescroll_resize(id, id_top, table_height, delta) {
	var wh = $(window).height();
	var $top = $(id_top);
	var tt = $top.position().top;
	var tl = $top.position().left;
	var ww = $(window).width();

	var h =  Math.min(wh-tt, table_height);
	if(delta) {h -= delta;}
    var w = ww - tl;
	var $target = $(id);
	//$target.tableScroll({width:w, height:h});
//	$('tablescroll_wrapper').css('height',h+'px').css('width', w+'px');
	$target.css('height',h+'px').css('width', w+'px');
//alert( 'w=' + w + ', h=' + h);
    return true;
}

function tablesorter_end(sort_headers, id, sort_list, max_height, id_top, delta) {
	var $target = $('table'+id);
	$target.tablesorter({
//		widthFixed: true,
		widgets: ['zebra']
	});
	if ( sort_list !== null) {
		$target.tablesorter({sortList: sort_list});
	}
	if ( sort_headers !== null ) {
//		$target.tablesorter({headers: stripslashes(sort_headers) });
//		$target.tablesorter({headers: sort_headers });
//		$target.tablesorter()['headers'] =  set_headers(sort_headers);
	}
	if ( $.browser.mozilla && max_height) {
		var wh = $(window).height();
		var tt = $(id_top).position().top;
		var h =  Math.min(wh-tt, Math.min($target.height(), max_height));
		var $menuho = $('#menuho ul');
		if ( $menuho.length ) {
			h -= $menuho.height();
		} else {
			if (delta) {h -= delta;}
		}
		$target.scrollable({tableHeight:h});
	}
}

function set_table_sorter_height($target, id_top, max_height, delta) {
	if ( $.browser.mozilla && max_height) {
		var wh = $(window).height();
		var tt = $(id_top).position().top;
		var h =  Math.min(wh-tt, Math.min($target.height(), max_height));
		var $menuho = $('#menu_ho ul');
		if ( $menuho.length ) {
			h -= $menuho.height();
		} else {
			if (delta) {h -= delta;}
		}
		$target.scrollable({tableHeight:h});
	}

    return;
}

function set_table_scroll_height(target, id_top, max_height, delta) {
	if (max_height != 0 ) {
		var wh = $(window).height();
		var tt = $(id_top).position().top;
		var h =  Math.min(wh-tt, Math.min(target.height(), max_height));
		var menuho = $('#menu_ho ul');
		if ( menuho.length ) {
			h -= menuho.height();
		} else {
			if (delta) {h -= delta;}
		}
		h -= 27;
		target.tableScroll({height:h});//,width:'100%',flush:false});
	}
    return;
}

function set_headers($a) {
    var $h = new Object();
    for( i=0; i<$a.length; i++){
    	$arg = $a[i].split(/;/);
    	$obj = {};//new Object();
//    	$obj['sorter'] = "'"+$arg[1]+"'";
    	$obj.sorter = "'"+$arg[1]+"'";
    	$h["'"+$arg[0]+"'"] = $obj;
    }

    return $h;
}


function get_scrollable_table_height(id, max_height, dwidth, idtop, idfooter) {
	var ww = $(window).width()-32;
	var $table = $(id);
	var tw = $table.width();
	var th = $table.height();
	var wh = $(window).height();
	var tt = $(idtop).position().top;
	var e = $(id +' > thead').height();
    var $idfooter = $(idfooter);
	if ( $idfooter.length ) {
//		e += $idfooter.height();
e += 2;
	}
	var h =  Math.min(Math.min(wh-tt-e, max_height),th);
	if ( dwidth > 0 ) {
		$nav = $('#nav');
		if (th>h ) {if (dwidth) {h -= ($nav.length) ? $nav.height(): 30;}}
		if (dwidth) {tw = ww;}
	} else if ( dwidth < 0 ) {
		h = Math.max(h,-dwidth);
		dwidth = 0;
	}

    return [h, dwidth];
}

function scrollable_table_end(id, max_height, dwidth, idtop, idfooter) {
	var ww = $(window).width()-32;
	var $table = $(id);
	var tw = $table.width();
//	var [h, dwidth] = get_scrollable_table_height(id, max_height, dwidth, idtop, idfooter);
	var r = get_scrollable_table_height(id, max_height, dwidth, idtop, idfooter);
	var th = $table.height();
    h = r[0];
    dwidth = r[1];
	var pager = $('#pager');
	var pw = pager.width();
	if ( th > h ) {
		if ( pw > tw ) tw = pw;//$table.width(pw);
	dwidth=0;
	    if ( dwidth && (tw > ww - dwidth)) {
			$(id).fixedHeader({
				width:  Math.min(ww, tw + 32) - dwidth,
				height: h

			});
		} else {
			$(id).fixedHeader({
				height: Math.max(h,48)

			});
		}
	}
	$(id + ' tr').click(function() {
		$(id + ' tr.hightlighted').removeClass('hightlighted');
		$(this).addClass('hightlighted');
	});

    return;
}

