$(document).ready(function() {
	$('#commentBlock').insertBefore('#relatedWrapper');
	setShareBar();
});

var openRecentStories = new Array();
var closingRecentStories = 0;
var rsAnimating = false;
var rsTimer;
var rscTimer;

function startRSTimer(celeb_id, currentStory) {
	rsTimer = setTimeout('recentStories(' + celeb_id + ', ' + currentStory + ')', 1000);
}

function clearRSTimer() {
	clearTimeout(rsTimer);
}

function startRSCTimer() {
	rscTimer = setTimeout('closeAllRecentStories()', 1000);
}

function clearRSCTimer() {
	clearTimeout(rscTimer);
}


function recentStories(celeb_id, currentStory) {
	if (rsAnimating == false) {
		if ($('#recentStories_' + celeb_id).html() == '') {
			$.getJSON('/ajax_latest_stories_on_topic.php?cursid=' + currentStory + '&cid=' + celeb_id, function(data) {
				newHtml = '<h3>RECENT STORIES</h3>';
				for(i=0;i<data.length;i++) {
					newHtml = newHtml + '<p>&raquo;&nbsp;<a href="' + data[i][0] + '">' + data[i][1] + '</a></p>';
				}
				$('#recentStories_' + celeb_id).html(newHtml);	
				animateRecentStories(celeb_id);
			});
		}
		else {
				animateRecentStories(celeb_id);
		}		
	}
}

function animateRecentStories(celeb_id) {	
	for(i=openRecentStories.length;i>=0;i--) {
		if (openRecentStories[i] != celeb_id) {
			$('#recentStories_' + openRecentStories[i]).animate({height: 0}, 500, function() {
				$(this).removeClass().addClass('hidden');	
				$(this).removeAttr("style");
				$(this).prev().removeClass('oneStoryCelebNoBorder').addClass('oneStoryCeleb');	
			});
			openRecentStories.splice(i,1);
		}
	}
	if ($('#recentStories_' + celeb_id).html() != '') {
		$('#oneStoryCeleb_' + celeb_id).removeClass('oneStoryCeleb').addClass('oneStoryCelebNoBorder');	
		$('#recentStories_' + celeb_id).removeClass().addClass('recentStories');
		rsAnimating = true;
		$('#recentStories_' + celeb_id).animate({height: 175}, 500, function(){rsAnimating = false;});
		inArray = false;
		for(i=0;i<openRecentStories.length; i++) {
			if (openRecentStories[i] == celeb_id) {
				inArray = true;
			}
		}
		if (inArray == false) {
			openRecentStories.push(celeb_id);
		}
	}		
	
}

function closeAllRecentStories() {
	for(i=openRecentStories.length;i>=0;i--) {
		$('#recentStories_' + openRecentStories[i]).animate({height: 0}, 500, function() {
			$(this).removeClass().addClass('hidden');	
			$(this).removeAttr("style");
			$(this).prev().removeClass('oneStoryCelebNoBorder').addClass('oneStoryCeleb');	
		});
		openRecentStories.splice(i,1);
	}	
}

function setShareBar() {
//	alert('in setsharebar');
	if ($(window).width() >= 1100) {
		shareLeft = (($(window).width() - 980) / 2) - (42 + 16); 
		if ($('html').scrollTop() > 300) {
			shareTop = $('html').scrollTop() + 100;
		}
		else {
			shareTop = 430;
		}
		$('#shareBar').css({'left': shareLeft, 'top': shareTop, 'display': 'block'});
		$('#shareBarHoriz').removeClass().addClass('hidden');
	}
	else {
		$('#shareBar').css({'display': 'none'});
		$('#shareBarHoriz').removeClass().addClass('shareButtonsHoriz');

	}
}

