var liked = null;	//Ostatni lubiany
var unliked = null;	//Ostatni nielubiany
var fb_photo = null; //Czy wpis posiada fotkę
var zoom = 1;	//Zoom zdjęcia przeslanego przez uzytkownika
var src_x = 0;	//Wsp x od ktorej zaczyna sie obszar do miniatury
var src_y = 0;	//Wsp y od ktorej zaczyna sie obszar do miniatury
var src_width = 90;	//Szerokosc obszaru do miniatury
var src_height = 90; //Wysokosc obszaru do miniatury

$(document).ready(function() {
	//START FACEBOOK WIDGETS
 window.fbAsyncInit = function() {
 	FB.init({appId: '103258079753574', status: true, cookie: true,
 	xfbml: true});
 	
 	FB.Event.subscribe('auth.sessionChange', function (response) {
  if (response.session) {
  // A user has logged in, and a new cookie has been saved
     window.location.reload();
  } else {
	 window.location.reload();
          // The user has logged out, and the cookie has been cleared
  }
 });
 
 FB.Event.subscribe('edge.create', function (href, widget) {
	var attr_itr = widget.dom.getAttribute('itr');
	if(liked != attr_itr)
	{
		$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "like=1&pid="+attr_itr
	 	});
		liked =  attr_itr;
		unliked = null;
	}
 });
 FB.Event.subscribe('edge.remove', function (href, widget) {
 	var attr_itr = widget.dom.getAttribute('itr');
	if(unliked != attr_itr)
	{
		$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "like=2&pid="+attr_itr
	 	});
		unliked =  attr_itr;
		liked = null;
	}
 });
 };
 (function() {
	var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
    '//connect.facebook.net/pl_PL/all.js';
     document.getElementById('fb-root').appendChild(e);

	 var s = document.createElement('script');
                s.type = 'text/javascript';
                s.src = 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
                s.async = true;
                document.getElementById('fb-root').appendChild(s);
 }());

	//END FACEBOOK WIDGET

var uploader = new qq.FileUploader({
                element: $('#upload')[0],
                debug: true,
                multiple: false,
                onSubmit: function(id, fileName){
                	$('#prompt').css('display', 'block');
                },
                onProgress: function(id, fileName, loaded, total){
                	$('#loader').css('display', 'block');
                },
                onComplete: function(id, fileName, result){
    				$('#loader').css('display', 'none');
                	$('#user-file').attr('src', '/img/user_files/'+result.newFileName);
                	$('#post-image').val('/user_files/'+result.newFileName+'_mini');
					var crop_user_file = $('#user-file').imgAreaSelect({
						instance: true,
						handles: true,
						parent: $('#selection-area-contener'),
						onInit: function (img, selection){
							if(img.width > 400 || img.height > 400)
							{
								var imgRatio = img.width/img.height;
								if(imgRatio > 1)
								{
									zoom = img.width/400;
									$('#user-file').css('width', '400px');
									width = 400;
									height = Math.floor(img.height/zoom);
								}else {
									zoom = img.height/400;
									$('#user-file').css('height', '400px');
									height= 400;
									width = Math.floor(img.width/zoom);
								}
							}
							$('#user-file').css('display', 'block');

							crop_user_file.setSelection(0, 0, 90, 90);
							crop_user_file.setOptions({
								aspectRatio: '1:1', show: true
							});
							crop_user_file.update();
							selection = crop_user_file.getSelection();
							this.onSelectEnd(img, selection);
						},
						onSelectEnd: function(img, selection){
							src_x = Math.floor(selection.x1 * zoom);
							src_y = Math.floor(selection.y1 * zoom);
							src_width = Math.floor((selection.x2 - selection.x1) * zoom);
							src_height = Math.floor((selection.y2 - selection.y1) * zoom);
						}
					});
                }
            });






function show_ascii(){
	$('#prompt').css('display', 'block');
	$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "prompt=ascii",
	   		success: function(data){
	   			$('.prompt-title-bar p').html('wstaw znaczki');
	   			$('#prompt-list').html(data);
	   			$('#prompt-list').addClass('prompt-list-short');
  			}
	 	});
}
function show_emot(){
	$('#prompt').css('display', 'block');
	$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "prompt=emot",
	   		success: function(data){
	   			$('.prompt-title-bar p').html('wstaw grafikę');
	   			$('#prompt-list').html(data);
	   			$('#prompt-list').addClass('prompt-list-short');
  			}
	 	});
}
function show_user_photo_page(){
	$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "prompt=user-photo",
	   		success: function(data){
	   			$('.prompt-title-bar p').html('wykadruj zdjęcie');
	   			$('#prompt-list').html(data);
	   			$('#prompt-list').removeClass('prompt-list-short');
  			}
	 	});
}

$('.post-text a').each(function(index) {
	if($(this).height() > 65){
		$(this).parent().parent().find('.post-button-view-all').css('display', 'block');
	}
  });
$('#submit-contener p').click(function(){
	var confirm_alert = confirm("Czy twój wpis jest gotowy?");
	if(confirm_alert)
		$('form').submit();
});

$('#button_ascii').click(show_ascii);
$('#button_emot').click(show_emot);
$('#button_file').click(show_user_photo_page);
$('#add-user-photo').live('click', function(){
	$.ajax({
	   		type: "POST",
	   		url: "http://nocnik.pl/Req",
	   		data: "src_x="+src_x+"&src_y="+src_y+"&src_width="+src_width+"&src_height="+src_height+"&filename="+$('#post-image').val(),
	   		success: function(data){
	   			$('#selection-area-contener').css('display', 'none');
	   			$('#exit-prompt').trigger('click');
	   			display_fb_content_photo('/img'+$('#post-image').val());
  			}
	 	});
});
$('#remove-fb-photo').click(function(){
	fb_photo = true;
	$('#post-image').val(null);
	$('#fb-content-photo').css('display', 'none');
	$('#remove-fb-photo').css('display', 'none');
	$('#fb-content-photo').attr('src', '');
});
$('#exit-prompt').click(function(){
		$('#prompt').css('display', 'none');
		$('#prompt-list').html('');
});

$('.symbol').live('click', function() {
  	var content = $('#post-content').val()+$(this).val();
  	$("#post-content").val(content);
  	$('#post-content').trigger('keyup');
});
$('.emot').live('click', function(){
	display_fb_content_photo('/img/emoty/'+$(this).attr('file'));
	$('#post-image').val('/emoty/'+$(this).attr('file'));
	$('#post-content').trigger('keyup');
});

	//Facebook preview controllers
function display_fb_content_photo(filepath){
	fb_photo = true;
	$('#fb-content-photo').attr('src', filepath);
	$('#fb-content-photo').css('display', 'block');
	$('#remove-fb-photo').css('display', 'block');
}

$('form').submit(function(){
	$('#post-content').trigger('keyup');
});
$('#post-content').live('keyup', function(){
	var content_len = $('#post-content').val().length;
	var content = $('#post-content').val();
	var title_len = 100;
	var text_len = 150;

	if(fb_photo == true){
		title_len = 50;
		text_len = 300;
	}

	if(content_len <= title_len)
		$('#fb-content-title').text(content);
	else
		$('#fb-content-title').text(content.substring(0, title_len));
	if(content_len <= text_len)
		$('#fb-content-text').text(content);
	else
		$('#fb-content-text').text(content.substring(0, text_len)+"...");
});











});
