
CatalogDetailRequest = Class.create();
CatalogDetailRequest.prototype =
{
    /**
     *
     * Parameters:
     * site           The site to search on [salen/are ..]
     * lang           The isolanguage code
     * rsIdx          ResultSet index in session
     * rs             Which rs to use
     *
     */
    initialize: function(site,lang,myIdx,rs,options)
    {
       this.site = site;
       this.lang = lang;
       this.rsIdx = myIdx;
       this.rs = rs;
       this.options = options;

       var p = $H({ site: site, lang: lang, idx: myIdx, rs: rs});
       var r = new Ajax.Request('/app/projects/common/templates/katalogbokning/catalogdetailapi.php',{method: 'get',parameters: p, onSuccess: this.onSuccess.bind(this)});
    },
    onSuccess: function(transport)
    {
        if(this.options.setData)
            this.options.setData(this,transport.responseText);
    }
};
CatalogDetailController = Class.create();
CatalogDetailController.prototype =
{
    initialize: function(tableElement,useRs,site,lang)
    {
        this.resultView = tableElement;
        // Cache
        this.detailedItems = $H();
        this.expandedItems = $H();
        this.site = site;
        this.lang = lang;
        this.useRs = useRs;
    },
    init: function()
    {
        // Lets bind ..
        var e = this.resultView.getElementsBySelector('tr.rsRow');

        for(var i = 0; i < e.length;i++)
        {
            var tr = e[i];
            tr.onclick = this.itemClick.bind(this,tr);
        }
    },
    itemClick: function(element)
    {
        var idx = element.id.substring(3);

        var isExpanded = this.expandedItems[idx];
        if(isExpanded)
        {
            this.expandedItems[idx] = false;

            // OK, simply hide
            this._hideDetails(idx);
        }
        else
        {
            this.expandedItems[idx] = true;
            this._showDetails(idx);
        }

    },
    _hideDetails: function(idx)
    {
        // Hide it
        var e = $('detail'+idx);
        if(e)
        {
            e.hide();
            var b = $('selectButton'+idx);
            if(b)
                b.className = 'itemExpand';
        }
    },
    _showDetails: function(idx)
    {
        // Show it
        var e = $('detail'+idx);
        if(e)
        {
            e.show();

            var b = $('selectButton'+idx);
            if(b)
                b.className = 'itemFold';

            html = this.detailedItems[idx];
            d = $('detailData'+idx);

            if(html)
            {
                d.update(html);
            }
            else
            {
                // We request data
                d.update('Loading ...');
                this._requestInfo(idx);
            }
        }
    },
    _requestInfo: function(idx)
    {
        new CatalogDetailRequest(this.site,this.lang,idx,this.useRs,{setData: this._setRawDetailInfo.bind(this)});
    },
    _setRawDetailInfo: function(r,html)
    {
        var data = $('detailData' + r.rsIdx);
        if(data)
        {
            data.update(html);
            this.detailedItems[r.rsIdx] = html;
        }
    }
};


