var Partners = {
    
    items : [],
    timer: null,
    timeout: 5000,
    fadeInterval: 400,

    init: function (){
        $.get('/templates/hostings.txt', function(data) {
            Partners.parse(data);
            Partners.update();
            Partners.timer = setInterval(Partners.update, Partners.timeout);
        });
    },
    
    update: function(){
        var i = Math.floor(Math.random()*Partners.items.length);
        var $item = $('<div class="carousel-item">').html( Partners.items[i] ).hide();
        var $carousel = $('#hosting-carousel');
        var $carousel_item = $('#hosting-carousel .carousel-item');
        $carousel_item.fadeOut(Partners.fadeInterval, function(){
            $carousel.empty();
            $carousel.html($item);
            $carousel.children().fadeIn(Partners.fadeInterval);
        });
    },
    
    parse: function(data){
        var hostings = data.split('\n');
        var index = 0;
        for (var i = 0; i < hostings.length; i++) {
            var s = $.trim(hostings[i]);
            if (s[0] == '#' || s == '' || s.indexOf('|')<0){
                continue;
            }

            Partners.items.push(Partners.getHtmlItem(s, index));
            index += 1;
        }
    },
    
    getHtmlItem: function(line, index){
        var parts = line.split('|');
        var hostname = parts[0];
        //var imgurl = '/images/hostings/' + hostname.replace('www.', '') + '.png';
        var products = parts[1].split(',');
        var result = '<a href="http://'+parts[0]+'" style="background-position: '+String(-index*250)+'px 0px" target="_blank"></a>';
        result += '<table><tr><td>';
        for(var i = 0; i<products.length; i++){
            result += '<img src="/images/v.png" width="8" height="8" />&nbsp;'+$.trim(products[i])
            
            if (i == 1){
                result += '</td><td>';
            } else {
                result += '<br />';
            }
        }
        result += '</td></tr></table>';
        return result;
    }
}

$(document).ready(Partners.init);

