
//Создание превью фотографии, появляющееся при наведении на неё мышью
function createTooltipPreviewDocument(photo_id, photo_extension, photo_width, photo_height, photo_size, photo_people, photo_tags)
{
    var result = '';

	if ( photo_height > photo_width )
	{		var result_width = ( 480 * photo_width / photo_height );
		var result_height = 480;
	}
	else
	{		var result_width = 480;
		var result_height = ( 480 * photo_height / photo_width );
	}

	result += '<table class=tooltip_preview border=0 cellpadding=0 cellspacing=0 width=' + result_width + '>';
	result += '<tr><td>';
	result += '';

    result += '<img';
    result += ' src=http://media.filmz.ru/photos/medium/m_' + photo_id + photo_extension;
	result += ' width=' + result_width;
	result += ' height=' + result_height;
	result += '>';

	result += '</td></tr>';
	result += '<td><tr>';

	result += 'Оригинальное изображение: ';
	result += '<b>' + photo_width + 'x' + photo_height + '</b>';

	if ( photo_size < 1048576 )
	{
		photo_size = photo_size / 1024;
		photo_size = photo_size.toFixed(2) + ' Кб';
	}
	else
	{
		photo_size = photo_size / 1024 / 1024;
		photo_size = photo_size.toFixed(2) + ' Mб';
	}

	result += ' (<b>' + photo_size + '</b>)';

	var people_array = peopleArray(photo_people, 'names');

	if ( people_array[0] != '' )
	{
		result += '<br>';
		result += 'Персоны: ';
		result += '<b>' + people_array.join(", ") + '</b>';
	}

	var tags_array = tagsArray(photo_tags, 'names');

	if ( tags_array[0] != '' )
	{
		result += '<br>';
		result += 'Теги: ';
		result += '<b>' + tags_array.join(", ") + '</b>';
	}

	result += '</td></tr>';
	result += '</table>';

	return result;
}



//Конвертация строки со списком людей из БД в JS-массив
function peopleArray(photo_people, mode)
{	var people_array = photo_people.split("|");

	for (var i = 0; i < people_array.length; i++)
	{
	    if ( mode == 'names' )
	    {
	    	people_array[i] = people_array[i].substring( people_array[i].indexOf(":") + 1 );
	  	}
	  	else
	  	{			people_array[i] = people_array[i].substring( 0, people_array[i].indexOf(":") );
	  	}

	}

	return people_array;
}



//Конвертация строки со списком тегов из БД в JS-массив
function tagsArray(photo_tags, mode)
{
	var tags_array = photo_tags.split(", ");

	for (var i = 0; i < tags_array.length; i++)
	{
	    if ( mode == 'names' )
	    {
	    	tags_array[i] = tags_array[i].substring( 0, tags_array[i].indexOf(":") );
	  	}
	  	else
	  	{
			tags_array[i] = tags_array[i].substring( tags_array[i].indexOf(":") + 1 );
	  	}

	}

	return tags_array;
}



//Генерация URL в случае добавления или удаления персоны или тега
function createPageURL(category, entity_id, type, current_people, current_tags, new_person, new_tag)
{
	var result = '/photos';
	if ( category != '' && category != 'false' )
	{
		result += '/' + category;

		if ( entity_id != '' && entity_id != 'false'  )
		{
			result += '/' + entity_id;
		}

	}

	if ( type != '' && type != 'false'  )
	{
		result += '/' + type;
	}

	if ( new_person != false )
	{
		var current_people_array = current_people.split("+");
		var new_people_array = new Array();

		for ( var i = 0; i < current_people_array.length; i++ )
		{
            if ( current_people_array[i] != Math.abs(new_person) && current_people_array[i] != 0 )
            {
            	new_people_array.push( current_people_array[i] );
            }
		}

		if ( new_person > 0 )
		{		    new_people_array.push( new_person );
		}

		new_people_array = new_people_array.sort( function(a, b){ return a - b; } );

		if ( new_people_array.length > 0 )
		{
			result += '/names';
			result += '/' + new_people_array.join("+");
		}
	}
	else
	{		if ( current_people != 0 )
		{
			result += '/names';
			result += '/' + current_people;
		}
	}

	if ( new_tag != false )
	{
		var current_tags_array = current_tags.split("+");
		var new_tags_array = new Array();

		for ( var i = 0; i < current_tags_array.length; i++ )
		{
            if ( current_tags_array[i] != Math.abs(new_tag) && current_tags_array[i] != 0 )
            {
            	new_tags_array.push( current_tags_array[i] );
            }
		}

		if ( new_tag > 0 )
		{
		    new_tags_array.push( new_tag );
		}

		new_tags_array = new_tags_array.sort( function(a, b){ return a - b; } );

		if ( new_tags_array.length > 0 )
		{
			result += '/tags';
			result += '/' + new_tags_array.join("+");
		}
	}
	else
	{
		if ( current_tags != 0 )
		{
			result += '/tags';
			result += '/' + current_tags;
		}
	}

	result += '/';
	return result;
}


//Скроллинг превьюшек
function previewScroll(direction)
{	var j = -1;
	for ( var i = 0; i < thumbnails.length; i++ )
	{		if ( j == -1 && thumbnails[i]["id"] == thumbnails_current[0] )
		{			j = 0;
		}

		if ( j >= 0 && j < thumbnails_current.length )
		{			if ( direction == "back" )
			{
				thumbnails_current[j] = thumbnails[ i - 1 ]["id"];
			}
			else
			{
		    	thumbnails_current[j] = thumbnails[ i + 1 ]['id'];
			}

			j = j + 1;
		}
	}

	previewShow();
}

function previewShow()
{	var result = "";

	for ( var i = 0; i < thumbnails_current.length; i++ )
	{		//Ищем соответствующую превью в массиве thumbnails
		for ( var j = 0; j < thumbnails.length; j++ )
		{			if ( thumbnails[j]["id"] == thumbnails_current[i] )
			{				var current_thumb = thumbnails[j];
				break;
			}
		}

		if ( photo_id != current_thumb['id'] )
		{
			result += '<a href="/photos/' + current_thumb['id'] + '/' + photo_size + '/">';
		}
		result += '<img';
		result += ' src="' + photo_path_small + current_thumb['id'] + current_thumb['extension'] + '"';
		result += ' align="middle"';

		if ( photo_id == current_thumb['id'] )
		{			result += ' style="border: 3px solid #000000;"';
		}
		else
		{			result += ' border="0"';
		}

		result += ' onmouseover="Tip( createTooltipPreviewDocument(' + current_thumb['id'] + ', \'' + current_thumb['extension'] + '\', ' + current_thumb['width'] + ', ' + current_thumb['height'] + ', ' + current_thumb['size'] + ', \'' + current_thumb['people'] + '\', \'' + current_thumb['tags'] + '\') );"';
		result += ' onmouseout="UnTip();"';
		result += '>';

		if ( photo_id != current_thumb['id'] )
		{
			result += '</a>';
		}

		result += '&nbsp;';
	}

	document.getElementById("scroll_preview").innerHTML = result;

}




////////////////////////////////////////////////////////////////////////////////
//                          Поиск людей и тегов                               //
////////////////////////////////////////////////////////////////////////////////
function postPeopleSearch(search)
{
	document.getElementById("search_people").innerHTML = "<i>Ждите, идет поиск...</i>";

	//Генерируем строку запроса
	var querystring = "action=people";
	    querystring += "&search=" + encodeURIComponent(search);
	    querystring += "&category=" + photos_category;
	    querystring += "&entity_id=" + photos_entity_id;
	    querystring += "&type=" + photos_type;

	//Делаем GET-запрос на сервер, а ответ передаём функции responseTagSearch
	ajaxGETRequest
	(
		"/photos/ajax.php",
		querystring,
		responsePeopleSearch
	);
}

function responsePeopleSearch(result)
{
    document.getElementById("search_people").innerHTML = result;
}

function postTagsSearch(search)
{
	document.getElementById("search_tags").innerHTML = "<i>Ждите, идет поиск...</i>";

	//Генерируем строку запроса
	var querystring = "action=tags&search=" + encodeURIComponent(search);

	//Делаем GET-запрос на сервер, а ответ передаём функции responseTagSearch
	ajaxGETRequest
	(
		"/photos/ajax.php",
		querystring,
		responseTagsSearch
	);
}

function responseTagsSearch(result)
{
    document.getElementById("search_tags").innerHTML = result;
}

function postEntitySearch(search)
{
	document.getElementById("search_entity").innerHTML = "<i>Ждите, идет поиск...</i>";

	//Генерируем строку запроса
	var querystring = "action=entity";
	    querystring += "&search=" + encodeURIComponent(search);

	//Делаем GET-запрос на сервер, а ответ передаём функции responseEntitySearch
	ajaxGETRequest
	(
		"/photos/ajax.php",
		querystring,
		responseEntitySearch
	);
}

function responseEntitySearch(result)
{
    document.getElementById("search_entity").innerHTML = result;
}
