//<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/ajax.php';
//jqwery
$(document).ready(function(){
	$(".photo_panel a").hover(
		function(e){
			$(this).children("div").animate({opacity: 0.0}, 1500 );
			//$(this).children('div').hide(2000)
		},
		function(){
			$(this).children("div").animate({opacity: 0.9}, 1 );
			//$(this).children('div').show();
	});	
});

//FUNCTION
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 animations(id){$(id).show(2000);}
//AJAX
function golos(server,photo,type){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=golos&server="+server+"&photo="+photo+"&type="+type,
		success: function(msg){
			document.getElementById('golos').innerHTML = msg;
		}
	});
}
function upload(server,pi){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=upload&server="+server,
		success: function(msg){
			if(msg=='exit'){
				document.getElementById('statist').innerHTML = "<b>Обработка завершена</b>"+document.getElementById('statist').innerHTML;
				document.getElementById('progres').style.width = "100%";
			}else{
				con=parseInt(document.getElementById('count_text').innerHTML);
				document.getElementById('count_text').innerHTML = con+1;
				proc=con/pi;
				document.getElementById('progres').style.width = proc+"%";
				document.getElementById('statist').innerHTML = msg + document.getElementById('statist').innerHTML;
				upload(server,pi);
			}
		}
	});
}

function reiting(server,photo){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=reiting&server="+server+"&photo="+photo,
		success: function(msg){
			document.getElementById('reiting').innerHTML = msg;
		}
	});
}

//Список городов
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 view_comment(id,type,view_type){
	$.ajax({
		type: "POST",
		cache: false,
		url: AJAX_PATCH,
		data: "function=view_comment&id="+id+"&type="+type+"&view_type="+view_type,
		success: function(msg){
			if(view_type=='view')document.getElementById('comment').innerHTML = msg;
			else document.getElementById('comment_count').innerHTML = " ("+msg+")";
		}
	});
}
//Новый комментарий
function new_comment(type,id_post,type_comment){
	if(type=='open'){
		text ='<div id="stat"></div><table cellpadding="2" cellspacing="2" width="100%"><tr><td width="100" valign="top">Ваше имя</td>';
		text+='<td><input type="text" id="UserName" /></td></tr><tr><td valign="top">Ваш e-mail</td>';
		text+='<td valign="top"><input type="text" id="UserMail" /></td></tr><tr><td valign="top">Ваш сайт</td>';
        text+='<td valign="top"><input type="text" id="UserWWW" /><br />без http://</td></tr><tr><td colspan="2">';
        text+='Решите пример: <strong>15+7 = </strong><input size="3" type="text" id="robot" /><textarea id="comment_text" ';
		text+='style="width:100%; height:70px"></textarea>';
        text+="<input type='button'onclick='new_comment(\"save\",\""+id_post+"\",\""+type_comment+"\")' value='Отправить'>";
        text+='</td></tr></table>';
		document.getElementById('new_comment').innerHTML = text;
	}else{
		document.getElementById('stat').innerHTML = '';
		text = document.getElementById('comment_text').value;
		robot = document.getElementById('robot').value;
		UserName = document.getElementById('UserName').value;
		UserMail = document.getElementById('UserMail').value;
		UserWWW = document.getElementById('UserWWW').value;
		if(robot==22){
			if(UserName.length>0 && text.length>0 ){
				hrefe ="function=new_comment&text="+text+"&id_post="+id_post+"&type="+type_comment+"&UserName="+UserName;
				hrefe+="&UserMail="+UserMail+"&UserWWW="+UserWWW;
				document.getElementById('new_comment').innerHTML = 'Сохранение';
				$.ajax({
					type: "POST",
					cache: false,
					url: AJAX_PATCH,
					data: hrefe,
					success: function(msg){
						
						view_comment(id_post,type_comment);
						new_comment("open",id_post,type_comment);
						document.getElementById('stat').innerHTML = 'Ваш комментарий отправлен на модерацию';
					}
				});
			}else document.getElementById('stat').innerHTML = 'Не заполнено одни из обезательных порлей: "Ваше имя" или сообщение!';
		}else document.getElementById('stat').innerHTML = 'Не верное решение примера!';
	}
}
//Регистрация
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-х символов';
	}
}
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;
	}	
}

