﻿var timer;
var interval = 15000;

$(document).ready(function () {
    //Load the slideshow
    theRotator();

    $('#buttons p').mouseover(function () {
        tabSelected(this);
    });
    $('#buttons p').mouseout(function () {
        timer = setInterval('rotate()', interval);
    });

    $('#showcase').hover(function () {
        clearTimeout(timer);
    });
});

function theRotator() {
    //Hide all items
    $('div.rotator ul li').hide();

    //Get the first item and display it
    $('div.rotator ul li:first').show();

    timer = setInterval('rotate()', interval);
}

function rotate() {
    //Get the first item
    var current = ($('div.rotator ul li.show') ? $('div.rotator ul li.show') : $('div.rotator ul li:first'));
    var currentBtn = ($('#buttons div.show') ? $('#buttons div.show') : $('#buttons div:first'));

    if (current.length == 0) current = $('div.rotator ul li:first');
    if (currentBtn.length == 0) currentBtn = $('#buttons div:first');

    var nextButton = ((currentBtn.next().length) ? ((currentBtn.next().hasClass('show')) ? $('#buttons div:first') : currentBtn.next()) : $('#buttons div:first'));

    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') : current.next()) : $('div.rotator ul li:first'));

    //Set the fade in effect for the next image, the show class has higher z-index
    next.show().addClass('show');

    //Hide the current image
    current.hide().removeClass('show');

    currentBtn.removeClass('videoboxbuttonsel').removeClass('show')
              .addClass('videoboxbutton');

    nextButton.removeClass('videoboxbutton').addClass('show')
              .addClass('videoboxbuttonsel');
};

function tabSelected(elem) {
    clearTimeout(timer);

    $f("*").each(function () {
        this.stop();
        this.unload();
    });
    
    $('div.rotator ul li').removeClass().hide();
    $('#img' + $(elem).attr('id')).show().addClass('show');

    $('#buttons div').removeClass('videoboxbuttonsel').removeClass('show').addClass('videoboxbutton');
    $(elem).parent().removeClass('videoboxbutton').addClass('show').addClass('videoboxbuttonsel');
}

function isIpad() {
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) 
            || (navigator.userAgent.indexOf('iPad') != -1)) {
        return true;
    }
    return false;
}

function initPlayer(id, img, video) {
    flowplayer(id, { src: "/player/flowplayer.swf", wmode: 'transparent' }, {
        key: '#$94d315e1ae0166d9ba8',
        clip: {
            scaling: 'orig',
            urlResolvers: null,
            url: isIpad() ? $('#' + id).attr('href') : escape($('#' + id).attr('href')),
            onStart: function () {
                $('#buttons p').unbind('mouseover');
                $('#buttons p').unbind('mouseout');
                clearTimeout(timer);

                $('#buttons p').click(function () {
                    tabSelected(this);
                    $('#buttons p').mouseover(function () {
                        tabSelected(this);
                        $('#buttons p').unbind('click');
                    });
                    $('#buttons p').mouseout(function () {
                        timer = setInterval('rotate()', interval);
                    });
                });
            }
        },
        canvas: {
            backgroundColor: '#000000',
            backgroundGradient: 'none'
        },
        plugins: {
            gatracker: {
                url: "flowplayer.analytics.swf",
                labels: {
                    start: "Start",
                    play: "Play",
                    pause: "Pause",
                    resume: "Resume",
                    seek: "Seek",
                    stop: "Stop",
                    finish: "Finish",
                    mute: "Mute",
                    unmute: "Unmute",
                    fullscreen: "Full Screen",
                    fullscreenexit: "Full Screen Exit"
                },
                debug: false,
                trackingMode: "AS3",
                googleId: _googleID
            },
            controls: {
                backgroundColor: '#000000',
                backgroundGradient: 'low',
                play: true,
                volume: true,
                mute: true,
                time: true,
                stop: true,
                playlist: false,
                fullscreen: true
            }
        }
    }).ipad();
}
