/**
 * Łukasz Podkalicki, lpodkalicki@gmail.com
 */
var ibody = null;
var hcmain = null;
var hmain = null; //gui.widget.get('main', 'main')
var canv = null;
var hwin = null;

function init() { 
	setTimeout('messages.update()', 60000);
	ibody = gui.widget.body('ibody');
	hcmain = gui.widget.container('main');
	hmain = hcmain.vbox('main');
	ibody.append(hcmain);
	//ieCanvasInit();
	//asystent.init();
}

/**
 * @class: PACHES 
*/


// Latki tylko dla IE, jak zwsze . Implementacja pozwala na wyszukiwanie w konkretnej galezi drzewa.
// Jest ok. 3 razy szybsze dla Firefox, ok. 20x szybsze dla IE.  
document.getElementsByClassName = function(className, parentElement) {
  if (typeof parentElement == 'string'){
    parentElement = document.getElementById(parentElement);
  } else if (typeof parentElement != 'object' ||
             typeof parentElement.tagName != 'string'){
    parentElement = document.body;
  }
  var children = parentElement.getElementsByTagName('*');
  var re = new RegExp('\\b' + className + '\\b');
  var el, elements = [];
  var i = 0;
  while ( (el = children[i++]) ){
    if ( el.className && re.test(el.className)){
      elements.push(el);
    }
  }
  return elements;
} 

if (window.attachEvent && document.documentElement.addBehavior) { // brzydkie przeladowania obrazkow *IE 6
	window.attachEvent('onload', function (e) { try { document.execCommand("BACKGROUNDIMAGECACHE", false, true); } catch (e) { } }); 
}
 
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}


/**
 * HELPO
 */


function HELPO() {}
HELPO.prototype.go = function(url) {
	if(document.location) {
		//alert('ullala ! :)');
		location.href = url;
	}
	else location.href = url;
	return true;
}


HELPO.prototype.projects = {

	add_project : function(id_content, id_add_form) {
		helpo.element.hide(id_content);
		helpo.element.show(id_add_form);
	}

}

HELPO.prototype.files = {
	
	add_dir : function(id_files, id_add_file, id_add_dir) {
		helpo.element.hide(id_files);
		helpo.element.hide(id_add_file);
		helpo.element.show(id_add_dir);
	},

	add_file : function(id_files, id_add_file, id_add_dir) {
		helpo.element.hide(id_files);
		helpo.element.hide(id_add_dir);
		helpo.element.show(id_add_file);	
	},

	rename_hide_or_show : function(id1, id2) {
		helpo.element.hide_or_show(id1);
		helpo.element.hide_or_show(id2);
	},
	
	rename_file : function(id)  {
		if(id && (obj=document.getElementById(id))) {
			helpo.fsubmit(id);
			obj.submit();
		}
	},
	
	rename_dir : function(id)  {
		if(id && (obj=document.getElementById(id))) {
			helpo.fsubmit(id);
			obj.submit();
		}
	}
}


HELPO.prototype.element = {
	
	hide : function(id) {
		if(id && (obj=document.getElementById(id))) {
			obj.style.display = 'none';
		}	
	},

	show : function(id) {
		if(id && (obj=document.getElementById(id))) {
			obj.style.display = 'block';
		}
	},
	
	hide_or_show : function(id) {
		if(id && (obj=document.getElementById(id))) {
			obj.style.display = (obj.style.display == 'none' ? 'block' : 'none');
		}	
	}
	
}

HELPO.prototype.gentoken = function() {
	a = 7;
	b = 1395;
	return a + Math.floor(Math.random() * (b - a + 1));
}

HELPO.prototype.fsubmit = function(id) {
	token_id = id + "_token";
	if(tokenObj = document.getElementById(token_id)) tokenObj.value = helpo.gentoken();
}

helpo = new HELPO();


/**
 * APPMSG - komunikaty
 */
function APPMSG(){
	this._container = new Array();
	this._last_id = null;
	this._size = [248, 148];
}

APPMSG.prototype.Open = function(obj, title, content, x, y) {
	var wx = this._size[0];
	var wy = this._size[1];
	
	if(obj){ 
		curPos = utils.currentPos(obj)
		curleft = curPos[0];
		curtop = curPos[1];
		usize = utils.usize();
		//destleft = (destleft = curleft - 70)>0 ? destleft : curleft + 70;
		//desttop = (desttop = curtop -165)>0 ? desttop : curtop + 45;
		destleft = parseInt((usize[0]-wx)/2);
		desttop = parseInt((usize[1]-wy)/2);
		destleft2 = x ? x : parseInt((usize[0] - 950)/2) + 100;
		desttop2 = y ? y : 50;
	}
	else {
		usize = utils.usize();
		curleft = x ? x : parseInt((usize[0] - 950)/2) + 100;
		curtop = y ? y : 50;
		destleft = parseInt((usize[0]-wx)/2);
		desttop = parseInt((usize[1] - wy)/2)-100;
		destleft2 = parseInt((usize[0]-wx)/2);
		desttop2 = parseInt((usize[1] - wy)/2)-100;
	}
	
	id = this._container.length;
	
	var chwin = gui.widget.container()
	var hwin = chwin.vbox('hwin');
	hwin.size(wx, wy);
	hwin.style('position:absolute;display:none;border-right:2px solid #DAE3EB;border-bottom:2px solid #DAE3EB');
	hwin2 = hwin.vbox('hwin2');
	hwin2.size(wx-2, wy-2);
	hwin2.style('background:#F0F2F7;border:1px solid #084376;padding:1px');
	h1 = hwin2.tager('h1').style('margin:0;padding:3px 0 4px 10px;background:#084376;color:#ffffff;font-size:11px;border-bottom:1px solid #C8D3E2');
    h1.text(title);
    hwin3 = hwin2.vbox().style('width:220px;margin:20px 0 0 13px');
    hwin4 = hwin3.vbox().style('background:transparent url(/imgs/alerts/alert.gif) 0 0 no-repeat;padding:5px 0 10px 50px;height:50px;border-bottom:1px solid #C8D3E2;color:#084376;line-height:14px');
    hwin4.text(content);
    hwin5 = hwin3.vbox().style('text-align:center;padding:10px 0 0 90px');
    hwin5.text('<a href="javascript:void(0);" onclick="appmsg.Ok('+id+');" style="background: #235785; color: #ffffff; border: 1px solid #084376; border-top: 1px solid #235785; text-decoration: none; padding: 1px; width: 50px; display: block; float: left; margin: 0 5px 0 0;text-align: center;">Ok</a><div style="clear: both;"></div>');	
	hwin.pos(destleft,desttop);
	hwin.layer(1000);
	hmain.append(chwin);
	
	this._container[id] = {curleft: destleft2, curtop: desttop2, widget: hwin};
	gui.effects.zoomout(hwin, curleft, curtop);
}

APPMSG.prototype.Ok = function(id) {
	if((id>=0) && (WObj = this._container[id])) {
		gui.effects.zoomin(WObj.widget, WObj.curleft, WObj.curtop);
		WObj.widget.remove();
		this._container[id] = null;
	}
}
var appmsg = new APPMSG();

/**
 * APPCONFIRM - komunikat potwierdzenia operacji
 */
function APPCONFIRM(){
	this._container = new Array();
	this._last_id = null;
	this._size = [248, 148];
}
APPCONFIRM.prototype.Open = function(obj, title, content, x, y, _url) {
	var wx = this._size[0];
	var wy = this._size[1];
	
	if(obj){ 
		curPos = utils.currentPos(obj)
		curleft = curPos[0];
		curtop = curPos[1];
		usize = utils.usize();
		//destleft = (destleft = curleft - 70)>0 ? destleft : curleft + 70;
		//desttop = (desttop = curtop -165)>0 ? desttop : curtop + 45;
		destleft = parseInt((usize[0]-wx)/2);
		desttop = curtop - wy - 30;
		destleft2 = x ? x : parseInt((usize[0] - 950)/2) + 100;
		desttop2 = y ? y : 50;
	}
	else {
		usize = utils.usize();
		curleft = x ? x : parseInt((usize[0] - 950)/2) + 100;
		curtop = y ? y : 50;
		destleft = parseInt((usize[0]-wx)/2);
		desttop = parseInt((usize[1] - wy)/2)-100;
		destleft2 = parseInt((usize[0]-wx)/2);
		desttop2 = parseInt((usize[1] - wy)/2)-100;
	}
	
	id = this._container.length;
	
	var chwin = gui.widget.container()
	var hwin = chwin.vbox('hwin');
	hwin.size(wx, wy);
	hwin.style('position:absolute;display:none;border-right:2px solid #DAE3EB;border-bottom:2px solid #DAE3EB');
	hwin2 = hwin.vbox('hwin2');
	hwin2.size(wx-2, wy-2);
	hwin2.style('background:#F0F2F7;border:1px solid #084376;padding:1px');
	h1 = hwin2.tager('h1').style('margin:0;padding:3px 0 4px 10px;background:#084376;color:#ffffff;font-size:11px;border-bottom:1px solid #C8D3E2');
    h1.text(title);
    hwin3 = hwin2.vbox().style('width:220px;margin:20px 0 0 13px');
    hwin4 = hwin3.vbox().style('background:transparent url(/imgs/alerts/confirm.gif) 0 0 no-repeat;padding:5px 0 10px 50px;height:50px;border-bottom:1px solid #C8D3E2;color:#084376;line-height:14px');
    hwin4.text(content);
    hwin5 = hwin3.vbox().style('text-align:center;padding:10px 0 0 50px');
    hwin5.text('<a href="javascript:void(0);" onclick="appconfirm.Ok('+id+');return false;" style="background: #235785; color: #ffffff; border: 1px solid #084376; border-top: 1px solid #235785; text-decoration: none; padding: 1px; width: 50px; display: block; float: left; margin: 0 5px 0 0;text-align: center;">Ok</a><a href="javascript:void(0);" onclick="appconfirm.Cancel('+id+');" style="background: #235785; color: #ffffff; border: 1px solid #084376; border-top: 1px solid #235785; text-decoration: none; padding: 1px; width: 50px; display: block; float: left; text-align: center;">Anuluj</a><div style="clear: both;"></div>');	
	hwin.pos(destleft,desttop);
	hwin.layer(1000);
	hmain.append(chwin);
	
	this._container[id] = {curleft: destleft2, curtop: desttop2, widget: hwin, _url: _url};
	gui.effects.zoomout(hwin, curleft, curtop);
}
APPCONFIRM.prototype.Ok = function(id) {
	if((id>=0) && (WObj = this._container[id])) {
		helpo.go(WObj._url);
		//location.href = ".";
	}
}
APPCONFIRM.prototype.Cancel = function(id) {
	if((id>=0) && (WObj = this._container[id])) {
		gui.effects.zoomin(WObj.widget, WObj.curleft, WObj.curtop);
		WObj.widget.remove();
		this._container[id] = null;
	}
}
var appconfirm = new APPCONFIRM();


/**
 * APPCONTACT - formularz kontaktowy
 */
function APPCONTACT(){
	this._container = null;
	this._uri = '/kontakt/send/';
	this._size = [450, 320];
}
APPCONTACT.prototype.Open = function() {
	var uri_ = this._uri;
	var q = new QUERY(uri_, 'appcontact.Open_onsuccess', 'appcontact.Open_onfailure');
	q.setParam('action', 'getform');
	q.execute();	
}
APPCONTACT.prototype.Open_onsuccess = function(s,result) {
	var r = result.split('|');
	var type_ = parseInt(r[0]);
	if(type_ == 0) {
		appmsg.Open(false, 'Alert', r[1], 0, 0);
	}
	var cform = r[1];
	var wx = appcontact._size[0];
	var wy = appcontact._size[1];
	
	usize = utils.usize();
	curleft = 0; //parseInt((usize[0] - 950)/2) + 100;
	curtop = 0; //50;
	destleft = parseInt((usize[0]-wx)/2);
	desttop = parseInt((usize[1] - wy)/2);
	
	var chwin = gui.widget.container()
	var hwin = chwin.vbox('hwin');
	hwin.size(wx, wy);
	hwin.style('position:absolute;display:none;border:1px solid #084376;background:#DAE3EB');
	hwin2 = hwin.vbox('hwin2');
	//hwin2.size(wx-2, wy-2);
	hwin2.style('background:#DAE3EB;margin:1px');
    hwin3 = hwin2.vbox().style('margin:0;padding:3px 0 4px 10px;background:#084376;color:#ffffff;font-size:11px;border-bottom:1px solid #C8D3E2');
    hwin3.text('Formularz kontaktowy');
    hwin4 = hwin2.vbox().style('padding:10px 0 10px 10px;height:'+(wy-100)+'px;color:#084376;line-height:14px');
    hwin4.text(cform);
    hwin5 = hwin2.vbox().style('text-align:center;padding:10px 0 0 80px');
    hwin5.text('<a href="javascript:void(0);" onclick="appcontact.send();" style="background: #235785; color: #ffffff; border: 1px solid #084376; border-top: 1px solid #235785; text-decoration: none; padding: 1px; width: 50px; display: block; float: left; margin: 0 5px 0 0;text-align: center;">wyślij</a><a href="javascript:void(0);" onclick="appcontact.Close();" style="background: #235785; color: #ffffff; border: 1px solid #084376; border-top: 1px solid #235785; text-decoration: none; padding: 1px; width: 50px; display: block; float: left; text-align: center;">Anuluj</a><div style="clear: both;"></div>');	
	hwin.pos(destleft,desttop);
	hwin.layer(1000-10);
	appcontact._container = hwin;
	hmain.append(chwin);
	//utils.elementFocus('contact_content', 1500);
	gui.effects.zoomout(hwin, curleft, curtop);
}
APPCONTACT.prototype.Open_onfailure = function(s,result) {
	
}
APPCONTACT.prototype.Close = function() {
	if(WObj = this._container){
		gui.effects.zoomin(WObj, 0, 0);
		WObj.remove();
		this._container = null;
	}
}
APPCONTACT.prototype.send = function() {
	var uri_ = this._uri;
	var q = new QUERY(uri_, 'appcontact.send_onsuccess', 'appcontact.send_onfailure');
	q.setParam('action', 'sendform');
	q.setParam('contact_to', document.getElementById('contact_to').value);
	q.setParam('contact_from', document.getElementById('contact_from').value);
	q.setParam('contact_content', document.getElementById('contact_content').value);
	q.execute();
}
APPCONTACT.prototype.send_onsuccess = function(s, result) {
	var r = result.split('|');
	var type_ = parseInt(r[0]);
	if(type_ == 0) {
		cobj = appcontact._container ? appcontact._container.widget : false;
		appmsg.Open(cobj, 'Błąd', r[1], 0, 0);
	}
	else{
		cobj = appcontact._container ? appcontact._container.widget : false;
		appmsg.Open(cobj, 'Formularz kontaktowy', 'Wiadomość została wysłana', 0, 0);
		setTimeout('appcontact.Close()', 500);
	}
	//alert('success:' + result);
}
APPCONTACT.prototype.send_onfailure = function(s, result) {
	alert('failure:' + result);
}
APPCONTACT.prototype.send_onsubmit = function(s, result) {
	alert('failure:' + result);
}
var appcontact = new APPCONTACT();

/**
 * @class: UTILS 
*/ 
var utils = { 
	usize : function() {
        wx = 0;
        wy = 0; 
        if(self.innerWidth){
            wx = self.innerWidth;
            wy = self.innerHeight;
        }
        else if(document.documentElement.clientWidth){
            wx = document.documentElement.clientWidth;
            wy = document.documentElement.clientHeight;
        }
        else if(document.body){
            wx = document.body.clientWidth;
            wy = document.body.clientHeight;
        }
        return [wx, wy];
    },
    
    elementFocus : function(id, nodelay) {
    	if(nodelay && (obj=document.getElementById(id))) obj.focus();
    	else setTimeout("utils.elementFocus('"+id+"', 1);", 500);
    },
    
    currentPos : function(obj) {
    	if(obj) {
	    	var curleft = 0;
		    var curtop  = 0;
		    if (obj.offsetParent) {
		        curleft = obj.offsetLeft;
		        curtop  = obj.offsetTop;
		        while (obj = obj.offsetParent) {
		            curleft += obj.offsetLeft;
		            curtop += obj.offsetTop;
		        } //while
		    } //if
		    return [curleft, curtop];
    	}
    	return [0,0];
    }
}

function employment_form_disable(){
	if( (to = document.getElementById("id_date_to_month"))  && 
	   (to1 = document.getElementById("id_date_to_year") )  )  {
		if (to.disabled){
			to.disabled=null;
			to1.disabled=null;
		}else{
			to.disabled="disabled";
			to1.disabled="disabled";
		}
	
	}
}



function education_form_disable(){
	if( (to = document.getElementById("id_date_to_month"))  && 
	   (to1 = document.getElementById("id_date_to_year") )  )  {
		if (to.disabled){
			to.disabled=null;
			to1.disabled=null;
		}else{
			to.disabled="disabled";
			to1.disabled="disabled";
		}
	
	}
}

function education_form(form){

	helpo.fsubmit(form);
	return true;
	if((from = document.getElementById("id_date_from")) &&  (to = document.getElementById("id_date_to"))
						 && (still = document.getElementById("id_still_learning")) 
						 && (er = document.getElementById("javascript_errors"))	){
		from = from.value;
		to =   to.value;
		still = still.checked;
		if (still){
			now =  new Date();
			d = now.getDate().toString();
			m = now.getMonth()+1;
			if (m<10){
				m = "0"+m.toString();
			}else{
				m = m.toString() 
			}
			if (d<10){
				d = "0"+d.toString();
			}else{
				d = d.toString() 
			}

			y = now.getFullYear().toString()
			now1 = y+"-"+m+"-"+d;
			if (now1<from){
				er.innerHTML = "Data rozpoczęcia nauki nie może być poźniejsza niż dzisiejsza !";
				return false;
			}
		}else{
			if(from>=to){
				er.innerHTML = "Data rozpoczęcia nauki nie może być poźniejsza niż data zakończenia !";
				return false;
			}
		} 
		
	}
	
} 	




/**
 * @class: AX 
*/

function AX() {
	this.POST 						= 'POST';
	this.GET 						= 'GET';
}
AX.prototype._xhr = function() {
	var xhr = null;
 	try {
 		xhr = new XMLHttpRequest(); 
	} catch(e) { 
     	try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } 
     	catch(e2) { 
       		try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } 
       		catch(e) {}
    	}
  	}
  	return xhr;
}
AX.prototype._onsuccess = function(status, result) {
	var EResponse = eval("(" + result + ")");
	alert('sukces')
}
AX.prototype._onfailure = function(status, result) {
		alert('Error '+status, result);
}
AX.prototype._makeParams = function(args) {
	var params = "";
	if(args && (args instanceof Array) && args.length){
		for(id_arg in args) {
			if(params == "") params += args[id_arg];
			else params += "&"+args[id_arg];
		}
	}
	return params;
}
AX.prototype.makeRequest = function(method, url, args, onsuccess_h, onfailure_h, xhr) {
	if(!xhr) xhr = this._xhr();
	if(xhr && url && url.length) {
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4) {
				 if(xhr.status == 200) {
				 	if(onsuccess_h) {
				 		fun = eval(onsuccess_h); 
				 		fun(xhr.status, xhr.responseText);
				 	}
				 	else 
				 		ax._onsuccess(xhr.status, xhr.responseText);
				 }
				 else {
				 	if(onfailure_h) {
				 		fun = eval(onfailure_h);
				 		fun(xhr.status, xhr.statusText);
				 	}
				 	else
				 		ax._onfailure(xhr.status, xhr.statusText);
				 }
			}
		}
		if(method == this.POST){
			xhr.open(ax.POST, url, true);
			if(args && (params = ax._makeParams(args))) xhr.send(params);
			else xhr.send(null); 
			return true;
		}
		else if(method == this.GET) {
			var params = "";
			if(args && (params = ax._makeParams(args))) {
				xhr.open(ax.GET, url + '?' + params, true);
			}	
			xhr.send(null);			
			return true;
		}
		
	}
	return xhr; 	
}

AX.prototype.get = function(_url, args, onsccess_h, onfailure_h, xhr) { if(_url && args) return this.makeRequest(ax.GET, _url, args, onsccess_h, onfailure_h, xhr); else return false;}

var ax = new AX();

/**
 * @class: QUERY 
*/

function QUERY(_url, onsccess_h, onfailure_h) {
	this._url = _url
	this.Params = new Array();
	this.onsccess_h = onsccess_h ? onsccess_h : false;
	this.onfailure_h = onfailure_h ? onfailure_h : false;
}
QUERY.prototype.setParam = function(_ParamKey, _ParamValue) { if(_ParamKey && (_ParamValue!== false)) this.Params.unshift( _ParamKey +'=' + _ParamValue) ; return this;}

QUERY.prototype.execute = function(xhr) {
	var _url = this._url;
	this.setParam('query', '1');
	var params 	= this.Params;
	onsuccess_h = this.onsccess_h;
	onfailure_h = this.onfailure_h;
	ax.get(_url, params, onsuccess_h, onfailure_h, xhr);
}

// --------------------------------------------------------
// PULSE

function PULSE() {
	this._container = new Array();
	this._delay_on = 600;
	this._delay_off = 300;
}

PULSE.prototype.stop = function(cid) {
	if(c = this._container[cid]) return (c.stop = true);
	else return false;
}

PULSE.prototype.border = function(obj_id, times, color1, color2) {
	if(obj = document.getElementById(obj_id)){
		cid = this._container.length;
		color1 = color1 ? color1 : 'FF0000';
		color2 = color2 ? color2 : 'c7d2e1';
		this._container[cid] = {obj: obj, color1: color1, color2: color2, times: times, stop: false, cnt:0, border_old: obj.style.border};			
		this.border_start(cid);
	}
	return cid
}
PULSE.prototype.border_start = function(cid) {
	this._container[cid].cnt += 1;
	this.border_animation(cid);
}

PULSE.prototype.border_animation = function(cid, s) {
	if((c = this._container[cid]) && !(c.stop) && (c.cnt<=c.times)) {
		c.obj.style.border = s ? '1px solid #' + c.color1 : '1px solid #'+c.color2;
		if(s) c.cnt += 1;
		setTimeout('pulse.border_animation('+cid+', '+(s ? 0 : 1)+')', s ? pulse._delay_on : pulse._delay_off);
	}
	else this.border_stop(cid);
}

PULSE.prototype.border_stop = function(cid) {
	if(c = this._container[cid]){
		c.obj.style.border = '1px solid #' + c.color2;
		this._container[cid] = null;
		return true;
	}
	else return false;
}


PULSE.prototype.color = function(obj_id, times, color1, color2) {
	if(obj = document.getElementById(obj_id)){
		cid = this._container.length;
		color1 = color1 ? color1 : 'FF0000';
		color2 = color2 ? color2 : 'c7d2e1';
		this._container[cid] = {obj: obj, color1: color1, color2: color2, times: times, stop: false, cnt:0, border_old: obj.style.border};			
		this.color_start(cid);
	}
	return cid
}
PULSE.prototype.color_start = function(cid) {
	this._container[cid].cnt += 1;
	this.color_animation(cid);
}

PULSE.prototype.color_animation = function(cid, s) {
	if((c = this._container[cid]) && !(c.stop) && (c.cnt<=c.times)) {
		c.obj.style.color = s ? '#' + c.color1 : '#'+c.color2;
		if(s) c.cnt += 1;
		setTimeout('pulse.color_animation('+cid+', '+(s ? 0 : 1)+')', s ? pulse._delay_on : pulse._delay_off);
	}
	else this.color_stop(cid);
}

PULSE.prototype.color_stop = function(cid) {
	if(c = this._container[cid]){
		c.obj.style.color = '#' + c.color2;
		this._container[cid] = null;
		return true;
	}
	else return false;
}

pulse = new PULSE();


// ------------------------------------------------
// project preview

function project_preview_onsuccess(s, result) {
	if(	(main = document.getElementById('main')) &&
		(splash = document.getElementById('splash')) &&
		true
		){
		main.style.display='none';
		splash.innerHTML = result;
		splash.style.display='block';
	}
}

function project_preview(u) {
		var q = new QUERY(u, 'project_preview_onsuccess', false);
		q.execute();
}

// ------------------------------------------------
// SG

function SG() {
		this._ckey = null;
		this._act_tab = 'new_questions';
}


SG.prototype.get_most_answered_questions = function(page){
	act_tab=this._act_tab;
	if (act_tab != 'most_answered'){
		if(obj = document.getElementById('most_answered')) {
		
			obj.className = 'selected';

			act_tab=this._act_tab;
			this._act_tab = 'most_answered';
			if (act_tab && (obj = document.getElementById(act_tab)) ){
				obj.className='';
			}

		
		}


	}

		var _url = '/sg/most_answered/';
		var q = new QUERY(_url, 'sg.get_most_answered_questions_onsuccess', 'sg.get_spec_by_key_onfailure');
		q.setParam('page', page ? page : 0);
		q.execute();
}


SG.prototype.get_most_answered_questions_onsuccess = function(s, result){
		if(obj = document.getElementById('questions_content')) {
			obj.innerHTML = result;
		}
		else return false;
}


SG.prototype.get_new_questions = function(page){
	act_tab=this._act_tab;
	if (act_tab != 'new_questions'){
		if(obj = document.getElementById('new_questions')) {
		
			obj.className = 'selected';

			this._act_tab = 'new_questions';
			if (act_tab && (obj = document.getElementById(act_tab)) ){
				obj.className='';
			}

		
		}
	}
			var _url = '/sg/new_questions/';
			var q = new QUERY(_url, 'sg.get_popular_questions_onsuccess', 'sg.get_spec_by_key_onfailure');
			q.setParam('page', page ? page : 0);
			q.execute();
}


SG.prototype.get_new_questions_onsuccess = function(s, result){
		if(obj = document.getElementById('questions_content')) {
			obj.innerHTML = result;
		}
		else return false;
}



SG.prototype.get_popular_questions = function(page){
	act_tab=this._act_tab;
	if (act_tab != 'popular_questions'){
		if(obj = document.getElementById('popular_questions')) {
		
		obj.className = 'selected';

		
		this._act_tab = 'popular_questions';
		if (act_tab && (obj = document.getElementById(act_tab)) ){
			obj.className='';
		}

		
		}
	}
			var _url = '/sg/popular_questions/';
			var q = new QUERY(_url, 'sg.get_popular_questions_onsuccess', 'sg.get_spec_by_key_onfailure');
			q.setParam('page', page ? page : 0);
			q.execute();
}


SG.prototype.get_popular_questions_onsuccess = function(s, result){
		if(obj = document.getElementById('questions_content')) {
			obj.innerHTML = result;
		}
		else return false;
}


SG.prototype.get_spec_by_key = function(ckey){
			if((ckey == sg._ckey) && (obj2 = document.getElementById('c_'+sg._ckey))) {
					obj2.innerHTML = "";
					obj2.style.display = "none";
					sg._ckey = null;	
					return false;
			}
			var _url = '/sg/specializations/';
			var q = new QUERY(_url, 'sg.get_spec_by_key_onsuccess', 'sg.get_spec_by_key_onfailure');
			q.setParam('ckey', ckey ? ckey : 0);
			q.execute();
}

SG.prototype.get_spec_by_key_onsuccess = function(s, result){
		if(result != '0'){
				data = result.split('|');
				ckey = parseInt(data[0]);
				html = data[1];
				if(obj = document.getElementById('c_'+ ckey)) {
						if(sg._ckey && (obj2 = document.getElementById('c_' + sg._ckey))) {
								obj2.style.display = "none";
								sg._ckey = null;
						}
						obj.innerHTML = html;	
						obj.style.display = "block";
						sg._ckey = ckey;
				}
		}
		else return false;
}

SG.prototype.get_spec_by_key_onfailure = function(s, result){
		//alert('Nie ma takiej kategorii');
}
sg = new SG();

// ------------------------------------------------
// filebrowser

function FILEBROWSER() {
	this.fb = null; // file browser
	this.fb_id = 'filebrowser'; // file browser ID
	this.ac = null; // attachment container
	this.ac_id = 'attachment_container'; // attachment container ID
	this._attachment = null; // attachment items
	this._key = null; // key
	this._atts = null; //attachments
}
FILEBROWSER.prototype.Open = function() {}
FILEBROWSER.prototype.Close = function() {}
FILEBROWSER.prototype.Dir = {	
		get_by_key : function(key) {
			 //alert(key);
			 var _url = '/filebrowser/dir/' + key + '/';
			 var q = new QUERY(_url, 'filebrowser.Dir._onsuccess', 'filebrowser.Dir._onfailure');
		  q.execute();
		},
		
		_onsuccess : function(s, result){
				if(!this.fb) {
						this.fb = document.getElementById('filebrowser');
		  }
		  this.fb.innerHTML = result;
		},
		
		_onfailure : function(s, result) {
				alert('Error ' + s, result);
		}
		
		
}
FILEBROWSER.prototype.Attachment = {
		
		add : function(key, filename) {
		// dodaje plik do zalacznika
				filebrowser._key = key; //set tmp var
				
				if((filebrowser._attachment == null) && (filebrowser._atts = document.getElementById('attachments'))) {
						if(filebrowser._atts.value)
								filebrowser._attachment = filebrowser._atts.value.split(',');
						else
								filebrowser._attachment = new Array();
								//alert(filebrowser._attachment);
				}
				if(filebrowser._attachment.every(filebrowser.Attachment.check_every)){
					 filebrowser._attachment.push(key);
					 if(filebrowser._atts) filebrowser._atts.value = filebrowser._attachment.toString();
						//Wizualne odwzorowanie zalacznikow
						if(!filebrowser.ac) filebrowser.ac = document.getElementById('attachment_container');
						filebrowser.ac.innerHTML += '<div id="attach_'+key+'">' + filename +' <span>|</span> <a href="javascript:void(0);" onclick="filebrowser.Attachment.del('+ key +')">usuń</a></div>';
						//alert(filebrowser._attachment);
				}
				else{
						//alert(filebrowser._atts.value);
					 	//alert(filebrowser._attachment);
						//alert('Plik jest juz w załączniku');
						pulse.border('attach_' + key, 1, 'FF0000', 'FFFFFF');
				}
				filebrowser._key = null; //unset tmp var
		},
	
		del : function(key) {
		// wyrzuca plik z zalacznika
				filebrowser._key = key;
				if((filebrowser._attachment == null) && (filebrowser._atts = document.getElementById('attachments'))) {
						if(filebrowser._atts.value)
								filebrowser._attachment = filebrowser._atts.value.split(',');
						else
								filebrowser._attachment = new Array();
								//alert(filebrowser._attachment);
				}
				filebrowser._attachment = filebrowser._attachment.filter(filebrowser.Attachment.del_filter);
				filebrowser._key = null;
				if(filebrowser._atts) filebrowser._atts.value = filebrowser._attachment.toString();
				if(obj = document.getElementById('attach_'+key)){
				  obj.parentNode.removeChild(obj);
				}
		},
	
	 // filtry	
		check_every : function(element, index, array) {
				return (element != filebrowser._key)
		},
		
		del_filter : function(element, index, array) {
				return (element != filebrowser._key)
		}
}

filebrowser = new FILEBROWSER();

// ------------------------------------------------
// Messages

function MESSAGES() {
		// vars
		this.check_name = 'checkbox';
		this.check_objs = null;
		this.pulse_cid = null;
		// zmienne validacji formumarza
		this._users_ok = 0;
		this._subject_ok = 0;
		this._content_ok = 0;
		this._newmsgs = 0;
		this._accepts = 0;
}
MESSAGES.prototype.checked = function(value){
		value = value ? true : false;
		if(!this.check_objs) this.check_objs = document.getElementsByClassName(messages.check_name);
		for(i in messages.check_objs) messages.check_objs[i].checked = value;
}
MESSAGES.prototype.submit = function(id) {
		if(!(this._users_ok && this._subject_ok && this._content_ok)){
			var s = false;
			if(!this._users_ok) {
					pulse.border('messages_to', 1);
					if(!s && (obj = document.getElementById('messages_to'))) {obj.focus(); s=true }
			}
			if(!this._subject_ok) {
					pulse.border('messages_subject', 1);
					if(!s && (obj = document.getElementById('messages_subject'))) {obj.focus(); s=true }
			}
			if(!this._content_ok) {
					pulse.border('messages_content', 1);
					if(!s && (obj = document.getElementById('messages_content'))) {obj.focus(); s=true }
			}
			return false;
		} 
		else{ 
			return helpo.fsubmit(id);
		}
}
MESSAGES.prototype.check_subject = function(obj) {
	if(obj.value.length>1) messages._subject_ok = 1;
	else {
		pulse.border('messages_subject', 1);
		messages._subject_ok = 0;
	}
}
MESSAGES.prototype.check_content = function(obj) {
	if(obj.value.length>1) messages._content_ok = 1;
	else {
		pulse.border('messages_content', 1);
		messages._content_ok = 0;
	}
}

MESSAGES.prototype.check_users = function(obj) {
		if(obj.value) {
			var _url = '/wiadomosci/check_users/';
			var q = new QUERY(_url, 'messages.check_users_onsuccess', 'messages.check_users_onfailure');
			q.setParam('users', obj.value ? obj.value : '');
			q.execute();
			this.pulse_cid = pulse.border('messages_to', 10);
		}
		else {
			if(obj = document.getElementById('messages_to')) obj.style.border = "1px solid #ff0000";		
		}
}
MESSAGES.prototype.check_users_onsuccess = function(s, result) {
	pulse.stop(messages.pulse_cid);
	if(obj = document.getElementById('messages_to')){
				
		if(result == '0') {
			// puste pole
			messages._users_ok = 0;
			obj.value = "";
			obj.style.border = "1px solid #ff0000";
		}
		else if(result == '1') {
			//wszystko ok
			messages._users_ok = 1;
		}
		else {
			// niektore loginy sa bledne
			pulse.border('messages_to', 1);			
			obj.value = result;
			messages._users_ok = 1;
		}
		
	} //if
}
MESSAGES.prototype.check_users_onfailure = function(s, result) {
	alert(result);
}

MESSAGES.prototype.update = function() {
	var _url = '/wiadomosci/check_messages/';
	var q = new QUERY(_url, 'messages.update_onsuccess', 'messages.update_onfailure');
	q.execute();
	setTimeout('messages.update()', 60000);
}
MESSAGES.prototype.update_onsuccess = function(s, result) {
	var out = result.split(',')
	
	//alert(result);
	
	if( (c_newmsgs = document.getElementById('newmsgs_container')) &&
		true
		){
		if(parseInt(out[0])>0) {
			c_newmsgs.innerHTML = ' ('+out[0]+')';
			pulse.color('newmsgs_container', 2, '00FF00', 'FFFFFF');
		}
		else {
			c_newmsgs.innerHTML = ''
		}
	}
	
	if( (c_accepts = document.getElementById('accepts_container')) &&
		true
		){
		if(parseInt(out[1])>0) {
			c_accepts.innerHTML = ' ('+out[1]+')';
			pulse.color('accepts_container', 2, '00FF00', 'FFFFFF');
		}
		else {
			c_accepts.innerHTML = ''
		}
	}
	
}
MESSAGES.prototype.update_onfailure = function(s, result) {
	//alert(result)
}
messages = new MESSAGES();

// ------------------------------------------------
// Projects

function PROJECTS() {
		this.check_name = 'checkbox';
		this.check_objs = null;
}
PROJECTS.prototype.checked = function(value) {
		value = value ? true : false;
		if(!this.check_objs) this.check_objs = document.getElementsByClassName(projects.check_name);
		for(i in projects.check_objs) projects.check_objs[i].checked = value;
}

projects = new PROJECTS();

// ------------------------------------------------
// Progres

function PROGRESS() {
	this._obj_form = null;
	this._uuid = null;
	this._delay = 1000;
	this._interval = null;
}
PROGRESS.prototype.start = function(form_id) {
	if( window.attachEvent ) return helpo.fsubmit(form_id);
		
	progress._uuid = "";
 	for (i = 0; i < 32; i++) {
  		progress._uuid += Math.floor(Math.random() * 16).toString(16);
 	}
 	//alert('plum');
 	if(progress._obj_form=document.getElementById("add_file_form")) {
 		
		if( window.attachEvent ){
			//return helpo.fsubmit(form_id);
			progress._obj_form.action.value = "./?X-Progress-ID=" + progress._uuid;
			progress._interval = window.setInterval( function () { progress.fetch(); }, progress._delay );
		}
		else{
			progress._obj_form.action="./?X-Progress-ID=" + progress._uuid
			progress._interval = setInterval( function () { progress.fetch(); }, progress._delay );
		}
			
 	}
 	//alert('plum2')
 	return helpo.fsubmit(form_id);
}
PROGRESS.prototype.fetch = function() {
	try{
		var xhr = ax._xhr();
		xhr.open("GET", "/progress", 1);
		//alert(progress._uuid)
		xhr.setRequestHeader("X-Progress-ID", progress._uuid);
 		xhr.onreadystatechange = function() {
	
  			if (xhr.readyState == 4) {
				
   				if (xhr.status == 200) {
    				/* poor-man JSON parser */
    				var upload = eval(xhr.responseText);
					//alert(xhr.responseText);
    				document.getElementById('tp').innerHTML = upload.state;

				    /* change the width if the inner progress-bar */
				    if (upload.state == 'done' || upload.state == 'uploading') {
	     				bar = document.getElementById('progressbar');
	     				w = 400 * upload.received / upload.size;
	     				bar.style.width = w + 'px';
    				}
				    /* we are done, stop the interval */
				    if (upload.state == 'done') {
				     	clearTimeout(progress._interval);
				    }
   				}
  			}
 		}
 	xhr.send(null);
	}
	catch(e) {
		alert(e.description);
	}
}
var progress = new PROGRESS();

/**
 *  SEARCHER - wyszukiwarka helpo
 */
function SEARCHER() {
	this._prefix = 'search_';
	this._tab_cnt = 4;
	this._classTabActive = 'tab_on';
	this._classTabInActive = 'tab_off';
	this._tmp_tab_id = 1;
	this._tabField = 'tab';
}
SEARCHER.prototype.setTab = function(id) {
	tmp_id = this._tmp_tab_id;
	if(tmp_id != id) {
		if(	(tmp_id) && 
			(OldTab = document.getElementById(searcher._prefix + tmp_id)) &&
			true
		){
			OldTab.className = this._classTabInActive;
			this._tmp_tab_id = 0;
		}
		
		if(	(NewTab = document.getElementById(searcher._prefix + id)) &&
			(tab = document.getElementById(searcher._tabField))  &&
			true		
		 ){
			NewTab.className = this._classTabActive;
			this._tmp_tab_id = id;
			tab.value = id;
		}
	}	
}
searcher = new SEARCHER();
