var photos_for_post = new Array();
function showAlbums(user_id, div_id, result_id, hidden_id){
	if($(div_id)){
		var albumsAjax = new Ajax(
							  ROOT+'in/media/'+user_id+'/photos/',
							  {
								  method: 'post',
								  data:{
									  ajax:'1', 
									  div_id:div_id,
									  result_id:result_id,
									  hidden_id:hidden_id
									},
								  update:$(div_id)
								}
							);
		albumsAjax.request();
		$(div_id).style.display = 'block';
		$(div_id).innerHTML = 'Загрузка альбомов...';
	}
}
function showPhotos(user_id, album_id, div_id, result_id, hidden_id, page){
	if($(div_id)){
		var photosAjax = new Ajax(
							  ROOT+'in/media/'+user_id+'/photos/album'+album_id+'/page'+(page?page:1),
							  {
								  method: 'post',
								  data:{
									  ajax:'1', 
									  div_id:div_id,
									  result_id:result_id,
									  hidden_id:hidden_id
									},
								  update:$(div_id)
								}
							);
		$(div_id).style.display = 'block';
		$(div_id).innerHTML = 'Загрузка фотографий альбома...';
		photosAjax.request();
	}
}
function addPhotoToPost(photo_id, div_id, result_id, hidden_id){
	var tf = true;
	var l_ = photos_for_post[result_id].length;
	for(i=0; i<photos_for_post[result_id].length; i++){
		if(photos_for_post[result_id][i][0] == photo_id){
			tf = false;
			break;
		}
	}
	if(tf == true){
		if($('photo_'+photo_id)){
			photos_for_post[result_id].push([photo_id, $('photo_'+photo_id).innerHTML]);
		}else{
			photos_for_post[result_id].push([photo_id, '<img src="'+ROOT+'content/albums_photos/lit/'+photo_id+'.jpg" alt="" />']);
		}
	}
	if(photos_for_post[result_id].length != l_ && $(result_id)){
		redrawSelected(result_id, hidden_id, photos_for_post[result_id]);
	}
}
function removePhotoFromPost(photo_id, result_id, hidden_id){
	var tf = false;
	for(i=0; i<photos_for_post[result_id].length; i++){
		if(photos_for_post[result_id][i][0] == photo_id){
			tf = true;
		}
		if(tf == true){
			if(i+1==photos_for_post[result_id].length){
				photos_for_post[result_id].pop();
			}else{
				photos_for_post[result_id][i] = photos_for_post[result_id][i+1];
			}
		}
	}
	redrawSelected(result_id, hidden_id, photos_for_post[result_id]);
}
function redrawSelected(result_id, hidden_id, photos_for_post){
	if(photos_for_post.length > 0){
		$(result_id).style.display = 'block';
	}else{
		$(result_id).style.display = 'none';
	}
	var str = '';
	var h_str = new Array();
	for(i=0; i<photos_for_post.length; i++){
		str += '<div class="res_photo"><a href="javascript:removePhotoFromPost(\''+photos_for_post[i][0]+'\', \''+result_id+'\', \''+hidden_id+'\')">'+photos_for_post[i][1]+'</a></div>';
		h_str.push(photos_for_post[i][0]);
	}
	$(result_id).innerHTML = str;
	$(hidden_id).value = h_str.join(',');
}
var cropDragId = 0;