function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a').bind('click', function() {
		if(jQuery(this).text() != 'P'){
			carousel.stopAuto();
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
			carousel.startAuto();
		} else {
			carousel.toggleAuto();
		}
		
	return false;
	});
};

function mycarousel_FirstInCallback(carousel, object, index, state ) {
	// Get the Index
	// Find the Element
	// Remove the old graphic from the element
	// Add the new graphic to the element
	$("#newscarousel" + index).removeClass("NewsItemUnselected");
	$("#newscarousel" + index).addClass("NewsItemSelected");
	
}

function mycarousel_FirstOutCallback(carousel, object, index, state) {
	// Get the Index
	// Find the Element
	// Remove the old graphic from the element
	// Add the new graphic to the element
	$("#newscarousel" + index).removeClass("NewsItemSelected");
	$("#newscarousel" + index).addClass("NewsItemUnselected");
}


$(document).ready( function() {  
	
	jQuery('#mycarousel').jcarousel({
		scroll: 4,
		visible: 4,
		wrap: 'both'
	});
	
	jQuery('#newstestcarousel').jcarousel({
		scroll: 1,
		visible: 1,
		auto: 6,
		size: 6,
		wrap: 'last',
		initCallback: mycarousel_initCallback		
	});
	
	jQuery('#newscarousel').jcarousel({
		scroll: 1,
		visible: 1,
		auto: 8,
		size: 5,
		wrap: 'last',
		initCallback: mycarousel_initCallback,
		itemFirstInCallback: mycarousel_FirstInCallback,
		itemFirstOutCallback: mycarousel_FirstOutCallback
	});
		
	var FB_API_KEY = 'b4094162f65142fb5709c6ff0060bb30';
	var ZYNGA_API_KEY = 'd789f04b2dcbec511afb9ceeb0b9af7a58fc8cd6875548bb2c44fa92a9f01c90';
	var FB_SNID = '1';
	var USER_LOGIN_URL = 'http://bulldog.dev.zynga.com/accounts/www/login/';
	var GAME_IFRAME_URL = 'http://jchum.mwdev.zynga.com/mwfb_connect/?fb_sig_in_iframe=1&fb_sig_locale=en_US&fb_sig_in_new_facebook=1';
	
	function setupDD()
	{
		var games_dropdown = new ZDropdown("games_dropdown_activator", "games_dropdown_content");
		var settings_dropdown = new ZDropdown("settings_dropdown_activator", "settings_dropdown_content");
		
		var modal_options = {
			overlayCss: {  backgroundColor: '#000' },
		  containerCss: {
				padding: "1em",
				backgroundColor: '#fff',
				border: '3px solid #ccc',
				textAlign: "left"
			}
		};
		
		$("#account_modal_link").click(function() { jQuery("#account_modal_content").modal(modal_options); });
		$("#privacy_modal_link").click(function() { jQuery('#privacy_modal_content').modal(modal_options); });
		
		$('.modal_close').css({'text-align':'right'});
		$('.modal_close a').click(function() { jQuery.modal.close(); }).css({
				"text-decoration":"underline",
				"color":"#0000FF"
		});
	}
	
	setupDD();
	
	FB_RequireFeatures(["XFBML"],
		function()
		{
			FB.Facebook.init(
				{
					"apiKey" : FB_API_KEY,
					"xdChannelUrl" : 'http://bulldog.dev.zynga.com/playonzynga/xd_receiver.htm'
				}
			);
			
			
			var logged_in_container = jQuery("#loggedInfo");
			var logged_out_container = jQuery("#loginLink");
			var logout_container = jQuery("#logOut");
			var game_info_container = jQuery("#game_info");
			
			var game_iframe = jQuery("#game_iframe");
			
			function FBUserConnected(FBUserID)
			{
				console.log('FB Session Ready and Logged In');
				logged_in_container.show();
				logged_out_container.hide();
				logout_container.show();
				game_info_container.hide();
				
				/*
				$.postJSON('http://bulldog.dev.zynga.com/playonzynga/RESTProxy.php',
					{
						'RESTProxyURL' : USER_LOGIN_URL,
						'method' : 'user.login',
						'sn_id' : FB_SNID,
						'sn_uid' : FBUserID,
						'api_key': ZYNGA_API_KEY
					},
					function(data) { 
					  console.log('user.login response: ');for(o in data){ console.log(o + ':' + data[o]); }
			        }
				);
				*/
				
				var iframe_url = game_iframe.attr('src');
				if(iframe_url != GAME_IFRAME_URL)
				{
					game_iframe.attr('src', GAME_IFRAME_URL);
				}
			}
			
			function FBUserNotConnected()
			{
				console.log('user not connected');
				var FBSessionState = FB.Facebook.get_sessionState();
				logged_in_container.hide();
				logged_out_container.show();
				logout_container.hide();
				game_info_container.show();
				
				jQuery("#game_iframe").attr("src", "");
				
				FBSessionState.waitUntilReady( function(session) { FBUserConnected(session.uid); } );
				
			}
			
			jQuery(
				function()
				{
					//the following line might be buggy, but it would be great if it worked b/c we'd get to eliminate some code
					//FB.Connect.ifUserConnected(FBUserConnected, FBUserNotConnected);
					
					var FBSessionState = FB.Facebook.get_sessionState();
					var FBSessionReadyStatus = FBSessionState.get_isReady();
					
					console.log('starting FB calls...');

					console.log( 'FB session state ready? ' + FBSessionReadyStatus );
					
					if(FBSessionReadyStatus) { FBUserConnected(FBSessionState.result.uid); }
					else { FBUserNotConnected(); }
				}
			);
		}
	);		
	
});