var oShortcuts = new function()
{
    this._sBoxId = 'user-shortcuts';
    this._isLoaded = false;
    this._iHideTimer = false;
    this._iShowTimer = false;
    this._oElements = false;
    this._bHideDisabled = false;
    this._iBlogId = false;
    this._iCurrentTab = false;
    this._getBoxElements = function() {
        if (oShortcuts._oElements)
            return oShortcuts._oElements;

        var oContainer = ById(this._sBoxId);

        return oShortcuts._oElements = {
            "oContainer" : oContainer,
            "oLink" : oContainer.getElementsByTagName('a')[0],
            "oContentContainer" : ById('shortcuts-box'),
            "oLoader" : ById('shortcuts-loader'),
            "oTabsContainer" : ById('shortcuts-tabs')
        }
    }
    this._setHideHandler = function() {
        var oElements = oShortcuts._getBoxElements();
        if (!oElements.oContainer.onmouseout)
        {
            oElements.oContainer.onmouseout = function(oEvent) {
                oShortcuts._iHideTimer = setTimeout('oShortcuts.hideBox()', 500);
            }
        }
    }
    this._unsetHideHandler = function() {
        var oElements = oShortcuts._getBoxElements();
        try { oElements.oContainer.onmouseout = false; } catch (e) {}
    }
    this.hideBox = function() {
        if (oShortcuts._bHideDisabled)
            return;

        var oElements = oShortcuts._getBoxElements();

        //oTransformations.transformElementAlpha(oElements.oContentContainer, 0.1, 0, 30, function(aThread) {
            //var oElements = oShortcuts._getBoxElements();
            //oElements.oLink.className = 'user-shortcuts_disabled';
            oElements.oContentContainer.className = 'user-shortcuts_disabled';
            oShortcuts._unsetHideHandler();
            oShortcuts.hideNewForm();
        //});
    }
    this.showBox = function(iDelay) {
        if (iDelay > 0) {
            clearTimeout(oShortcuts._iShowTimer);
            oShortcuts._iShowTimer = setTimeout('oShortcuts.showBox()', iDelay);
            return;
        } else if (iDelay < 0 && oShortcuts._iShowTimer) {
            return;
        }
        clearTimeout(oShortcuts._iHideTimer);
        oShortcuts._setHideHandler();
        var oElements = oShortcuts._getBoxElements();
        if (oElements.oContentContainer.className == 'user-shortcuts')
            return;
        //oUtils.setOpacity(oElements.oContentContainer, 0);
        oElements.oContentContainer.className = 'user-shortcuts';
        //oTransformations.transformElementAlpha(oElements.oContentContainer, 0.1, 1, 30);
        if (!oShortcuts._isLoaded) {
            oShortcuts._ajaxGet({
                url : '/misc/ajax_shortcut_tabs/',
                onComplete : function(xhrObj) { ById('shortcuts-tabs-container').innerHTML = xhrObj.responseText; }
            });
            var oElements = oShortcuts._getBoxElements();
            oElements.oContainer.onmouseover = function() { oShortcuts.showBox(-1) };
            oShortcuts._isLoaded = true;
        }
    }
    this._ajaxPost = function(oParams) {
        oParams.onInitialization = oShortcuts.setBusyOn;
        oParams.onFinalization = oShortcuts.setBusyOff;
        oParams.disableForm = false;
        advAJAX.post(oParams);
    }
    this._ajaxGet = function(oParams) {
        oParams.onInitialization = oShortcuts.setBusyOn;
        oParams.onFinalization = oShortcuts.setBusyOff;
        oParams.disableForm = false;
        advAJAX.get(oParams);
    }
    this._ajaxSubmit = function(oForm, oParams) {
        oParams.onInitialization = oShortcuts.setBusyOn;
        oParams.onFinalization = oShortcuts.setBusyOff;
        oParams.disableForm = false;
        advAJAX.submit(oForm, oParams);
    }
    this.addCurrentSite = function() {
        var newForm = ById('shortcuts-new-form');

        this.showTab(SHORTCUT_TYPE_LINK);

        oShortcuts._ajaxPost({
            url : '/misc/ajax_shortcut_add/' + encodeURIComponent(newForm.title.value),
            onComplete : function(xhrObj) {
                oShortcuts.setTabContent(SHORTCUT_TYPE_LINK, xhrObj.responseText);
            }
        });

        oShortcuts.hideNewForm();
    }
    this.hideNewForm = function() {
        ById('shortcuts-new-form').style.display = 'none';
        ById('shortcuts-new-form').title.value = 'Nowy skrót';
        oShortcuts._bHideDisabled = false;
    }
    this.showNewForm = function() {
        var newForm = ById('shortcuts-new-form');
        newForm.title.value = oShortcuts.getCurrentSiteTitle();
        newForm.style.display = 'block';
        newForm.title.focus();
        oShortcuts._bHideDisabled = true;

        newForm.title.onfocus = function() {
            oShortcuts._bHideDisabled = true;
        }
        newForm.title.onblur = function() {
            oShortcuts._bHideDisabled = false;
        }
    }
    this.setTabContent = function(iType, sContent) {
        ById('shortcuts-tab-' + iType).innerHTML = sContent;
    }
    this.loadTab = function(iType, iShortcutId) {
        var iType = iType;
        oShortcuts._ajaxPost({
            url : '/misc/ajax_shortcut_tab/' + parseInt(iType) + '/' + (parseInt(iShortcutId) ? parseInt(iShortcutId) : ''),
            onComplete : function(xhrObj) {
                oShortcuts.setTabContent(iType, xhrObj.responseText);
            }
        });
    }
    this.showOptions = function(iShortcutId) {
        /*var oEdit = ById('shortcut-options-edit-' + iShortcutId),
            oDelete = ById('shortcut-options-delete-' + iShortcutId);
        if (oEdit) oEdit.style.display = 'block';
        if (oDelete) oDelete.style.display = 'block';*/
    }
    this.hideOptions = function(iShortcutId) {
        /*var oEdit = ById('shortcut-options-edit-' + iShortcutId),
            oDelete = ById('shortcut-options-delete-' + iShortcutId);
        if (oEdit) oEdit.style.display = 'none';
        if (oDelete) oDelete.style.display = 'none';*/
    }
    this.save = function(iType) {
        var iType = iType;
        oShortcuts._ajaxSubmit(ById('shortcut-form'), {
            url : '/misc/ajax_shortcut_save/' + iType,
            onComplete : function(xhrObj) {
                oShortcuts.setTabContent(iType, xhrObj.responseText);
            }
        });
    }
    this.addBlog = function() {
        if (oShortcuts._iBlogId)
        {
            this.showTab(SHORTCUT_TYPE_BLOG);
            oShortcuts._ajaxPost({
                url : '/misc/ajax_shortcut_addblog/' + oShortcuts._iBlogId,
                onComplete : function(xhrObj) {
                    oShortcuts.setTabContent(SHORTCUT_TYPE_BLOG, xhrObj.responseText);
                }
            });
        }
    }
    this.setBusyOn = function() {
        var oElements = oShortcuts._getBoxElements(),
            w = oElements.oContentContainer.offsetWidth,
            h = oElements.oContentContainer.offsetHeight;

        oElements.oLoader.style.display = 'block';
        oElements.oLoader.style.width = (w-1) + 'px';
        oElements.oLoader.style.height = (h-1) + 'px';
    }
    this.setBusyOff = function(){
        var oElements = oShortcuts._getBoxElements();
        oElements.oLoader.style.display = 'none';
    }
    this.showTab = function(iType) {
        if (oShortcuts._iCurrentTab == iType)
            return;

        var oLastTab = ById('shortcuts-tab-' + oShortcuts._iCurrentTab),
            oCurrentTab = ById('shortcuts-tab-' + iType);

        ById('shortcuts-tablink-' + iType).className = 'shortcuts-tab-on';
        ById('shortcuts-tablink-' + oShortcuts._iCurrentTab).className = 'shortcuts-tab';

        oLastTab.style.display = 'none';
        oCurrentTab.style.display = 'block';
        oShortcuts._iCurrentTab = iType;
    }
    this.getCurrentSiteTitle = function() {
        try {
            var oNavi = ById('navi'),
                oLinks = oNavi.getElementsByTagName('a'),
                oLink, aElements = [],
                iMinIndex = oLinks.length - 3;

            for (var i = oLinks.length - 2; i >= 0; i--) {
                oLink = oLinks[i];
                if (!oLink || !oLink.nodeType || oLink.nodeType != 1) continue;
                aElements.push(oLink.innerHTML);
                if (i <= iMinIndex) break;
            }

            if (aElements.length) {
                aElements.reverse();
                return aElements.join(' - ');
            }
            return document.title;
        }
        catch (e) {
            return document.title;
        }
    }
    this.cancelShow = function() {
        clearTimeout(oShortcuts._iShowTimer);
        oShortcuts._iShowTimer = false;
    }
}