var iDelay = 300;
var ids    = new Array(1);

function startTicker() {
    timestamp = new Date().getTime();
    $.getJSON("/ticker/?initial&timestamp="+timestamp,
        function(data) {
            if (data.length<1) { return false; }
            var ms_per_record = data[0].ms_per_record;
            data = data.splice(1,3);
            var n = 2;
            $('div#ticker_loading').remove();
            $.whileAsync({
                delay: iDelay,
                bulk: 0,
                test: function() { return n >= 0 },
                end:  function() { setTimeout('getNew('+ms_per_record+')',ms_per_record) },
                loop: function() {
                    var item = data[n];
                    if (item.fields.pricecut == '0') {
                        item_pricecut = 'New Product';
                        item_price    = '&pound;'+item.fields.low_price.toFixed(2);
                    } else {
                        item_pricecut = item.fields.pricecut+'% Off';
                        item_price    = 'Was &pound;'+item.fields.high_price.toFixed(2)+' Now &pound;'+item.fields.low_price.toFixed(2);
                    }
                    $('div#ticker_frame').append(
                    '<div id="'+item.pk+'" style="display: none; height: 70px" class="ticker_loop"> \
                        <a href="/products/'+item.fields.url_name+'/" class="clear"> \
                            <img width="70" height="70" src="'+item.fields.thumb_url+'" alt="'+item.fields.name+'" class="ticker_product" /> \
                            <ul class="split1"> \
                                <li id="'+item.pk+'_title" class="ticker_meta1">'+item.fields.name+'</li> \
                                <li class="ticker_meta2">'+item_pricecut+'</li> \
                                <li class="ticker_meta3">'+item_price+'</li> \
                            </ul> \
                            <span class="split2">Get It Now</span> \
                        </a> \
                    </div>');
                    $('#'+item.pk+'_title').truncate(45);
                    $('div#'+item.pk).animate( { height: "show", opacity: "show", queue: true }, iDelay );
                    ids.push(item.pk);
                    n--;
                }
            });
        }
    );
}

function getNew(ms_per_record) {
    $.whileAsync({
        delay: ms_per_record,
        bulk: 0,
        test: function() { return true },
        loop: function() {
            var item = "";
            timestamp = new Date().getTime();
            $.getJSON("/ticker/?timestamp="+timestamp,
                function(data) {
                    item = data[0];
                    if (item.fields.pricecut == '0') {
                        item_pricecut = 'New Product';
                        item_price    = '&pound;'+item.fields.low_price.toFixed(2);
                    } else {
                        item_pricecut = item.fields.pricecut+'% Off';
                        item_price    = 'Was &pound;'+item.fields.high_price.toFixed(2)+' Now &pound;'+item.fields.low_price.toFixed(2);
                    }
                    $('div#ticker_frame').prepend(
                    '<div id="'+item.pk+'" style="display: none; height: 70px" class="ticker_loop"> \
                        <a href="/products/'+item.fields.url_name+'/" class="clear"> \
                            <img width="70" height="70" src="'+item.fields.thumb_url+'" alt="'+item.fields.name+'" class="ticker_product" /> \
                            <ul class="split1"> \
                                <li id="'+item.pk+'_title" class="ticker_meta1">'+item.fields.name+'</li> \
                                <li class="ticker_meta2">'+item_pricecut+'</li> \
                                <li class="ticker_meta3">'+item_price+'</li> \
                            </ul> \
                            <span class="split2">Get It Now</span> \
                        </a> \
                    </div>');
                    $('#'+item.pk+'_title').truncate(45);
                    $('div#'+item.pk).animate( { height: "show", opacity: "show", queue: true }, iDelay, function() {
                        $('div#'+ids[0]).remove();
                    });
                    
                    ids.push(item.pk);
                    ids.splice(0,1);
                    
                }
                
            )
            
            data = '';
        }
    });
}