/* homepage-initialization.js */

$(document).ready(function(){
	$("#homepage-carousel").scrolling_carousel({slidePadding:20, loop:true, homepage:true});

	//move the homepage carousel shadow for IE7
	if($.browser.msie && parseInt($.browser.version, 10) == 7) {
		$("#carousel-shadow").appendTo("#homepage-carousel");
	}   
	
	populateBornOnThisDayProfiles(false);
});

/* BEGIN: Born On This Day Module */
function populateBornOnThisDayProfiles(loggedInToFacebook, facebookProfiles) {
	$('#on-this-day-birthday-container').empty();

	// Fill in the profiles
	var index = 0;
	var max = 6;
	$('#otd-profiles > div').each(function() {
		if(index < max) {
			if((index == 1 || index == 4) &&
					loggedInToFacebook &&
					facebookProfiles.length && facebookProfiles.length > 0) {
				// Get a profile from the Facebook list
				$('#on-this-day-birthday-container').append(facebookProfiles.shift());

				index ++;
			}

			// Get a profile from the Biography.com system
			var profile = $(this).clone();
			$('#on-this-day-birthday-container').append(profile);
			profile.find(".profile-rollover").universal_rollover();

			index ++;
		}
	});

	// Show or hide elements as needed
	if(loggedInToFacebook) {
		$('#fb-connect-home-on-day-button').hide();
		$('#not-logged-in-txt').hide();
		$('#more-birthdays').show();
	} else {
		// Swap out the 6th element with the facebook silhouette placeholder
		$('#on-this-day-birthday-container > div').eq(5).remove();
		$('#on-this-day-birthday-container').append($('#temp-fb-user-bornonthisday').clone());
	}
}

function onThisDayFacebookFriends(){
	var currentDate = new Date();
	var currentDateDay = currentDate.getDate();
	var currentDateMonth = currentDate.getMonth()+1;
	var fbFriendArray = new Array();

	for(var z=0; z < facebookUserLoginData.loggedInFriendsData.data.length; z++){
		var currentFBUser = facebookUserLoginData.loggedInFriendsData.data[z];

		if(currentFBUser.birthday){
			var birthdayArray = currentFBUser.birthday.split('/');
			// Need to parse in decimal mode so dates like '08' and '09' don't get read as invalid octal (and treated as zero)
			var birthdayMonth = parseInt( birthdayArray[0], 10 );
			var birthdayDay = parseInt( birthdayArray[1], 10 );

			if( (birthdayDay == currentDateDay) && (birthdayMonth == currentDateMonth) && fbFriendArray.length < 2){
				var fbFriendHtml='';
				fbFriendHtml+='<div class="facebook-user on-this-day">';
					fbFriendHtml+='<img class="facebook-image" src="'+currentFBUser.picture+'">';
					fbFriendHtml+='<img class="facebook-icon" src="/assets/images/common/facebook_favicon.png">';
				fbFriendHtml+='</div>';
				fbFriendArray.push(fbFriendHtml);
			}
		}
	}

	populateBornOnThisDayProfiles(true, fbFriendArray);
};

$(document).bind("gigyaLoginComplete", function(e, initiateSSO, response){
	if(initiateSSO){
		$('#fb-connect-home-on-day-button').hide();
		$('#temp-fb-user-bornonthisday').hide();
		$('#not-logged-in-txt').hide();
		$('#more-birthdays').show();
	}
	else{
		$('#more-birthdays').hide();
		$('#fb-connect-home-on-day-button').show();
		$('#not-logged-in-txt').show();
	}
});
/* END: Born On This Day Module */

