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, "")}


/* 
document.onclick=function(e) {
	e=e||event;
	var element=e.target||e.srcElement;
	//alert(element.nodeName);
	return true;
} */

function to_admin()
{
	
	//send_error=setTimeout(function(){alert('Ioeaea');document.getElementById('go_').disabled=false;}, 13000);
	//document.getElementById('go_').disabled=true;
	//JsHttpRequest.method='post';
	JsHttpRequest.query(
		'/to_admin.php',
		{
			'admin_name': $('#admin_name').attr('value'),
			'admin_mail': $('#admin_mail').attr('value'),
			'admin_message': $('#admin_message').attr('value'),
			'admin_code': $('#admin_code').attr('value')
		},
		function(result, errors)
		{
			document.getElementById('div_errors').innerHTML=errors;
			if (result)
			{
				if (result['res']==1)
				{
					alert('Сообщение отправлено');
					$('#admin_name').attr('value','');
					$('#admin_mail').attr('value','');
					$('#admin_message').attr('value','');
					$('#admin_code').attr('value','');
					$('.code').text(result['rnd']); //
				}
				if (result['res']==2) {
					alert('Не все поля заполнены');
				}
				if (result['res']==3) {
					alert('Неверный защитный код!');
				}
            }
		},
		true
	);
}


function go_away()
{
	
	//send_error=setTimeout(function(){alert('Ioeaea');document.getElementById('go_').disabled=false;}, 13000);
	//document.getElementById('go_').disabled=true;
	//JsHttpRequest.method='post';
	JsHttpRequest.query(
		'/comment.php',
		{
			'comm_name': $('#comm_name').attr('value'),
			//'comm_email': $('#comm_email').attr('value'),
			'comm_message': $('#comm_message').attr('value'),
			'where_id': $('#where_id').attr('value'),
			'where': $('#where').attr('value'),
			'comm_code': $('#comm_code').attr('value')
		},
		function(result, errors)
		{
			document.getElementById('div_errors').innerHTML=errors;
			if (result)
			{
				//clearTimeout(send_error);
				//document.getElementById('go_').disabled=false;
				if (result['res']==1)
				{
				
					$('#comment_add_ok').text(' Комментарий добавлен');
					$('#count_comment').text(parseInt($('#count_comment').text())+1);
					$('#comment_add_ok').css({display:'block',float:'right',margin:'3px 8px 0px 5px'});
					
					var new_comm = '<div class="line1"><img class="spacer"></div><div class="name_comment">' + 
					'<span>только что :)</span>'+
					'<div><b>'+$('#comm_name').attr('value')+'</b> пишет:</div>' +
					'<p>'+$('#comm_message').attr('value')+'</p></div>';
					$('.c_wr').removeClass('display_none');
					
					$('.comments').html($('.comments').html()+new_comm);
					$('#comm_name').attr('value','');
					$('#comm_email').attr('value','');
					$('#comm_message').attr('value','');
					$('#comm_code').attr('value','');
					$('.code').text(result['rnd']); //
				}
				if (result['res']==2) {
					alert('Не все поля заполнены');
				}
				if (result['res']==3) {
					alert('Неверный защитный код!');
				}
            }
		},
		true
	);
}

function Data2Text()
{
	$(ids).each(function(ind){
		var inpt = $(this).children().eq(0);
		inpt.css({'display':'block'});
		$(this).data('inpt', inpt); //'id_'+$(this).attr('id')
		
		if (inpt.attr('type'))
		{
			var d="";
			t=inpt.attr('type').toLowerCase();
			if (t=='text' || t=='textarea') d=inpt.val().replace(/\n/gi,"<br>");
			if (t=='password')
			{
				inpt.attr('value','');
				d=inpt.val();
			}
			if (t=='checkbox') d=(inpt.attr('checked')==true)?'да':'нет';
			if (t=='file') {d=inpt.css({'display':'none'});$(this).css({'height':'0px'})}
			$(this).html(d).wrapInner('<span></span>');
		}
	}); 
}

function Data2DOM()
{
	$(ids).each(function(){
		u=$(this).data('inpt'); //'id_'+$(this).attr('id')
		if (u.attr('type')=='file')
		{
			$(this).css({'height':'20px'});
			u="<input type='file' id='"+u.attr('id')+"' class='"+u.attr('class')+"' style='"+u.attr('style')+"'>";
			u=u.replace('none','block');
			
		}
		//alert(u.attr('type'));
		//if (u.attr('type')=='password')
		{
			//u.val('');
			//alert(1);
		}
		$(this).html(u);
		//alert($(this).html());
	});
}



$(function(){

	$('#cabina_closet').click(function(){
		if ($.cookie('cabr')!=1) //спрятана, раскрываем
		{
			$('#cabina_').slideDown('normal');
			$(this).html('свернуть');
			$.cookie('cabr', '1', {expires: 7, path: '/'});
		}
		else
		{
			$('#cabina_').slideUp('normal');
			$(this).html('раскрыть');
			$.cookie('cabr', '0', {expires: 7, path: '/'});
		}
		return false;
	});
	if ($('#cabina_').length>0 && $.cookie('cabr') == null )
	{
		$('#cabina_').slideDown('normal');
		$('#cabina_closet').html('свернуть');
		$.cookie('cabr', '1', {expires: 7, path: '/'});
	}
	
	
	
	//ids='#cab_country, #cab_mail, #cab_pass, #cab_spec, #cab_comment, #cab_s1, #cab_s2, #cab_s3, #cab_logo';
	//Data2Text();
	
	
	$('#change_personal_data').toggle(
 		function()
		{
			Data2DOM();
			$(this).val('Сохранить личные данные');
			$('#cancel_personal_data').css({'display':'block'});
			
		},
		function()
		{
			//$('#change_personal_data').attr('disabled',true);
			var params={};
			params.type='personal_data';
			params.country= $('#cab_country > input').val();
			params.email= $('#cab_mail > input').val();
			params.pass= $('#cab_pass > input').val();
			params.spec= $('#cab_spec > input').val();
			params.comment= $('#cab_comment > textarea').val();
			if ($('#ulogo').val()!='') params.ulogo=document.getElementById('ulogo');
			params.s1= $('#cab_s1 > input').attr('checked');
			params.s2= $('#cab_s2 > input').attr('checked');
			params.s3= $('#cab_s3 > input').attr('checked');
			JsHttpRequest.query(
				'/udata.php',
				params,
				function(result, errors)
				{
					$('#div_errors').html(errors);
					if (result)
					{
						//$('#change_personal_data').attr('disabled',false);
						if (result['res']==1)
						{
							if (result['logo'])
							{
								$('#avatara').hide(1000);
								setTimeout(function(){
									$('#avatara').attr('src','/upload/ulogo/'+result['logo']).bind('load',function(){
										$(this).show(1000);
									});
								},1000);
							}
							alert('Данные успешно сохранены :)');
						}
					}
				},
				true
			);
			Data2Text();
			$(this).val('Изменить личные данные');
			$('#cancel_personal_data').css({'display':'none'});
		}
	);
	
	
	$('#save_folio_link').click(function(){
		
		var obj_check = {};
		obj_check['type']='folio_link';
		$('.check_site').each(function(){
			obj_check[$(this).attr('name')]=(($(this).attr('checked')==true)?'checked':'');
		});
		$('.check_site_url').each(function(){
			obj_check[$(this).attr('name')]=$(this).val();
		});
		//for (ddddd in obj_check) //alert(obj_check[ddddd]);
		JsHttpRequest.query(
			'/udata.php',
			obj_check,
			function(result, errors)
			{
				$('#div_errors').html(errors);
				if (result)
				{
					if (result['res']==1)
					{
						alert('Ссылки на портфолие в фотобанках успешно сохранены :) ');
					}
				}
			},
			true
		);
	});
	
	$('#clear_comm').toggle(
		function()
		{
			$('#block_comm').fadeTo('normal',0);
			$('#block_comm').css('display','none');
			$('#block_bann').fadeTo('normal',0);
			$('#block_bann').css('display','none');
			$('.block1').addClass('block3');
			$('.block3').removeClass('block1');
			$('.block11').addClass('block33');
			$('.block33').removeClass('block11');
			$(this).text('показать комментарии');
		},
		function()
		{
			$('#block_comm').fadeTo('normal',1);
			$('#block_comm').css('display','block');
			$('#block_bann').fadeTo('normal',1);
			$('#block_bann').css('display','block');
			$('.block3').addClass('block1');
			$('.block1').removeClass('block3');
			$('.block33').addClass('block11');
			$('.block11').removeClass('block33');
			$(this).text('скрыть комментарии');
		}
	);
	
	$('.sub_mnu').hover(
		function(){$('#'+$(this).attr('id')+'pi').css('display','block');},
		function(){$('#'+$(this).attr('id')+'pi').hide(200);}
	);

	$(".gallery > div > a > img").each(
		function(){
			if ($('#where_id').attr('value')==$(this).attr('id')) $(this).fadeTo(1, 1);
	});
	
	$(".gallery > div > a > img").hover(
		function()
		{
			if ($('#where_id').attr('value')!=$(this).attr('id')) $(this).fadeTo(300, 1);
		},
		function()
		{
			if ($('#where_id').attr('value')!=$(this).attr('id')) $(this).fadeTo(300, 0.3);
		}
	);

	$("input").focus(function()
	{
		//if ($(this).attr('type')=="text"||$(this).attr('type')=="password"||$(this).attr('type')=="textarea")
		{
			//if (!/str/.test($(this).attr('name')) && ($(this).attr('value')=="Eiaei" || $(this).attr('value')=="*****")) $(this).attr('value','');
			$(this).addClass('over');
		}
	});

 	$("input").blur(function()
	{
		//if ($(this).attr('type')=="text"||$(this).attr('type')=="password"||$(this).attr('type')=="textarea")
		{
			$(this).removeClass('over');
		}
	});
	
	$("textarea").focus(function(){
		$(this).addClass('over');
	});
	$("textarea").blur(function(){
		$(this).removeClass('over');
	});
	
	$(".img1").click(function(){
		//alert($(this).attr('id'));
		var wert=$(this).attr('id');
		//alert(wert);
		JsHttpRequest.query(
			'/comment.php',
			{
				'ccc':wert,
				'ccc1':1
			},
			function(result, errors)
			{
				if (result)
				{
					if (result['ccc1']=='ccc1') document.location=document.location; // :)
				}
			},
			true
		);
		
	});
	
	$('#comm_name').keypress(function(e){if(e.which==13)go_away();});
	$('#comm_code').keypress(function(e){if(e.which==13)go_away();});
	
	$('#admin_name').keypress(function(e){if(e.which==13)to_admin();});
	$('#admin_code').keypress(function(e){if(e.which==13)to_admin();});
	
	$(".bottom2 > tbody > tr > td > span").fadeTo(1,0.2);
	$(".bottom2 > tbody > tr > td > span").hover(
		function()
		{
			$(this).fadeTo('normal', 1);
		},
		function()
		{
			$(this).fadeTo('normal', 0.2);
		}
	);
	
	
	$('#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',
				{
					'login2': login2
				},
				function(result, errors)
				{
					$('#div_errors').html(errors);
					if (result)
					{
						if (result['res']==1)
						{
							alert('Новый пароль выслан на ваш email ');
							$('#rememberMe').trigger('click');
						}
						if (result['res']==2)
						{
							alert('Нет такого пользователя или пользователь не активен');
						}
						
					}
				},
				true
			);
		}
	});
	
	//$("h1").each(function(){$(this).html('<span style="position:absolute;margin:1px 0px 0px 1px;color:black;">'+$(this).text()+'</span>'+'<span style="position:absolute;">'+$(this).text()+'</span>&nbsp;');});
	
	dl=$(document).attr('location').toString().replace("#","");
	dl=(dl.split("?")[1]) ? dl+"&" : dl+"?" ;
	$('#add_work').click(function(){
		$(document).attr('location', dl+'add');
		return false;
	});

	$('#edit_work').click(function(){
		$(document).attr('location', dl+'edit');
		return false;
	});
	
	$('#accepted').click(function(){
		$(document).attr('location', dl+'accepted');
		return false;
	});

	$('#del_work').click(function(){
		if (confirm('Удалить работу безвозвратно и безвоздмездно?'))
		{
			$(document).attr('location', dl+'del');
		}
		return false;
	});
	
	$('#del_work_a').click(function(){
		if (mess=prompt('Причина удаления',''))
		{
			$(document).attr('location', dl+'del&del_mess='+encodeURIComponent(mess));
		}
		return false;
	});
	
	$('#go_comm').click(function(){
		JsHttpRequest.query(
			'/mail_avtory.php',
			{
				'comm_name': $('#comm_name').val(),
				'comm_mail': $('#comm_mail').val(),
				'comm_message': $('#comm_message').val(),
				'comm_avtor': $('#comm_avtor').val(),
				'comm_code': $('#comm_code').val()
			},
			function(result, errors)
			{
				document.getElementById('div_errors').innerHTML=errors;
				if (result)
				{
					if (result['res']==1)
					{
						alert('Сообщение успешно отправленно');
						
						$('#comm_name').attr('value','');
						$('#comm_mail').attr('value','');
						$('#comm_message').attr('value','');
						$('#comm_code').attr('value','');
						$('.code').text(result['rnd']); //
					}
					if (result['res']==2) {
						alert('Не все поля заполнены');
					}
					if (result['res']==3) {
						alert('Неверный защитный код!');
					}
				}
			},
			true
		);
	});
	
	$('#m_f_a').click(function(){
		if (mess=prompt("Уведомление автору", ""))
		{
			JsHttpRequest.query(
				'/udata.php',
				{
					'type': 'message_fa',
					'where_id': $('#where_id').val(),
					'mfa': mess
				},
				function(result, errors)
				{
					$('#div_errors').html(errors);
					if (result['res']==1)
					{
						alert('Сохранено!');
						//$('#rememberMe').trigger('click');
					}
				},
				true
			);
		}
		return false;
	});
	
	$("a[rel='nofollow'][target='_blank']").click(function(){
		var id_s=parseInt($(this).attr('data'));
		var id_f=parseInt($('#where_id').val());
		$.get("/go_away.php",{ 'id_s':id_s, 'id_f':id_f });
		return true;
	});
	
});

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
 }

/* startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
} */