//------------------------------------------------------------------------------------------------
// INIT
//------------------------------------------------------------------------------------------------
function initFeatured(containerId, data, target) {
	if (data == null || data == undefined) data = {};
	if (target == null || target == undefined) target = '#featured_UL';
	
	$('#'+containerId).mouseenter(function(){
		stopTimer(containerId);
	}).mouseleave(function(){
		startTimer(containerId);
	});
	
	var tabs = $('#'+containerId+' div.featured-nav li');
	tabs.click(function(){
		tabs.removeClass('active');
		this.className += 'active';
		var category = this.id.substring(this.id.indexOf(':')+1);
		if (data[category]) {
			featuredPopulate(data[category], target);
		} else {
			fetchData(data, category, target);
		}
	});

	startTimer(containerId);
}

function startTimer(containerId) {
	$('#'+containerId).eq(0).data('timerId', setInterval(function(){ nextTab(containerId); }, 15000));
}

function stopTimer(containerId) {
	clearInterval($('#'+containerId).eq(0).data('timerId'));
}

function nextTab(containerId) {
	var currentTab = $('#'+containerId+' div.featured-nav li.active');

	if (currentTab.is(':last-child')) {
		$('#'+containerId+' div.featured-nav li:first').triggerHandler('click');
	} else {
		currentTab.next().triggerHandler('click');
	}
}

//------------------------------------------------------------------------------------------------
// AJAX POPULATE
//------------------------------------------------------------------------------------------------

function fetchData(data, category, target) {
	$.ajax({
		url: '/v2/tiles/featured/type/'+category,
		cache: false,
		dataType: 'json',
		success: function(returnData) {
			data[category] = returnData[category];
			featuredPopulate(data[category], target);
		}
	});
}

function featuredPopulate (data, target) {
	data.push({}); /* empty object added so I can wait for iteration complete */
	$.each(data, function(i, val){
		if (i == data.length-1) {
				slideUL_left(target);
				return false;
		}
		addProfileUnit(target);
		$(target).children('li:last').find('img').attr("src",val.thumbnail).attr("height",val.height).attr("width",val.width).css({"background-image":"url(images/waiting.gif)"}).parent().attr("href",val.url);
		$(target).children('li:last').find('.name a').text(val.name).attr("href",val.url);
		$(target).children('li:last').find('.profileSummary a').text(val.profileSummary).attr("href",val.url);
	});
	data.pop();
}


//------------------------------------------------------------------------------------------------
// MANIPULATE FEATURED AREA
//------------------------------------------------------------------------------------------------

function addProfileUnit(target) {
	var newListItem = $(target).children('li:last').clone();
	$(target).append($(newListItem).clone());
	var numItems = $(target).children().length;
	var ulWidth = numItems * ($(target).children('li:last').outerWidth(true));
	$(target).css({"width":ulWidth});
}

function slideUL_left(target) {
	var delta = 3 * $(target).children().outerWidth(true);
	$(target).parent().andSelf().css({"position":"relative"});
	$(target).animate({"left": -delta}, 800, "easeInOutQuint", function(){
		removeOld_Items(target);
	});
}

function removeOld_Items(target) {
	$(target).children().slice(0,3).remove();
	$(target).css({"left":0});
}
