﻿
function playerReady(obj) {
    /*var id = obj['id'];
    var version = obj['version'];
    var client = obj['client'];
    alert('the videoplayer ' + id + ' has been instantiated');
    var player = document.getElementById(id);*/
};


function setupCarouselRelations(car) {
    adjustCarouselOffset();
    $('#thumbnail-carousel li a').each(function(index, item) {
        $(item).bind('click', function() {
            if (car.scroll(index + 1) != false) {
                setThumbFocus($(item).parent());
            }
        });
    });
}

function setThumbFocus(listItem) {
    if (listItem.hasClass('on')) {
        return false;
    }
    else {
        $('#thumbnail-carousel li').each(function(item) { $(this).removeClass('on'); });
        listItem.addClass('on');
    }
}

function adjustCarouselOffset() {
    var offset = ($('#top-menu').offset().left);
    $('#work-carousel').css('margin-left', String(offset) + 'px');
    $('.jc-skin-thumbs').css('margin-left', String(offset) + 'px');
}

function showItem(itemId, passingLink) {
    var item = $('#' + itemId);
    var link = $(passingLink);

    var itemParentId = item.parent().get(0).id;
    var linkParentId = link.parents('.inner-controls').get(0).id;

    var foundIt = false;

    $('#' + itemParentId + ' .item-box').each(function(i) {
        if (this.style.display != 'none' && !foundIt) {
            $(this).fadeOut('slow');
            item.fadeIn('slow');
            foundIt = true;
        }
    });

    $('#' + itemParentId + ' object').each(function(i) {
    try {
        this.sendEvent("STOP");
    }
    catch (err) {
    }
    });

    $('#' + linkParentId + ' a').each(function(i) {
        if ($(this).hasClass('on')) {
            $(this).removeClass('on');
        }
    });
    
    link.addClass('on');
}

function bindPlayerCall(car) {
    $('#thumbnail-carousel a').bind('click', function(e) {
        $('.item object').each(function(i) {
            try {
                this.sendEvent("STOP");
            }
            catch (err) {
            }
        });
    });
}

$(document).ready(function() {
    if ($(window).width() > 1263) {
        $('#work-carousel > li').addClass('biggest');
    }

    $('#thumbnail-carousel').jcarousel({ scroll: 8, initCallback: bindPlayerCall });
    $('#work-carousel').jcarousel({
        animation: 1000,
        initCallback: setupCarouselRelations,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    $(window).bind('resize', function() { adjustCarouselOffset(); });
    $('#thumbnail-carousel li:first').addClass('on');
    $('#work-curtain').fadeOut();
});
