var previewWidth = 0;
var previewHeight = 0;

$(document).ready(function() {
	bindBlogTypesSelector();
	
	$('textarea[id*=wysiwyg]').wysiwyg();
	
	$('#dialogClose, #dialogClose *').live('click', function() {
		closeDialog();
		return false;
	});
	
	$('#dialogWrapper input[name=url]').change(function() {
		$('#dialogPreview').html('<img src="' + $(this).val() + '" />');
		previewWidth = w = $('#dialogPreview img').width();
		previewHeight = h = $('#dialogPreview img').height();
		
		$('#dialogWrapper input[name=width]').val(w);
		$('#dialogWrapper input[name=height]').val(h);
	});
	
	$('#dialogWrapper input[name=width]').keyup(function() {
		coef = previewWidth/$(this).val();
		$('#dialogWrapper input[name=height]').val(Math.ceil(previewHeight/coef));
	});
	
	$('#dialogWrapper input[name=height]').keyup(function() {
		coef = previewHeight/$(this).val();
		$('#dialogWrapper input[name=width]').val(Math.ceil(previewWidth/coef));
	});
	
	$('#dialogInsertButton').live('click', function() {
		if($('#dialogPreview img').attr('src') && $('#dialogWrapper input[name=width]').val())
			insertEditorImage($('#dialogPreview img').attr('src'), $('#dialogWrapper input[name=width]').val(), $('#dialogWrapper input[name=height]').val(), $('#dialogWrapper select[name=align]').val());
	});
	
	$('.publish_button').click(function() {
		$('.blog_new p.error').remove();
		if(!trim($('.blog_new textarea[name=title]').val())) {
			$('.blog_new textarea[name=title]').after('<p class="error">Заполните поле "Заголовок"</p>');
			return false;
		}
		if(!trim($('.blog_new textarea[name=text]').val())) {
			$('.blog_new textarea[name=text]').after('<p class="error">Заполните поле "Текст"</p>');
			return false;
		}
	});
	
	$('.icont_footer_mnenra a.complain').live('click', function(e) {
		obj = $(this);
		blog_id = $(this).parents('.incont_item:eq(0)').attr('realid');
		$.post("/blogcomplain", { blog_id: blog_id }, function(data) {
			if(data == '1') {
				obj.hide();
				var html = $('#complain_msg').html();
				showPopup(html, 321, 140, "Сообщение", null, obj, 0, true);
			}
		});
		return false;
	});
	
	$('.icont_footer_mnenra a.plusminus').live('click', function() {
		obj = $(this);
		
		if($('#mconfirm').length) {
			html = $('#mconfirm').html();
			showPopup(html, 321, 150, "Сообщение", bindMConfirm, $('#login_block'), 1, true);
			return false;
		}
		
		obj.hide();
		blog_id = $(this).parents('.incont_item:eq(0)').attr('realid');
		$.post("/blograting", { type: 'rating', blog_id: blog_id }, function(data) {
			if(data == '1') {
				obj.hide();
				rating_block = obj.next('span.bold');
				current_rating = parseInt(trim(rating_block.text()));
				rating_block.text(' ' + (current_rating+1) + ' ').removeClass('black').addClass('blue');
			}
		});
		return false;
	});
	
	$('.incont_item').each(function() {
		if(userID == $(this).attr('author') || !userID) {
			$(this).find('.complain:first').hide();
			$(this).find('.plusminus:first').hide();
		} else {
			var cid = $(this).attr('realid');
			if(blogsRatedByUser && in_array(cid, blogsRatedByUser))
				$(this).find('.plusminus:first').hide().next('span:first').removeClass('black').addClass('blue');
			
			if(blogsComplainedByUser && in_array(cid, blogsComplainedByUser))
				$(this).find('.complain:first').hide();
		}
	});
});

function closeDialog() {
	$('#dialogWindow').hide();
	$('#dialogWindow input, #dialogWindow select').val('');
}

function bindBlogTypesSelector() {
	$('.blog_types a').live('click', function() {
		$('.blog_new input[name=type]').val($(this).attr('realid'));
		$('.blog_types a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
}

function openModalWindow(title, height) {
	timer = 300;

	t = $('.blog_new').offset().top;
	l = $('.blog_new').offset().left;
	
	$('#dialogTitle').text( title );
	$('#dialogWindow #dialogMain').css({ 'width': '0px', 'height': '0px' }).animate({ 'height': height + 'px', 'width': '395px'}, timer);
	$('#dialogTop, #dialogBottom').css({ 'width': '0px', 'height': '0px' }).animate({ 'height': '14px', 'width': '425px'}, timer+10);
	$('#dialogLeft, #dialogLeft img, #dialogRight, #dialogRight img').css({ 'width': '0px', 'height': '0px' }).animate({ 'height': (height+2) + 'px', 'width': '14px'}, timer);
	$('#dialogWindow').css({ 'width': '0px', 'height': '0px', 'top': '240px', 'left': '400px' }).show().animate({ 'width': '425px', 'height': (height+28) + 'px', 'top': '200px', 'left': '300px' }, timer);
}
