function big_foto(w,h,win_name,left)
{
	var url='';
	var win_name;
	if (!win_name) win_name='big';
	if (!left) left=100;
	var features='menubar=no,scrollbars=yes,status=no,resizable=yes,top=8,left='+left+',width='+w+',height='+h;
	window.open(url,win_name,features);
}

function trim(value){return value.replace(/(^\s*)|(\s*$)/g, "")}



function buy()
{
	var params={};
	params['type']='buy';
	params['tema']= (($('#tema').length)? $('#tema').val():'');
	params['subject']= (($('#subject').length)? $('#subject').val():'');
	params['type_work']= (($('#type').length)? $('#type').val():'');
	params['lang']= (($('#lang').length)? $('#lang').val():'');
	params['format']= (($('#format').length)? $('#format').val():'');
	params['pages']= (($('#pages').length)? $('#pages').val():'');
	params['of_time']= (($('#of_time').length)? $('#of_time').val()+' '+$('#of_time_hours').val()+':00':'');
	if ($('#ufile').length>0 && $('#ufile').val()!='') params['ufile']=document.getElementById('ufile');

	params['fio']= (($('#fio').length)? $('#fio').val():'');
	params['email']= (($('#email').length)? $('#email').val():'');
	params['phone']= (($('#phone').length)? $('#phone').val():'');
	params['type_buy']= (($('#type_buy').length)? $('#type_buy').val():'');
	params['comment']= (($('#comment').length)? $('#comment').val():'');
	params['user']= (($('#user_id').length)? $('#user_id').val():'');
	params['work']= (($('#work_id').length)? $('#work_id').val():'');
	params['code']= (($('#code').length)? $('#code').val():'');
	
	loading_start('#orderBtn1, #orderBtn');
	JsHttpRequest.query(
		'/order.php',
		params,
		function(result, errors) {
			$('#div_errors').html(errors);
			loading_end();
			if (result) {
				if (result['res']==1) {
					//alert('Заказ успешно отправлен');
					alert(messages_arr.m1);
					if ($('#BuyBtn').length==1)
					{
						$('#BuyBtn').trigger('click');
						$('#BuyBtn').hide('normal');
					}
					$("form").each(function() {
						this.reset();
					});
				}
				if (result['res']==2) {
					//alert('Не все поля заполнены или заполнены неверно');
					alert(messages_arr.m2);
				}
				if (result['res']==3) {
					//alert('Неверный защитный код');
					alert(messages_arr.m3);
				}
            }
		},
		true
	);
}

var loading_start_ids;
function loading_start(ids)
{
	loading_start_ids=ids;
	$(loading_start_ids).attr('disabled',true);
	$(loading_start_ids).after("<span class='loading' style='float:right;display:block;'>loading...</span>");
	setTimeout(function(){loading_end();}, 30000);
}
function loading_end()
{
	$(loading_start_ids).attr('disabled',false);
	$('.loading').css({display:'none'});
}

function go_away()
{
	loading_start('#go_');
	JsHttpRequest.query(
		'/guest.php',
		{
			'fio': $('#fio').val(),
			'email': $('#email').val(),
			'message': $('#message').val(),
			'code': $('#code').val()
		},
		function(result, errors)
		{
			$('#div_errors').html(errors);
			loading_end();
			if (result)
			{
				if (result['res']==1)
				{
					//alert('Сообщение успешно отправленно');
					alert(messages_arr.m4);
					$('#fio').val('');
					$('#email').val('');
					$('#message').val('');
					$('#code').val('');
					$('.sec').text(result['rnd']); //
				}
				if (result['res']==2)
				{
					//alert('Не все поля заполнены или заполнены неверно');
					alert(messages_arr.m5);
				}
				if (result['res']==3)
				{
					//alert('Неверный защитный код');
					alert(messages_arr.m6);
				}
            }
		},
		true
	);
}


$(document).ready(function()
{  
	$("h1, .h1zag").each(function(){$(this).wrap('<div class="h11"></div>').wrap('<div class="h12"></div>').html('<img src="/img/h1_pipka.gif">'+$(this).text());});
	//$('h1').click(function(){alert($(this).text());});
	
	$("input, textarea").focus(function()
	{
		if ($(this).attr('type')=="text"||$(this).attr('type')=="password"||$(this).attr('type')=="textarea")
		{
			//if (!/str/.test($(this).attr('name')) && ($(this).attr('value')=="Логин" || $(this).attr('value')=="*****")) $(this).attr('value','');
			if ($(this).attr('name')=='q') $(this).attr('value','');
			if ($(this).attr('name')!='q') $(this).toggleClass('over');
		}
	});

 	$("input, textarea").blur(function()
	{
		if ($(this).attr('type')=="text"||$(this).attr('type')=="password"||$(this).attr('type')=="textarea")
		{
			//if ($(this).attr('name')=='q' && $(this).attr('value')=='') $(this).attr('value','поиск');
			if ($(this).attr('name')!='q') $(this).toggleClass('over');
		}
	});
	
	$("#skrepka > div > table > tbody > tr").everyTime(10, 'timer2', function(i) {
		$(this).append('<td>&nbsp;</td>');
		$("#skrepka > div > table").css({'width':(i*4)+'%'});
	}, 25);
	
	$('#go_').click(function(){go_away();});
	
	//$('#code').keydown(function(e){if(e.keyCode==13)go_away();	});
	
	//alert($.cookie('typeWork'));
	
	$('#type_work').change(function(){
		ip=$('div.td_main').attr('rel');
		ip_val=$(this).val();
		$.cookie('ip'+ip,ip_val);
		document.location=document.location;
		return false;
	});

	$('#BuyBtn').click(function(){
		$('#workOrder').slideToggle('normal');
		setTimeout(function(){window.scroll(10000,10000);},600);
	});
	
	$('#freeDown').click(function(){
		if ($('#code').val().length>0)
		{
			if ($('#code').val()==$.trim($('.sec').text()))
			{
				document.location='http://'+document.domain+document.location.pathname+'?'+$('#code').val();
				$('#code').val('');
			}
			else
			{
				//alert('Неверный код');
				alert(messages_arr.m7);
			}
		}
		else
		{
			//alert('Введите защитный код');
			alert(messages_arr.m8);
		}
		return false;
	});
	
	$('#orderBtn1,#orderBtn').click(function(){
		buy();
	});
	
	$('#login2').focus(function(){$(this).attr('value','');});
	
	$('#rememberme').click(function(){
		$('#remem').slideToggle(100);
		return false;
	});
	
	$('#remem1').click(function(){
		if (login2=$.trim($('#login2').val()))
		{
			JsHttpRequest.query(
				'/rememberme.php',
				{
					'email': login2
				},
				function(result, errors)
				{
					$('#div_errors').html(errors);
					if (result)
					{
						if (result['res']==1)
						{
							//alert('Пароль выслан на ваш e-mail');
							alert(messages_arr.m9);
							$('#rememberme').trigger('click');
						}
						if (result['res']==2)
						{
							//alert('Нет такого пользователя или пользователь не активен');
							alert(messages_arr.m10);
						}
						
					}
				},
				true
			);
		}
	});
	
	
	$('#BuyBtnCloset').click(function(){$('#BuyBtn').trigger('click');});
	
	if ($.cookie('message')=='confirm_ok')
	{
		$.cookie('message','');
		//alert('Регистрация подтверждена');
		alert(messages_arr.m11);
		document.location='/';
	}
	
	if ($("#of_time").length) $("#of_time").datepicker();
	if ($("#kalendar").length) $("#kalendar").datepicker();
	
});

