/// <reference path="jquery-vsdoc.js" />

var isAnimating = false;
var quoteIndex = 0;
var quoteLinks = ['Martin', 'Sumo', 'McBaney', 'Marion'];

$(document).ready(function() {
    nav();
    clickpanels();
    quoterotator();
    videoscroller();
    imagescroller();
    jumpback();
});

function nav() {
    $('#nav > li > a').hover(function() {
        if ($('#nav li').index($(this).parent()) == 2) $(this).next().show();
        else $('#nav ul').hide();
    }, function() { });
    $('#nav ul').hover(function() { }, function() {
        $(this).hide();
    });
}

function clickpanels() {
    $('#cast li, #quotes, #who, #testimonial').clickpanel();
    $('#cast li, #quotes, #who, #testimonial').hover(function() {
        $(this).find('a').addClass('over');
        Cufon.replace($(this).find('a'), { fontFamily: 'HelveticaBlackCondensed', hover: true });
    }, function() {
        $(this).find('a').removeClass('over');
        Cufon.replace($(this).find('a'), { fontFamily: 'HelveticaBlackCondensed', hover: true });
    });
}

function quoterotator() {
    if ($('#quotes.widget').length == 0) return;

    $('#quotes.widget li:eq(' + quoteIndex + ')').show();
    $('#quotes.widget a').attr('href', '/Team/' + quoteLinks[quoteIndex] + '.aspx');
    setInterval(rotatequote, 6000);
}

function rotatequote() {
    if (++quoteIndex > 3) quoteIndex = 0;

    if (!$.browser.msie) $('#quotes.widget li').hide().eq(quoteIndex).show('normal');
    else $('#quotes.widget li').hide().eq(quoteIndex).show();
    $('#quotes.widget a').attr('href', '/Team/' + quoteLinks[quoteIndex] + '.aspx');
    if (quoteIndex == 0) Cufon.replace($('#quotes h5').text('Meet the team'));
    else if (quoteIndex == 3) Cufon.replace($('#quotes h5').text('Meet the client'));
}

function videoscroller() {
    if ($('#videos').length == 0) return;

    var width = 597;
    var panels = Math.ceil($('#videos #frame li').length / 3);
    var current = 1;
    var duration = 1000;
    var easing = 'easeOutExpo';

    $('#videos #frame .next').click(function() {
        if (isAnimating) return;
        toggleAnimation();
        var left = parseInt($('#videos #frame ul').css('margin-left').split('px')[0]);
        $('#videos #frame ul').stop().animate({ 'marginLeft': (left - width) }, duration, easing, toggleAnimation);
        if (++current == panels) $(this).hide();
        $('#videos #frame .back').show();
    }).hide();

    $('#videos #frame .back').click(function() {
        if (isAnimating) return;
        toggleAnimation();
        var left = parseInt($('#videos #frame ul').css('margin-left').split('px')[0]);
        $('#videos #frame ul').stop().animate({ 'marginLeft': (left + width) }, duration, easing, toggleAnimation);
        if (--current == 1) $(this).hide();
        $('#videos #frame .next').show();
    }).hide();

    $('#videos ul a').click(function() {
        swfobject.embedSWF("/Common/Flash/player.swf", "player", "577", "357", "9", "", { Vid: $(this).attr('href') }, { wmode: 'transparent' });
        return false;
    });

    if (panels > 1) $('#frame .next').show();
}

function imagescroller() {
    if ($('#gallery').length == 0) return;
    
    var height = 237;
    var panels = Math.ceil($('#gallery #frame li').length / 6);
    var current = 1;
    var duration = 1000;
    var easing = 'easeOutExpo';

    $('#gallery .next').click(function() {
        if (isAnimating) return;
        toggleAnimation();
        var top = parseInt($('#gallery ul').css('margin-top').split('px')[0]);
        $('#gallery ul').stop().animate({ 'marginTop': (top - height) }, duration, easing, toggleAnimation);
        if (++current == panels) $(this).hide();
        $('#gallery .back').show();
    }).hide();

    $('#gallery .back').click(function() {
        if (isAnimating) return;
        toggleAnimation();
        var top = parseInt($('#gallery ul').css('margin-top').split('px')[0]);
        $('#gallery ul').stop().animate({ 'marginTop': (top + height) }, duration, easing, toggleAnimation);
        if (--current == 1) $(this).hide();
        $('#gallery .next').show();
    }).hide();

    if (panels > 1) $('#gallery .next').show();

    $('#gallery #large').attr('src', $('#gallery #frame img:eq(0)').attr('src').split('.jpg')[0] + '_L' + '.jpg').parent().attr('href', $('#gallery #frame img:eq(0)').attr('src').split('.jpg')[0] + '_XL' + '.jpg');
    $('#gallery #frame img').click(function() {
        var l = $(this).attr('src').split('.jpg')[0] + '_L' + '.jpg';
        var xl = $(this).attr('src').split('.jpg')[0] + '_XL' + '.jpg';
        $('#gallery #large').attr('src', l).parent().attr('href', xl);
    });
}

function toggleAnimation() {
    isAnimating = !isAnimating;
}

function jumpback() {
    $('.indent .back').click(function() {
        history.back(1);
    });
}