Import('myLib/html/DisplayFlash.js');
Import('myLib/util/OpenFile.js');

function Service(){};

Service.activateField = function(field, isActive){
	field.disabled = !isActive;
};

/**
 * Zwraca rozszerzenie pliku.
 */
Service.getSuffix = function(fileName){
	var fileName = fileName.toLowerCase();
	var pos = fileName.lastIndexOf('.');
	return fileName.substring(pos + 1);
};

/**
 * Wyswietla banery w formatach: swf, jpg, gif, png.
 */
Service.dispalyBanner = function(params){
	var suffix = Service.getSuffix(params['src']);
	if(suffix == 'swf'){
		DisplayFlash.init(params);
	}
	else{
		var img = '<img src="' + params['src'] + '" width="' +
		params['width'] + '" height="' + params['height'] + '" border="0" />';

		if(typeof(params['href']) != 'undefined' && params['href'] != ''){
			img = '<a href="' + params['href'] + '" target="' + params['target']
		+ '">' + img + '</a>';
		}

		document.write(img);
	}
};

/**
 * Pyta czy skasowac plik.
 */
Service.confirmDelete = function(href, text){
	if(typeof(text) == 'undefined'){
		var text = "Czy jesteś pewien, że chcesz usunąć tę informacę?";
	}
	text += "\nJeżeli tak kliknij [OK], jeżeli nie kliknij [Anuluj].";

	if(confirm(text)){
		document.location.href = href;
	}
};

Service.checkPassword = function(field1, field2){
	if(field1.value != field2.value){
		alert('Wprowadzone hasła muszą być identyczne');
		field1.value = field2.value = '';
		field1.focus();
		return false;
	}
	else{
		return true;
	}
};

Service.openSubEditor = function(href, params){
	var p = {
		width:	600,
		top:	200,
		left:	100
	};
	if(typeof(params) == 'object'){
		for(var i in params){
			p[i] = params[i];
		}
	}
	Service.openWindow(href, p);
};

Service.openWindow = function(href, params){
	var p = '';
	var name = '';
	if(typeof(params) == 'object'){
		for(var i in params){
			if(i == 'name')
				name = params[i];
			else
				p += i + '=' + params[i] + ',';
		}
	}
	window.open(href, name, p);
};

Service.showPanel = function(filedName){
	document.write('<div style="margin: 5 0 5 0; text-align: center;">' +
	'<input type="button" value="Edytor" class="bMain" onclick="Service.showHTMLEditor(\''
	+ filedName + '\');"></div>');
};

Service.showHTMLEditor = function(filedName){
	var href = Config.getPathToAdmin() + 'editor.php?field=' + filedName;
	//alert(href);
	window.open(href, '', 'width=700, height=500');
};

Service.openImage = function(pathFile){
	//Service.openFile(pathFile);
	//var path = Config.getPathToData() + pathFile;
	//window.open(path, '', 'left=100, top=100, scrollbars=yes');
	//var url = 'http://' + document.location.host + '/' + Config.dir.DATA + pathFile;
	//document.write(url);
	var x = new OpenFile();
	x.focus = false;
	x.showImage(pathFile);
};

Service.openFile = function(pathFile){
	var path = Config.getPathToData() + pathFile;
	window.open(path);
};

Service.insertString = function(field, text){
	if(typeof(text) != 'string'){
		var text = 'http://';
	}

	if(field.value == ''){
		field.value = text;
		field.select();
		field.onblur = function(){
			if(field.value == text){
				field.value = '';
			}
		}
	}
};

/*

Service.bg = '';

Service.changeBg = function(id, x, color){
    var element = document.getElementById(id);
    if(x){
        Service.bg = element.bgColor;
        if(!color){
            var color = "#eeeeee";
		}
		element.bgColor = color;
    }
	else{
		element.bgColor = Service.bg;
	}
};

Service.insertString = function(field, text){
	if(typeof(text) != 'string'){
		var text = 'http://';
	}

	if(field.value == ''){
		field.value = text;
		field.select();
		field.onblur = function(){
			if(field.value == text){
				field.value = '';
			}
		}
	}
};

Service.insertDefaultValue = function(field, selectInput){
	var isInsert = false;

	if(field.value == ''){
		isInsert = true;
	}
	else{
		for(var i = 0; i < selectInput.options.length; i++){
			if(selectInput.options[i].text == field.value){
				isInsert = true;
				break;
			}
		}
	}

	if(isInsert){
		var x = selectInput.options[selectInput.selectedIndex];
		if(x.value == ''){
			field.value = x.value;
		}
		else{
			field.value = x.text;
			field.focus();
			field.select();
		}
	}

	return isInsert;
};

Service.sendSearchForm = function(form){
	if(form.szukaj.value == ''){
		alert('Podaj szukane wyrażenie.');
		form.szukaj.focus();
	}
	else{
		form.submit();
	}
};

Service.addFavorite = function(){
	var l = document.location;
	var link = l.protocol + '//' + l.hostname; // l.href - full address
	var title = l.hostname;

	if(document.all){ // for IE
		if(document.title != ''){
			title = document.title;
		}
		window.external.AddFavorite(link, title);
	}
	else{
		var t = document.getElementsByTagName('title');
		var title = t[0].firstChild.nodeValue;
		window.sidebar.addPanel(title, link, '');
	}
};
*/