var globalController = _gC = new Object();


_gC.getInstance = function() {}

_gC.messages;

dojo.addOnLoad(function(){
	dojo.query('.Qmark img').forEach(function(node) {
		node.onclick = function() { document.location.href='/faq'; }
	});
});

_gC.error = function(message,d) {
	if(d == null) d=10000;
	var cont = dojo.byId("alerts");
	var ret = '';
	if(cont) {
		ret += '<div id="fmErrors"><div class="scarfE">';
		ret += message;
		ret += '</div><div class="bottomE"></div></div>';
		cont.innerHTML = ret;
		_gC.hideAlert(d);
		window.scrollTop(0);
	}
}

_gC.notice = function(message,d) {
	if(d == null) d=10000;
	var cont = dojo.byId("alerts");
	var ret = '';
	if(cont) {
		ret += '<div id="fmNotices"><div class="scarfN">';
		ret += message;
		ret += '</div><div class="bottomN"></div></div>';
		cont.innerHTML = ret;
		_gC.hideAlert(d);
		window.scrollTop(0);
	}
}

_gC.hideAlert = function(d) {
	dojo.require("dojo.fx");
	var cont = dojo.byId("alerts");
	var params = {
		node: "alerts",
		duration: 1000,
		delay: d,
		onEnd: function() {
			cont.innerHTML = '';
			dojo.style(cont,'display','block');
		}
	};
	dojo.fx.wipeOut(params).play();
}

_gC.newMessages = function() {
	var alertText = dojo.byId('messAlert').innerHTML;
	if(alertText.length>0) {
		dojo.xhrGet({
			url: "/nowe-wiadomosci",
			handleAs:"text",
			load: function(data){
				if(parseInt(data)>0) {
					if(data!=_gC.messages) {
						_gC.notice(alertText.replace('%s',data),60000);
						_gC.messages = data;
						window.setTimeout("checkMess",30000);
					}
				}
			}
		});
	}
}

_gC.showItemsMore = function(obj) {
	if(obj) {

		var changeText = obj.getAttribute('changeText')
		var origText = obj.innerHTML;

		obj.innerHTML=changeText;
		obj.setAttribute('changeText', origText);

		var info = obj.offsetParent;
		info.id="currentMore";

		var info_div = dojo.query('#currentMore .oI_info');
		if(info_div[0]) {
			if(info_div[0].style.display=="" || info_div[0].style.display=="none") {
				info_div[0].style.display='block';
				obj.parentNode.className='oI_more oI_more_up';
			} else {
				info_div[0].style.display='none';
				obj.parentNode.className='oI_more';
			}
		}

		info.id="";
	}
}

_gC.buyPoints = function(amount,obj) {
	dojo.xhrPost({
			url: "/main/payment/start",
			handleAs:"text",
			content: {"amount":amount},
			load: function(data) {
				if(!data || data.substr(0,5)!='<form') {
					alert("500 Internal Error. Colud not start transfer payment at this time. Try again later!")
				} else {
					obj.innerHTML = data;
					dojo.byId('pmntFrm').submit();
				}
			},
			error: function(response, ioArgs) {
				alert("500 Internal Error\n"+ioArgs.xhr.responseText);
			}
		});
}



function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}



function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function divSwitch(show,hide) {
	dojo.byId(hide).style.display = 'none';
	dojo.byId(show).style.display = 'block';
}

function openPasswdInput() {
	dojo.byId('fakePass').style.display='none';
	dojo.byId('password').focus();
}

function showSlide(id) {
	
	if(isScreenOpen==0) {	
		lb.startup();
		lb2.startup();
		lb3.startup();
		lb4.startup();
		isScreenOpen=1;
	}
		
	if(id==1) lb.show();
	if(id==2) lb2.show();
	if(id==3) lb3.show();
	if(id==4) lb4.show();
}

function wipe(ob) {
    if(dojo.byId(ob).style.display == 'none') {
    	dojo.byId(ob).style.display = 'block';
		basicAjax('/profile/settings/set-wipe/wipeaction/0');
    }
    else {
    	dojo.byId(ob).style.display = 'none';
		basicAjax('/profile/settings/set-wipe/wipeaction/1');
    }
}

function basicAjax(url) {
	var xhrArgs = {
            url: url,
            handleAs: "text"
    }
	dojo.xhrGet(xhrArgs);
}

_gC.changeHintSG = function(side) {

	var cnt = dojo.byId('hintsgCnt').value;
	if(cnt==9) { cnt = 1; }
	if(cnt==0) { cnt = 8; }
	var oldCnt = cnt;
	var duration = 600;
	var size = 786;

	if(side=='l') {
		cnt--;
		dojo.byId('hintsgCnt').value = cnt;
		var margin = size*oldCnt;
		dojo.animateProperty({
			node: "hintCnt",
			duration: duration,
			properties: {
				marginLeft: {
					start: -margin,
					end: -margin+size,
					unit: 'px'
				}
			},
			onEnd: function() {
				if(cnt==0) {
					cnt = 8;
					dojo.style('hintCnt','marginLeft',-size*cnt+'px')
					dojo.byId('hintsgCnt').value = cnt;
				}
			}
		}).play();
	}
	if(side=='r') {
		cnt++;
		dojo.byId('hintsgCnt').value = cnt;
		var margin = size*oldCnt;
		dojo.animateProperty({
			node: "hintCnt",
			duration: duration,
			properties: {
				marginLeft: {
					start: -margin,
					end: -margin-size,
					unit: 'px'
				}
			},
			onEnd: function() {
				if(cnt==9) {
					cnt = 1;
					dojo.style('hintCnt','marginLeft',-size+'px')
					dojo.byId('hintsgCnt').value = cnt;
				}
			}
		}).play();
	}
}

function filterExpand(id) {

	var hide = dojo.byId(id+'_hide');
	if(hide.style.display == 'block') {
		hide.style.display = 'none';
		dojo.byId('filter_arrow_'+id).src = '/res/img/items/fltArrow_b.gif';
	}
	else {
		hide.style.display = 'block';
		dojo.byId('filter_arrow_'+id).src = '/res/img/items/fltArrow_t.gif';
	}
}
function selectExpand(id) {

	var hide = dojo.byId(id+'_hide');
	if(hide.style.display == 'block') {
		hide.style.display = 'none';
	}
	else {
		hide.style.display = 'block';
	}
}
function selectAction(id,unid,name,where,onchange) {
	dojo.byId(unid+'_hide').style.display = 'none';
	dojo.byId(unid+'_inpval').innerHTML = name;
	if(where) dojo.byId(where).value = id;
	dojo.byId(unid).value = id;
	if(onchange) {
		eval(onchange);
	}
}

function filterAction(id,unid) {
	dojo.byId(unid+'_hide').style.display = 'none';
	dojo.byId('filter_arrow_'+unid).src = '/res/img/items/fltArrow_b.gif';
}

var sfHover = function() {
	var sfEls = document.getElementById("topmenu").getElementsByTagName("div");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			if(this.id=='menuProfil' && !noPlayMenu) {
				showPlayMenu();
			}
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			if(this.id=='menuProfil'  && !noPlayMenu) {
				playMenu = setTimeout('closePlayMenu()',100);
			}
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function showBox(id) {
	var box = dojo.byId(id);
	if(box.style.display == 'block')
		box.style.display = 'none';
	else
		box.style.display = 'block';
}

String.prototype.pad = function(l, s){
    return (l -= this.length) > 0
        ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, s.length) + this + s.substr(0, l - s.length) : this;
};

var timer = function(id,timeId,boxId) {
	if(boxId) { dojo.byId(boxId).style.display = 'none'; }
	this.id = id;
	this.timeId = timeId;
	this.boxId = boxId;
	this.endTime = dojo.byId(this.timeId).value;
	this.serverTime = server_time;

    this.get = function() {
    	if(boxId) { dojo.byId(this.boxId).style.display = 'block'; }
    	if(this.serverTime<this.endTime) {
    		this.seconds = (this.endTime-this.serverTime);
    		this.time = Math.floor(this.seconds / 60) + ":" + (this.seconds % 60).toFixed().pad(2, "0");
			if(this.seconds <= 0) {
				this.end();
			}
			else {
				dojo.byId(this.id).innerHTML = this.time;
			}
			if(this.instance) { this.end() }
			this.serverTime = this.serverTime+1;
			this.setInstance();
    	}
    	else {
    		this.end();
    	}
	},
	this.setInstance = function() {
		this.instance = setTimeout(dojo.hitch(this, "get"), 1000);
	},
	this.end = function() {
		clearTimeout(this.instance);
		if(this.serverTime>=this.endTime) {
			if(boxId) { dojo.byId(this.boxId).style.display = 'none'; }
		}
	}
	this.setInstance();
}

loadFacebook = function() {
	var xhrArgs = {
            url: "/main/home/get-facebook",
            handleAs: "text",
            load: function(data) {
				dojo.byId('facebookDiv').innerHTML = data;
            }
        }
	dojo.xhrGet(xhrArgs);
}

showPlayMenu = function() {

	if(playMenu) {
		clearTimeout(playMenu);
	}

	var c = dojo.coords('menuProfil', true);
	box = dojo.byId('hiddenPlayMenu');
	//alert(c.x);
	box.style.top = c.y+41+'px';
	box.style.left = c.x-46+'px';
	box.style.display = 'block';
}

closePlayMenu = function() {
	box = dojo.byId('hiddenPlayMenu');
	box.style.display = 'none';
}

function setTemptationOrPrayer(str) {
 	if(str=="temptation") {
 		document.location = '/pokusy';
 	}
 	else if(str=="prayer") {
 		document.location = '/modlitwy';
	}
}

function closeAmpouleBox(e) {
	if(!e) { e = window.event; }
	if(!e) { return; }
	var src = e.target?e.target:e.srcElement;
	var op = src;
	while(op=op.offsetParent) {
		if(op.className=="ampoulesBox") {
			return;
		}
	}
	if(ampouleChk) {
		clearTimeout(ampouleChk);
	}
	dojo.query('.ampoulesBox').forEach(function(node) {
		dojo.byId('mainBody').onclick=null;
		node.style.display = 'none';
	});
}

ampouleContainerF = function() {
	dojo.byId('mainBody').onclick = closeAmpouleBox;
}

showAmpouleBox = function(id,crd) {
	dojo.byId('mainBody').onclick=null;
	dojo.query('.ampoulesBox').forEach(function(node) {
		node.style.display = 'none';
	});
	var c = dojo.coords(crd, true);
	dojo.byId(id).style.display = 'block';
	dojo.byId(id).style.left = c.x-168+'px';
	ampouleChk = setTimeout('ampouleContainerF()', 2);
}

_gC.onlyNumeric = function(e) {

    var chr = e.keyCode;

    if(!chr) { chr=e.which; }
    
    if((chr>=48 && chr<=57) || chr==8 || chr==9 || chr==37 || chr==46 || chr==116) {
        return true;
    } else {
        return false;
    }
}

_gC.showChurch = function() {	
	var mChBox = dojo.byId('globalChurch');	
	if(mChBox.style.display=='none') { 
		_cH.stop(dojo.byId('chatStart'));		
		if(dojo.byId('mChurchInfo').innerHTML=='') {			
			var xhrArgs = {
	            url: '/township/index/get-township-info',
	            handleAs: "text",
	            load: function(data) {	            	
	            	dojo.byId('mChurchInfo').innerHTML = data;
	            	var cl = dojo.coords('churchStart', true);
	            	mChBox.style.display='block' 					 
					mChBox.style.left = cl.x-(251/2)-9+'px';	
	            }
	  		}
			dojo.xhrGet(xhrArgs);		
		}
		else {
			mChBox.style.display='block';
			var cl = dojo.coords('churchStart', true); 
			mChBox.style.left = cl.x-(251/2)-9+'px';
		}
	} else {  mChBox.style.display='none' }	
}

_gC.loadBanners = function(id) {
	if(id) {
		var xhrArgs = {
	            url: "/res/html/banners.html",
	            handleAs: "text",
	            load: function(data) {
					dojo.byId(id).innerHTML = data;
	            }
	        }
		dojo.xhrGet(xhrArgs);
	}
}