
/**
 * 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.HomeFeaturedManager = function(wsUrl, countTitles, linkTitle, containerId, loadingScript, classActive, classInactive)
    {
        var currentObject      = this;

        this.wsUrl          = (wsUrl != null) ? wsUrl : '/wsdl/main.ajax.php';
        this.sRootUrl       = '/';

        this.loadingScript  = loadingScript;
        this.linkTitle      = linkTitle;
        this.countTitles    = (countTitles != null) ? countTitles : 0;
        this.containerId    = containerId;
        this.classActive    = classActive;
        this.classInactive  = classInactive;

        for (i = 1; i < this.countTitles; i++)
        {
            if (i == 1)
            {
                document.getElementById(this.linkTitle + i).onclick   = function(){};
                document.getElementById(this.linkTitle + i).className = this.classActive;
                currentObject.doShowContent(this.linkTitle + i);
            }
            else
            {
                document.getElementById(this.linkTitle + i).onclick     = function(){currentObject.doShowContent(this.id);}
                document.getElementById(this.linkTitle + i).className   = this.classInactive;
            }
        }
    }

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

        var str = new String(linkId);
        var showFeaturedId = 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 (showFeaturedId == 4)
        {
           showGallery();
           var currentObject = this;
           for(i = 1; i <= this.countTitles; 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.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doShowFeaturedHome');
        this.serverResponse.add('id', showFeaturedId);
        this.serverResponse.setHandler(this.onShowFeaturedChecked);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject  = this;
        this.serverResponse.execute();

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

        this.showLoading(divMessage);
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.HomeFeaturedManager.prototype.onShowFeaturedChecked = function()
    {
        var divMessage = document.getElementById(this.currentObject.containerId);
        if (divMessage)
        {
            divMessage.innerHTML = this.response.Response.Answer.Description;
        }

        var currentObject = this.currentObject;

        for(i = 1; i <= this.currentObject.countTitles; i++)
        {
             linkTitleElement = document.getElementById(this.currentObject.linkTitle + i);
             if (i == this.response.Response.Answer.Id)
             {
                linkTitleElement.className = this.currentObject.classActive;
             }
             else
             {
                linkTitleElement.className = this.currentObject.classInactive;
                linkTitleElement.onclick = function(){currentObject.doShowContent(this.id);};
             }
        }
        isBusy = false;
        return true;
    }

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

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.HomeFeaturedManager.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.HomeFeaturedManager.prototype.showLoading = function(divMessage)
    {
        // --- Setting Loading Data Element --- //
        if (divMessage)
        {
            this.HomeFeaturedManagerLoadingPanel = divMessage;
            this.HomeFeaturedManagerLoadingPanel.innerHTML   = this.loadingScript;
            this.HomeFeaturedManagerLoadingPanel.style.display = '';
        }
    }

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

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