function respond(data){
    if ("user" in data && data.user != 'new') {
        window.location.href = '/login/';
    }
    else {

        $("<div>" + data.html + "<\/div>").dialog({
            title: data.title,
            width: 400,
            height: 300,
            position: ['center', 300],
            close: function(e, ui){
                $(this).dialog('destroy');
            },
            buttons: {
                'Sign-Up': function(e, ui){
                    $(this).dialog('destroy');
                    $('#social-signup').submit();
                    //
                }
            }
        });
    }
}

window.fbLoginDone = function(response) {
	//console.log(response);
      if (response.session) {
                // user successfully logged in
                $.ajaxSetup({
                    cache: false
                });
                $.getJSON("/social-login.pimp?sn=facebook", response.session, snConnectClosed);
                //alert(response);
            }
	}
	
jQuery(function($){
    var loginWindow;
    $('#fbbtn').click(function(e){
		//window.open("/social-login.pimp?sn=facebook", "login", "width=500,height=350");
		//return;
		
        FB.login(function(response){
            if (response.session) {
                // user successfully logged in
                $.ajaxSetup({
                    cache: false
                });
                $.getJSON("/social-login.pimp?sn=facebook", response.session, snConnectClosed);
                //alert(response);
            }
        }, { perms: 'offline_access,email,user_birthday' });
    });
    $('#msbtn').click(function(e){
        //document.domain = "pimpwar.com";
        loginWindow = window.open("/social-login.pimp?sn=myspace", "login", "status=1,height=350,width=500");
        e.preventDefault();
        return false;
    });
	$('#ggbtn').click(function(e){
		//document.domain = "pimpwar.com";
        loginWindow = window.open("/social-login.pimp?sn=google", "login", "scrollbars=yes,status=1,height=350,width=500");
        e.preventDefault();
        return false;
	});
    window.snConnectClosed = function(data){
		if (loginWindow) loginWindow.close();
		//console.log('snConnectClosed()');
		//console.log(typeof data);
        if (typeof data == 'object') {
            if (data.userStatus == 'new') {
				$('#new-user-dialog .social-network').text(data.socialNetwork);
				$('#new-user-dialog .merge-link').attr("href", "/login/redir-mergeos/?sn="+data.socialNetwork);
                $('#new-user-dialog').dialog({
                    title: data.title || "Log-in",
                    width: 400,
                    height: 300,
                    position: ['center', 300],
                    close: function(e, ui){
                        $(this).dialog('destroy');
                    }
                    /*,
                    buttons: {
                        'Sign-Up': function(e, ui){
                            $(this).dialog('destroy');
                            $('#social-signup').submit();
                            //
                        }
                    }*/
                });
                if ('me' in data && data['me']) {
					//console.log(data['me']);
					$('.ui-dialog-title').text('Welcome to Pimp War, ' + data['me'] + '!');
				}
				$('#btnMerge').click(function(){
					window.location.href="/login/redir-mergeos/?sn="+data.socialNetwork;
				});
            }
            else
                if (data.userStatus == 'existing') {
					if (loginWindow) loginWindow.close();
					//console.log('existing user');
                    top.location.href = 'http://www.pimpwar.com/login/';
                }

        }
    }
});

