
/**
 * File contains JS Library for Category Select Panel Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     Maxim Shaev <shaev@zfort.net>
 * @copyright  (c) 2004-2006 by ZFort Group
 * @version    SVN: $Id: 206$
 * @link       http://www.zfort.net
 * @since      File available since Release 2.3.0
 */
if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();

    String.prototype.trim = function() {
        a = this.replace(/^\s+/, '');
        return a.replace(/\s+$/, '');
    };

    /**
     * PHP2Controls.CategorySelectPanel is the namespace and JS Class for Category Select control.
     *
     * @author   Maxim Shaev <shaev@zfort.net>
     * @version  $Id: categoryselectpanel.common.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.tabsManager = function(wsUrl, countTabs, linkTitle, containerId, loadingScript,
                                        classActive, classInactive, data, activeTab, label, callbackFunc, suffix, onclickFunc, mouseoverClass, mouseoutClass)
    {
        this.wsUrl          = (wsUrl != null) ? wsUrl : '/wsdl/artist.tabs.ajax.php';
        this.sRootUrl       = '/';

        this.loadingScript  = loadingScript;
        this.linkTitle      = linkTitle;
        this.countTabs    = (countTabs != null) ? countTabs : 0;
        this.containerId    = containerId;
        this.classActive    = classActive;
        this.classInactive  = classInactive;
        this.data           = data;
        //this.activeTab      = activeTab ? activeTab : 1;
        this.activeTab      = parseInt(activeTab);
        this.labelPage      = label ? label : 0;
        this.callbackFunc   = callbackFunc ? callbackFunc : false;
        this.currPage       = 1;
        this.suffix         = suffix ? suffix : '';
        this.onclickFunc    = typeof onclickFunc == 'function' ? onclickFunc : function(){};
        this.mouseoverClass = mouseoverClass ? mouseoverClass : false;
        this.mouseoutClass  = mouseoutClass ? mouseoutClass : false;
        var currentObject      = this;

        for (i = 1; i <= this.countTabs; i++)
        {
            if (i == this.activeTab)
            {
                document.getElementById(this.linkTitle + i).onclick      = function(){};
                document.getElementById(this.linkTitle + i).className    = this.classActive;
                if (this.mouseoverClass) document.getElementById(this.linkTitle + i).onmouseover  = function(){};
                if (this.mouseoutClass)  document.getElementById(this.linkTitle + i).onmouseout   = function(){};
                currentObject.doShowContent(this.linkTitle + i);
            }
            else
            {
                document.getElementById(this.linkTitle + i).onclick      = function(){currentObject.onclickFunc(); currentObject.doShowContent(this.id);}
                document.getElementById(this.linkTitle + i).className    = this.classInactive;
                if (this.mouseoverClass) document.getElementById(this.linkTitle + i).onmouseover  = function(){this.className=currentObject.mouseoverClass;};
                if (this.mouseoutClass)  document.getElementById(this.linkTitle + i).onmouseout   = function(){this.className=currentObject.mouseoutClass;};
            }
        }
    }

    PHP2Controls.tabsManager.prototype.doShowContent = function(linkId)
    {
        var currentObject = this;
        if (isBusy) {/*var timeOut = setTimeout('tabsManager.doShowContent(\''+linkId+'\');', 300);*/ return false;};
        isBusy = true;
        //clearTimeout(timeOut);

        var str = new String(linkId);
        var showTabId = str.substring(this.linkTitle.length, str.length);

        var divMessage      = document.getElementById(this.containerId);
        // --- Recreate Div --- //
       /* var parentContainer = divMessage.parentNode;
        parentContainer.removeChild(divMessage);

        var divMessage = document.createElement('div');
        divMessage.setAttribute('id', this.containerId);

        parentContainer.appendChild(divMessage);
        // --- Recreate Div --- //

        if (showTabId == 4)
        {
           showGallery();
           var currentObject = this;
           for(i = 1; i <= this.countTabs; i++)
           {
                linkTitleElement = document.getElementById(this.linkTitle + i);
                if (i == 4)
                {
                   linkTitleElement.className = this.classActive;
                }
                else
                {
                   linkTitleElement.className = this.classInactive;
                   linkTitleElement.onclick = function(){currentObject.doShowContent(this.id);};
                }
           }
           isBusy = false;
           return ;
        }*/
        this.activeTab = showTabId;
        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowTab');
        this.serverResponse.add('id',   showTabId);
        this.serverResponse.add('data', this.data);
        this.serverResponse.setHandler(this.onShowTabChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject  = this;
        this.serverResponse.execute();

        //document.getElementById(linkId).onclick = function(){};

        this.showLoading(divMessage);
    }

    PHP2Controls.tabsManager.prototype.doShowSubContent = function(tabId, containerId, subdata, sublinkId, countTab, callbackFunc, cPage, itemId)
    {
        //alert(containerId);
        this.subTabId        = tabId;
        this.subContainerId  = containerId;
        this.sublinkId       = sublinkId;
        this.countSubTabs    = countTab;
        this.subData         = subdata;
        this.currPage        = cPage ? cPage : 1;
        this.callbackSubFunc = callbackFunc;
        this.itemId          = itemId;

        var currentObject = this;
        if (isBusy)
        {
            //var timeOut = setTimeout('tabsManager.doShowSubContent('+tabId+', \''+containerId+'\', '+subdata+', \''+sublinkId+'\', '+countTab+', '+callbackFunc+', '+cPage+', '+itemId+');', 300);
            return false;
        };
        isBusy = true;
        //clearTimeout(timeOut);

        var divMessage      = document.getElementById(containerId);

        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowTab');
        this.serverResponse.add('id',       tabId);
        this.serverResponse.add('data',     this.data);
        this.serverResponse.add('subdata',  subdata);
        this.serverResponse.add('currpage', this.currPage);
        if (this.itemId)
        {
            this.serverResponse.add('itemId',   this.itemId);
        }
        if (pageSize)
        {
           this.serverResponse.add('pagesize', pageSize);
        }
        this.serverResponse.setHandler(this.onShowSubTabChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject   = this;
        this.serverResponse.execute();

        //document.getElementById(sublinkId).onclick = function(){};
        this.contentContainer = divMessage.innerHTML;
        this.showLoading(divMessage);
    }

    PHP2Controls.tabsManager.prototype.doShowNextPage = function(containerId)
    {
        this.currPage       += 1;

        var currentObject = this;
        if (isBusy)
        {
            //var timeOut = setTimeout('tabsManager.doShowNextPage();', 300);
            return false;
        };
        isBusy = true;
        //clearTimeout(timeOut);

        var divMessage      = document.getElementById(containerId);

        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowTab');
        this.serverResponse.add('id',       this.subTabId);
        this.serverResponse.add('data',     this.data);
        this.serverResponse.add('subdata',  this.subData);
        this.serverResponse.add('currpage', this.currPage);
        this.serverResponse.add('pagesize', pageSize);
        this.serverResponse.setHandler(this.onShowSubTabChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject   = this;
        this.serverResponse.execute();

//        sublink.onclick = function(){};

        this.showLoading(divMessage);
    }

    PHP2Controls.tabsManager.prototype.doShowPrevPage = function(containerId)
    {
        this.currPage       -= 1;

        var currentObject = this;
        if (isBusy)
        {
            //var timeOut = setTimeout('tabsManager.doShowPrevPage();', 300);
            return false;
        };
        isBusy = true;
        //clearTimeout(timeOut);

        var divMessage      = document.getElementById(containerId);

        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowTab');
        this.serverResponse.add('id',       this.subTabId);
        this.serverResponse.add('data',     this.data);
        this.serverResponse.add('subdata',  this.subData);
        this.serverResponse.add('currpage', this.currPage);
        this.serverResponse.add('pagesize', pageSize);
        this.serverResponse.setHandler(this.onShowSubTabChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject   = this;
        this.serverResponse.execute();

//        sublink.onclick = function(){};

        this.showLoading(divMessage);
    }

    PHP2Controls.tabsManager.prototype.doJustRequest = function(containerId, id, data)
    {
        var currentObject = this;
        if (isBusy)
        {
            //var timeOut = setTimeout('tabsManager.doShowPrevPage();', 300);
            return false;
        };
        isBusy = true;
        //clearTimeout(timeOut);

        var divMessage      = document.getElementById(containerId);

        this.data = data;
        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowTab');
        this.serverResponse.add('id',   (id ? id : this.subTabId));
        if (data)
        {
            this.serverResponse.add('data', data);
        }
        if (!this.subData) this.subData = 1;
        if (!this.currPage) this.currPage = 1;
        this.serverResponse.add('subdata',  this.subData);
        this.serverResponse.add('currpage', this.currPage);
        this.serverResponse.add('pagesize', pageSize);
        this.serverResponse.setHandler(this.onShowSubTabChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject  = this;
        this.serverResponse.execute();

        this.showLoading(divMessage);
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.tabsManager.prototype.onShowTabChecked = function()
    {
        isBusy = false;

        var divMessage = document.getElementById(this.currentObject.containerId);
        if (divMessage)
        {
            divMessage.innerHTML = this.response.Response.Answer.Content;
        }

        var currentObject = this.currentObject;
        currentObject.activeTab = parseInt(this.response.Response.Answer.Id);
        currentObject.resultOk = parseInt(this.response.Response.Answer.Result);

        for(i = 1; i <= currentObject.countTabs; i++)
        {
             linkTitleElement = document.getElementById(this.currentObject.linkTitle + i);
             if (i == this.response.Response.Answer.Id)
             {
                linkTitleElement.className   = this.currentObject.classActive;
                if (this.currentObject.mouseoverClass) linkTitleElement.onmouseover = function(){};
                if (this.currentObject.mouseoutClass)  linkTitleElement.onmouseout  = function(){};
             }
             else
             {
                linkTitleElement.className   = this.currentObject.classInactive;
                if (this.currentObject.mouseoverClass) linkTitleElement.onmouseover = function(){this.className = currentObject.mouseoverClass;};
                if (this.currentObject.mouseoutClass) linkTitleElement.onmouseout  = function(){this.className = currentObject.mouseoutClass;};
                linkTitleElement.onclick = function(){currentObject.onclickFunc(); currentObject.doShowContent(this.id);};
             }
        }

        if (document.getElementById('countCommunity'))
        {
            var countCommunity = parseInt(document.getElementById('countCommunity').value)
            for (i = 1; i <= countCommunity; i++)
            {
               writeFlash(document.getElementById('communityId_'+i).value,
                             document.getElementById('communitySrc_'+i).value,
                             document.getElementById('communityUrl_'+i).value);
            }
        }

        if (currentObject.callbackFunc)
        {
            currentObject.callbackFunc();
        }

        return true;
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.tabsManager.prototype.onShowSubTabChecked = function()
    {
        isBusy = false;

        var divMessage = document.getElementById(this.currentObject.subContainerId);
        if (divMessage)
        {
            if(this.response.Response.Answer.Content.length > 0)
            {
                divMessage.innerHTML = this.response.Response.Answer.Content;
            }
            else divMessage.innerHTML = '';//this.currentObject.contentContainer;
        }
        var currentObject = this.currentObject;
        currentObject.activeSubTab = this.response.Response.Answer.SubId;
        currentObject.currPage     = parseInt(this.response.Response.Answer.CurrentPage);
        for(i = 1; i <= currentObject.countSubTabs; i++)
        {
             //linkTitleElement = document.getElementById('subTab' + i);
             var subTabName = this.currentObject.sublinkId;
             subTabName = subTabName.substr(0,subTabName.length-1);
             linkTitleElement = document.getElementById(subTabName+i);

             if (linkTitleElement)
             {
                if (i == this.response.Response.Answer.SubId)
                {
                   linkTitleElement.className = 'active';
                   linkTitleElement.onmouseover = function (){/*this.className = 'simple_link_ov active';*/};
                   linkTitleElement.onmouseout = function (){/*this.className = 'active';*/};
                }
                else
                {
                   linkTitleElement.className = '';
                   linkTitleElement.onmouseover = function (){this.className = 'simple_link_ov';};
                   linkTitleElement.onmouseout = function (){this.className = '';};
                }
            }
        }
        // --- Init pagenavigation --- //
        if (document.getElementById('spPageNavContent'+currentObject.suffix))
        {
            document.getElementById('spPageNavContent'+currentObject.suffix).innerHTML = this.response.Response.Answer.PageNavContent;
            document.getElementById('spPageNavContent'+currentObject.suffix).style.display = '';
        }
        if (document.getElementById('aPageNavPrev'+currentObject.suffix))
        {
            document.getElementById('aPageNavPrev'+currentObject.suffix).style.display = '';
            if (this.response.Response.Answer.PageNavFirstPage == '1')
            {
                document.getElementById('aPageNavPrev'+currentObject.suffix).onclick = function(){};
                document.getElementById('aPageNavPrev'+currentObject.suffix).className = '';
                document.getElementById('aPageNavPrev'+currentObject.suffix).onmouseover = function(){this.className=''};
                document.getElementById('aPageNavPrev'+currentObject.suffix).onmouseout  = function(){this.className=''};
            }
            else {
                document.getElementById('aPageNavPrev'+currentObject.suffix).onclick = function(){currentObject.doShowPrevPage(currentObject.subContainerId);};
                document.getElementById('aPageNavPrev'+currentObject.suffix).className = 'active';
                document.getElementById('aPageNavPrev'+currentObject.suffix).onmouseover = function(){this.className='simple_link_ov active'};
                document.getElementById('aPageNavPrev'+currentObject.suffix).onmouseout  = function(){this.className='active'};
            }
        }
        if (document.getElementById('aPageNavNext'+currentObject.suffix))
        {
            document.getElementById('aPageNavNext'+currentObject.suffix).style.display = '';
            if (this.response.Response.Answer.PageNavLastPage == '1')
            {
                document.getElementById('aPageNavNext'+currentObject.suffix).onclick = function(){};
                document.getElementById('aPageNavNext'+currentObject.suffix).className = '';
                document.getElementById('aPageNavNext'+currentObject.suffix).onmouseover = function(){this.className=''};
                document.getElementById('aPageNavNext'+currentObject.suffix).onmouseout = function(){this.className=''};
            }
            else
            {
                document.getElementById('aPageNavNext'+currentObject.suffix).onclick = function(){currentObject.doShowNextPage(currentObject.subContainerId);};
                document.getElementById('aPageNavNext'+currentObject.suffix).className = 'active';
                document.getElementById('aPageNavNext'+currentObject.suffix).onmouseover = function(){this.className='simple_link_ov active'};
                document.getElementById('aPageNavNext'+currentObject.suffix).onmouseout  = function(){this.className='active'};
            }
        }
        if ((this.response.Response.Answer.PageNavLastPage == '1') && (this.response.Response.Answer.PageNavFirstPage == '1'))
        {
            if (document.getElementById('spPageNavContent'+currentObject.suffix)) document.getElementById('spPageNavContent'+currentObject.suffix).style.display = 'none';
            if (document.getElementById('aPageNavPrev'+currentObject.suffix)) document.getElementById('aPageNavPrev'+currentObject.suffix).style.display = 'none';
            if (document.getElementById('aPageNavNext'+currentObject.suffix)) document.getElementById('aPageNavNext'+currentObject.suffix).style.display = 'none';
        }
        // -------------------------------- //
        if (currentObject.callbackSubFunc)
        {
            currentObject.callbackSubFunc();
        }

        return true;
    }

    /**
     * Set Skin Area Root Url
     *
     * @param string sRootUrl
     */
    PHP2Controls.tabsManager.prototype.setSRootUrl = function(sRootUrl)
    {
        this.sRootUrl = sRootUrl;
    }

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.tabsManager.prototype.onResponseError = function()
    {
        // alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.alert = new PHP2Controls.Alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.hideLoading();
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.tabsManager.prototype.showLoading = function(divMessage)
    {
        // --- Setting Loading Data Element --- //
        if (divMessage)
        {
            this.tabsManagerLoadingPanel = divMessage;
            this.tabsManagerLoadingPanel.innerHTML   = this.loadingScript;
            this.tabsManagerLoadingPanel.style.display = '';
        }
    }

    /**
     * Hide Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.tabsManager.prototype.hideLoading = function()
    {
        // --- Setting Loading Data Command --- //
        if (this.tabsManagerLoadingPanel != null) this.tabsManagerLoadingPanel.style.display  = 'none';
        isBusy = false;
    }

    /**
     * Alert Error Message to browser
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.tabsManager.prototype.alert = function(errorMessage, errorCode)
    {
        this.currentAlert = new PHP2Controls.Alert(errorMessage);
    }
