var mouse_big_photo=0;
var big_photo_slideshow=0;
var big_photo_slideshow_play=0;
var big_photo_slideshow_interval=500;
var anchor_album_status="#album_status";
var item_posting=0;
function ref()
{
	//location.href=window.location;
	//location.refresh();
	window.location=window.location;
}
function redirect_url(url)
{
	location.href=url;
}
function photo_rotate_clock(id)
{
	$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "photo_rotate_clock",
	   		   'id':id
	   		 }, 
	   		 function(data){ // ref();
	   		 //alert($('#photo'+id).attr('src'));
	   		$('#photo'+id).attr('src',$('#photo'+id).attr('src')+'?rand='+new Date().getTime());   
		});
}
function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function album_big_photo_resize()
{
var a,b,z;
a=getClientWidth();
if($('.album_big_photos').html())
	b=getClientHeight()-200;
else
	b=getClientHeight()-100;
//c=getClientHeight()-200;
//z=b>a?a:b;
//document.getElementById('album_big_photo_photo').width=z;
$('#album_big_photo_photo').css('max-width',a);
$('#album_big_photo_photo').css('max-height',b);
$('#album_big_photo_height').css('height',b);
}
function board_item_ref(id)
{
	$.post(
		"ajax/ajaxBoard.php",
		{
			mode:'item_ref',
			id:id
		}
		,
		function(data)
		{
		ref();
	});
}
function board_item_delete_select(id)
{
	$.post('ajax/ajaxBoard.php',{mode:'delete_select',id:id},function(data){$('#extra_area').html(data);});
}
function board_item_delete_true(id)
{
	$.post('ajax/ajaxBoard.php',{mode:'send_notification_to_owner_by_admin',id:id,mode_del:$('#board_mode_del').val()},function(data){$('#extra_area').html(data);});
	delete_item_article(id);
}
function board_item_delete_censel()
{
	$('#extra_area').html('');
}
function Album_Save(id)
{
	var board_id,name,description,phone,comments;
	
	board_id=$('#board_id').val();
	name=$('#posLable').val();
	description=$('#posDesc').val();
	phone3=$('#posPhone3').val();
	phone2=$('#posPhone2').val();
	phone1=$('#posPhone1').val();
	comments=$('#album_comments').val();
	//for(i=1;i<elements;i++)
	//	photoTextMas['uploadPhoto'+i]=$('#Text_uploadPhoto'+i).val();
	//alert($.toJSON(photoTextMas));
	/*alert(name);
	alert(description);
	alert(phone);
	alert(comments);
	alert($.toJSON(photoTextMas));*/
	if(!id)
	{
		if(!Album_Save_Check(5))
		{ 
		$('#album_succes').html("<img src='http://www.spicylogic.com/allenday/blog/wp-content/uploads/2008/09/ajax_activity_indicators_download_animated_indicator_circle_ball.gif'> Создание объявления");
		item_posting=1;
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "save_item",
	   		   'board_id':board_id, 
	   		   'name': name,
	   		   'description':description,
	   		   'phone3':phone3,
	   		   'phone2':phone2,
	   		   'phone1':phone1,
	   		   'comments':comments,
	     	    'photoNamesMas': $.toJSON(photoNamesMas)
	   		 }, 
	   		 function(data){ 
	   		 	//alert(data);
	   		 	if(data=='error')
	   		 	{
	   		 		$('#album_succes').html("");
	   		 		item_posting=0;
	   		 		$('#board_idError').text('Выберите другой раздел.');
	   		 		//$('#board_id').val(0);
	   		 	}
	   		 	else
	   		 		window.location='index.php?o=board&id=element&item_id='+data;      
		});
		 //album_extra_area(4);
		 //redirect_url(anchor_album_status);
	   			 //$('#posLable').val('');
	   			 //$('#posDesc').val(''); 
	   			 //foto_loaded=0;
	   			 //$('.photo_uploadPhoto_area').each(function(i,n){$(this).html('');});
	   			 //photoNamesMas={};
	   			 //photoZipMas='';
	   			 //elements=0; 
	   			//for(i=1;i<=elements;i++)
				//	$('#Text_uploadPhoto'+i).val(''); 
	   			// $('#status_uploadzipLoad').text('');
	   			// $('#album_photo_block').html(''); 	
		}
	}
}
function Album_Edit(id)
{
	var board_id,name,description,phone,comments,date,time;
	
	board_id=$('#board_id').val();
	name=$('#posLable').val();
	description=$('#posDesc').val();
	phone3=$('#posPhone3').val();
	phone2=$('#posPhone2').val();
	phone1=$('#posPhone1').val();
	comments=$('#album_comments').val();
	if($('#inpSDate').val())
		date=$('#inpSDate').val();
	else
		date=0
	if($('#inpSDateTime').val())
		time=$('#inpSDateTime').val();
	else
		time=0
	//for(i=1;i<elements;i++)
	//	photoTextMas['uploadPhoto'+i]=$('#Text_uploadPhoto'+i).val();
	//alert($.toJSON(photoTextMas));
	/*alert(name);
	alert(description);
	alert(phone);
	alert(comments);
	alert($.toJSON(photoTextMas));*/
	//alert(time);
	//alert(date);
	if(id)
	{
		if(!Album_Save_Check(5))
		{ 
		$('#album_succes').html("<img src='http://www.spicylogic.com/allenday/blog/wp-content/uploads/2008/09/ajax_activity_indicators_download_animated_indicator_circle_ball.gif'> Сохранение объявления");
		item_posting=1;
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "edit_item",
	   		   'item_id':id,
	   		   'board_id':board_id, 
	   		   'name': name,
	   		   'description':description,
	   		   'phone3':phone3,
	   		   'phone2':phone2,
	   		   'phone1':phone1,
	   		   'comments':comments,
	     	    'photoNamesMas': $.toJSON(photoNamesMas),
	     	    'date':date,
	     	    'time':time
	   		 }, 
	   		 function(data){ 
	   		 	//alert(data);
	   		 	if(data=='error')
	   		 	{
	   		 		$('#album_succes').html("");
	   		 		item_posting=0;
	   		 		$('#board_idError').text('Выберите другой раздел.');
	   		 		//$('#board_id').val(0);
	   		 	}
	   		 	else
	   		 		window.location='index.php?o=board&id=element&item_id='+id;      
		});
		 //album_extra_area(4);
		 //redirect_url(anchor_album_status);
	   			 //$('#posLable').val('');
	   			 //$('#posDesc').val(''); 
	   			 //foto_loaded=0;
	   			 //$('.photo_uploadPhoto_area').each(function(i,n){$(this).html('');});
	   			 //photoNamesMas={};
	   			 //photoZipMas='';
	   			 //elements=0; 
	   			//for(i=1;i<=elements;i++)
				//	$('#Text_uploadPhoto'+i).val(''); 
	   			// $('#status_uploadzipLoad').text('');
	   			// $('#album_photo_block').html(''); 	
		}
	}
}
function board_extra_area(id)
{
	var k;
	for(k=1;k<=5;k++)
		$('#board_extra_area'+k).hide();
	if(id)
		$('#board_extra_area'+id).show();
}
/*function delete_album(id,name)
{
	if(confirm('Удалить альбом \"'+name+'\"?'))
	{
		$('#album'+id).text('Альбом удален \"'+name+'\"');
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "delete_album", 
	   		   'id':id
	   		 });
	}
}
*/
function delete_photo(id)
{
	//if(confirm('Удалить фото ?'))
	//{
		//$('#photo_element'+id).text('Фото удалено');
		$('#photo_element'+id).hide();
		$('#photo_restore'+id).show();
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "delete_photo_pre", 
	   		   'id':id
	   		 },function(data){
	   		 	//alert(data);
	   		 	});
	//}
}
function board_photo_restore(id)
{
	    $('#photo_element'+id).show();
		$('#photo_restore'+id).hide();
		board_extra_area(0);
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "restore_photo", 
	   		   'id':id
	   		 },function(data){
	   		 	//alert(data);
	   		 	});
}
function delete_item(id)
{
	//if(confirm('Удалить объявление ?'))
	//{
		//$('#item'+id).html('Объявление удалено');
		$('#item_restore'+id).show();
		$('#item'+id).hide();
		//$('#board_extra_area3_href').attr('onclick',"board_item_restore('"+id+"')");
		//board_extra_area(3);
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "delete_item_pre", 
	   		   'id':id
	   		 });
	//}
}
function delete_item_article(id)
{
	//if(confirm('Удалить объявление ?'))
	//{
		//$('#item'+id).html('Объявление удалено');
		//$('#item_restore'+id).show();
		//$('#item'+id).hide();
		board_extra_area(3);
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "delete_item_pre", 
	   		   'id':id
	   		 });
	//}
}
function board_item_restore(id)
{
		$('#item'+id).show();
		$('#item_restore'+id).hide();
		board_extra_area(0);
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "restore_item", 
	   		   'id':id
	   		 });
}
/*function delete_photo(id)
{
	//if(confirm('Удалить фото ?'))
	//{
		$('#photo_element'+id).hide();
		$('#photo_restore'+id).show();
		album_extra_area(2);
		$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "delete_photo_pre", 
	   		   'id':id
	   		 },function(data){
	   		 	//alert(data);
	   		 	});
	//}
}
*/
function edit_photo_reset()
{
	$('.album_photo_edit_area').each(function(){$(this).hide();});
	$('.album_photo_edit_button').each(function(){$(this).show();});
}
function edit_photo(id)
{
	edit_photo_reset();
	$('#album_photo_edit_button'+id).hide();
	$('#album_photo_edit_area'+id).show();
}
function edit_photo_redirect(id)
{
	var adr='/people_test/index.php?o=photo&id=';
	if(id)
		adr+=id+'#photo_edit';
	else
		adr='';
	redirect_url(adr);
}
function edit_photo_save(id)
{
	var description=$('#album_photo_edit_text'+id).val();
	$.post(
	   		 "ajax/ajaxBoard.php", 
	   		 { 'mode': "edit_photo", 
	   		   'id':id,
	   		   'description':description,
	   		   'visible':$('#photo_visible'+id).val()
	   		 },function(data){
	   		 	//alert(data);
	   		 	});
	edit_photo_censel(id);	
	album_extra_area(3);
}
function edit_photo_censel(id)
{
	$('#album_photo_edit_button'+id).show();
	$('#album_photo_edit_area'+id).hide();
	album_extra_area(0);
}
function sign_to_empty(msg)
{
	var preg;
	preg=/ /g;
	return msg.replace(preg,'');
}
function test_phone(phone,num)
{
	switch(num)
	{
		case 3:return (new RegExp ("^[0-9+]+$").test(phone) ? 1 : 0);break;
		case 2:return (new RegExp ("^[0-9]+$").test(phone) ? 1 : 0);break;
		case 1:return (new RegExp ("^[0-9-]+$").test(phone) ? 1 : 0);break;
	}
}
function Album_Save_Check(id)
{
	var error=0;
	$('#album_error').html('');
	//$('#album_succes').html('');
	if(item_posting)
	{
		$('#album_error').append('Подождите.<br>');
		error++;
	}
	else
	switch(id)
	{
		case 5:
			/*if(!foto_loaded)
			{
				$('#album_error').append('Загрузите фото.<br>');
				error++;
			}
			else
			{
				if(zip_status==1)
				{
					$('#album_error').append('Загрузка архива еще не завершена.<br>');
					error++;
				}
			}*/
		case 4:
			/*if(!test_phone($('#posPhone3').val(),3) || !test_phone($('#posPhone2').val(),2) || !test_phone($('#posPhone1').val(),1))
			{
				$('#posPhoneError').text('Введите номер телефона.');
				error++;
			}
			else
			{
				$('#posPhoneError').text('');
			}*/
		case 3:
			if(sign_to_empty($('#posDesc').val())=='')
			{
				$('#posDescError').text('Введите текст.');
				error++;
			}
			else
			{
				$('#posDescError').text('');
			}
		case 2:
			if(sign_to_empty($('#posLable').val())=='')
			{
				$('#posLableError').text('Введите заголовок.');
				error++;
			}
			else
			{
				$('#posLableError').text('');
			}
		case 1:
			if($('#board_id').val()=='0')
			{
				$('#board_idError').text('Выберите раздел.');
				error++;
			}
			else
			{
				$('#board_idError').text('');
			}
	}
	return error;
}
function board_photos_next(id,page)
{
	$.post(
		"ajax/ajaxBoard.php",
		{
			mode:'board_photos_list',
			id:id,
			page:page
		}
		,
		function(data)
		{
	$('#photo_in_album'+id).html(data);
	}
	);
}
function album_add_more_photo()
{
	$.post(
			"ajax/ajaxBoard.php",
			{
			'mode':'more_photo',
			'num':elements
			},
			function(data){
				$('#album_photo_block').append(data);
				makeButton();
				elements++;
				}
			)
}
function makeButton()
{
	   photoFormsDisplayedCount=$(".uploadPhotoBtn").length;
	       $(".uploadPhotoBtn").each(function(btn)
			{
			   var status = $("#status_"+this.id);
		  	   var btnUpload = $("#"+this.id);
		  	   new AjaxUpload(btnUpload, {  
		  		         action: '/ajax/ajaxUploadPhoto.php',  
		  		         name: 'uploadfile',  
		  		         onSubmit: function(file, ext){  
		  		             if (!(ext && /^(jpg|jpeg)$/.test(ext))){  
		  		                   // check for valid file extension  
		  		                 status.text('Только JPG/JPEG формат');  
		  		                 return false;  
		  		             }
		  		             Album_Save_Check(4);  
		  		             loadedPhotoId=currentPhotoId;
		  		             status.text('Загрузка...');  
		  		         },     
		  		         onComplete: function(file, response){  
		  		        	 status.text('');  
		  		             if(response!=""){  
		  		             		file=response;
		  		             		//alert('<img src="/temp/'+file+'" alt="" width="300"/>');
		  		             		photoNamesMas[loadedPhotoId]=file;
  							       $("#photo_"+loadedPhotoId).html('<img src="/temp/'+file+'" alt="" width="300" id="file_'+loadedPhotoId+'"/>');
  							       //$("#TR_"+loadedPhotoId).css('display','');
  							       //$("#rotate_"+loadedPhotoId).css('display','');
  							      	//$("#rotate_"+loadedPhotoId).attr('onclick',"temp_photo_rotate_clock('"+loadedPhotoId+"')");
  							     	//$("#photo_"+loadedPhotoId).html('Фото загружено');
  							     foto_loaded++;
  							     $("#status_uploadPhoto").html('');
			  		          } else{  
			  		        	status.text('Ошибка загрузки на сервер');  
		  		             }
		  		         }      
		  		       
		  	     });
		  				  		        
			}); 	   
}
