$(document).ready(function() {
    $('#deleteAvatar').change(function() {
		$('div#uploadAvatar').toggle('fast');
	});

	$('a.orly').click(function() {
		var a = $(this);
		var link = $(a).attr('href');
		$(a).removeAttr('href');
		var text = $(a).attr('rel');
		$.dialog.orly(text, function(data) {
			if (data) {
                window.location = link;
            } else {
                $(a).attr('href', link);
            }
		});
	});

	$('.lolwhat').qtip({
		position:{
            corner:{
                target: 'topMiddle',
                tooltip: 'bottomLeft'
            }
        },
		style:{
            background: '#EEEEEE',
      		color: '#151515',
      		border: {
      			width: 3,
	         	//radius: 3,
	         	color: '#0099FF'
			},
            tip: true
        }
	});

	$('img#yt0').click(function() {
		var rnd = Math.floor(10000001 * Math.random());
		var src = $(this).attr('src').replace(/#[0-9]+$/, '');
		$(this).attr('src', src + '#' + rnd);
	});

	$('a.submit').click(function() {
		$(this).removeClass('submit').addClass('loading').html('<span>' + $(this).html() + '</span>');
		$('form[name=' + $(this).attr('rel') + ']').submit();
	});

	$('form').keypress(function(e) {
		if (e.keyCode == 13 && !$(e.target).is('textarea') && !$(e.target).is('#Entry_savedTags')) {
			var submitButton = $('form[name=' + $(this).attr('name') + '] a.submit');
			$(submitButton).removeClass('submit').addClass('loading').html('<span>' + $(submitButton).html() + '</span>');
			$(this).submit();
		}
	});
	
    if ($('input#Entry_savedTags').length) {
        $('input#Entry_savedTags').tag_add({
            maxitem: 0,
            minlength: 2,
            maxlength: 10,
            loadinfo: base + 'blog/getTags'
        });
    }
    
    $('#comment_form form').submit(function() {
		var text = $('textarea[name*=text]').val();
		var entryID =  $('input[name*=entryID]').val();
		var parentID =  $('input[name*=parentID]').val();
    	if (!text) {
			$('div#comment_form a.button').removeClass('loading').addClass('submit');
	        return false;
	    }
	    $.dialog.loading();
		$.post(base + 'comment/add', {text: text, entryID: entryID, parentID:parentID}, function(data) {
			if (!data) {
				$('div#comment_form a.button').removeClass('loading').addClass('submit');
	            $.dialog.loading(true);
	            return false;
	        }
			$('div#comment_form').appendTo('div.comments');
			$('textarea#editor').val('');
			$('div#comment_form').hide();
			$('div.comment a[rel=reply]').show();
			$('div.comments a.big').show();
			$('div#comments').html(data);
			$('div#comment_form a.button').removeClass('loading').addClass('submit');
	        $.dialog.loading(true);
		});
		return false;
    });
    
	$('.entry .text img, .comment .text img').each(function() {
		$(this).wrap('<a href="' + $(this).attr('src') + '" rel="expand"></a>');
	});
	
	$('*[rel*=expand]').fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': true
	});
	
	if ($('#pictures').length) {
		$('#pictures').MultiFile({
			accept: 'gif|jpg|png|jpeg',
			STRING: {
				remove: 'Удолить!'
			}
		});
	}

	$('.gallery-picture').hover(function() {
		$(this).find('img').fadeTo(0, 0.9);
		$(this).find('.gallery-picture-info').show();
	}, function() {
		$(this).find('img').fadeTo(0, 1);
		$(this).find('.gallery-picture-info').hide();
	});
	
	/*$('#logo-slide').crossSlide({
  		sleep: 10,
  		fade: 1
	}, [
		{src: base + 'themes/dropped/images/head-300-1.jpg'},
		{src: base + 'themes/dropped/images/head-300-2.jpg'},
		{src: base + 'themes/dropped/images/head-300-3.jpg'},
		{src: base + 'themes/dropped/images/head-300-4.jpg'},
		{src: base + 'themes/dropped/images/head-300-5.jpg'},
		{src: base + 'themes/dropped/images/head-300-6.jpg'},
		{src: base + 'themes/dropped/images/head-300-7.jpg'},
		{src: base + 'themes/dropped/images/head-300-8.jpg'},
		{src: base + 'themes/dropped/images/head-300-9.jpg'}
	]);*/

    $('#hide-sidebar').click(function() {
        $('.sidebar').toggle('slide', {side: 'right'}, 300);
        return false;
    });
});

function replyTo(commentID) {
	$('div.comment a[rel=reply]').show();
	$('#i-wanna-comment').show();
	var appendTo = 'div#comment' + commentID;
	if (commentID == 0) {
		$('#i-wanna-comment').hide();
		appendTo = 'div.comments';
	}
	$('div#comment_form').appendTo(appendTo).show();
	window.location = '#comment-form';
	$('div#comment' + commentID + ' a[rel=reply]').hide();
	$('input#Comment_parentID').attr('value', commentID);
}

function deleteComment(commentID) {
	$.dialog.orly('Вы уверены, что хотите удалить этот комментарий? Все ответы на него тоже пойдут пиздой.', function(data) {
		if (!data) {
            return;
        }
		$.dialog.loading();
		$.post(base + 'comment/delete', {commentID: commentID}, function(data) {
			$('div#comment_form').appendTo('div.comments');
			$('textarea#Comment_text').val('');
			$('div#comment_form').hide();
			$('div.comment a.dashed').show();
			$('div.comments a.big').show();
			$('div#comments').html(data);
			$.dialog.loading(true);
		});
	});
}

function rate(entryID, rating) {
	$.post(base + 'blog/rate', {entryID: entryID, rating: rating}, function(data) {
		if (!data) {
            return;
        }
		$('div#entry_' + entryID + ' span.rating').replaceWith(data);
		$('div#entry_' + entryID + ' a.rate').remove();
	});
}

function communityJoin(communityID) {
	$.dialog.loading();
	$.post(base + 'community/join', {communityID :communityID}, function(data) {
		if (data) {
			$('a#community_join').addClass('hidden');
			$('a#community_leave').removeClass('hidden');
			$('#button_create').removeClass('hidden');
			$.dialog.loading(true);
		}
	}, 'json');
}

function communityLeave(communityID) {
	$.dialog.orly('Вы уверены, что хотите покинуть это сообщество?', function(data) {
		if (!data) {
            return;
        }
		$.dialog.loading();
		$.post(base + 'community/leave', {communityID: communityID}, function(data) {
			if (data) {
				$('a#community_join').removeClass('hidden');
				$('a#community_leave').addClass('hidden');
				$('#button_create').addClass('hidden');
			}
			$.dialog.loading(true);
		});
	});
}

function friendAdd(userID) {
	$.dialog.orly('Вы уверены, что хотите добавить этого пользователя в друзья?', function(data) {
		if (!data) {
            return;
        }
		$.dialog.loading();
		$.post(base + 'user/friendAdd', {userID: userID}, function(data) {
			if (data) {
				$('a#add_friend').remove();
				$.dialog.loading(true);
				$.dialog.ritoric('Ждем пока пользователь подтвердит это...');
			}
		});
		$.dialog.loading(true);
	});
}

function friendConfirm(userID) {
	$.dialog.loading();
	$.post(base + 'user/friendConfirm', {userID: userID}, function(data) {
		if (data) {
			$('#friend_' + userID).removeClass('new');
			$('#friend_' + userID + ' span#titsorgtfo').remove();
		}
	});
	$.dialog.loading(true);
}

function friendReject(userID) {
	$.dialog.loading();
	$.post(base + 'user/friendReject',{userID: userID}, function(data) {
		if (data) {
			$('#friend_' + userID).remove();
		}
	});
	$.dialog.loading(true);
}

function friendDelete(userID) {
	$.dialog.orly('Вы уверены, что хотите удалить этого пользователя из списка друзей?', function(data) {
		if (!data) {
            return;
        }
		$.dialog.loading();
		$.post(base + 'user/friendDelete', {userID: userID}, function(data) {
			if (data) {
				$('#friend_' + userID).remove();
			}
		});
		$.dialog.loading(true);
	});
}
