var errors;

$(function(){
	$('#dialog-error-alert').dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		open: function(data) {
			$('#dialog-error-alert').append(errors);
		},
		close: function() { $('#dialog-error-alert').text(''); errors = null;},
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	$('#dialog-add-imges').dialog({
		autoOpen: false,
		width: 360,
		resizable: false,
		draggable: false,
		open: function() {
//			$('#dialog-add-imges').append("<form id='form-add-imges'><ul style='list-style: none;'><li><label>Выбирете картинку</label></li><li><input type='file' name='images[0]' size='30' /></li><li><label>Описание</label></li><li><input type='text' name='title' value='' maxlength='128' /></li></ul></form>");
			$('#dialog-add-imges').load("/modules/images/AddImageForm.php");
		},
		close: function() {
			$('#dialog-add-imges').text('');
		},
		buttons: {
			'Отправить': function() {
				var options = {
					url: '/ajaxAddImg.php',
					type: 'post',
					beforeSubmit: function(data) {/*alert(data.toSource())*/},
					success: function(data) {
						if (data == 'true') {
							$('#dialog-add-imges').dialog('close');
						} else {
							errors = data;
							$('#dialog-error-alert').dialog('open');
						}
					}
				}; 
				$('#form-add-imges').ajaxSubmit(options);
			}
		}
	});

	$('#dialog-add-imges-url').dialog({
		autoOpen: false,
		width: 360,
		resizable: false,
		draggable: false,
		open: function() {
			$('#dialog-add-imges-url').append("<form id='form-add-imges'><ul style='list-style: none;'><li><label>URL</label></li><li><input type='text' name='URL' value='' /></li><li><label>Описание</label></li><li><input type='text' name='title' value='' maxlength='128' /></li></ul></form>");
		},
		close: function() {
			$('#dialog-add-imges-url').text('');
		},
		buttons: {
			'Отправить': function() {
				var options = {
						url: '/ajaxAddImg.php',
						type: 'post',
						success: function(data) {
							if (data == 'true') {
								$('#dialog-add-imges-url').dialog('close');
							} else {
								errors = data;
								$('#dialog-error-alert').dialog('open');
							}
						}
				}; 
				$('#form-add-imges').ajaxSubmit(options);
			}
		}
	});
	
	$('#link-add-imges').click(function(){
		$('#dialog-add-imges').dialog('open');
		return false;
	});
	
	$('#link-add-imges-url').click(function(){
		$('#dialog-add-imges-url').dialog('open');
		return false;
	});

	$('ul#icons li.ui-corner-all, #link-send-to-friend, a.ui-corner-all, .submit').hover(
		function() { $(this).addClass('ui-state-hover'); },
		function() { $(this).removeClass('ui-state-hover'); }
	);

	$('#dialog-send-to-friend').dialog({
		autoOpen: false,
		width: 360,
		resizable: false,
		draggable: false,
		buttons: {
			'Отправить': function() {
				var options = {
					url: '/ajaxSendToFriend.php',
					type: 'post',
					success: function() {
						$('#dialog-send-to-friend').dialog('close');
					}
				}; 
				$('#form-send-to-friend').ajaxSubmit(options);
			}
		}
	});

	$('#link-send-to-friend').click(function(){
		$('#dialog-send-to-friend').dialog('open');
		return false;
	}); 
	
	$('#comments-block').accordion({
		collapsible: true
	})
});

function Vote(id, mark) {
	$.ajax({
		'url':'/ajaxImageVote.php',
		'data':{'vote': mark, 'id': id},
		'type':'POST', 
		'success': function(data) { 
			if (data != false) {
				$('#rating').html(data);
			}
		}
	});
	return false;
}

function VoteComment(id, mark) {
	$.ajax({
		'url':'/ajaxCommentVote.php',
		'data':{'vote': mark, 'id': id},
		'type':'POST', 
		'success': function(data) { 
			if (data != false) {
				var node = '#comment-rating-' + id;
				$(node).html(data);
			}
		}
	});
	return false;
}
function ReplaceDescription(id1, id2) {
	try {
		document.getElementById(id1).style.display = 'none';
		document.getElementById(id2).style.display = 'inline';
	} catch (e) {}
}