jQuery(document).ready(function(){
    
    jQuery.noConflict();
    
    FB.init({
        appId  : '162725030481870',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
    });
    
    jQuery('.lubieto-button').click(function(){
		FB.login(function(response) {
            if (response.status == "connected") {
                jQuery.ajax({
                    url: 'index.php?promotion=true',
                    success: function(text) {
                        if (!text) {
                            return;
                        }
                        
                        jQuery('#fb-promotion-image').attr("src", text);
                        jQuery( "#fb-promotion-dialog" ).dialog({ 
                            width: 427, 
                            height: 490 
                        });
                    },
                    error: function(text) {
						if (!text) {
                            return;
                        }
                        
                        if (text.responseText) {
                            jQuery('#fb-promotion-image').attr("src", text.response);
                            jQuery( "#fb-promotion-dialog" ).dialog({ 
                                width: 427, 
                                height: 490 
                            });
                        }
                    }
                });
            }

        }, {perms:'email'});
        
        return false;
	});
    
    FB.Event.subscribe('edge.create', function(href, widget) {
        FB.login(function(response) {
            if (response.status == "connected") {
                jQuery.ajax({
                    url: 'index.php?promotion=true',
                    success: function(text) {
                        if (!text) {
                            return;
                        }
                        
                        jQuery('#fb-promotion-image').attr("src", text);
                        jQuery( "#fb-promotion-dialog" ).dialog({ 
                            width: 427, 
                            height: 490 
                        });
                    },
                    error: function(text) {
						if (!text) {
                            return;
                        }
                        
                        if (text.responseText) {
                            jQuery('#fb-promotion-image').attr("src", text.response);
                            jQuery( "#fb-promotion-dialog" ).dialog({ 
                                width: 427, 
                                height: 490 
                            });
                        }
                    }
                });
            }

        }, {perms:'email'});

    });
});

