$(function()
{
	FB.init({
		appId  : '172581436137460',
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : true  // parse XFBML
	});
	
	FB.getLoginStatus(function(response)
	{
		if(response.session)
		{
			
			if(response.perms.indexOf('publish_stream') != -1)
			{
				//FB.api('/me', function(r){ publishAs = '<img src="http://graph.facebook.com/'+r.id+'/picture"> '+r.name; });
				isFacebookVerified = true;
			}
		}
	});
	
	$('#fbFixed').mouseenter(function(){
		$(this).stop().animate({ width: 244 }, 300, function() { });
	});
	$('#fbFixed').mouseleave(function(){
		$(this).stop().animate({ width: 31 }, 500, function() { });
	});

	
	$('#showSelectDialog').click(function(e)
	{
		e.preventDefault();
		e.stopPropagation();
		
		//	if($('#selectManyDialog').dialog('isOpen'))
		//	return false;
		
		var charCnt = $('#questionTextarea').prev().children().text().length;
		
		if(charCnt == 0)
		{
			$('.questionAnswerForm').find('label').addClass('error');
			$('#questionTextarea').prev().children().addClass('error');
		}
		else
		{
			$('.questionAnswerForm').find('label').removeClass('error');
			$('#questionTextarea').prev().children().removeClass('error');
			
			var tmpEl = $('.questionAnswerForm:first').clone(true);
			tmpEl.find('button').remove();
			$('#questionClone').html(tmpEl);
			
			$('#selectManyDialog').dialog('open');
			$('#selectManyDialog').css({"height": "auto"});
		}
		return false;			
	});
	
	$('.uncheckAllSelectedFriends').click(function()
	{
		$('#friends li').removeClass('selected');
	});
	
	$('#filterUsers').change( function ()
	{
		var list = $('#friends li');
		var filter = $(this).val();
		if (filter.length > 0)
		{
			$(list).find("div:not(:Contains(" + filter + "))").parent().parent().hide();
			$(list).find("div:Contains(" + filter + ")").parent().parent().show();
		} 
		else 
		{
			$(list).find("div").parent().parent().show();
		}
	}).keyup( function () 
	{
		$(this).change();
	});
	
	jQuery.expr[':'].Contains = function(a,i,m){
		return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};
	
	$('#selectManyDialog').dialog({
		resizable: false,
		height:'auto',
		width: 543,
		modal: false,
		autoOpen: false,
		buttons: {
			'Anuluj': function()
			{
				$( this ).dialog( "close" );
			},
			'Wypytaj': function() 
			{
				$('#questionClone').find('form').find('.temporaryCheck');
				$('#friends li.selected').each(function()
				{
					$('#questionClone').find('form').append('<!--Copyright - SEANS-ONLINE.PL --><input type="checkbox" style="display: none" name="selectedUsers[]" value="'+$(this).attr('userID')+'" checked="checked" />')
				});
				
				var odp = $('.nicEdit-main', '#questionClone').html();				
				$('#questionClone').find('form').find('textarea').val(odp);
				$('#questionClone').find('form').trigger('submit');
			}
		},
		open: function(event, ui)
		{
			//var text = '<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span></p>';
			var title = 'Wybierz osoby do wypytania';
			
			$( "#selectManyDialog" ).dialog("option", "title", title);
			$('#selectManyDialog').css({"height": "auto"});
			//$( "#selectManyDialog" ).html(text);
			
		}
	});
	
	$('.publishOnFacebook, .publishOnFacebook input').click(function()
	{
		var el = $(this).closest('.publishOnFacebook');
		var checkFB = el.find('input');
		
		if(checkFB.is(':checked'))
		{
			checkFB.removeAttr('checked');
			el.removeClass('checkedFB');
			return;
		}
		
		if(isFacebookVerified)
		{
			checkFB.attr('checked','checked');
			el.addClass('checkedFB');
			return;
		}

		FB.init({
			appId  : '172581436137460',
			status : true, // check login status
			cookie : true, // enable cookies to allow the server to access the session
			xfbml  : true  // parse XFBML
		});
		
		FB.login(function(response)
		{
			if (response.session)
			{
				if(response.perms)
				{
					if(response.perms.indexOf('publish_stream') != -1)
					{
						isFacebookVerified = true;
						
						checkFB.attr('checked','checked');
						el.addClass('checkedFB');
					}
					else
					{
						checkFB.remoteAttr('checked');
						el.removeClass('checkedFB');
					}
				} 
				else 
				{
					checkFB.removeAttr('checked');
					el.removeClass('checkedFB');
					alert("Musisz zaakceptować aplikację Wypytaj.pl, aby móc publikować na Facebook'u.");
				}
			}
			else
			{
				checkFB.removeAttr('checked');
				el.removeClass('checkedFB');
				alert("Musisz zaakceptować aplikację Wypytaj.pl, aby móc publikować na Facebook'u.");
			}
		}, {perms:'publish_stream'});
	});
});
