/**
 * Used to load, expang and colapse detail info about an
 * object
 */
AccomodationDetailController = Class.create();
AccomodationDetailController.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('div.expandCmd');

        for(var i = 0; i < e.length;i++)
        {
            var div = e[i];
            div.onclick = this.itemClick.bind(this,div);
        }
    },
    itemClick: function(element)
    {
        var idx = element.id.substring(12);
        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 expandCmd';
        }
    },
    _showDetails: function(idx)
    {
        // Show it
        var e = $('detail'+idx);
        
        if(e)
        {
            e.show();

            var b = $('selectButton'+idx);
            if(b)
                b.className = 'itemFold expandCmd';

            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 AccomodationDetailRequest(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;
        }
    }
};

