var JB = JB || {};

JB._Defaults = JB._Defaults || {};
JB.Facets = JB.Facets || {};
JB.Home = JB.Home || {};
JB.Util = JB.Util || {};

JB._Defaults.INITIAL_FACETS_TO_DISPLAY = 5

JB.Facets.getInitialFacetsToDisplay = function() {
	return (
		JB._Defaults.INITIAL_FACETS_TO_DISPLAY
	);
}

JB.Facets.doDisplayFacets = function() {
	var _num_facets = JB.Facets.getInitialFacetsToDisplay();
	jQuery('.facet-options').each(function() {
		if (jQuery(this).children('.facet-option-link').length > _num_facets) {
			var _parent_id = jQuery(this).attr('id')
			jQuery(this)
				.children('.facet-option-link:gt(' + eval(_num_facets-1) + ')').css({
					'display': 'none',
					'visibility': 'hidden'
				});
			jQuery(this).children('.facet-option-link-less').css({
				'display': 'none',
				'visibility': 'hidden'
			});
			jQuery(this).children('.facet-option-link-more').css({
				'display': 'block',
				'visibility': 'visible'
			});
		}
	});
}

JB.Facets.doShowFacets = function() {
	var _parent_id = jQuery(this).parent().parent().attr('id');
	var _facet = '#' + jQuery(this).parent().parent().attr('id').replace('facet-option-link-more-','');
	jQuery(_facet).children('.facet-option-link').css({
		'display': 'block',
		'visibility': 'visible'
	});
	jQuery(_facet).children('.facet-option-link-less').css({
		'display': 'block',
		'visibility': 'visible'
	});
	jQuery(_facet).children('.facet-option-link-more').css({
		'display': 'none',
		'visibility': 'visible'
	});
	return false;
}

JB.Facets.doHideFacets = function() {
	var _num_facets = JB.Facets.getInitialFacetsToDisplay();
	var _parent_id = jQuery(this).parent().parent().attr('id');
	var _facet = '#' + jQuery(this).parent().parent().attr('id').replace('facet-option-link-less-','');
	jQuery(_facet).children('.facet-option-link:gt(' + eval(_num_facets-1) + ')').css({
		'display': 'none',
		'visibility': 'hidden'
	});
	jQuery(_facet).children('.facet-option-link-less').css({
		'display': 'none',
		'visibility': 'hidden'
	});
	jQuery(_facet).children('.facet-option-link-more').css({
		'display': 'block',
		'visibility': 'visible'
	});
	return false;
}

JB.Facets.doResizeColumns = function() {
	var _facetsColumn = jQuery('#search-facets');
	var _resultsColumn = jQuery('#search-results');
	if (_facetsColumn && _resultsColumn) {
		if (_facetsColumn.height() > _resultsColumn.height()) {
			_resultsColumn.height(_facetsColumn.height());
		} else {
			_facetsColumn.height(_resultsColumn.height());
		}
	}
}

JB.Home.doResizeColumns = function() {
	var _mainColumn = jQuery('#bd>#yui-main');
	var _sideColumn = jQuery('#bd>.yui-b');
	if (_mainColumn && _sideColumn) {
		// reset explicitly-set column heights
		_mainColumn.css({'height': 'auto'});
		_sideColumn.css({'height': 'auto'});
		if (_mainColumn.height() > _sideColumn.height()) {
			_sideColumn.height(_mainColumn.height());
			_sideColumn.css({
				'background': '#f0eeea'
			});
		} else {
			_mainColumn.height(_sideColumn.height());
		}
	}
}

JB.Home.doShowBannerClose = function() {
	jQuery(this)
		.css({'background': '#333 url(/images/close_banner.gif) no-repeat 99% 3%'})
		.fadeTo('slow', '0.4');
}

JB.Util.doConvertRGBtoHex = function(_rgb) {
	_rgb = _rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  return "#" + JB.Util.doHex(_rgb[1]) + JB.Util.doHex(_rgb[2]) + JB.Util.doHex(_rgb[3]);
}

JB.Util.doHex = function(_x) {
	var hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
	return isNaN(_x) ? "00" : hexDigits[(_x - _x % 16) / 16] + hexDigits[_x % 16];
}

JB.Home.doHideBannerClose = function() {
	jQuery(this).css({
		'background': 'none'
	});
}

JB.Util.doRoundCorners = function() {
	// nav bar tabs
	jQuery('html:not(.msie6) #NavBar li.current a').corner('round tl tr 8px');
	
	// chooser
	jQuery('html:not(.msie6) ul.chooser li span').corner('8px');

	/*
	// text, password, and file fields; buttons
	jQuery('input[type=text]').corner('6px');
	jQuery('input[type=password]').corner('6px');
	jQuery('input[type=file]').corner('6px');
	jQuery('input[type=button]').corner('6px');
	jQuery('input[type=submit]').corner('6px');
	jQuery('button').corner('6px');

	// tags
	jQuery('ul.tags li a').corner('6px');

	// links acting as buttons
	jQuery('.action_button a').corner('6px');
	jQuery('.content-add-button').corner('6px');

	// calendar widget
	jQuery('.ui-corner-all').corner('0px');
*/
}

JB.Util.doFixPNGTransparency = function() {
	jQuery('.msie6 .result-marker img').each(function() {
		jQuery(this).supersleight({shim: '/images/transparent.gif'});
	});
}

jQuery(document).ready(function() {
	JB.Util.doRoundCorners();
	JB.Home.doResizeColumns();
	JB.Facets.doDisplayFacets();
	JB.Util.doFixPNGTransparency();
	jQuery('.facet-option-link-more a').click(JB.Facets.doShowFacets);
	jQuery('.facet-option-link-less a').click(JB.Facets.doHideFacets);
});