jQuery(document).ready(function($){

	var widgetID = "which-configuration-widget";
	var tabList;

	tabList = $('#secondary-content dl#' + widgetID);
	if ( tabList.length > 0 ) {
    		tabList.accordion( { collapsible: true, active: false });
	}

	tabList = $('#sidebar dl#' + widgetID);
	if ( tabList.length > 0 ) {
    		tabList.accordion( { collapsible: true, active: false });
	}

	tabList = $('#main-content dl#' + widgetID);
	if ( tabList.length > 0 ) {
		var newDiv  = $("<div>").attr("id", tabList.attr("id"));

		tabList.find('dd').each(function(i) {
			var img = $(this).find('img').first().attr('src');
			newDiv.append(
				$("<div>").append(
					$(this).clone().contents()
				).attr("id", $(this).attr("id"))
			 	.attr("class", $(this).attr("class"))
				.css('background', 'url(' + img + ') 10px center no-repeat')
			);
		});

		var newUl = $("<ul>");
		tabList.find('dt').each(function(i) {
			var a    = $(this).clone().contents();
			var href = $(this).next('dd').find('.learn-more-link a').first().attr('href');
			var tab  = $(this).next('dd').attr('id');

			$(a).hoverIntent({
				'over' : function(e){
					var prev_fx = $(newDiv).tabs('option', 'fx');
					$(newDiv).tabs('option', 'fx', { 'opacity' : 'toggle', 'duration' : 'slow' });
					$(newDiv).tabs('select', tab);
					$(newDiv).tabs('option', 'fx', prev_fx);
				},
				'out'  : function(){},
				'interval' : 750
			});

			$(a).click(function(){
				if ( $(this).parents('.ui-tabs-selected').length > 0 ) {
					location.href = href;
				}
			});

			newUl.append( $("<li>").append(a) );
		});

		newDiv.append(newUl);
		$('#main-content dl#' + widgetID).replaceWith(newDiv);

		newDiv.tabs( {cookie: {expires: (1 / 12)} } );
	}
});

