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

	var widgetID = "home-widget";
	var tabList;

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

	tabList = $('#home-marquee 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.panel-bg').attr('src');
			newDiv.append(
				$("<div>").append(
					$(this).clone().contents()
				).attr("id", $(this).attr("id"))
			 	.attr("class", $(this).attr("class"))
				.css('background', 'url(' + img + ') left bottom no-repeat')
			);
		});

		var newUl = $("<ul>");
		tabList.find('dt').each(function(i) {
			var a    = $(this).clone().contents();
			var href = $(this).next('dd').find('.tab-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);
		$('#home-marquee dl#' + widgetID).replaceWith(newDiv);

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

