// try { console.log(); } catch(e){ console = {log:function(){}}; }
//
var SHOP_URL = 'http://friendandco.bigcartel.com';
var products;
var _p_interval;
var pos = 0;
var image_loaded = true;
var product_image;
var product_image_container;

function updateProduct(){
    if(image_loaded){
        image_loaded = false
        pos = pos < products.length-1 ? pos+1 : 0;
        product_image_container.css('height', product_image.height());
        product_image.hide();
        product_image.attr('src', products[pos].image_url);    
    }
}

$(document).ready(function(){

    product_image_container = $('#image_container');
    product_image = $('#product_image');
    products = [];
    for(var p in PRODUCTS_JSON){
        var obj = PRODUCTS_JSON[p].fields;
        obj.description = obj.description.replace(/^(.*)$/mg, '<p>$1</p>')
        products.push(obj);       
        if(PRODUCTS_JSON[p].fields.product_id == first_product_id) pos = p; 
    } 
    if(products.length > 1) {
        _p_interval = setInterval(updateProduct, 5000);
    }
    $('#product_image').load(function(){
        image_loaded = true;
        product_image_container.animate({'height' : product_image.height()}, 250, function(){
            product_image.fadeIn(250);
        });
        $('#product_name').html(products[pos].name);
        $('#product_description').html(products[pos].description);
        $('#product_url').attr('href', SHOP_URL + products[pos].url);
    });

    var headline_count;
    var headline_interval;
    var current_headline = 0;
    var old_headline = -1;
    var first = true;

    $('#news-slide-list li').css('width', '100%');
    headline_count = $('#news-slide-list li').size(); 
    $('#news-slide-list li:eq(0)').css('left', '950px'); 
    headline_rotate(); 
    headline_interval = setInterval(headline_rotate,10000); 

    function headline_rotate() { 
        current_headline = (old_headline + 1) % headline_count; 
        if(old_headline >= 0) {
            $('#news-slide-list li:eq(' + old_headline + ')').animate(
                {left: -950}, 5000, function() { $(this).css('left', '950px'); }
            ); 
        }
        $('#news-slide-list li:eq(' + current_headline + ')').animate( {left: 0}, 5000 ); 
        if(headline_count < 2)
            clearInterval(headline_interval);
        old_headline = current_headline; 
    } 


});

