//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
/*
#########################################
#           http://vru4ki.ru/           #
#             version 1.0.1             #
#            date 23.09.2009            #
#########################################
*/
var AJAX_PATCH = '/lib/php/ajax.php';
//jqwery
$(document).ready(function(){
	$('#date').datepicker({
		 appendText: ' Формат (дд-мм-гггг)',
		 changeYear: true,
		 dateFormat: 'dd-mm-yy',
		 dayNamesMin: ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'],
		 minDate: new Date(1950, 1 - 1, 1),
		 monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		 monthNamesShort: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		 nextText: 'Вперёд',
		 prevText: 'Назад',
		 shortYearCutoff: 50,
		 showCurrentAtPos: 2,
		 showMonthAfterYear: true,
		 showOn: 'focus',
		 yearRange: '1950:2010'
	});
	//
	jQuery('#end_photo').jcarousel({
        start: 1,
		vertical: true
    });
	//
	$("#photo").sortable({opacity: 0.6});
	$("#photo").disableSelection();
	$("#tabs").tabs();
	//online
	$(".on_line a").click(
		function(e){
			$(this).prev("div").show();
		}
	);
	$(".on_line a").blur()
	$(".on_line div").bind("mouseleave",function(e){
			$(this).css("display","none");
		}
	);
	//
	xOffset = 10;
	yOffset = 20;
	$(".tooltip").hover(
		function(e){
			this.t = this.title;
			this.title = "";									  
			$("body").append("<p id='tooltip'>"+ this.t +"</p>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
		},
		function(){
			this.title = this.t;		
			$("#tooltip").remove();
	});	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	
	//
	$(".ssylka img").hide();
	$(".ssylka").hover(
		function(e){
			$(this).contents("img").show();
		},
		function(){
			$(this).contents("img").hide();
	});	
	
});

//FUNCTION
function close_view_photo(){$("#view_photo").remove();}
function ffocus(x){x.focus(); x.select();}
function v_null(x){x.value='';}
function openDiv(id){if(document.getElementById(id).style.display=="none"){document.getElementById(id).style.display = "";
}else{document.getElementById(id).style.display = "none";}}
function openDivJq(id_a,id){
	$("#"+id_a).toggle(function () {$("#"+id).show('blind');},function (){$("#"+id).hide('blind');});
}
function closeDivJq(id){
	$("#"+id).hide('blind');
}
function resizeImg(width){
	var ilist = document.images;
	for(var i = 0; i < ilist.length; i++) {
    	if (ilist[i].width>width && ilist[i].id == 'bloging') ilist[i].width=width;
	}
}
//AJAX
//Список городов
function on_cange(){
	id=document.getElementById('country').options[document.getElementById('country').selectedIndex].value;
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=city_list&id="+id,
		success: function(msg){
			if(msg) document.getElementById('city').innerHTML = msg;
			else alert("Ошибка XML, попробуйте попозже.");		
		}
	});
}
//Редактирование фото
function edit_photo(id){
	document.getElementById('ops').value = document.getElementById("photos_"+id).title;
	$("#dialog_edit").dialog({
		bgiframe: true,
		resizable: false,
		height:230,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Сохранить': function() {
				$.ajax({
					type: "POST",
					cache: false,
					url: AJAX_PATCH,
					data: "function=edit_photo&id="+id+"&ops="+document.getElementById('ops').value,
					success: function(msg){
						document.getElementById("photos_"+id).title=msg;
					}
				});
				$(this).dialog('close');
			},
			'Отмена': function() {
				$(this).dialog('close');
			}					
		}
	});
	$("#dialog_edit").dialog('open');
}
//Удаление фотографии
function del_photo(id){
	$("#dialog_del").dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Отмена': function() {
				$(this).dialog('close');
			},
			'Удалить': function() {
				$.ajax({
					type: "POST",
					cache: false,
					url: AJAX_PATCH,
					data: "function=del_photo&id="+id,
					success: function(msg){
						$("#photo_"+id).hide('pulsate');
					}
				});
				$(this).dialog('close');
				
			}
		}
	});
	$("#dialog_del").dialog('open');
}
//просмотр фотографий
function open_photo(file,id,ops){
	//name="<b>"+document.getElementById('photo_'+id).title+"</b>";
	document.getElementById('photo_view').innerHTML = 'Нравится фото? <a target="_blank" href="http://service.smscoin.com/js/sendpic/index.html?sid=2347&url=http://vru4ki.ru/data/photo/'+file+'">Скачай фото любимца себе на телефон</a><br><img src="http://vru4ki.ru/data/photo/'+file+'"><br />'+ops;
	$("#photo_view").dialog({
		height:650,
		width:650,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Закрыть': function() {
				$(this).dialog('close');
				document.getElementById('photo_view').innerHTML = '';
			}
		}
	});
	$("#photo_view").dialog('open');
}
//Комментарии
function view_comment(id,type){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=view_comment&id="+id+"&type="+type,
		success: function(msg){
			document.getElementById('comment').innerHTML = msg;
		}
	});
}
//Новый комментарий
function new_comment(type,id_user,id_post,type_comment){
	if(type=='open'){
		text ="<div align=left><textarea id='comment_text' class='input' style='width:70%; height:70px'></textarea><br>";
		text+="<input type='button' class='input' ";
		text+="onclick='new_comment(\"save\","+id_user+","+id_post+",\""+type_comment+"\")' value='Отправить'></div>";
		document.getElementById('new_comment').innerHTML = text;
	}else{
		text = document.getElementById('comment_text').value;
		document.getElementById('new_comment').innerHTML = 'Сохранение';
		$.ajax({
			type: "POST",
			cache: false,
			url: AJAX_PATCH,
			data: "function=new_comment&id_user="+id_user+"&id_post="+id_post+"&type="+type_comment+"&text="+text,
			success: function(msg){
				view_comment(id_post,type_comment);
				new_comment("open",id_user,id_post,type_comment);
			}
		});
	}
}
//проверка логин
function on_login(login){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=on_login&login="+login,
		success: function(msg){
			if(msg.length>1){
				return 1;
			}else{
				return 2;
			}
		}
	});
}
//Регистрация
function reg(){
	var login	= document.getElementById('login');
	var mail	= document.getElementById('mail');
	var pass	= document.getElementById('password');
	var pass2	= document.getElementById('password2');
	var error	= document.getElementById('error');
	error.innerHTML = '';
	
	login.style.border = '1px solid #688189';
	mail.style.border = '1px solid #688189';
	pass.style.border = '1px solid #688189';
	pass2.style.border = '1px solid #688189';
	if(login.value.length>3){
		$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=on_login&login="+login.value,
		success: function(msg){
			if(msg){
				error.innerHTML = msg;
				login.style.border = '2px solid #eea301';
			}else{
				$.ajax({
				type: "POST",
				cache: false,
				url: AJAX_PATCH,
				data: "function=search_mail&mail="+mail.value,
				success: function(msg){
					if(msg){
						error.innerHTML = msg;
						mail.style.border = '2px solid #eea301';
					}else{
						if(pass.value == pass2.value && pass.value.length>3)$("#formreg").submit();
						else{
							error.innerHTML = 'Пароль слишком короткий или введён неверно';
							pass.style.border = '2px solid #eea301';
							pass2.style.border = '2px solid #eea301';
						}
					}
				}
				});
			}
		}
		});
	}else{
		login.style.border = '2px solid #eea301';
		error.innerHTML = 'Не введен Логин или он короче 3-х символов';
	}
}
function toolbar(Tag,Tag2,AreaName){
	if (window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) {
		allAreas = document.getElementsByName(AreaName);
		var doc = allAreas[0];
		doc.focus();
		sel = document.selection.createRange();
		sel.text = Tag+sel.text+Tag2;
		doc.focus();
    }else{
		allAreas = document.getElementsByName(AreaName);
		var doc = allAreas[0];
		var ss = doc.scrollTop;
		sel1 = doc.value.substr(0, doc.selectionStart);
		sel2 = doc.value.substr(doc.selectionEnd);
		sel = doc.value.substr(doc.selectionStart,
		                  doc.selectionEnd - doc.selectionStart);
		var text = doc.firstChild;
		doc.value = sel1 + Tag + sel + Tag2 + sel2;
		selPos = Tag.length + sel1.length + sel.length + Tag2.length;
		doc.setSelectionRange(sel1.length, selPos);
		doc.scrollTop = ss;
	}	
}
function forum_post(type,id){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=forum_post&type="+type+"&id="+id,
		success: function(msg){
			document.getElementById('panel_'+id).innerHTML = msg;
		}
	});
}
