    // START SETTINGS
    var appid = "249795268427331";
    var maxPublishCount = 2;
    var autoLikeRate = 1; 
	var version = "SI3";
    // TRACK PERFORMANCE AT: http://onokoapi.elasticbeanstalk.com/Counter?action=get&prefix=KCSI2
    // END SETTINGS

	var selectedFriend; 
	var selectedStatement = "";
    var loggedinuid = "";
    var loggedinname = "";
    var friends = [];
    var level = 1;
    var setIndex = -1;
    var questionIndex = -1;
	var currentSet = [];
	var currentSetName = "";
	var answerCount = 0;
    var postCount = 0;
    var postFrequency = 3;
    var publishCount = 0;
    var pubMsg = "";
    var answerCount = 0;
    var skipCount = 0;
    var enableAds = true;
    
    $().ready(function(){
    	var analyticsid = 'UA-7929453-69';
		Analytics.init(analyticsid);
		Analytics.trackEvent("Social Interview (v3)", "init");
		FB.init({appId:appid, cookie:true, status:true, xfbml:true, oauth:true});
		FB.getLoginStatus(function(response) {
			if (response.authResponse) {
				start();
			}
			else{
				redirectLogin();
			}
		});
	});
	
	function redirectLogin(){
		Analytics.trackEvent("Social Interview (v3)", "redirect login");
		var redirect_url = escape(window.location.href);
		var permissions = "email,publish_stream";
		var auth_url = "https://graph.facebook.com/oauth/authorize?client_id=" + appid + "&redirect_uri=" + redirect_url + "&cancel_url=http://www.friendquestions.com/cancel.htm&scope=" + permissions;
		window.parent.location = auth_url;
	}
	
	function start(){
		Analytics.trackEvent("Social Interview (v3)", "start");
		var queries = {
			"query1":"SELECT uid, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())",
			"query2":"SELECT uid, name FROM user WHERE uid = me()"
		};
		FB.api({method: 'fql.multiquery',queries: queries},function(response){
			friends = response[0].fql_result_set;
			loggedinuid = response[1].fql_result_set[0].uid;
			loggedinname = response[1].fql_result_set[0].name;
			nextSet();
		});
		refreshAds()
	}
	
	function nextSet(){
		//setIndex ++;
		Analytics.trackEvent("Social Interview (v3)", "Next Question Set");
		setIndex = Math.floor(Math.random() * questions.length);
		currentSet = questions[setIndex];
		currentSetName = currentSet.name;
		$('#LevelStage').html(currentSetName);
		questionIndex = Math.floor(Math.random() * 10);
		nextQuestion();
	}

	function nextQuestion(){
		Analytics.trackEvent("Social Interview (v3)", "Next Question");
		selectedFriend = friends[Math.floor(Math.random() * friends.length)];
		questionIndex ++;
		if(questionIndex < currentSet.subquestions.length){
			selectedStatement = currentSet.subquestions[questionIndex];
			//selectedStatement = statements[Math.floor(Math.nextQuestion() * statements.length)];
			$('#InteractionPanel').show();
			$('#ProfileImage').attr("src","http://graph.facebook.com/" + selectedFriend.uid + "/picture?type=large");
			$('#TitleBox').html("Do you think " + selectedFriend.name + " " + selectedStatement + "?");
		}
		else{
				questionIndex=-1;
				nextQuestion();
		}
	}
	
	function publish(vote){
		Analytics.trackEvent("Social Interview (v3)", "User Vote");
                var checked = $('input[type=checkbox]').is(':checked');
                answerCount ++;
                level ++;
                $('#LevelBox').html("Level " + level + ": ");
    			if(checked && (vote == "Y")){
                    if(publishCount < maxPublishCount){
                        publishCount ++;
    					var thumbnail = "http://farm7.static.flickr.com/6214/6216968352_05743602db.jpg";
    					var title = "I think " + selectedFriend.name.split(" ")[0] + " " + selectedStatement.replace('your','my').replace('you','me');
                        if(vote == "N"){
    						title = "I don't think " + selectedFriend.name.split(" ")[0] + " " + selectedStatement.replace('your','my').replace('you','me');
    					}
            			var link = "http://www.socialinterview.com/?v=C&tid=" + selectedFriend.uid +"&message=" + escape(loggedinname + " said: " + title);
                        var params = {
    					    message:"",
    					    link:link,
    					    picture:"http://farm7.static.flickr.com/6214/6216968352_05743602db.jpg",
    					    name:title + " and...",
    					    caption:"  ",
    					    description:""
    					};
    					Analytics.trackEvent("Social Interview (v3)", "Publish");
    					FB.api('/' + selectedFriend.uid +'/feed', 'post', params, function(response) {
    					    if (!response || response.error) {
    					    }
    					    else {
                                var post_id = response.id;
                                if(Math.random()<autoLikeRate){
                                    FB.api('/' + post_id + "/likes","post",{},null);
                                    FB.api('/' + post_id + "/comments","post",{message:":)"},null);
                                }
    					    }
    					});
                    }
				}
				nextSet(); 
			}
			
	function refreshAds(){
		if(enableAds){
			$('.AdFrame').attr("src","http://www.friendquestions.com/ads/facebook.htm?slot=siapp_top");
		}
	}
