var site = {
	baseUrl: 'http://excellence.quicklink.pl/',
	fb: {
		apiKey: 'ff1ae1072461364bd340c3e488ffe2ad',
		channelPath: '/fb/xd_receiver.htm',
		//permissions: 'publish_stream',
		publish: function(name, hash, img) {
			if (typeof(this.publish_internal) == 'function') {				
				this.publish_internal(name, site.baseUrl+'#'+hash, site.baseUrl+img);
			} else {
				//alert('Facebook API not ready');
			}
		},
		init: function() {
			FB_RequireFeatures(['Api'], function() {
				var showDialog = function(title, infoUrl) {
					var iframe = FB.XdComm.Server.singleton.createNamedHiddenIFrame('dialogContent', infoUrl, 'fb_permission_iframe', null);
					var dialog = new FB.UI.PopupDialog(title, iframe, true, false);
					dialog.show();
				};
				FB.Facebook.init(site.fb.apiKey, site.fb.channelPath);
				site.fb.publish_internal = function(name, url, img) {
					var fbPublish = function() {
						var attachment = {
							'name': name,
							'href': url,
							'description': 'Sprawdź inne nasze czekolady',
							'media': [{
								'type': 'image',
								'src': img,
								'href': url
							}]
						};
						FB.Connect.streamPublish('poleca czekoladę:', attachment, null, null, 'Podziel się swoją opinią', null/*function(id, ex) {
							if (ex) {
								showDialog('Błąd', '/fb/not_published.html');
							} else if (id > 0) {
								showDialog('Potwierdzenie', '/fb/published.html');
							}
							showDialog(id, '/fb/published.html');
						}*/, false);
					};
					
					FB.Connect.requireSession(function() {
//						FB.Facebook.apiClient.users_hasAppPermission(site.fb.permissions, function(result) {
//							if (result) {
//								fbPublish();
//							} else {
//								FB.Connect.showPermissionDialog(site.fb.permissions, function(perm) {
//									if (perm) {
										fbPublish();
//									} else {
//										showDialog('Błąd', '/fb/permissions_required.html');
//									}
//								});
//							}
//						});
					}, function() {
						showDialog('Błąd', '/fb/connection_required.html');
					}, true);
				};
			});
		}
	}
};

$(document).ready(function() {
	site.fb.init();
	/*$('#publish').click(function() {
		flash_publish('Testowy produkt', 'foobar', 'img/02.jpg');
	});*/
});

function flash_publish(name, hash, img) {
	site.fb.publish(name, hash, img);
}

