



document.write('<div id="commentsWriter_390814_1329845403717_CRhKkC4V"></div>');
jQuery(function() {
	var el = jQuery("#commentsWriter_390814_1329845403717_CRhKkC4V");
	el.addClass("commentsWriter");
	
	function updateWriterBox(el, uid) {
		el.html("");
		
		var img = '<img src="http://static.cupmanager.net/images/silhouette.jpg" />';
		var name = '<span>$wr.user.person.name.name (admin)</span>';
		if (uid) {
			img = '<fb:profile-pic size="square" uid="'+uid+'" />';
			name = '<fb:name linked="false" useyou="false" uid="'+uid+'" />';
		}
		
		var table = jQuery('<table style="width:100%"><tr><td valign="top" align="left"></td><td valign="top" align="left"></td></tr></table>');
		
		table.find("td:first").html(img);
		table.find("td:first").addClass("profilePic");
		table.find("td:last").html(name);
		table.find("td:last").append("<br/>");
		
		var body = jQuery("<textarea></textarea>");
		body.height(16);
		body.autoResize({
		    // Quite slow animation:
		    animateDuration : 300,
		    // More extra space:
		    extraSpace : 16
		});
        
		
		table.find("td:last").append(body);
		
		var facebookPublish = jQuery("<div />");
					var fbCheck = jQuery('<input type="checkbox" id="fbpublish_390814_1329845403717_CRhKkC4V" />');
			
							fbCheck.attr("checked", "checked");
						
			var fbLabel = jQuery("<label>Visa på min Facebook-profil</label>");
			fbLabel.attr("for", "fbpublish_390814_1329845403717_CRhKkC4V");
			
			facebookPublish.append(fbCheck);
			facebookPublish.append(fbLabel);
			
			facebookPublish.css("float", "left");
				
		var button = jQuery('<button type="button"></button>');
		button.html("Kommentera");
        button.css("margin-top","3px");
        button.css("margin-right","4px");
            
		
		button.click(function() {
			var data = {
				body: body.val(),
				contextId: 390814
			};
			CupMan.callService("website.PostCommentService", {
				data: data,
				success: function() {
					jQuery(".commentContext_390814").each(function() {
						var func = jQuery(this).data("refreshGuestbook");
						func();
						
													if (fbCheck[0].checked) {
								// Publish to Facebook
																
																
								var caption = "{*actor*} wrote a comment on Vintrosa Beach Handboll Cup's web site about SUBJECT";
								caption = caption.replace("SUBJECT", jQuery("#cupman .paddedContent h1:first").text());
								
								
								var fbData = {
									'message': body.val(),
									'name': document.title,
									'link': window.location.href,
									'actions': {
										'name': "Visit page",
										'link': window.location.href
									},
									'caption': caption
								};
								if (facebookPictureSrc) {
									fbData.picture = facebookPictureSrc;
								}
								if (facebookPictureCaption) {
									 fbData.description = facebookPictureCaption;
								}
								
								
								FB.api('/me/feed', 'post', fbData, function(response) {
                                  
                                });
								
								/*
								FB.Connect.streamPublish(body.val(), attachment, [{
									text: "Visit page",
									href: window.location.href
								}], null, '', null, true);
								*/
							}
												
						body.val("").blur();
						
						
						CupMan.Tracker.trackEvent( 
							"Comments", 
							"WriteComment", 
							390814
						);
						
					});
				}
			});
		});
		
		button.css("display", "block");
		// button.wrap("<div></div>").parent().attr("align", "right");
		
		button.css("float", "right");
		
		button.hide();
		facebookPublish.hide();
		
		var focusFunc = function() {
			clearTimeout(blurTimeoutHandle);
			button.show();
			facebookPublish.show();
		};
		
		var blurFunc = function() {
			clearTimeout(blurTimeoutHandle);
			if (body.val() == "")  {
				button.hide();	
				body.height(16);
				facebookPublish.hide();
			}
		};
		
		var blurTimerFunc = function() {
			clearTimeout(blurTimeoutHandle);
			blurTimeoutHandle = setTimeout(blurFunc, 200);
		};
		
		var blurTimeoutHandle = null;
		
		body.focus(focusFunc);
		body.blur(blurTimerFunc);
		
					fbCheck.focus(focusFunc);
			fbCheck.blur(blurTimerFunc);
				
		button.focus(focusFunc);
		button.blur(blurTimerFunc);
		
		
		table.find("td:last").append(facebookPublish);
		table.find("td:last").append(button);
		
		table.find("td:last").append('<div class="clear"></div>');
		
		el.append(table);
		
					FB.XFBML.parse();
			}
	
    

	
			if (!CupMan.ie6) {
			ensureFacebookInit(function() {
				FB.Event.subscribe('auth.login', function(response) {
            		updateWriterBox(el, response.authResponse.userID);
                });
				
				FB.getLoginStatus(function(response) {
					if (response.authResponse) {
						updateWriterBox(el, response.authResponse.userID);
					} else {
						var loginDiv = jQuery("<div/>");
						loginDiv.css("padding", "10px");
						
						loginDiv.append("Du måste logga in för att kunna kommentera<br/>");
						loginDiv.append('<fb:login-button v="2" scope="publish_stream" size="medium">Logga in med Facebook</fb:login-button>');
						
						el.append(loginDiv);
						
						FB.XFBML.parse();
					}
				});
			});
		} else {
			el.append("Du använder en för gammal webbläsare. För att kunna skriva kommentarer ber vi dig att uppgradera till en nyare version.<br/>");
		}
	});
