//<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';
var haracter= Array('А он вообще живой?',
		"Абсолютно невозмутим",
		"Спокойный как тапок",
		"А я спать люблю",
		"Невозмутимый",
		"Спокойный",
		"Люблю побегать и поспать",
		"На свете столько интересного, и я везде должен сунуть свой нос",
		"А как это &mdash; сидеть на месте?",
		"Всех догоню!",
		"А он вообще спит?");
//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'
	});
	//
	$('#enerje').slider({
		animate: true,
		max: 10,
		min: 0,
		step: 1,
		value: $("#enr").val(),
		slide: function(event, ui) {
			$("#enr").val(ui.value);
			document.getElementById('enerje_view_text').innerHTML = haracter[ui.value];
		}
	});
	
	jQuery('#end_photo').jcarousel({
        start: 1,
		vertical: true
    });
	
	//Presents
		$(".present_link_open").click(function (e) {
			d=Math.max(document.documentElement.offsetHeight,document.body.offsetHeight);
			$('#present_list div span').hide();
			$('#presenrs').show();
			$('#presenrs').css({height:d+50+'px'});
			$('#presents_form').show();
			$('OBJECT, PARAM, EMBED').hide();
		});
		$(".present_link_close").click(function () {
			$('#presenrs').hide();
			$('#presents_form').hide();
			$('OBJECT, PARAM, EMBED').show();
		});
		$("#present_list .list").hover(
			function(e){
				$(this).children('div').show();
			},
			function(){
				$(this).children('div').hide();
		});	
		$("#present_list .list").click(function () {
			$('#present_list div span').hide();
			$(this).children('span').show();
			id=this.id;
			document.getElementById('present_id').value = id;
		});
		//View
		$("#present_view .list").hover(
			function(e,z){
				//alert(e.pageTop);
				l=$(this).children('div').position().left;
				t=$(this).children('div').position().top;
				//alert(t);
				$(this).children('div').css({left:l-50+'px',top:t-30+'px'});
				//
			},
			function(){
				l=$(this).position().left;
				t=$(this).position().top;
				$(this).children('div').css({left:l+'px',top:t+'px'});
		});	
		
			//.css("left",(e.pageX + yOffset) + "px");
	//
	$("#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");
		}
	);
	//PHOTO VIEW
	$(".view_photo").click(
		function(e){
			this.t = this.title;
			this.img = this.alt;
			$("body").append("<div id='view_photo' align='center'>Нравится фото? <b><a href=\"#\" class=\"sendpic\" rel=\"http://vru4ki.ru"+ this.img +"\">Скачай фото любимца себе на телефон</a></b><br><img id='view_photo1' onClick='close_view_photo()' src='"+ this.img +"' /><br><b>"+ this.t +"</b></div>");
			$("#view_photo")
				.css("top",(e.pageY - 280) + "px")
				.css("left","30%")
				.fadeIn("fast");
			sendpic_clientInit();
		}
	);
		
	//
	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");
	});	
	//
	xOffset = 10;
	yOffset = 200;
	$(".tooltip_r").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_r").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY + xOffset) + "px")
			.css("left",(e.pageX - yOffset) + "px")
	});	
		
	
	xOffset = 10;
	yOffset = 20;
	$(".tooltip_img").hover(
		function(e){
			this.t = this.title;
			this.title = "";									  
			$("body").append("<p id='tooltip'><img src='"+ 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");
	});	
	//
	
	$("#autorison").toggle(
		function () {
			$(".registrat").css("filter","alpha(opacity=100)");
			$(".registrat").css("opacity","1");
			$('#in').show('blind');
		},
		function () {
			$("a").blur();
			$(".registrat").css("filter","alpha(opacity=80)");
			$(".registrat").css("opacity",".8");
			$('#in').hide('blind');
		}
	);
	//
	$('#accordion').accordion({ 
		animated: 'bounceslide',
		autoHeight: false
	});
	//News/article for index page
	$(".prev_index div").hide();
	$(".prev_index .open_prev").toggle(
		function () {
			//$(".prev_index div").hide();
			$(this).next("div").show('blind');
		},
		function () {
			$(this).next("div").hide('blind');
		}
	);
	
});

//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
//ajax present
function present(kto,type,komy){
	id=document.getElementById('present_id').value;
	ops=document.getElementById('present_ops').value;
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=present&id="+id+"&ops="+ops+"&kto="+kto+"&type="+type+"&komy="+komy,
		success: function(msg){
			if(msg) document.getElementById('present_info').innerHTML = msg;
			else{
				$('#presenrs').hide();
				$('#presents_form').hide();
				$('OBJECT, PARAM, EMBED').show();
			}
		}
	});
}
//Список городов
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 group(id_user,id_group,type){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=group&id_user="+id_user+"&id_group="+id_group+"&type="+type,
		success: function(msg){
			if(msg) document.getElementById('in_group_'+id_group).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 del_album(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_album&id="+id,
					success: function(msg){
						$("#photo_"+id).hide('pulsate');
					}
				});
				$(this).dialog('close');
				
			}
		}
	});
	$("#dialog_del").dialog('open');
}

//Голосование
function golos_plus(id_photo,id_user,type){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=golos_plus&id_photo="+id_photo+"&id_user="+id_user+"&type="+type,
		success: function(msg){
			document.getElementById('pr_'+id_photo).innerHTML = msg;
			document.getElementById('prcl_'+id_photo).innerHTML = 'Вы уже проголосовали';
		}
	});
}
//платное и бесплатное объявление
function view_cena(){
	id=document.getElementById('type').options[document.getElementById('type').selectedIndex].value;
	if(id=='free'){
		document.getElementById('cena_view').style.display = 'none';
		document.getElementById('cena').style.display = 'none';
	}else{
		document.getElementById('cena_view').style.display = '';
		document.getElementById('cena').style.display = '';
	}
}
//Рейтинг
function reyting(id_komy,id_kto,operator,type){
	if(type=='user')f='reyting_user';
	else f='reyting';
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function="+f+"&id_komy="+id_komy+"&id_kto="+id_kto+"&type="+type+"&operator="+operator,
		success: function(msg){
			if(msg){
				document.getElementById('dialog').innerHTML = msg;
				$("#dialog").dialog({
					bgiframe: true,
					resizable: false,
					modal: true,
					overlay: {backgroundColor: '#000',opacity: 0.5},
					buttons: {'Ок': function() {$(this).dialog('close');}}
				});
				$("#dialog").dialog('open');
			}
			else ireyting(id_komy,type,'reyting_');		
		}
	});
}
//Избранное
function selected(id_user,id_type,type){
	div=document.getElementById('selected_'+type+'_'+id_type);
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=selected&id_user="+id_user+"&id_type="+id_type+"&type="+type,
		success: function(msg){
			if(msg=='in')div.src='/template/images/system/in_selected.png';
			else div.src='/template/images/system/selected.png';	
		}
	});
}
//просмотр фотографий
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 ="<textarea id='comment_text' class='input' style='width:100%; height:70px'></textarea>";
		text+="<input type='button' class='input' ";
		text+="onclick='new_comment(\"save\","+id_user+","+id_post+",\""+type_comment+"\")' value='Отправить'>";
		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);
			}
		});
	}
}
//IРейтинг
function ireyting(id,type,div){
	span = document.getElementById(div+id);
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=i_reyting&id="+id+"&type="+type,
		success: function(msg){
			if(msg<0)span.style.color="#eea301";
			else span.style.color="#688189";
			span.innerHTML = msg;
		}
	});
}
//Список пород
function on_cange_vid(){
	id=document.getElementById('vid').options[document.getElementById('vid').selectedIndex].value;
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=poroda_list&id="+id,
		success: function(msg){
			if(msg) document.getElementById('poroda').innerHTML = msg;
			else alert("Ошибка XML, попробуйте попозже.");		
		}
	});
}
//Регистрация
function reg(){
	var name	= document.getElementById('name');
	var famil	= document.getElementById('famil');
	var mail	= document.getElementById('mail');
	var pass	= document.getElementById('password');
	var pass2	= document.getElementById('password2');
	var error	= document.getElementById('error');
	error.innerHTML = '';
	
	name.style.border = '1px solid #688189';
	famil.style.border = '1px solid #688189';
	mail.style.border = '1px solid #688189';
	pass.style.border = '1px solid #688189';
	pass2.style.border = '1px solid #688189';
	if(name.value.length>3){
		if(famil.value.length>3){
			$.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{
			error.innerHTML = 'Не введена фамилия или она короче 3-х символов';
			famil.style.border = '2px solid #eea301';
		}
	}else{
		name.style.border = '2px solid #eea301';
		error.innerHTML = 'Не введено имя или оно короче 3-х символов';
	}
}
//new Питомиц
function new_favorit(){
	var nick	= document.getElementById('nick');
	var vid		= document.getElementById('vid');
	var error	= document.getElementById('error');
	error.innerHTML = '';
	
	nick.style.border = '1px solid #688189';
	vid.style.border = '1px solid #688189';
	if(nick.value.length>1){
		id=document.getElementById('vid').options[document.getElementById('vid').selectedIndex].value;
		if(id!=0)$("#formreg").submit();
		else{
			error.innerHTML = 'Не выбран вид питомца';
			vid.style.border = '2px solid #eea301';
		}
	}else{
		error.innerHTML = 'Не введена кличка или она слишком короткая';
		nick.style.border = '2px solid #eea301';
	}
}

function edit_favorit(){
	var nick	= document.getElementById('nick');
	var error	= document.getElementById('error');
	error.innerHTML = '';
	
	nick.style.border = '1px solid #688189';
	if(nick.value.length>1)$("#formreg").submit();
	else{
		error.innerHTML = 'Не введена кличка или она слишком короткая';
		nick.style.border = '2px solid #eea301';
	}
}

function carma_group(id){
	carm=document.getElementById('group');
	val=carm.options[carm.selectedIndex].value;
	if(val!='my' && val!='project'){
		$.ajax({
			type: "POST",
			cache: false,
			url: AJAX_PATCH,
			data: "function=carma_group&user="+id+"&group_id="+val,
			success: function(msg){
				if(msg){
					document.getElementById('dialog').innerHTML = msg;
					carm.options[0].selected='selected';
					$("#dialog").dialog({
						modal: true,
						overlay: {backgroundColor: '#000',opacity: 0.5},
						buttons: {'Закрыть': function(){$(this).dialog('close');}}
					});
					$("#dialog").dialog('open');
				}
			}
		});
	}
}
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;
	}	
}

