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 dialogShow(src, id, title)
{
	if (!id)
		id='dialogWindow';
						 
	if ($('#' + id + '_header').length)
	{
		$('#' + id + '_header').remove();
	}
	
	if (!title)
		title = "&nbsp;";
	$('<div class="modalwindow modalwindow-header" id="' + id + '_header"><div class="modalwindow-header-title"><h5><a class="modalwindow-close" id="' + id + '_" href="#"></a>' + title + '</h5></div><div class="modalwindow-footer"><div class="modalwindow-body" id="' + id + '"><div class="modalwindow-body-waiting">&nbsp;</div></div></div></div>').appendTo(document.body);
	

	$('#' + id).load(src, function()
	{
			$('#' + id).attr("class", "modalwindow-body");  });


	var top = (getClientHeight()-$('#' + id + '_header').height())/2+"px";
	var left = (getClientWidth()-$('#' + id + '_header').width())/2+"px";
			
	$('#' + id + '_header').css({ "left": left, "top": top, "position": "fixed"});
	
	$('.modalwindow-close').click(function()
	{
		var idheader = $(this).attr('id');
		idheader = idheader + 'header';
		
		$('#' + idheader).remove();
		
		
	}

	);	
}

function dialogClose(id)
{
	$('#' + id + '_header').remove();
}

function dialogSetContentAndShow(src, id,  title)
{
	if (!id)
		id='dialogWindow';
						 
	if ($('#' + id + '_header').length)
	{
		$('#' + id + '_header').remove();
	}
	
	if (!title)
		title = "&nbsp;";
	$('<div class="modalwindow modalwindow-header" id="' + id + '_header"><div class="modalwindow-header-title"><h5><a class="modalwindow-close" id="' + id + '_" href="#"></a>' + title + '</h5></div><div class="modalwindow-footer"><div class="modalwindow-body" id="' + id + '">&nbsp;</div></div></div>').appendTo(document.body);
//	$('<div class="modalwindow modalwindow-header" id="' + id + '_header"><div class="modalwindow-header-title"><h5><a class="modalwindow-close" id="' + id + '_" href="#"></a>' + title + '</h5></div><div class="modalwindow-footer"><div class="modalwindow-body"><div class="modalwindow-body" id="' + id + '">&nbsp;</div></div></div></div>').appendTo(document.body);
	$('.modalwindow-close').click(function()
	{
		var idheader = $(this).attr('id');
		idheader = idheader + 'header';
		
		$('#' + idheader).remove();
	});	
/*	
	$('#' + id + '_header').css("position","absolute");
	$('#' + id + '_header').css("top",(getClientHeight()-$('#' + id + '_header').height())/2+"px");
	$('#' + id + '_header').css("left",(getClientWidth()-$('#' + id + '_header').width())/2+"px");	*/
	var top = (getClientHeight()-$('#' + id + '_header').height())/2+"px";
	var left = (getClientWidth()-$('#' + id + '_header').width())/2+"px";
			
	$('#' + id + '_header').css({ "left": left, "top": top, "position": "absolute"});

	$(src).appendTo($('#' + id));
/*	if (!id)
		id='#dialogWindow';
		
	if (!body)
		 body='.dialogWindowBody';

	$(body).empty();
	$(src).appendTo($(body));
	$(id).jqmShow();*/
}

function dialogHide(id)
{
	if (!id)
		id='#dialogWindow';

	$(id).jqmHide();
}


////// Для сервиса выбора фоток
var AlbumNumber = -1;
var GalleryHistory = new Array();
var CurrentHistoryPosition = -1;
var CurrentID;

function AddToHistory(Url)
{
	// Если после текущей страницы в истории есть другие страницы - их нужно удалить
	if ((CurrentHistoryPosition + 1) < GalleryHistory.length)
	{	
		GalleryHistory.slice(CurrentHistoryPosition);
	}
	CurrentHistoryPosition = CurrentHistoryPosition + 1;
	GalleryHistory[CurrentHistoryPosition] = Url;
}

function GoBack()
{
//	alert('GoBack');
	if (CurrentHistoryPosition > 0)
	{
		CurrentHistoryPosition = CurrentHistoryPosition - 1;
		ShowContentInFrame(GalleryHistory[CurrentHistoryPosition], CurrentID, true);
	}
}

function GoNext()
{	
//c	alert('GoNext');
	if (CurrentHistoryPosition+1 < GalleryHistory.length)
	{
		CurrentHistoryPosition = CurrentHistoryPosition + 1;
		ShowContentInFrame(GalleryHistory[CurrentHistoryPosition], CurrentID, true);
	}
}

function ShowContentInFrame(src, id, save_to_history)
{
	$.ajax({
		url: src,
		success: function(data) 
		{				
			$(id).html(data);
			
			CurrentID = id;
	
			if (!save_to_history)
				AddToHistory(src);
				
			
			if (isBack())
			{
				$("#dialogWindow2_back_disabled").css('display', 'none');
				$("#dialogWindow2_back").css('display', 'block');
			}
			else
			{				
				$("#dialogWindow2_back_disabled").css('display', 'block');
				$("#dialogWindow2_back").css('display', 'none');
			}
		
			if (isNext())
			{
				$("#dialogWindow2_next_disabled").css('display', 'none');
				$("#dialogWindow2_next").css('display', 'block');
			}
			else
			{
				$("#dialogWindow2_next_disabled").css('display', 'block');
				$("#dialogWindow2_next").css('display', 'none');
			}
		}
	});		
}

function isBack()
{
	if (CurrentHistoryPosition > 0)	
		return true;
	else		
		return false;	
}

function isNext()
{
	if (CurrentHistoryPosition+1 < GalleryHistory.length)
		return true;
	else
		return false;	
}

function ServiceGallery_SetPageNumber(i)
{
	ShowContentInFrame('index.php?service=service_gallery&saction=show_albums_list&page_id='+i, '#gallery_content');
}
function ServiceGallery_Album_SetPageNumber(i)
{
	if (AlbumNumber >= 0)
		ShowContentInFrame('index.php?service=service_gallery&saction=show_album&album_id='+AlbumNumber+'&page_id=' + i, '#gallery_content');		
} 

var GalleryChoosePictureCallback = '';

function CloseChoosePictureDialog()
{
	dialogClose("GalleryModalWindow");
}
	
function ShowChoosePictureDialog(Caption, Callback)
{
	var Link;
		
		
	var url = document.location.href;
	url = url.split('?');
		
	if (CurrentPage == 'my_albums')
		Link = 	url[0] + "?service=service_gallery";

	GalleryChoosePictureCallback = Callback;

	dialogShow(Link, 'GalleryModalWindow', Caption);
}

function CallChoosePictureDialogFunc($id)
{
	GalleryChoosePictureCallback($id);
}


function ServiceGallery_ShowAlbum(i)
{
	AlbumNumber = i;

	ShowContentInFrame('index.php?service=service_gallery&saction=show_album&album_id=' + i, '#gallery_content');
}
