
var setSessionHref;
var shadeTimer;
var request = new Array();

//{{{ function setSessionRedirect()
function setSessionRedirect() {

    setSessionHref = setSessionHref.replace('#', '');
    window.location.href = setSessionHref;

}
//}}}

//{{{ function setSessionVar( path, name, value, href )
function setSessionVar( path, name, value, href ) {

    var sid = self.location.search.match(/(PHPSESSID\=[a-z0-9]+)/g);

    var today = new Date();
    if ( !href ) href = document.location.href;
    setSessionHref = href;

    var srcPath = '/setvar.php5?hash='+today.getTime()+'&path='+path+'&varName='+name;
    if ( value != null ) srcPath += '&varValue='+value;
    if ( sid ) srcPath += '&'+sid;

    new Ajax.Request(srcPath, {
	    method: 'get', 
	    onComplete: setSessionRedirect
        });

}
//}}}

//{{{ function setSessionVarNR( path, name, value )
function setSessionVarNR( path, name, value ) {

    var sid = self.location.search.match(/(PHPSESSID\=[a-z0-9]+)/g);
    var today = new Date();

    var srcPath = '/setvar.php5?hash='+today.getTime()+'&path='+path+'&varName='+name;
    if ( value != null ) srcPath += '&varValue='+value;
    if ( sid ) srcPath += '&'+sid;

    new Ajax.Request(srcPath, {
	    method: 'get', 
	    onComplete: setSessionRedirect
        });

}
//}}}

//{{{ function parseURL( url )
function parseURL( url ) {

    var href;
    var param = new Object();
    var sep = '&';

    if ( url.match('&amp;') ) sep = '&amp;';

    var p = new Array();
    p = url.split('?');
    href = p[0];

    if ( p[1] != null ) {

        p = p[1].split(sep);
        for ( var i = 0; i < p.length; i++ ) {

            var pp = p[i].split('=');
            param[pp[0]] = pp[1];

        }

    }

    return {"href": href, "param": param, "separator": sep};

}
//}}}

//{{{ function addParamToURL( url, name, value )
function addParamToURL( url, name, value ) {

    var url = parseURL(url);
    url.param[name] = value;

    var param = new Array();
    for (var key in url.param) {
        param.push(key+'='+url.param[key]);
    }

    return url.href+'?'+param.join(url.separator);

}
//}}}

//{{{ function show_picture(id, file, width, height)
function show_picture(id, file, width, height) {

    day = new Date();
    rid = day.getTime();
    
    page = window.open('photo.php5?id='+id+'&img='+file, rid, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height);

}
//}}}

//{{{ function shadeChange(id, show, speed)
function shadeChange(id, show, speed) {

    if ( show && shadeTimer != null )
        clearTimeout(shadeTimer);

    var value = Element.getOpacity(id);

    if ( (!show && value > 0) || (show && value < 1) ) {

        if ( !show )
            value -= 0.25*speed;

        else value += 0.25*speed;
    
        shadeTimer = setTimeout('shadeChange(\''+ id +'\', '+ show +', '+ speed +');', 10);
        Element.setOpacity($(id), value);

    } else {

        shadeTimer = null;

    }


}
//}}}

//{{{ function shadeScreen(value, dest)
function shadeScreen(value, dest) {

    if ( value ) {

        if (document.getElementById($(dest).id+'_shade'))
            return;

        var containerNode = document.createElement('div');
        containerNode.id = $(dest).id+'_shade';
        containerNode.className = 'Shade';
        containerNode.style.marginTop = -1*($(dest).offsetHeight)+'px';
        containerNode.style.width = $(dest).clientWidth+'px';
        containerNode.style.height = $(dest).offsetHeight+'px'; 
        Element.setOpacity(containerNode, 0.3);
        containerNode.style.display = 'none';
        $(dest).appendChild(containerNode);
        containerNode.style.display = 'block';

    }

}
//}}}

//{{{ function getObject(dest, path, withLoading)
function getObject(dest, path, withLoading) {

    path = (path.match('get_object.php5') ? path : 'get_object.php5?path='+path);

    r = new AReq(request.length, path, dest, false, withLoading);
    if (r.isWorking) {

        if (!withLoading)
            shadeScreen(true, dest);

        request.push(r);

    }
    
}
//}}}

function TrimString(sInString) {

    sInString = sInString.replace(/^\s+/g, '');
    return sInString.replace(/\s+$/g, '');

}

//{{{ function getForm(dest, path)
function getForm(dest, path) {

    shadeScreen(true, dest);
    path = (path.match('get_object.php5') ? path : 'get_object.php5?path='+path);
    request.push(new AReq(request.length, path, dest, true));

    return false;

}
//}}}

//{{{ AReq = Class.create();
AReq = Class.create();
AReq.prototype = {

    id: null,
    resources: new Array(),
    resourceCount: 0,
    failed: 0,
    isWorking: false,

    dest: null,
    url: null,

    //{{{ initialize: function (id, url, dest, post, withLoading)
    initialize: function (id, url, dest, post, withLoading) {

        this.id = id;
        this.url = url;
        this.dest = dest;

        //prompt('init: '+dest+'; method: '+(post != null ? 'post' : 'get'), url);

        if (withLoading) {

            if (!document.getElementById('loading')) {

                var loading = document.createElement('div');
                loading.className = 'loading';
                loading.id = 'loading';
    
                var dest = document.getElementById(this.dest);
                dest.appendChild(loading);

            } else return;

        }

        if (!new Ajax.Request(

            this.url,
            {

                asynchronous: true,
                method: (post ? 'post' : 'get'),
                parameters: (post ? Form.serialize($(this.dest+'_form')) : '')+'&_phase=init',
                onSuccess: new Function('r', 'request['+this.id+'].loadResources(r.responseXML, r.responseText)'),
                onFailure: new Function('r', 'document.location.reload();')

            }

        )) { document.location.reload(); }

        this.isWorking = true;

    },
    //}}}

    //{{{ loadResources: function (data, txt)
    loadResources: function (data, txt) {

        if (data == null)
            document.location.reload();

        var response = data.documentElement;

        if (response.tagName == 'parsererror') {

            page = window.open('', 'error', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=480');
            page.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf8" \/><title>Error!<\/title><\/head><body>'+txt+'<\/body><\/html>');
            page.document.close();

            document.location.reload();

        }
    
        var x;

        x = response.getElementsByTagName("redirect");

        if (typeof(x[0]) != 'undefined' && x[0] != null) {
        
            x[0].firstChild.data = TrimString(x[0].firstChild.data);
            //prompt('Redirect', x[0].firstChild.data);
            document.location.href = x[0].firstChild.data;
            return;

        }

        x = response.getElementsByTagName("getform");
        if (typeof(x[0]) != 'undefined' && x[0] != null) {

            x[0].firstChild.data = TrimString(x[0].firstChild.data);
            //prompt('GetForm', x[0].firstChild.data); 
            getForm(x[0].getAttribute('id'), x[0].firstChild.data);
            return;

        }

        x = response.getElementsByTagName("file");

        var loadInProgress = 0;
        for (var i = 0; i < x.length; i++) {

            var filename = x[i].firstChild.data;
            if (loadedObjects.indexOf(filename) == -1)
                this.resources.push(new Array(filename, x[i].getAttribute('type')));

        }

        this.resources.reverse();
        this.resourceCount = this.resources.length;

        //alert('loadResources: '+this.resourceCount);

        if (this.resourceCount == 0)
            return this.show();

        var fileInfo = this.resources.pop();
        this.loadResource(fileInfo[0], fileInfo[1]);

    },
    //}}}

    //{{{ loadResource: function (filename, type)
    loadResource: function (filename, type) {
        
        //alert('load: '+filename);

        new Ajax.Request(

            filename,
            {

                method: 'get',
                onComplete: new Function('r', 'request['+this.id+'].evalResource(\''+ filename +'\', \''+ type +'\', r.responseText)')

            }

        ); 

    },
    //}}}

    //{{{ evalResource: function ( filename, type,  data )
    evalResource: function ( filename, type, data ) {

        //alert('eval: '+filename);

        try {

            if ( type == 'js' )
                eval(data); 

            if ( type == 'css' ) {

                var fileref = document.createElement('link');
                fileref.setAttribute('rel', 'stylesheet');
                fileref.setAttribute('type', 'text/css');
                fileref.setAttribute('href', filename);

                document.getElementsByTagName('head').item(0).appendChild(fileref);

            }

        } catch (e) {

            //alert(filename+': Failed'+"\nErr: "+e);

            if ( this.failed < 2 )
                this.loadResource(filename, type);

            else document.location.reload();

            this.failed++;

        }

        loadedObjects += filename+' ';
        //alert(filename+': OK - pozostało: '+(this.resourceCount-1));

        if ( --this.resourceCount == 0 )
            return this.show();

        var fileInfo = this.resources.pop();
        this.loadResource(fileInfo[0], fileInfo[1]);

    },
    //}}}

    enableLoading: function() {

        this.withLoading = true;

    },

    //{{{ show: function ()
    show: function () {

        //alert('show - '+this.id); 

        new Ajax.Updater(

            this.dest,
            this.url,
            {

                parameters: '&_phase=content',
                method: 'get',
                evalScripts: true,
                onFailure: new Function('r', 'document.location.reload();'),
                onSuccess: new Function('r', 'shadeScreen(false, \''+ $(this.dest).id +'\');')

            }

        );

    }
    //}}}

}
//}}}

//{{{ Notify = Class.create();
Notify = Class.create();
Notify.prototype = {

    message: null,
    type: null,
    title: null,

    //{{{ initialize: function(message, type, title)
    initialize: function(message, type, title) {

        this.message = message;
        this.type = type;
        this.title = title;

    },
    //}}}

    //{{{ getType: function()
    getType: function() {

        switch (this.type) {

            case 1: return 'error';
            case 2: return 'warning';

        }

        return 'info';

    },
    //}}}

    //{{{ getTitle: function()
    getTitle: function() {

        return this.title;

    },
    //}}}

    //{{{ clean: function(container)
    clean: function(container) {

        document.getElementById('notifyMessage').removeChild(document.getElementById('notifyMessage').firstChild);
        document.getElementById('notifyTitle').removeChild(document.getElementById('notifyTitle').firstChild);

    },
    //}}}

    //{{{ show: function(container)
    show: function(container) {

        var element = document.createElement('div');
        element.appendChild(document.createTextNode(this.title));
        document.getElementById('notifyTitle').appendChild(element);

        var element = document.createElement('div');
        element.appendChild(document.createTextNode(this.message));
        document.getElementById('notifyMessage').appendChild(element);

        document.getElementById('notifyIcon').className = 'notifyIcon_'+this.getType();

    }
    //}}}

}
//}}}

//{{{ Notifier = Class.create();
Notifier = Class.create();
Notifier.prototype = {

    id: null,
    messages: new Array(),
    showedMessage: null,

    container: null,
    hider: null,

    //{{{ initialize: function(id)
    initialize: function(id) {

        this.id = id;

    },
    //}}}

    //{{{ notify: function(message, type, title)
    notify: function(message, type, title) {

        setTimeout(this.id+'.showNextMessage()', (this.messages.length*2)*1500);
        this.messages.push(new Notify(message, type, title));

        if (this.hider != null) {

            clearTimeout(this.hider);
            this.hider = null;

        }

    },
    //}}}

    //{{{ showNextMessage: function()
    showNextMessage: function() {

        if (this.showedMessage != null) {

            this.showedMessage.clean(this.container);
            this.showedMessage = null;

        }

        msg = this.messages.pop();

        if (this.container == null)
            this.showContainer();

        msg.show(this.container);

        this.showedMessage = msg;

        if (this.messages.length == 0)
            this.hider = setTimeout(this.id+'.hideMessage()', 3500);

    },
    //}}}

    //{{{ hideMessage: function()
    hideMessage: function() {

        document.body.removeChild(this.container);
        this.showedMessage = null;
        this.container = null;

    },
    //}}}

    //{{{ showContainer: function()
    showContainer: function() {

        this.container = document.createElement('div');
        this.container.id = 'notifierContainer';

        var element = document.createElement('div');
        element.id = 'notifyTitle';
        element.className = 'top';
        this.container.appendChild(element);

        var element = document.createElement('div');
        element.id = 'notifyBody';
        element.className = 'middle';
        this.container.appendChild(element);

        var icon = document.createElement('div');
        icon.id = 'notifyIcon';
        element.appendChild(icon);

        var msg = document.createElement('div');
        msg.id = 'notifyMessage';
        element.appendChild(msg);

        var clear = document.createElement('div');
        clear.style.clear = 'both';
        element.appendChild(clear);

        document.body.appendChild(this.container);

    }
    //}}}

}
//}}}

var n = new Notifier('n');

