﻿jQuery().ready(function() {

    var bottomBoxMax = 100;
    $('body #container #content-container #leftAndMiddle .startPageBox').each(function() {
        if (bottomBoxMax < $(this).height()) bottomBoxMax = $(this).height();       
    });
    $('body #container #content-container #leftAndMiddle .startPageBox').css('height', bottomBoxMax + ($('body #container #content-container #leftAndMiddle .startPageBox a').length > 0 ? 0/*20*/ : 0));
    $('body #page #bottom-box-container').css('height', '100%');

    // Content boxes
    var bottomItemMax = 100;
    $('#fullViewContainer ul li').each(function() {
    if (bottomItemMax < $(this).height()) bottomItemMax = $(this).height();
    });
    $('#fullViewContainer ul li').css('height', bottomItemMax);

    // BANNER
    $('#bannerRoller').after('<div id="bannernav" class="bannerlinks">').cycle({
        fx: 'fade',
        pager: '#bannernav',
        pause: 1,
        pauseOnPagerHover: 1
    });
    $('#bannernav a').click(function() {
        $('#bannerRoller').cycle('pause');

    });
    // VIDEO
    if ($('body .video-player').length == 0) {
        var backdrop = $('<div class="video-backdrop" style="height: ' + $('#aspnetForm').height() + 'px;"></div>').click(function() { hideVideo() }).hide();
        var outline = $('<div class="video-outline"></div>').hide();
        var player = $('<div class="video-player" style="display: none;"><div class="close"><img src="/publicweb/styles/images/close.png" /></div><div class="top"><div></div></div><div class="player"><div></div></div><div class="bottom"><div></div></div></div>');
        player.find('.close').click(function() { hideVideo() });
        $('body').append(backdrop, player, outline);
    }

    $('body a.video').click(function() {
        showVideo(this);
        return false;
    });

    // li and borders
    $("li:last").css("border", "none");
});

var videoWidth, videoHeight, afterURL;

function showVideo(e) {

    $('.video-backdrop').css('opacity', 0).show().fadeTo('fast', 0.65);
    var width = new Number($($(e).find('span')[0]).html());
    var height = new Number($($(e).find('span')[1]).html());
    afterURL = $($(e).find('span')[2]).html();
    var url = $(e).attr('href');
    var offset = $(e).offset();

    var player = '/PublicWeb/Styles/Mediaplayer/player.swf';

    if (url.match(/youtube.com/)) {
        player = url.split('v=')[1].replace('&', '?');
        player = 'http://youtube.com/v/' + player + '&autoplay=1';
    }

    var so = new SWFObject(player, 'playerobject', width, height, '9', '#');

    so.addParam('allowfullscreen', 'true');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('wmode', 'opaque');
    so.addVariable('image', '/publicweb/styles/images/play_video_default.jpg');
    //so.addVariable('image', '/PublicWeb/Styles/images/body-background.png');
    so.addVariable('file', url);
    so.addVariable('skin', '/PublicWeb/Styles/Mediaplayer/modieus.zip');
    so.addVariable('frontcolor', 'ffffff');
    so.addVariable('lightcolor', 'cc9900');
    so.addVariable('screencolor', 'ffffff');
    so.addVariable('stretching', 'fill');
    so.addVariable('autostart', 'true');

    $('.video-outline').show().css({
        opacity: 0.75,
        width: $(e).find('img').width() - 2,
        height: $(e).find('img').height() - 2,
        left: offset.left,
        top: offset.top
    }).animate({
        width: width,
        height: height,
        left: ($(window).width() / 2) - (width / 2),
        top: ($(window).height() / 2) - (height / 2)
    }, 'normal', 'swing', function() {
        $('.video-outline').hide();
        $('.video-player').hide().css({
            width: Number(width) + 10 + 'px',
            height: Number(height) + 'px',
            left: ($(window).width() / 2) - (width / 2) - 5,
            top: ($(window).height() / 2) - (height / 2) - 5
        }).fadeIn('fast');
        so.write($('.video-player').find('.player div')[0]);
        if (afterURL != '') {
            setTimeout(attachVideoListener, 1000);
        }
    });
}

function attachVideoListener() {
    if ($('#playerobject').length == 0) {
        setTimeout(attachVideoListener, 1000);
    }

    return false;
    //else {
    //    $('#playerobject').get(0).addModelListener('STATE', 'videoStateMonitor');
    //}

}

function videoStateMonitor(player) {
    if (player.newstate == 'COMPLETED') {
        this.window.focus();
        window.location.href = afterURL;
    }
}


function hideVideo() {
    $('.video-player').find('.player div').empty();
    $('.video-player').animate({
        width: '20px',
        height: '10px',
        left: ($(window).width() / 2) - 10,
        top: ($(window).height() / 2) - 5
    }, 'fast');
    $('.video-backdrop').add('.video-outline').add('.video-player').fadeOut('fast');
}


