dc = {
    version: '1.0.0'
}

dc.init = function() {
    // Update Twitter box
    /*$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=indeox&include_rts=1&callback=?', function(data) {
        var output = '',
            count = 1;
        $.each(data, function(i, val) {
            if (!val.in_reply_to_user_id) {
                output += '<li id="twitter-'+count+'">'+val.text+'</li>';
                count++;
            }
        });

        $('#twitter .content').html(output);
    });*/


    // Kick off VideoJS
    //VideoJS.setupAllWhenReady();

    // Update header transparent background
    dc.updateHeaderImg($('.grid-24.feature img').attr('src'));

    var host = "http://cdn.deepcobalt.com/";

    dc.carousel('.grid-24.feature', [
        {'html': '<img src="'+host+'img/redcanary.jpg" />', 'caption':'Sunrise over Canary Wharf'},
        {'html': '<img src="'+host+'img/greengozo.jpg" />', 'caption':'Green Gozo'},
        {'html': '<img src="'+host+'img/vanda.jpg" />', 'caption':'Decode at V&amp;A'},
        {'html': '<img src="'+host+'img/monastery.jpg" />', 'caption':'Monastery in Rabat'},
        {'html': '<img src="'+host+'img/harbour.jpg" />', 'caption':'Valletta Sunrise'},
        {'html': '<img src="'+host+'img/jelly.jpg" />', 'caption':'Jellyfish in Dwejra'},
        {'html': '<img src="'+host+'img/sunrise.jpg" />', 'caption':'Limehouse sunrise'},
        {'html': '<img src="'+host+'img/dwejra.jpg" />', 'caption':'Azure window'},
        {'html': '<img src="'+host+'img/staircase.jpg" />', 'caption':'Old staircase in Bush House'}
    ]);

    dc.gallery('.gallery');
}

dc.updateHeaderImg = function(imgSrc) {
   // Update header transparent background
    headerNode = $('#header p');
    //$('#header').css('background-image','url('+imgSrc+')')
    //            .css('background-color','rgba(0,0,0,0.6)');
    if (imgSrc == undefined) { return; }


    headerNode.animate({ 'backgroundColor':'rgba(0,0,0,1)' }, 500, function() {
        $('#header').css('background-image','url('+imgSrc+')');
        headerNode.animate({ 'backgroundColor':'rgba(0,0,0,0.6)' }, 1000);
    });
}



dc.carousel = function(selector, data) {
    var node           = $(selector),
        carouselWidth  = node.width(),
        carouselHeight = node.height(),
        currentPage    = 0;

    var html = '<div class="carousel-window"><div class="carousel-content clearfix">';
    for (var item in data) {
        item = data[item];
        html += '<div class="carousel-item" style="width: '+carouselWidth+'px">'+item.html;
        if (item.caption) { html += '<p class="caption">'+item.caption+'</p>'; }
        html += '</div>';
    }
    // Repeat first item at the end to enable looping
    html += '<div class="carousel-item carousel-pad-item" style="width: '+carouselWidth+'px">'+data[0].html+'</div>';
    html += '</div></div>';

    node.html(html);
    node.find('.carousel-window').width(carouselWidth);
    node.find('.carousel-item').height(carouselHeight);
    var numItems = node.find('.carousel-item').length;
    node.find('.carousel-content').width(numItems*(carouselWidth+4));
    goToPage(currentPage);

    node.click(function(e) {
        if (e.layerX < 478) { goToPage(currentPage-1); } else { goToPage(currentPage+1); }
    });

    function goToPage(page) {
        var animDuration = 500;
        if (page >= numItems) { page = 0; }
        if (page < 0) { page = numItems-2; animDuration = 0; }

        var scrollDistance = -(page*(carouselWidth+4));
        node.find('.carousel-content').animate({ marginLeft: scrollDistance }, animDuration, function() {
            if (page == numItems-1) {
                node.find('.carousel-content').css('margin-left', 0);
                page = 0;
                currentPage = 0;
            }
        });

        var imgNode = node.find('.carousel-item:eq('+page+') > img');
        if (imgNode.length) {
            dc.updateHeaderImg(imgNode.attr('src'));
        }
        currentPage = page;
    }
}



// Gallery
$(window).load(function() {
    var galleryNode = $('.gallery'),
        galleryWidth = galleryNode.width();


    var currentRowWidth = 0;
    galleryNode.find('li').each(function() {
        var node      = $(this),
            nodeWidth = node.width();

        currentRowWidth += nodeWidth + 1; // 1px is for the margins
        if (currentRowWidth > galleryWidth) {
            // Chop the image from the right
            var difference = currentRowWidth - galleryWidth
                newWidth   = nodeWidth - difference;
            node.width(newWidth);

            // Reposition the image to the center
            node.find('img').css('margin-left', '-'+difference+'px');

            currentRowWidth = 0;
        }
    });
});

dc.gallery = function(selector) {
    var node          = $(selector),
        overlayActive = false,
        images        = node.find('li a'),
        currentIndex,
        overlayNode;

    node.find('li a').click(function(e) {
        e.preventDefault();

        currentIndex = images.index(this);

        var imgSrc = $(this).attr('href');

        if (!overlayActive) {
            overlayNode = $('<div class="gallery-overlay"></div>').hide();
            $('body').append(overlayNode);
            //overlayNode = $('.gallery-overlay');
            overlayActive = true;
        }

        overlayNode.fadeIn();
        overlayNode.html('<img class="main-image" src="'+imgSrc+'" width="940">');
    });

    $('body').delegate('.gallery-overlay', 'click', function(e) {
        if (e.target.tagName.toLowerCase() == 'img') {
            // Go to next image if the click is on the image
            currentIndex++;
            if (currentIndex >= images.length) { currentIndex = 0; }

            var imageNode = $('.main-image'),
                imgSrc = $(images[currentIndex]).attr('href');

            imageNode.fadeOut(function() {
                var preload = new Image(1,1);
                preload.onload = function() {
                    imageNode.removeAttr('width').removeAttr('height');
                    imageNode.attr('src', imgSrc);

                    var windowHeight = $(window).height(),
                        height       = imageNode.height(),
                        width        = imageNode.width();


                    //if (width > 940) { width = 940; imageNode.attr('width', width); }
                    if (height > 400) {
                        height = 400;
                        imageNode.removeAttr('width');
                        imageNode.attr('height', height);
                    } else {
                        width = 940;
                        imageNode.attr('width', width);
                    }

                    var margins = (windowHeight-height)/2;

                    imageNode.css('margin-top', margins+'px');

                    imageNode.fadeIn();
                }
                preload.src = imgSrc;

            });




        } else {
            // Exit overlay if click is anywhere else
            $('.gallery-overlay').fadeOut();
        }

    });
}





/* 960 Gridder settings */
var gOverride = {
  urlBase: 'http://gridder.andreehansson.se/releases/latest/',
  gColor: '#990000',
  gColumns: 24,
  gOpacity: 0.45,
  gWidth: 2,
  pColor: '#666666',
  pHeight: 4,
  pOffset: 0,
  pOpacity: 0.55,
  center: true,
  gEnabled: true,
  pEnabled: true,
  setupEnabled: true,
  fixFlash: true,
  invert: true,
  size: 960
};

/* Kick off main JS */
$(document).ready(function() { dc.init(); });
