//<![CDATA[

// DHTML
var DHTML = (document.getElementById || document.all || document.layers);

// Return object by name.
function GetObject(name) {
  	if (document.getElementById) {
  		this.obj = document.getElementById(name);
  		if(this.obj!=null)
  			this.styl = document.getElementById(name).style;
  	}
  	else if (document.all) {
		this.obj = document.all[name];
  		if(this.obj!=null)
			this.styl = document.all[name].style;
  	}
  	else if (document.layers) {
   		this.obj = document.layers[name];
  		if(this.obj!=null)
   			this.styl = document.layers[name];
  	}
}

// Question. If user accept question, site is forwarded to address.
function aar(question, address) {
	if (confirm(question))
		window.location=address;
}

// Confirm deleting operation.
function deleteRecord(arg) {
	aar("Czy napewno chcesz usunąć wpis?", "index.php"+arg);
}

// Layout function v3.0
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// Layout function v3.0
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// Layout function v4.01
function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Layout function v3.0
function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Valid form.
// @return Bool True or false dependence on form valid.
function nameSurnameSearchValidation() {
	if (!DHTML)
		return;

	// nameSurnameSearchmessageBox object.
	var nameSurnameSearchmessageBox = new GetObject('nameSurnameSearchmessageBox');
        
	// filtrNameSurnameSearch object.
	var filtrNameSurnameSearch = new GetObject('filtrNameSurnameSearch');

	// Valid filtrNameSurnameSearch object.
	if(!validValue(filtrNameSurnameSearch, "isEmpty", nameSurnameSearchmessageBox, "test")) {
        filtrNameSurnameSearch.obj.value = "Pole nie może być puste!";
        return false;
    }

	// Valid filtrNameSurnameSearch object.
	if(!validValue(filtrNameSurnameSearch, "isString:3:100", nameSurnameSearchmessageBox, "test")) {
        filtrNameSurnameSearch.obj.value = "Minimum 3 znaki!";
        return false;
    }
	
	if(
		filtrNameSurnameSearch.obj.value == "Pole nie może być puste!"
		||
		filtrNameSurnameSearch.obj.value == "imię i nazwisko osoby zmarłej"
	)
		return false;
       
	return true;
}
    
function moveScrollLeft() {
        parent.scrollIframe.document.getElementById('offset').innerHTML = "-1";
}
    
function moveScrollRight() {
        parent.scrollIframe.document.getElementById('offset').innerHTML = "1";
}

function checkSuggestBox(){
	var suggestBox = new GetObject('formCatalogSuggestBody');
	if(suggestBox.obj != null){
		suggestBox.obj.style.display = 'none';
		suggestBox.obj.style.height = '0';
		var formCatalogStateSelectFrame = new GetObject('formCatalogStateSelectFrame');
		if(formCatalogStateSelectFrame.obj != null){
			formCatalogStateSelectFrame.styl.width = '0';
			formCatalogStateSelectFrame.styl.height = '0';
		}
	}
}


// opens a new window with current tag a href value
function openHREF(that){
	var url = that.href;
	window.open(url);
	return false;
}

//
//
// Manage category menu
function changeCatalogCategory(catId){
	if(catId > 0){
		var formCatalogCategory = new GetObject('formCatalogCategory');
		var formCatalogCategorySelect = new GetObject('formCatalogCategorySelect');
		var formCatalog = new GetObject('catalogViewForm');
		if(formCatalogCategory != null && formCatalog != null){
			formCatalogCategory.obj.value = catId;
			
			for(var opt = 0; opt < formCatalogCategorySelect.obj.options.length; opt++){
				if(formCatalogCategorySelect.obj.options[opt] && formCatalogCategorySelect.obj.options[opt].value == catId){
					formCatalogCategorySelect.obj.selectedIndex = opt;
					break;
				}					
			}

			formCatalog.obj.submit();	
			return false;		
		}
	}else{
		return false;
	}
}
function expandCatalogCategory(that, catId){
	if(catId > 0){
		var categoryMenu = new GetObject('categoryMenu');
		if(categoryMenu != null){
			var trs = categoryMenu.obj.rows;
			var search_name = eval('/subcategory'+catId+'/i');
			var trs_length = trs.length;
			if(trs.length>0){
				for(var i = 0; i < trs_length; i++){
					if(trs[i].className.search(search_name)>=0){
						if(trs[i].style.display == 'none'){
							trs[i].style.display = '';
						}else{
							trs[i].style.display = 'none';
						}
					}
				}
			}	
		}
		return false;
	}else{
		return false;
	}
}



function blankBackground(that){
	if(that){
		that.style.background = '#FFFFFF';
	}
}
function blankBackground2(that){
	if(that){
		that.style.backgroundImage = 'none';
	}
}


function getRemebersList(obiId){
	if(obiId>0){
		var parameters = new Array('formDescribeId');
		var parameters = 'obituaryId='+obiId
		ajaxRequestMethodSimple('showobituary', 'getRemembersList', parameters, getRemebersListResponse, null, 'newsletterBox');
	}else{
		
	}
}

function getRemebersListResponse(){
	var response = ajaxResponseMethod(null);
	var message = '';

	// Request faild.
	if (response == '-1') {
		//message = '<font color="#aa0000">Pusta lista!</font>';
		message = '';
	}else if (response == null) {
		//message = '<font color="#aa0000">Błąd pobierania listy!</font>';
		message = '';
	}else{
		var pamietamyList = new GetObject('pamietamyList');
		pamietamyList.obj.innerHTML = '';
		var splitted = response.split('^^');
		var rem_list = '';
		if(splitted.length > 0){
			var i = 0;
			var max_i = splitted.length;
			for (i;i<max_i;i++){
				var s_row = splitted[i].split('%%');
				if(s_row[0] && s_row[1] && s_row[2] && s_row[3]){
					rem_list += '<li class="remembersElement">'+
								'<div class="remNo opis-1">'+(i+1)+'.</div><div class="remName"><a href="'+s_row[3]+'" class="opis-1" title="Kliknij aby wysłać email do tej osoby">'+s_row[1]+'</a></div>'+
								'</li>';
				}else continue;
			}
		}else{
			rem_list = '<li class="center"><em>Brak wpisów</em></li>';
		}
		var pamietamyMoreDiv = new GetObject('pamietamyMoreDiv');
		pamietamyMoreDiv.styl.display = 'none';
		pamietamyList.obj.innerHTML = rem_list;
	}
	var messageBox = new GetObject('newsletterBox');
	messageBox.obj.innerHTML = message;
}



function anythingSetSomething(responseAr, text_ok_0, text_ok_1, text_error, idField, setOthers, messageDiv, valuesTableId) {
	if(typeof (valuesTableId) == "undefined") var valuesTableId = 'valuesTable';
	var message = '<font color="#00aa00">'+text_ok_0+'</font>';
	var error_message = '<font color="#aa0000">'+text_error+'</font>';
	if(responseAr == '-1'){
		messageDiv.obj.innerHTML = error_message;
		return false;
	}
	if(typeof(responseAr)=="undefined") return;
	var res = responseAr.split(':^:');
	if(!res[0] || !res[1] || !res[2]){
		messageDiv.obj.innerHTML = error_message;
		return false;
	}
	if(res[1]<='0'){
		messageDiv.obj.innerHTML = error_message;
		return false;
	}
	if(res[2]<'0' || res[2]>'1'){
		messageDiv.obj.innerHTML = error_message;
		return false;
	}
	var changeObj = new GetObject(idField+'_'+res[1]);
	if(changeObj == null){
		messageDiv.obj.innerHTML = '<font color="#aa0000">Błąd aktualizacji pola!</font>';
		return false;
	}
	// Restore success.
    if (res[0] == '1') {
    	if(res[2] == '0')
    		messageDiv.obj.innerHTML = '<font color="#00aa00">'+text_ok_0+'</font>';
    	else{
			messageDiv.obj.innerHTML = '<font color="#00aa00">'+text_ok_1+'</font>';
			if(setOthers==1){
				var valuesTable = new GetObject(valuesTableId);
				var aArray = valuesTable.obj.getElementsByTagName('A');
				for(var i = 0; i<aArray.length; i++){
					if(typeof(aArray[i].id) != "undefined" && aArray[i].id.indexOf(idField) != -1){
						aArray[i].style.color = '#AA0000';
    					aArray[i].innerHTML = 'nie';
					}
				}
			}
		}
		changeObj.styl.color = ( res[2] == '0' ? '#AA0000' : '#00AA00' );
    	changeObj.obj.innerHTML = ( res[2] == '0' ? 'nie' : 'tak' );
		return true;
	}
	// Restore failed.
	else if (res[0] == '-1') {
		messageDiv.obj.innerHTML = '<font color="#aa0000">'+text_error+'</font>';
		return false;
	}
}

function redirectThis(){
	if(typeof redirectTime == "undefined" || redirectTime == '')
		redirectTime = 1000;
   	if(typeof redirectLink != "undefined" && redirectLink != '')
    	setTimeout(eval("window.location='"+redirectLink+"';"), redirectTime);
}



function goTo(gotoURL){
	if(!gotoURL || typeof gotoURL == 'undefiend'){
		window.location = '/?';
	}else{
		window.location = gotoURL;
	}
	return false;
}

function goToNew(gotoURL){
	if(!gotoURL || typeof gotoURL == 'undefiend'){
		window.open('/?');
	}else{
		window.open(gotoURL);
	}
	return false;
}

function closeComment(that){
	var pare = that.parentNode.parentNode;
	if(pare.className == 'comment')
		pare.style.display = 'none';
}


function evalJSON(what){
	return eval('(' + what + ')')
}



//
// Shows notofication about new elements
//
function showNotifications(){
		var infoWindows = {
			info0 : { // kondolencje
				left : -80,
				top : 36,
				file : 'tr',
				imgCss : {
					'top' : '-15px',
					'right' : '-15px'
				}
			},
			info1 : { // znicze
				left : 162,
				top : 36,
				file : 'tl',
				imgCss : {
					'top' : '-15px',
					'left' : '-15px'
				}
			},
			info2 : { // zdjecia
				left : -80,
				top : 36,
				file : 'tr',
				imgCss : {
					'top' : '-15px',
					'right' : '-15px'
				}
			},
			info3 : { // wspomnienia
				left : 162,
				top : 36,
				file : 'tl',
				imgCss : {
					'top' : '-15px',
					'left' : '-15px'
				}
			},
			info4 : { // pamietamy
				left : -80,
				top : 36,
				file : 'tr',
				imgCss : {
					'top' : '-15px',
					'right' : '-15px'
				}
			}
		};
		
	$('.hiddenInformation').each(function(){
		var elem = $(this);
		
		var obj = null;
		var i_pos = 0;
		if(this.id == "hiddenInformation0"){
			obj = infoWindows.info0;
			i_pos = 0; 
		}else if(this.id == "hiddenInformation1"){
			obj = infoWindows.info1;
			i_pos = 1;
		}else if(this.id == "hiddenInformation2"){
			obj = infoWindows.info2;
			i_pos = 2;
		}else if(this.id == "hiddenInformation3"){
			obj = infoWindows.info3;
			i_pos = 3;
		}else if(this.id == "hiddenInformation4"){
			obj = infoWindows.info4;
			i_pos = 4;
		}	
		elem.width(200).removeClass('hiddenInformation').addClass('visibleInformation').css({
			'top'  : obj.top,
			'left' : obj.left
		}).attr('title', 'Kliknij, aby ukryć powiadomienie');
		
		var oDIV = document.createElement('div');
		var oIMG = document.createElement('img');
		oIMG.src = '/gfx/corners/'+obj.file+'.png';
		$(oIMG).css(obj.imgCss);
		$(oDIV).append(oIMG);
		$(oDIV).appendTo(elem);
		
		elem.hover(function(){
			elem.css('backgroundColor', '#FBFBAA');
		}, function(){
			elem.css('backgroundColor', '#FBFBFB');
		}).click(function(){
			elem.fadeOut('slow', function(){elem.remove();});
			$.post('index.php', {'do':'start', 'ajax': true, 'action':'hideInfoWindow', 'i':i_pos}, function(r){});
		});
		
		//return false;
	});
}

/**
 * Pokazuje/Ukrywa przycisk logowania ukrywając/pokazujac formularzyk logowania
 */
function lbToggle(that){
	var $t = $(that);
	var $p = $t.parent('div');
	var $lb = $('#logowanie_box');
	
	if($lb.is(':hidden')){
		// Pokazujemy formularz
//		$lb.show();
//		$p.hide();
		$p.css('position', 'absolute').fadeOut('fast');
		$lb.fadeIn('slow', function(){
			$('#formLoginBox').focus();			
		});
	}else{
		// Ukrywamy formularz
//		$lb.hide();
//		$p.show();
		$lb.fadeOut('fast');
		$p.fadeIn('fast');
	}
	return false;
}

/**
 * Zmienia sposób wyświetlania koemtarza na stronie na pełnoekranowy
 */
function comment_modalize(){
	var $c = $('#commentBox');
	if($c.length == 1){
		var overlib_content = '<div class="center" style="padding:10px 0;">'+$c.children('.comment_body').html()+'</div>';
		return overlib(overlib_content, SHADOW, SHADOWOPACITY, 60, CENTERPOPUP, STICKY, CAPTION, 'Komunikat', CLOSECLICK, CLOSETEXT, '[x]', WIDTH, 400, HEIGHT, 300, DISABLEPAGE, EXCLUSIVE);

	}
}


/**
 * Boks logowania - ukrywanie okienka logowania
 */
var _login_box = (function(){
	var private = {
		_timer : null,
		
		_hide : function(){
			$('#logowanie_box').hide();
			$('#logowanie_box_trigger').css('position', 'relative').fadeIn('fast');
			private._disable_hide();
		},
		_enable_hide : function(){
			$('table.main').click(function(){
				public.hide(true);
				private._hide();
			});
		},
		_disable_hide : function(){
			$('table.main').unbind('click');
		}
	};

	var public = {
		show : function(that){
			var $t = $(that);
			$t.parent('div').css('position', 'absolute').hide();
			$('#logowanie_box').fadeIn('fast');
			$('#formLoginBox').focus();		
			private._enable_hide();
			return false;
		},
		hide : function(stop){
			if(typeof stop == "undefined") var stop = false;
			if(stop === true){
				clearTimeout(private._timer);
			}else{
				private._timer = setTimeout(function(){
					private._hide();
				}, 1000);
			}
		}
	}; 
	return public;
})();

function isIE(ver){
	if(typeof ver == "undefined") ver = 'any'; 
	if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		var ieversion=new Number(RegExp.$1)
		if(ver == '8' && ieversion>=8) return true;
		else if(ver == '7' && ieversion>=7 && ieversion<8) return true;
		else if (ver == '6' && ieversion>=6 && ieversion<7) return true;
		else if (ver == '5' && ieversion>=5 && ieversion<6) return true;
		else if (ver == 'any' && ieversion>=5) return true;
	}
	return false;
}
//]]>
