Import('other/ImageLayer.js');
Import('native/String.js');
Import('myLib/layers/LayerManager.js');

PageService = {};

Import.clone(Service, PageService);

//---

PageService.showPersonLayer = function(id, ev){
	PageService.showInfoLayer(id, 'person', ev);
};

PageService.hidePersonLayer = function(id, ev){
	PageService.hideInfoLayer(id, 'person');
};

PageService.showRecordLayer = function(id, ev){
	PageService.showInfoLayer(id, 'record', ev);
};

PageService.hideRecordLayer = function(id, ev){
	PageService.hideInfoLayer(id, 'record');
};

PageService.showInfoLayer = function(id, prefix, ev){
	var element = document.getElementById(prefix + id);
	if(element != null){
		var body = (document.documentElement || document.body);
		element.style.left = (body.scrollLeft + ev.clientX + 40) + 'px';
		element.style.top = (body.scrollTop + ev.clientY - 100) + 'px';
		element.style.display = 'block';
	}
};

PageService.hideInfoLayer = function(id, prefix){
	var element = document.getElementById(prefix + id);
	if(element != null){
		element.style.display = 'none';
	}
};

//---



//---

PageService.logonUser = function(form){
	var login = form['login'].value;
	var password = form['haslo'].value;

	if(login == '' || password == ''){
		alert('Wprowadź login i hasło.');
	}
	else{
		MyAJAX.init('logonUser', [login, password], function(obj){
			//alert(obj.responseText);
			var result = parseInt(obj.responseText);
			if(result == 0)
				form.submit();
			else
				alert('Podany login lub hasło nie są poprawne.');
		});
	}
	return false;
};

PageService.logoutUser = function(){
	MyAJAX.init('logoutUser', null, function(obj){
		//alert('Potwierdzenie wylogowania.');
		document.location.href = Config.file.MAIN;
	});
};

PageService.removeUser = function(){
	if(confirm('Czy napewno chcesz usunąć konto?')){
		MyAJAX.init('removeUser', null, function(obj){
			//alert(obj.responseText);
			alert('Konto zostało usunięte.');
			document.location.href = Config.file.MAIN;
		});
	}
};

//---

PageService.openPage = function(href, target){
	var href = href.replaceAll('@', '&');
	if(target == '_self')
		document.location.href = href;
	else
		window.open(href);
};

PageService.closeLayer = function(id) {
	LayerManager.close(id);
};

//---

PageService.openImageById = function(id, description){
	var path = Config.getPathToPage() + Config.file.VIEWER + '?id=' + id;
	ImageLayer.showOnce(path, description);
};

PageService.openImage = function(image, description){
	var path = Config.getPathToData() + image;
	ImageLayer.showOnce(path, description);
};

PageService.openAddedImageById = function(id){
	ImageLayer.showById(id);
};

PageService.setImage = function(id, image, description){
	var path = Config.getPathToData() + image;
	ImageLayer.setValues(id, path, description);
};

//---

/*
PageService.openPrintVersion = function(url){
	var params = {
		left:		20,
		top:		20,
		width: 		627, //faktyczna: 587
		height: 	550,
		scrollbars: 'yes',
		menubar: 'yes'
	};
	Service.openWindow(url, params);
};

PageService.openViewNewsletter = function(){
	var params = {
		left:		20,
		top:		20,
		width: 		580,
		height: 	550,
		scrollbars: 'yes',
		resizable:	'yes'
	};
	var path = Config.PATH + Config.dir.PAGE + Config.file.NEWSLETTER;
	Service.openWindow(path, params);
};

PageService.openFormNotifier = function(){
	var params = {
		left:		100,
		top:		100,
		width: 		500,
		height: 	240
	};
	var path = Config.PATH + Config.dir.PAGE +
	Config.file.NOTIFIER + document.location.search;
	Service.openWindow(path, params);
};
*/
