/**
 * @author Chernov
 */
ClassLibrary.TestPage = Base.extend({
	
	init: function() {
		this.initEvents();
	},
	
	initEvents: function() {
		var t = this;
		/*var level = 0;
		$("#vkAuth").click(function() {
			VK.Auth.login(function(Response) {
				if (response.session) {
					s = VK.Auth.getSession();
					VK.Api.call('getProfiles', {
						uids: s.mid,
						fields: 'first_name,last_name,sex,photo_big,contacts,bdate'
					}, function (R) {
						console.log(R);
						$.ajax({
							url: "/service/auth/vkAuth",
							type: "POST",
							data: "hash="+R.hash+"&uid="+R.uid+"&first_name="+R.first_name+"&last_name="+R.last_name+"&photo="+R.photo_big+"&bdate="+R.bdate+"&sex="+R.sex,
							dataType: "json",
							success: function(Response) {
								window.location.reload();
							}
						});
					});
				}
			}, level);
		});*/
		
		VK.Widgets.Auth("vk_auth", {
			width: "280px",
			onAuth: function(data) {
				VK.Api.call('getProfiles', {
					uids: data.uid,
					fields: 'first_name,last_name,sex,photo_big,contacts,bdate,education'
				}, function (R) {
					var D = R.response[0];
					$.ajax({
						url: "/service/auth/vkAuth",
						type: "POST",
						data: "hash="+data.hash+
							  "&uid="+data.uid+
							  "&photo="+data.photo+
							  "&first_name="+encodeURIComponent(D.first_name)+
							  "&last_name="+encodeURIComponent(D.last_name)+
							  "&bdate="+D.bdate+
							  "&sex="+D.sex+
							  "&graduation="+D.graduation+
							  "&university="+encodeURIComponent(D.university_name)+
							  "&faculty="+encodeURIComponent(D.faculty_name)+
							  "&phone="+encodeURIComponent(D.mobile_phone),
						dataType: "json",
						success: function(Response) {
							if (Response.RESULT && true === Response.RESULT) {
								window.location.reload();
							}
						}
					});
				});
			} 
		});		
        $(".fb_button").click(function() {            
            FB.login(function(response) {
                if (response.session) {
                    if (response.perms) {
                    } else {
                      // user is logged in, but did not grant any permissions
                    }
                    var query = '';
                    $.each(response.session, function (key, value) {
                        query += key+"="+value+"&";
                    })
                    FB.api('/me', function(R) {
                        $.each(R, function (key, value) {
                            query += key+"="+value+"&";
                        });
                        $.ajax({
                            url: "/service/auth/fbAuth",
                            type: "POST",
                            data: query,
                            dataType: "json",
                            success: function(Resp) {
                                if (Resp.RESULT && true === Resp.RESULT) {
                                    window.location.reload();
                                }
                            }
                        });
                    });
                    
                }
            }, {perms:'email,publish_stream,offline_access'});
        });
	},
	
	end: function() {}
});

$(document).ready(function() {
	Namespace.registerObj('Pages', 'TestPage');
	Pages.TestPage.init();
});
