﻿$(document).ready(function() {
    var fxspeed = 200;
    var timeout;
    $('.item').hover(function() {
        var item = $(this).addClass('itemhover');
        timeout = setTimeout(function() {
            var tooltip = item.clone().addClass('tooltip').hide();
            item.append(tooltip);
            var h = tooltip.height()+45; // adds the new image height minus old image height
            tooltip.css({'height': item.height()});
            tooltip.animate({
                width: '250',
                height: h,
                top: '-23',
                left: '-40'
            }, fxspeed).show();
            tooltip.find('img').animate({
                width: '240',
                height: '135'
            }, fxspeed);
            tooltip.find('.itemplayicon').animate({
                top: '73',
                left: '125'
            }, fxspeed);
        }, 600);
    }, function() {
        clearTimeout(timeout);
        $(this).removeClass('itemhover');
        $('.tooltip').remove();
    });
    $('.item').click(function() {
        if (!$(this).hasClass('itemactive')) {
            $('.itemactive').removeClass('itemactive');
            var vidid = $(this).addClass('itemactive').find('.itemid').attr('id').substring(5);
            $(this).find('.tooltip').addClass('itemactive');
            setActiveVideo(vidid);
            $('html, body').animate({ scrollTop: $('#menu').offset().top }, 600);
            callToActionscript(vidid);
        }
    });
    $('.tooltip').click(function() {
        $(this).parents('.item').trigger('click');
    });
    setActiveVideo = function(vidid) {
        var item = $('#vidid' + vidid).parent();
        $('.itemactive').removeClass('itemactive');
        item.addClass('itemactive').find('.tooltip').addClass('itemactive');
    }
    // Filter Buttons
    $("#submenu a").click(function() {
        var filter = $(this).attr('rel');
        $('#submenu .active').removeClass('active');
        $(this).addClass('active');
        if (filter != "") {
            // Filter Videos
            $("h2[rel]").not("[rel=" + filter + "]").slideUp(fxspeed).nextUntil(':not(.item)').slideUp(fxspeed);
            $("h2[rel=" + filter + "]").slideDown(fxspeed).nextUntil(':not(.item)').slideDown(fxspeed);
        } else {
            // Show All
            $("h2[rel]").slideDown(fxspeed);
            $(".item").slideDown(fxspeed);
        }
        return false;
    });
});
var setActiveVideo;
function callToActionscript(vidid) {
    document['myFlashContent'].sendToActionscript(vidid);
}
function sendToJavaScript(arg) {
    var id = -1;
    if (arg == 'init') {
        // Flash is ready
        var anchor = window.location.hash.substring(1);
        if ($('#vidid' + anchor).length > 0) {
            id = anchor;
        }
    } else if (arg == 'first') {
        // Play first video
        if ($('.item:first').length > 0) {
            id = $('.item:first').find('.itemid').attr('id').substring(5);
        }
    } else if (arg == 'complete') {
        // Video complete
        if ($('.itemactive').next('.item').length > 0) {
            id = $('.itemactive').next('.item').find('.itemid').attr('id').substring(5);
        }
    } else if (arg == 'close') {
        // Close player
    }
    setActiveVideo(id);
    callToActionscript(id);
}

