$(document).ready(function(){
	$.ajaxSetup({
		url: "/includes/request.php?lang="+$('#lang').attr('value'),
		timeout: 5000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded"
	});
	$('a.banner-but, a.context-but').click(function(){
		var id = $(this).attr('name');
		var action = $(this).attr('rel');
		var module;
		if ($(this).attr('class') == 'banner-but') module = 'banner';
		else module = 'context';
		if ($('#select_'+id).attr('value') == action && $('#banner_content_'+id).html())
			$('#banner_content_'+id).toggle('fast', function(){$('#close_'+id).toggle();});
		else
		{
			$('#banner_content_'+id).html('<img src="/img/loading.gif">');
			$('#banner_content_'+id).show('fast', function(){$('#close_'+id).show();});
			$.ajax({
				data:'module='+module+'&action='+action+'&id='+id,
				success:function(XMLHttpRequest, textStatus){
					$('#banner_content_'+id).html(XMLHttpRequest);
					$('#select_'+id).attr('value', action);
				}
			});
		}
	});
	$('input.close').click(function(){$('#banner_content_'+$(this).attr('name')).hide('fast'); $(this).hide();});
	$('#ban-add').click(function(){$('#code').hide('fast'); $('#buy').hide('fast'); $('#add').toggle('fast');});
	$('#ban-buy').click(function(){$('#code').hide('fast'); $('#add').hide('fast'); $('#buy').toggle('fast');});
	$('#ban-code').click(function(){$('#add').hide('fast'); $('#buy').hide('fast'); $('#code').toggle('fast');});
	$('input.put-date').attachDatepicker({yearRange: '-10:+0'});
	$('#birth_dt').attachDatepicker({yearRange: '-50:+10'});
	$('#wedd_dt').attachDatepicker({yearRange: '-60:+10'});
	$('input.birth').attachDatepicker({yearRange: '-60:+10'});
	$("select.tender-calc").change(function(){
		$.ajax({
			data:'module=tender&action=calc&d='+$("#calc_d").attr('value')+
				 '&m='+$("#calc_m").attr('value')+'&y='+$("#calc_y").attr('value'),
			success:function(XMLHttpRequest, textStatus){
				$("#cost").html(XMLHttpRequest);
			}
		});
	});
	
	$("div.tp").mouseover(function(){
		var data;
		if ($(this).hasClass('user-preview')) data = '&type=user&user_id='+$(this).attr('name')+'&ava_id='+$(this).attr('id');
		else if ($(this).hasClass('group-preview')) data = '&type=group&group_id='+$(this).attr('name');
			else return;
		var span = $(this).children('div').children('span');
		if (span.html()) return;
		span.html('Loading...');
		$.ajax({
			data:'module=menu'+data,
			success:function(request, textStatus){
				span.html(request);
			}
		});
	});
	$("#close_msg").click(function(){
		var url = document.location;
		$.ajax({
			data:'module=close_msg&id='+$(this).attr('name'),
			success:function(request, textStatus){
				if (request) document.location = url;
				else $("#new_message").slideUp('slow');
			}
		});
	});
	$("input.shop-buy").click(function(){
		$(this).attr('src', '/img/to_backet_gray.jpg').removeClass('shop-buy');
		$.ajax({
		   data:"module=backet&action=add&id="+$(this).attr("name"),
		   success: function(request, textStatus){
			   var data = request.split('&');
			   if ($("#small-backet").html())
			   {
				   $("#backet_count").html(data[0]);
				   $("#backet_cost").html(data[1]);
			   }
			   else $("#small-backet").css({'display':'none'}).html(data[3]).show('slow');
			   $("#backet_name").html(data[2]);
			   $("#backet").css({'top':getInnerHeight()/3 + getScrollY()});
			   $("#backet").show(20, function(){
					$("#backet").animate({'top':getInnerHeight()/4 + getScrollY()},1500,function(){$("#backet").hide(200);})
				});
		   }
	   });
	});
	$("input.shop-del").click(function(){
		$.ajax({
		   data:"module=backet&action=del&id="+$(this).attr("name"),
		   success: function(request, textStatus){
			   var data = request.split('&');
			   $("#backet_count, #order_count").html(data[0]);
			   $("#backet_cost, #order_cost").html(data[1]);
			   if (data[0] == 0)
			   {
				   $("#small-backet").slideUp("slow");
				   $("#order").slideUp("slow");
			   }
		   }
	   })
		$(this).parent().parent().remove();
	});
	$("input.shop-set").keyup(function(){
		$.ajax({
		   data:"module=backet&action=set&id="+$(this).attr("name")+'&count='+$(this).attr('value'),
		   success: function(request, textStatus){
			   var data = request.split('&');
			   $("#backet_count, #order_count").html(data[0]);
			   $("#backet_cost, #order_cost").html(data[1]);
			   if (data[0] == 0)
			   {
				   $("#small-backet").slideUp("slow");
				   $("#order").slideUp("slow");
			   }
		   }
	   })
	});
	$("img.simply-image").click(function(){
		var img_link = $(this).attr('src').replace('s.jpg', 'b.jpg');
		void window.open('/simimage.php?src='+img_link, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=550,left=20,top=20');
	});
	$("input.reg-select").click(function(){
		if ($(this).attr('id') == 'user') $("#firm").removeAttr('disabled');
		else $("#user").removeAttr('disabled');
		$(this).attr("disabled","disabled");
		$("#reg-content").html('<img src="/img/loading.gif">');
		$.ajax({
			data:'module=registration&action=get&type='+$(this).attr('id'),
			success:function(request, textStatus){
				$("#reg-content").html(request);
			}
		});
	});
	$("span.locked").hover(function(){$(this).children('div').show();},function(){$(this).children('div').hide();});
	$("span.locked div").hover(function(){$(this).show();},function(){$(this).hide();});
	$("#kon-part").click(function(){
		if ($("#kon-part-content").html()) $("#kon-part-content").slideToggle('slow');
		else
		{
			$.ajax({
				data:'module=konkurs&action=get_uimages&id='+$(this).attr('name'),
				success:function(request, textStatus){
					$("#kon-part-content").hide().html(request).slideDown('slow');
				}
			});
		}
	});
	$("a.tender-part").click(function(){
		$.ajax({
			data:'module=tender&action=part&id='+$(this).attr('name'),
			success:function(request, textStatus){
				if (request) alert(request);
			}
		});
	});
	$("#pass-change-btn").click(function(){
		$("#pass-change").toggle('slow', function(){
			if ($("#pass-change").css('display') == 'none') $('#change_pass').attr('value', 0);
			else $('#change_pass').attr('value', 1);
		});
	});
});

function Relation(action, id)
{
	if (action == 'add_friend' && !confirm('Хотите пригласить этого пользователя в друзья?')) return;
	$.ajax({
		data:'module=relation&action='+action+'&id='+id,
		success:function(request, textStatus){
			if (request)
				switch(action)
				{
					case 'confirm_friend':
					case 'dismiss_friend':
						$('#inputs_'+id).html(request);
						break;
					case 'add_black':
						if ($('#inputs_'+id).html())
							$('#inputs_'+id).html('<div style="padding:15px; background:#FF39C3; color:#FFFFFF; text-align:center; width:150px"><b>'+request+'</b></div>');
						else alert(request);
						break;
					default: alert(request);
				}
		}
	});
}

function getScrollY() 
{
    scrollY = 0;    
    if (typeof window.pageYOffset == "number") {
        scrollY = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    }  else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop; 
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }
    return scrollY;
}
  
function getInnerHeight()
{
    height = 0;
    if (window.innerHeight) {
        height = window.innerHeight - 18;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    return height;
}

function PayCheck(how_match)
{
	$.ajax({
		data:'module=pay_check&how_match='+how_match,
		success:function(request, textStatus){
			if (request == '') alert('Нет ответа от сервера');
			else
				if (confirm(request)) document.forms['forpay'].submit();
		}
	});
}

function Konkurs(action, kon_id, photo_id, obj)
{
	$.ajax({
		data:'module=konkurs&action='+action+'&id='+kon_id+'&photo='+photo_id,
		success:function(request, textStatus){
			obj.attr("disabled","disabled").parent('div').children('img').css({'border-color':(action=='add')?'#FF39C3':'#FFE3FF'});
		}
	});
}

function ShowPayForm()
{
	if ($('#pay-form').html()) return;
	$.ajax({
		data:'module=show_pay_form',
		success:function(request, textStatus){
			$('#pay-form').html(request);
		}
	});
}

function City(country, id)
{
	if (id == null) id = 'user_city';
	$.ajax({
		data:'module=city&country='+country,
		success:function(request, textStatus){
			$('#'+id).html(request);
		}
	});
}

function Pay(vars)
{
	window.open('/pay.php?lang='+$('#lang').attr('value')+'&'+vars, 'Pay', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=700,height=600,left=50,top=100');
}

//	--------------------------------------------------------------------------------

function Prep_Req()
{
	if (window.XMLHttpRequest) { http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }}
	else if (window.ActiveXObject)
	{ 	try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}}
	}
	if (!http_request) { alert('Невозможно создать XMLHTTP объект'); return false; }
	return http_request;
}

function ServiceCheck(obj, s_id)
{
	var id = obj.id;
	var action = obj.name;
	http_request = Prep_Req();
	http_request.open('POST', 'includes/serv_check.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('what='+action+'&id='+id+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				if (http_request.responseText == '1')
					if (action == 'add')
					{
						$('img.serv-change').attr('src', 'img/unchecked.gif').attr('name', 'add');
						obj.src = 'img/checked.gif';
						obj.name = 'del';
					}
					else
					{
						obj.src = 'img/unchecked.gif';
						obj.name = 'add';
					}
			}
			else alert('Сервер не отвечает.');
	}
}

function ChangeDay(id, date, s_id)
{
	var action = 'set';
	if (document.getElementById('day_ch_'+id).checked == false) action = 'del';
	http_request = Prep_Req();
	http_request.open('POST', 'includes/calendar.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('action='+action+'&date='+date+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				if (http_request.responseText == '1')
				{
					if (action == 'set') document.getElementById('day_'+id).className = 'busy';
					else document.getElementById('day_'+id).className = null;
				}
			}
			else alert('Сервер не отвечает.');
	}
}

function ListCalendar(id, date, s_id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/calendar.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('action=list&u_id='+id+'&date='+date+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('calendar').innerHTML = http_request.responseText;
			}
			else alert('Сервер не отвечает.');
	}
}

function CalendarShow(act, s_id)
{
	if (act == true) act = 1;
	else act = 0;
	http_request = Prep_Req();
	http_request.open('POST', 'includes/calendar.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('action=show&show='+act+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
			}
			else alert('Сервер не отвечает.');
	}
}

function GroupMenu(id, group, s_id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/group_menu.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&group_id='+group+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('group_menu_'+id).innerHTML = http_request.responseText;
			}
	}
}

function Horo(sign1, sign2)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/horo.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('sign1='+sign1+'&sign2='+sign2);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('horo_view').innerHTML = http_request.responseText;
			}
	}
}

function BanCh(id, type, s_id)
{
	var loc = document.getElementById('ban_local_'+id).checked - 0;
	var net = document.getElementById('ban_network_'+id).checked - 0;
	var loc_rub = document.getElementById('local_rub_'+id).value;
	var sh_day = document.getElementById('show_per_day_'+id).value;
	var p = document.getElementById('ban_pause_'+id).checked - 0;
	http_request = Prep_Req();
	http_request.open('POST', 'includes/ban_change.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&type='+type+'&loc='+loc+'&net='+net+'&loc_rub='+loc_rub+'&sh_day='+sh_day+'&p='+p+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				if (http_request.responseText) alert(http_request.responseText);
			}
			else alert('Сервер не отвечает.');
	}
}

function SendMessage(id)
{
	void window.open('/write.php?lang='+$('#lang').attr('value')+'&id='+id, 'SendMessage', 'resizable=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=650,height=600,left=20,top=20');
	return false;
}

function SimPreview(obj)
{
	var img_link = obj.src.substr(0, obj.src.length-5);
	void window.open('/simimage.php?src='+img_link+'b.jpg', 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=550,left=20,top=20');
	return false;
}

function Preview(id)
{
	void window.open('/image.php?lang='+$('#lang').attr('value')+'&id='+id, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=650,left=20,top=20');
	return false;
}

function PreviewKon(id, kon_id, s_id)
{
	void window.open('/image_kon.php?lang='+$('#lang').attr('value')+'&id='+id+'&kon_id='+kon_id, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=650,left=20,top=20');
	return false;
}

function VideoPreview(id, s_id)
{
	void window.open('/video.php?lang='+$('#lang').attr('value')+'&id='+id+'&sid='+s_id, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=650,left=20,top=20');
	return false;
}

function getCaretPos(obj)
{
  obj.focus();
  
  if(obj.selectionStart) return obj.selectionStart;//Gecko
  else if (document.selection)//IE
  {
    var sel = document.selection.createRange();
    var clone = sel.duplicate();
    sel.collapse(true);
    clone.moveToElementText(obj);
    clone.setEndPoint('EndToEnd', sel);
    return clone.text.length;
  }
  
  return 0;
}

function DisplaySearch(id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/search.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('search_form').innerHTML = http_request.responseText;
			}
	}
}

function checkAll(oForm, cbName, checked)
{
	for (var i=0; i < oForm[cbName].length; i++) oForm[cbName][i].checked = checked;
}

function BanCalc(value, coef)
{
	var res = value * coef;
	res = res.toFixed(2)
	document.getElementById('cost').innerHTML = res;
}

function BlockNet()
{
	if (document.getElementById('ban_size1').checked == true)
	{
		document.getElementById('ban_network').checked = false;
		document.getElementById('ban_network').disabled = true;
	}
	else
	{
		document.getElementById('ban_network').disabled = false;
	}
}
