$(function() {
	VK.init({
		apiId: 1896262,
		nameTransportPath: "/xd_receiver.html"
	});
	
	mailru.loader.require('api', function() {
		mailru.connect.init(539312, '694cabb4d2268b61b60241e858b10460');
		mailru.events.listen(mailru.connect.events.login, function(session) {
					mailru.target_form.find("input[name='oid_type']").val(2);
					mailru.target_form.find("input[name='oid_id']").val(session.vid);
					mailru.target_form.submit();
				});
	});

    $('body').after('<div id="fb-root"></div>');
    FB.init({ 
        appId: '270977052913537',
        cookie:true,
        status:true,
        xfbml:true,
        channelUrl: 'http://animal.ru/fb_channel.html'
        /*oauth  : true*/
    });
});

/* ВКонтакте */

VK.is_authorizing = false;
VK.is_authorized = false;

VK.Observer.subscribe('auth.login', function(response) {
	authVK(response);
});

function authVK(response)
{
	if (response.session != null)
	{
		VK.target_form.find("input[name='oid_type']").val(1);
		VK.target_form.find("input[name='oid_id']").val(response.session.mid);
		
		if (VK.is_authorizing == true)
			VK.target_form.submit();
		else
			VK.is_authorized = true;
	}
}

function tryVK(target)
{
	VK.target_form = $(target);
	if (VK.is_authorized)
		VK.target_form.submit();
	else
	{
		VK.is_authorizing = true;
		VK.Auth.login(authVK);
	}
}


/* Мой Мир */
function changeMailRu(target)
{
	mailru.target_form = $(target);
	mailru.connect.logout();
	setTimeout(function() { 
		mailru.connect.login() }, 1000);
}

function tryMailRu(target)
{
	mailru.target_form = $(target);
	mailru.connect.login();
}

/* facebook */
function tryFacebook(target) {
    FB.target_form = $(target);
    
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
                FB.target_form.find("input[name='oid_type']").val(8);
                FB.target_form.submit();
        } else {
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.target_form.find("input[name='oid_type']").val(8);
                    FB.target_form.submit();
                }
            }, { 
                scope: 'user_about_me, user_birthday, user_location, email, user_photos'
            });
        }
    });
}

/* twitter */
function tryTwitter(target) {
    MRD.target_form = $(target);

    MRD.Inst.init({
        width : 710,
        height : 700,
        url : '/loginsocial.php?type=16'
    });
    MRD.Inst.open();

    MRD.target_form.find("input[name='oid_type']").val(16);
}

/* odnoklassniki */
function tryOdnoklassniki(target) {
    MRD.target_form = $(target);

    MRD.Inst.init({
        width : 900,
        height : 250,
        url : '/loginsocial.php?type=64'
    });
    MRD.Inst.open();

    MRD.target_form.find("input[name='oid_type']").val(64);
}

/********************************************************************************************************/
/********************************************************************************************************/
/********************************************************************************************************/

if (!window.MRD) {
    window.MRD = {};
}

// window info
window.MRD.WI = {
    getHeight : function() {
        var result = 0;

        if (typeof (top.window.innerHeight) == 'number') {
            result = top.window.innerHeight;
        } else if (top.document.documentElement && top.document.documentElement.clientHeight) { // IE 6+ in
            result = top.document.documentElement.clientHeight;
        } else if (top.document.body && top.document.body.clientHeight) { // IE 4
            result = top.document.body.clientHeight;
        }

        return result;
    },
    getWidth : function() {
        var result = 0;

        if (typeof (top.window.innerWidth) == 'number') { // Non-IE
            result = top.window.innerWidth;
        } else if (top.document.documentElement && top.document.documentElement.clientWidth) { // IE 6+ in
            result = top.document.documentElement.clientWidth;
        } else if (top.document.body && top.document.body.clientWidth) { // IE 4
            result = top.document.body.clientWidth;
        }

        return result;
    },
    getTop : function() {
        return (top.window.screenTop != undefined ? top.window.screenTop : top.window.screenY);
    },
    getLeft : function() {
        return (top.window.screenLeft != undefined ? top.window.screenLeft : top.window.screenX);
    }
}

window.MRD.Inst = {
    params : {
        width : 900,
        height : 600,
        url : 'about:blank'
    },
    pdialog : null,
    init : function(params) {
        if (params && params.width) {
            this.params.width = params.width;
        }
        if (params && params.height) {
            this.params.height = params.height;
        }
        if (params && params.url) {
            this.params.url = params.url;
        }

        return true;
    },
    open : function() {
        var pxLeft = Math.round(MRD.WI.getLeft() + (MRD.WI.getWidth() / 2) - (this.params.width / 2));
        var pxTop = Math.round(MRD.WI.getTop() + (MRD.WI.getHeight() / 2) - (this.params.height / 2));
        this.pdialog = top.window.open(this.params.url, '', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,top=' + pxTop + ',left=' + pxLeft + ',width=' + this.params.width + ',height=' + this.params.height);

        return false;
    }
}
