jQuery(document).ready(function () { 
    var hdc = new hotDealsControl();
    
    jQuery('#hot-deals-container .aRent').click(function() {
        hdc.switchTab('rent');
        return false;
    });
    jQuery('#hot-deals-container .aBuy').click(function() {
        hdc.switchTab('buy');
        return false;
    });
    jQuery('#hot-deals-container .aLrow').click(function() {
        hdc.previosItem();
        return false;
    });
    jQuery('#hot-deals-container .aRrow').click(function() {
        hdc.nextItem();
        return false;
    });
});

function hotDealsControl()
{
    function collection(tab, countItems) {
        var _tab         = tab;
        var _areaItems   = 3;
        var _step        = 0;
        var _countItems  = countItems;
        var _pixelByStep = 160;
        
        this.showRows = function() {
            if (_step + _areaItems < _countItems) {
                jQuery('#hot-deals-container .aRrow').show();
            }
            
            if (_step > 0) {
                jQuery('#hot-deals-container .aLrow').show();
            }
        }
        
        this.initRows = function() {
            jQuery('#hot-deals-container .aLrow, #hot-deals-container .aRrow').hide();
            this.showRows();
        }
        
        this.changeItem = function() {
            var parent = this;
            jQuery('#hot-deals-container .aLrow, #hot-deals-container .aRrow').hide();
            jQuery('#hot-deals-items-container .items-' + _tab).animate({left: -(_step * _pixelByStep)}, 150, function() {
                parent.showRows();
            });
        }
        
        this.nextItem = function() {
            if (_step + _areaItems < _countItems) {
                _step++;
                this.changeItem();
            }
        }
        
        this.previosItem = function() {
            if (_step > 0) {
                _step--;
                this.changeItem();
            }
        }
        
        this.initRows();
    }
    
    var _countRent = 1 * jQuery('#rent-params').html();
    var _countBuy = 1 * jQuery('#buy-params').html();
    
    var _tabs = {
        'rent': new collection('rent', _countRent),
        'buy':  new collection('buy', _countBuy)
    }
    
    this.switchTab = function(tab) {
        if (tab == _currentTab) {
            return false;
        }
        
        switch(_currentTab) {
            case 'rent':
                jQuery('#hot-deals-items-container .items-rent').hide();
                jQuery('#hot-deals-container .aBuy').hide();
                jQuery('#hot-deals-container').addClass('bgLeft');
                jQuery('#hot-deals-items-container .items-buy').show();
                jQuery('#hot-deals-container .aRent').show();
                break;
            case 'buy':
                jQuery('#hot-deals-items-container .items-buy').hide();
                jQuery('#hot-deals-container .aRent').hide();
                jQuery('#hot-deals-container').removeClass('bgLeft');
                jQuery('#hot-deals-items-container .items-rent').show();
                jQuery('#hot-deals-container .aBuy').show();
                break;
            default:
                return false;
                break;
        }
        
        _currentTab = tab;
        _tabs[_currentTab].initRows();        
    }
    
    this.nextItem = function() {
        _tabs[_currentTab].nextItem();
    }
    
    this.previosItem = function() {
        _tabs[_currentTab].previosItem();
    }
    
    var _currentTab = 'buy';
    this.switchTab('rent');
}
